Skip to content

AssertionError while using TrailingStrategy #316

Closed
@zlpatel

Description

@zlpatel

I am seeing this error when try to run backtest with 10000 days worth data. It doesn't happen for all the stocks, but some. e.g AAPL, AMZN.

Index: 1946  Close: 35.88999938964844  ATR: 1.6433353276970901
Index: 1947  Close: 35.779998779296875  ATR: 1.5673829350797757
Index: 1948  Close: 35.779998779296875  ATR: 1.5232842084983242
Index: 1949  Close: 36.029998779296875  ATR: 1.4951925556138177
Index: 1950  Close: 36.13999938964844  ATR: 1.438393141851363
Index: 1951  Close: 35.65999984741211  ATR: 1.3906505347952947
Traceback (most recent call last):
  File "backtesting_usa.py", line 116, in <module>
    output = bt.run()
  File "/usr/local/lib/python3.8/site-packages/backtesting/backtesting.py", line 1165, in run
    strategy.next()
  File "backtesting_usa.py", line 83, in next
    super().next()
  File "/usr/local/lib/python3.8/site-packages/backtesting/lib.py", line 446, in next
    trade.sl = max(trade.sl or -np.inf,
  File "/usr/local/lib/python3.8/site-packages/backtesting/backtesting.py", line 633, in sl
    self.__set_contingent('sl', price)
  File "/usr/local/lib/python3.8/site-packages/backtesting/backtesting.py", line 652, in __set_contingent
    assert price is None or 0 < price < np.inf
AssertionError

I tried printing the index values for each iteration and I found that for this dataset, it processed till index 1951 and failed on 1952.

If I reduce the backtesting period to 5000, it works because it doesn't come across that piece of data (index 1952). I checked that data and verified the close price, and calculated ATR(14).

Below is the code snippet:

class Breakout(TrailingStrategy):
	timeperiod = 10
	position_size_decimal = 0.2
	min_close = []
	max_close = []
	def init(self):
		super().init()
		self.ema20 = self.I(EMA,self.data.Close,20,overlay=True)
		self.atr14 = self.I(ATR,self.data.High,self.data.Low,self.data.Close,14)
		self.set_atr_periods(14)
		self.set_trailing_sl(1)
		print(type(self.data.Close))
		self.min_close, self.max_close = MINMAX(self.data.Close, timeperiod=self.timeperiod)

	def next(self):
		super().next()
		index = len(self.data)-1
		print('Index: '+f'{index} '+' Close: '+f'{self.data.Close[index]} '+' ATR: '+f'{self.atr14[index]}')
		if not self.position.is_long and self.min_close[index] > (self.max_close[index] * 0.98) and self.max_close[index] < (self.min_close[index] * 1.02):
			#print('BUY ZONE:'+ f'{self.data.index[index]} {self.rsi2[index]} {self.rsi5[index]} {self.ema200[index]} {self.data.Close[index]}')
			self.buy(size=self.position_size_decimal)

I download data using yfinance python api. Any help will be greatly appreciated.

Originally posted by @zlpatel in #315

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions