How could I plot Crossover Signal on master window(plotinfo.plotmaster == self.datas[0]) as triangle indicating where it happens??
-
Hi everyone.
Currently I want to plot some Crossover Signal (bt.ind.CrossOver) on the price to gain some insight.
I want to plot it as triangle (just like the buy and sell point) with probably different colors and directions (up-triangle indicating it's crossing up, down-triangle indicating it's crossing down), just like selling gives a red downside triangle and buying gives a green upside triangle.
Is there anyone know how to do that? -
I think I found some part of solution in this article.
https://www.backtrader.com/blog/posts/2016-12-10-buysellarrows/buysellarrows/
But this only re-defines buy-sell signal pattern, not exactly what I want.
-
You may want to create custom indicator with two lines
up
anddown
which has a CrossOver indicator inside. Something like this (not tested):class CrossOverWithArrows(bt.Indicator): lines = ('up', 'down') plotlines = dict( up=dict(marker='$\u21E7$', markersize=12.0), down=dict(marker='$\u21E9$', markersize=12.0) def __init__(self): up = bt.ind.CrossUp(smthng, smthng) down = bt.ind.CrossDown(smthng, smthng)
-
@ab_trader Cool, I'll try it latter!
-
@ab_trader said in How could I plot Crossover Signal on master window(plotinfo.plotmaster == self.datas[0]) as triangle indicating where it happens??:
plotlines = dict( up=dict(marker='$\u21E7$', markersize=12.0), down=dict(marker='$\u21E9$', markersize=12.0)
Thanks for your advice, here is what I've done and it works exactly the same as expected.
import datetime import backtrader as bt class DoubleSMAWithLoc(bt.Indicator): lines = ('fast', 'slow', 'loc') params = ( ('pfast', 10), ('pslow', 30), ) plotlines = dict(loc=dict(_plotskip='True',)) def __init__(self): self.l.fast = bt.ind.SMA(self.data, period=self.p.pfast) self.l.slow = bt.ind.SMA(self.data, period=self.p.pslow) self.l.loc = self.data.close class CrossOverWithArrows(bt.Indicator): lines = ('up', 'down') plotlines = ( ('up', dict(ls='', marker='^', markersize=8.0, color='green', fillstyle='full')), ('down', dict(ls='', marker='v', markersize=8.0, color='red', fillstyle='full')) ) def __init__(self): self.cross_up = bt.ind.CrossUp(self.data.fast, self.data.slow) self.cross_down = bt.ind.CrossDown(self.data.fast, self.data.slow) self.l.up = bt.If(self.cross_up, self.data.loc, -1) self.l.down = bt.If(self.cross_down, self.data.loc, -1) class SMACross(bt.Strategy): params = ( ('pfast', 10), ('pslow', 30), ) def __init__(self): self.sma = DoubleSMAWithLoc( self.datas[0], pfast=self.p.pfast, pslow=self.p.pslow, plotmaster=self.datas[0]) self.cross = CrossOverWithArrows( self.sma, plotmaster=self.datas[0]) if __name__ == '__main__': cerebro = bt.Cerebro(stdstats=False) cerebro.addstrategy(SMACross) datapath = 'datas/orcl-1995-2014.txt' # Create a Data Feed data = bt.feeds.YahooFinanceCSVData( dataname=datapath, # Do not pass values before this date fromdate=datetime.datetime(2000, 1, 1), # Do not pass values before this date todate=datetime.datetime(2000, 12, 31), # Do not pass values after this date reverse=False) cerebro.adddata(data) cerebro.addwriter(bt.WriterFile, csv=True, out='temp.csv') cerebro.run() cerebro.plot()
-
@xyshell I think this will be better
('up', dict(_skipnan=True, ls='', marker='^', markersize=8.0, color='green', fillstyle='full')),
('down', dict(_skipnan=True, ls='', marker='v', markersize=8.0, color='red', fillstyle='full')),
and
self.l.up = bt.If(self.cross_up, self.data.loc, float('NaN'))
self.l.down = bt.If(self.cross_down, self.data.loc,float('NaN'))