For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

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?


Log in to reply