LinesCoupler is plotted on different figure when passing line instead of data to SMA indicator
-
I am currently trying to implement a strategy which uses a larger timeframe indicators. I started to learn from the "Mixing Timeframes in Indicators" article and have the following amended code to achieve the goal. In particular, I would like to have resample data using 7-day instead of month timeframe, and have a SMA indicator.
import argparse import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind import backtrader.utils.flushfile class St(bt.Strategy): params = dict(multi=True) def __init__(self): self.sma = sma = btind.SMA(self.data1) sma.plotinfo.plot = True # show plotting if self.p.multi: sma1 = sma() # couple the entire indicators self.sellsignal = self.data0.close < sma1.sma else: self.sellsignal = self.data0.close < sma.sma() def next(self): txt = ','.join( ['%04d' % len(self), '%04d' % len(self.data0), '%04d' % len(self.data1), self.data.datetime.date(0).isoformat(), '%.2f' % self.data0.close[0], '%.2f' % self.sma.sma[0], '%.2f' % self.sellsignal[0]]) print(txt) def runstrat(): args = parse_args() cerebro = bt.Cerebro() data = btfeeds.BacktraderCSVData(dataname=args.data) cerebro.adddata(data) cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=7) cerebro.addstrategy(St, multi=args.multi) cerebro.run(stdstats=False, runonce=False) if args.plot: cerebro.plot(style='bar') def parse_args(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Sample for pivot point and cross plotting') parser.add_argument('--data', required=False, default='../../datas/2005-2006-day-001.txt', help='Data to be read in') parser.add_argument('--multi', required=False, action='store_true', help='Couple all lines of the indicator') parser.add_argument('--plot', required=False, action='store_true', help=('Plot the result')) return parser.parse_args() if __name__ == '__main__': runstrat()
The following figure shows the result of plot and everything is working as excepted after amendments.
Since I would like to have a SMA of different prices such as open and high, I found this useful post, and I further changed
self.sma = sma = btind.SMA(self.data1)
into
self.sma = sma = btind.SMA(self.data1.open)
In that case, the LinesCoupler is plotted on 7-Day figure as shown below.
Although I believe that this may not affect my mogic and calculation, as I am able to verify values of lines from the
print(txt)
, I would like to know if there is way to put LinesCoupler back to the first plot in order to better presentation.