Skip to content

Commit 19dde7a

Browse files
authored
AssertionError while using TrailingStrategy - Adding Unit Test kernc#316
1 parent 5a59a97 commit 19dde7a

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

backtesting/test/_test.py

+27-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
plot_heatmaps,
2929
random_ohlc_data,
3030
)
31-
from backtesting.test import GOOG, EURUSD, SMA
31+
from backtesting.test import GOOG, EURUSD, SMA, AMZN
3232
from backtesting._util import _Indicator, _as_str, _Array, try_
33+
from talib import RSI,EMA,ATR,MINMAX
3334

3435
SHORT_DATA = GOOG.iloc[:20] # Short data for fast tests with no indicator lag
3536

@@ -860,7 +861,31 @@ def next(self):
860861
self.buy()
861862

862863
stats = Backtest(GOOG, S).run()
863-
self.assertEqual(stats['# Trades'], 51)
864+
self.assertEqual(stats['# Trades'], 57)
865+
866+
def test_TrailingStrategy_should_not_raise_Assert_error(self):
867+
class Breakout(TrailingStrategy):
868+
timeperiod = 10
869+
position_size_decimal = 0.2
870+
min_close = []
871+
max_close = []
872+
def init(self):
873+
super().init()
874+
self.ema20 = self.I(EMA,self.data.Close,20,overlay=True)
875+
self.atr14 = self.I(ATR,self.data.High,self.data.Low,self.data.Close,14)
876+
self.set_atr_periods(20)
877+
self.set_trailing_sl(1.5)
878+
print(type(self.data.Close))
879+
self.min_close, self.max_close = MINMAX(self.data.Close, timeperiod=self.timeperiod)
880+
881+
def next(self):
882+
super().next()
883+
index = len(self.data)-1
884+
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):
885+
self.buy(size=self.position_size_decimal)
886+
887+
stats = Backtest(AMZN,Breakout).run()
888+
self.assertEqual(stats['# Trades'], 23)
864889

865890

866891
class TestUtil(TestCase):

0 commit comments

Comments
 (0)