For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
Larger time frame candles forms early
-
Hi,
I am trying to plot 125 min pivot points on a 5 mins chart, everything works fine except the first candle of a larger timeframe. The first candle of 125 mins forms just after four 5 mins candles, there is no issue for consecutive 125 mins candles. please assist us to resolve this.cerebro = bt.Cerebro(runonce=False) # create a "Cerebro" engine instance # Create a data feed # data = bt.feeds.PandasData(dataname=data,fromdate=datetime(2017,1,1),todate=datetime(2017,3,1)) data = bt.feeds.PandasData(dataname=data,fromdate=start_date,todate=end_date,timeframe=bt.TimeFrame.Minutes,compression=5) cerebro.adddata(data) # Add the data feed cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=125) cerebro.addstrategy(SmaCross) # Add the trading strategy
Chart
-
As I analyzed further, I have an issue with every day's first candle in 125 mins chart. Please refer to the below screenshot, two 125 mins(4th and 5th candles) have formed in the second day's first 30 mins interval.
Strategy code
class SmaCross(bt.Strategy): # list of parameters which are configurable for the strategy params = dict( pfast=10, # period for the fast moving average pslow=30 # period for the slow moving average ) def __init__(self): self.pp = bt.ind.PivotPoint(self.data1) sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average # To set the stop price self.atr = bt.indicators.ATR(self.data0, period=20) self.crossover = self.data0.close > self.pp.r1() # crossover signal def next(self): if not self.position: # not in the market if self.crossover > 0: # if fast crosses slow to the upside self.order = self.buy() pdist = self.atr[0] * 1.0 self.pstop = self.data.close[0] - pdist self.log('high- %.2f low - %.2f close - %.2f' % (self.data1.high[0],self.data1.low[0],self.data1.close[0])) else: # in the market pclose = self.data.close[0] pstop = self.pstop if pclose < pstop: self.close() # stop met - get out else: pdist = self.atr[0] * 1.0 # Update only if greater than self.pstop = max(pstop, pclose - pdist)