@void Your question made me think. I was wondering what would happen if you change the value of say, self.data.close in the init of the strategy. Would that work? Appears to.
def __init__(self): self.data.close = self.data.close / 100 self.sma = bt.ind.SMA(self.data.close, period=20)Results in:
2020-11-20, o 117.40 h 119.98 l 117.26 c 1.19 sma 1.09 2020-11-23, o 119.73 h 120.29 l 116.95 c 1.18 sma 1.10 2020-11-24, o 119.44 h 121.01 l 118.90 c 1.21 sma 1.10 2020-11-25, o 121.65 h 122.63 l 119.68 c 1.20 sma 1.12 2020-11-26, o 120.14 h 121.36 l 119.22 c 1.21 sma 1.13 2020-11-27, o 120.72 h 121.68 l 120.23 c 1.21 sma 1.14 2020-11-30, o 120.64 h 121.05 l 119.33 c 1.19 sma 1.14 2020-12-01, o 119.42 h 121.37 l 119.35 c 1.21 sma 1.15 2020-12-02, o 120.29 h 122.73 l 118.88 c 1.23 sma 1.16 2020-12-03, o 122.19 h 123.79 l 121.80 c 1.23 sma 1.17I have to wonder if there aren't dangers being missed here. I think I would still likely preprocess with Pandas unless this method was thoroughly tested.
Here's the whole code.
import datetime import backtrader as bt class Strategy(bt.Strategy): def log(self, txt, dt=None): """ Logging function fot this strategy""" dt = dt or self.data.datetime[0] if isinstance(dt, float): dt = bt.num2date(dt) print("%s, %s" % (dt.date(), txt)) def print_signal(self): self.log( "o {:7.2f}\th {:7.2f}\tl {:7.2f}\tc {:7.2f}\tsma {:5.2f}".format( self.datas[0].open[0], self.datas[0].high[0], self.datas[0].low[0], self.datas[0].close[0], self.sma[0], ) ) def __init__(self): self.data.close = self.data.close / 100 self.sma = bt.ind.SMA(self.data.close, period=20) def next(self): self.print_signal() if __name__ == "__main__": cerebro = bt.Cerebro() data = bt.feeds.YahooFinanceData( dataname="VOD.L", timeframe=bt.TimeFrame.Days, fromdate=datetime.datetime(2020, 10, 25), todate=datetime.datetime(2020, 12, 31), reverse=False, ) cerebro.adddata(data) cerebro.addstrategy(Strategy) cerebro.run()