Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Dynamic Asset Allocation Weights Indicator

    General Discussion
    1
    1
    116
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Ronny Li
      Ronny Li last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors