Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. Ronny Li
    3. Posts
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 8
    • Best 3
    • Controversial 0
    • Groups 0

    Posts made by Ronny Li

    • Chaining Strategies

      I have two strategies that I'd like to run sequentially. The first strategy allocates a portion of the portfolio between its assets (using self.order_target_percent) and the second strategy uses what remains of the portfolio.

      What is the best way in backtrader to achieve the above?

      posted in General Discussion
      Ronny Li
      Ronny Li
    • RE: ZeroDivisionError: float division by zero

      Hi Sabir, bt.DivByZero is what you're looking for. Here's an example where I use it

      class WeighInverseVol(bt.Strategy):
          params = (
              ('period', 30),
          )
      
          def __init__(self):
              rs = [bt.ind.PctChange(d, period=1) for d in self.datas]
              vs = [bt.ind.StdDev(ret, period=self.params.period) for ret in rs]
              inv = [bt.DivByZero(bt.LineNum(1), v) for v in vs]
              invsum = bt.LineNum(0)
              for inv_ in inv:
                  invsum += inv_
              # self.weights is an array of asset allocation weights
              # one weight per asset in self.datas
              self.weights = [inv_ / invsum for inv_ in inv]
      

      Here's the source code for bt.DivByZero: https://github.com/mementum/backtrader/blob/master/backtrader/functions.py#L43

      posted in General Code/Help
      Ronny Li
      Ronny Li
    • Dynamic Asset Allocation Weights Indicator

      Has anyone here implemented an indicator that returns weights for assets in a portfolio? Here is the Strategy version:

      class WeighInverseVol(bt.Strategy):
      
          def __init__(self):
              rs = [bt.ind.PctChange(d.close, period=1) for d in self.datas]
              vs = [bt.ind.StdDev(ret, period=30) for ret in rs]
              inv = [bt.DivByZero(bt.LineNum(1), v) for v in vs]
              invsum = bt.LineNum(0)
              for inv_ in inv:
                  invsum += inv_
              # self.weights is an array of asset allocation weights
              # one weight per asset in self.datas
              self.weights = [inv_ / invsum for inv_ in inv]
      

      I'd like WeighInverseVol to be an Indicator so I can use it within other strategies but I don't know how to return a dynamic number of lines within an indicator. Any advice?

      Thanks!

      posted in General Discussion
      Ronny Li
      Ronny Li
    • RE: Is Yahoo API down right now? Cannot get any data feed through function

      @backtrader The PR addressing this issue has been approved and just needs to be merged: https://github.com/mementum/backtrader/pull/401

      posted in General Code/Help
      Ronny Li
      Ronny Li
    • 1 / 1