|
28 | 28 | plot_heatmaps,
|
29 | 29 | random_ohlc_data,
|
30 | 30 | )
|
31 |
| -from backtesting.test import GOOG, EURUSD, SMA |
| 31 | +from backtesting.test import GOOG, EURUSD, SMA, AMZN |
32 | 32 | from backtesting._util import _Indicator, _as_str, _Array, try_
|
| 33 | +from talib import RSI,EMA,ATR,MINMAX |
33 | 34 |
|
34 | 35 | SHORT_DATA = GOOG.iloc[:20] # Short data for fast tests with no indicator lag
|
35 | 36 |
|
@@ -860,7 +861,31 @@ def next(self):
|
860 | 861 | self.buy()
|
861 | 862 |
|
862 | 863 | 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) |
864 | 889 |
|
865 | 890 |
|
866 | 891 | class TestUtil(TestCase):
|
|
0 commit comments