No trade is made... what's wrong with this code?
-
Hi. I think I can't find any problem with my data but my cerebro.run() doesn't yield any trade execution at all though the graph is coming out well with cerebro.plot.
#Since the time interval is 10 minutes so I set compression as 10 and timeframe as "Minutes"
data = dataFeed(dataname='ticks3.csv',timeframe=bt.TimeFrame.Minutes, compression=10,sessionstart=datetime.time(9, 0), sessionend=datetime.time(15, 10))#, timeframe=bt.TimeFrame.Minutes, compression=60) #dataFeed(dataname='ticks3.csv')#I put the codes like this below
cerebro = bt.Cerebro()
cerebro.adddata(data)startcash=10000
cerebro.broker.setcash(startcash)class SmaCross(bt.Strategy):
params = dict(
pfast=4, # period for the fast moving average
pslow=9 #30 # period for the slow moving average
)
def init(self):
sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
def next(self):
if not self.position: # not in the market
if self.crossover > 0: # if fast crosses slow to the upside
close = self.data.close[0]
#size = int(self.broker.getcash() / close)
self.buy(size=1)
elif self.crossover < 0: # in the market & cross to the downside
self.close()
cerebro.addstrategy(SmaCross)cerebro.run()
portvalue = cerebro.broker.getvalue()
pnl = portvalue - startcash#Print out the final result
print('Final Portfolio Value: ${}'.format(portvalue))
print('P/L: ${}'.format(pnl))#Finally plot the end results
cerebro.plot(style='candlestick',iplot=False)"""
Unnamed: 0 Date_Time o h l c v
0 0 2020-07-09 15:10:00 8.00 8.00 7.50 7.94 41
1 1 2020-07-09 15:00:00 7.61 8.24 7.61 8.24 10
2 2 2020-07-09 14:50:00 8.30 8.30 7.70 7.70 7
3 3 2020-07-09 14:40:00 8.72 8.72 8.30 8.30 7
4 4 2020-07-09 14:30:00 8.72 8.72 8.39 8.39 8
.. ... ... ... ... ... ... ..
895 895 2020-06-08 12:30:00 15.98 15.98 15.98 15.98 1
896 896 2020-06-08 12:20:00 16.48 16.48 15.98 15.98 3
897 897 2020-06-08 12:10:00 16.58 16.58 16.48 16.48 3
898 898 2020-06-08 11:40:00 16.38 16.38 16.38 16.38 1
899 899 2020-06-08 11:00:00 15.48 15.68 15.48 15.68 2
""" -
First step would be to reverse your data so the time increases from top to bottom.
Use backtiks as described above on the page to post code blocks.
-
@ab_trader Thank you so much! I really appreciate your help:)( I haven't been able to fix this for like 2 months and now it was solved)
-
Hi Bangol Phoenix,
it would be great if you could post the working code!
Cheers!