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

Avoid optstrategy redundant calculation



  • How can I calculate self.ma2 and self.rsi first (and only once) and feed them to TestStrategy?
    I do not want TestStrategy to recalculate self.ma2 and self.rsi during optstrategy iteration. They remain same.

    Code:

    class TestStrategy(bt.Strategy):
    
    def __init__(self):
    self.ma1 = bt.talib.SMA(self.datas[0].close, timeperiod=int(self.p.maShort_period))  
    self.ma2 = bt.talib.SMA(self.datas[0].close, timeperiod=int(self.p.maLong_period))
    self.rsi = bt.talib.RSI(self.datas[0].close, timeperiod=int(self.p.rsi_period))
    
    cerebro.optstrategy(
        TestStrategy, maShort_period=range(15, 25)