I'm trying to get a crossover signal for Pivot Points. I used the recomendation from the documentation to stop automatically plotting the non-resampled data based on the suggestion: "The indicator will try to automatically plo to the non-resampled data. To disable this behavior use the following during construction:
_autoplot=False
"
but I cannot seem to get the crossover indicator to show on the graph. I tried enabling the possible parameters for the plotinfo of the crossover indicator as:
self.pivotSignalP.plotinfo.plot = True
self.pivotSignalP.plotinfo.subplot = True
But it doesn't seem to work.
My Data is an intraday 1 minute data and the pivot points are generated based on a resampled data with timeframe = bt.TimeFrame.Days
My code is:
import backtrader as bt
from datetime import datetime as dt
plotStart = dt(2020, 11, 11, 9, 30)
plotEnd = dt(2020, 11, 11, 12, 0)
dataTimeFrame = bt.TimeFrame.Minutes
compression = 1
class PivotPoint(bt.Strategy):
params = dict(
pivot = bt.indicators.PivotPoint,
cross = bt.indicators.CrossOver,
emovav = bt.indicators.ExponentialMovingAverage,
)
def __init__(self):
self.pivots = self.p.pivot(self.data1)
self.pivots.autoplot = False
self.pivotSignalP = self.p.cross(self.data1.open, self.pivots.lines.p)
self.pivotSignalP.plotinfo.plotname = "Pivot P Crossover"
self.ema9 = self.p.emovav(self.data0, period = 9)
self.ema21 = self.p.emovav(self.data0, period = 21)
self.emaSignal = self.p.cross(self.ema9, self.ema21)
self.emaSignal.plotinfo.plotname = "EMA Crossover"
def next(self):
if self.order:
return
if not self.position:
if self.emaSignal > 0:
self.buy()
else:
if self.emaSignal < 0:
self.close()
cerebro = bt.Cerebro(stdstats = False)
cerebro.addobserver(bt.observers.BuySell, barplot = True, bardist = 0.0001)
cerebro.addobserver(bt.observers.Value)
cerebro.addobserver(bt.observers.Trades)
data = bt.feeds.GenericCSVData(
dataname = 'SPY_11_2020_1M.txt',
name = 'SPY',
datetime = 0,
dtformat = ('%m/%d/%Y %H:%M'),
timeframe = dataTimeFrame,
compression = compression,
fromdate = dt(2020, 11, 10),
todate = plotEnd,
open = 1,
high = 2,
low = 3,
close = 4,
volume = 5,
openinterest = -1,
)
cerebro.adddata(data)
data1 = cerebro.resampledata(data, timeframe = bt.TimeFrame.Days)
data1.plotinfo.plot = False
cerebro.broker.setcash(100000.0)
startValue = cerebro.broker.get_value()
print('Start Portfolio Value: %.2f' % startValue)
cerebro.addstrategy(PivotPoint)
cerebro.run()
cerebro.plot(style = 'candle', barup = 'green', bardown = 'red', start= plotStart, end= plotEnd, volume = True)
endValue = cerebro.broker.get_value()
pl = 100 * (endValue - startValue) / startValue
print('\lFinal Portfolio Value: {}\t\t\t P\L: {}%'.format(round(endValue, 2), round(pl, 2)))