| 1 | # pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, C0330, unused-argument |
| 2 | import logging |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from freqtrade.data.history import get_timerange |
| 7 | from freqtrade.optimize.backtesting import Backtesting |
| 8 | from freqtrade.strategy.interface import SellType |
| 9 | from tests.conftest import patch_exchange |
| 10 | from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe, |
| 11 | _get_frame_time_from_offset, tests_timeframe) |
| 12 | |
| 13 | |
| 14 | # Test 0: Sell with signal sell in candle 3 |
| 15 | # Test with Stop-loss at 1% |
| 16 | tc0 = BTContainer(data=[ |
| 17 | # D O H L C V B S |
| 18 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 19 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 20 | [2, 4987, 5012, 4986, 4600, 6172, 0, 0], # exit with stoploss hit |
| 21 | [3, 5010, 5000, 4980, 5010, 6172, 0, 1], |
| 22 | [4, 5010, 4987, 4977, 4995, 6172, 0, 0], |
| 23 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 24 | stop_loss=-0.01, roi={"0": 1}, profit_perc=0.002, use_sell_signal=True, |
| 25 | trades=[BTrade(sell_reason=SellType.SELL_SIGNAL, open_tick=1, close_tick=4)] |
| 26 | ) |
| 27 | |
| 28 | # Test 1: Stop-Loss Triggered 1% loss |
| 29 | # Test with Stop-loss at 1% |
| 30 | tc1 = BTContainer(data=[ |
| 31 | # D O H L C V B S |
| 32 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 33 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 34 | [2, 4987, 5012, 4600, 4600, 6172, 0, 0], # exit with stoploss hit |
| 35 | [3, 4975, 5000, 4980, 4977, 6172, 0, 0], |
| 36 | [4, 4977, 4987, 4977, 4995, 6172, 0, 0], |
| 37 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 38 | stop_loss=-0.01, roi={"0": 1}, profit_perc=-0.01, |
| 39 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)] |
| 40 | ) |
| 41 | |
| 42 | |
| 43 | # Test 2: Minus 4% Low, minus 1% close |
| 44 | # Test with Stop-Loss at 3% |
| 45 | tc2 = BTContainer(data=[ |
| 46 | # D O H L C V B S |
| 47 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 48 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 49 | [2, 4987, 5012, 4962, 4975, 6172, 0, 0], |
| 50 | [3, 4975, 5000, 4800, 4962, 6172, 0, 0], # exit with stoploss hit |
| 51 | [4, 4962, 4987, 4937, 4950, 6172, 0, 0], |
| 52 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 53 | stop_loss=-0.03, roi={"0": 1}, profit_perc=-0.03, |
| 54 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=3)] |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | # Test 3: Multiple trades. |
| 59 | # Candle drops 4%, Recovers 1%. |
| 60 | # Entry Criteria Met |
| 61 | # Candle drops 20% |
| 62 | # Trade-A: Stop-Loss Triggered 2% Loss |
| 63 | # Trade-B: Stop-Loss Triggered 2% Loss |
| 64 | tc3 = BTContainer(data=[ |
| 65 | # D O H L C V B S |
| 66 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 67 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 68 | [2, 4987, 5012, 4800, 4975, 6172, 0, 0], # exit with stoploss hit |
| 69 | [3, 4975, 5000, 4950, 4962, 6172, 1, 0], |
| 70 | [4, 4975, 5000, 4950, 4962, 6172, 0, 0], # enter trade 2 (signal on last candle) |
| 71 | [5, 4962, 4987, 4000, 4000, 6172, 0, 0], # exit with stoploss hit |
| 72 | [6, 4950, 4975, 4975, 4950, 6172, 0, 0]], |
| 73 | stop_loss=-0.02, roi={"0": 1}, profit_perc=-0.04, |
| 74 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2), |
| 75 | BTrade(sell_reason=SellType.STOP_LOSS, open_tick=4, close_tick=5)] |
| 76 | ) |
| 77 | |
| 78 | # Test 4: Minus 3% / recovery +15% |
| 79 | # Candle Data for test 3 – Candle drops 3% Closed 15% up |
| 80 | # Test with Stop-loss at 2% ROI 6% |
| 81 | # Stop-Loss Triggered 2% Loss |
| 82 | tc4 = BTContainer(data=[ |
| 83 | # D O H L C V B S |
| 84 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 85 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 86 | [2, 4987, 5750, 4850, 5750, 6172, 0, 0], # Exit with stoploss hit |
| 87 | [3, 4975, 5000, 4950, 4962, 6172, 0, 0], |
| 88 | [4, 4962, 4987, 4937, 4950, 6172, 0, 0], |
| 89 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 90 | stop_loss=-0.02, roi={"0": 0.06}, profit_perc=-0.02, |
| 91 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)] |
| 92 | ) |
| 93 | |
| 94 | # Test 5: Drops 0.5% Closes +20%, ROI triggers 3% Gain |
| 95 | # stop-loss: 1%, ROI: 3% |
| 96 | tc5 = BTContainer(data=[ |
| 97 | # D O H L C V B S |
| 98 | [0, 5000, 5025, 4980, 4987, 6172, 1, 0], |
| 99 | [1, 5000, 5025, 4980, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 100 | [2, 4987, 5025, 4975, 4987, 6172, 0, 0], |
| 101 | [3, 4975, 6000, 4975, 6000, 6172, 0, 0], # ROI |
| 102 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 103 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 104 | stop_loss=-0.01, roi={"0": 0.03}, profit_perc=0.03, |
| 105 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 106 | ) |
| 107 | |
| 108 | # Test 6: Drops 3% / Recovers 6% Positive / Closes 1% positve, Stop-Loss triggers 2% Loss |
| 109 | # stop-loss: 2% ROI: 5% |
| 110 | tc6 = BTContainer(data=[ |
| 111 | # D O H L C V B S |
| 112 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 113 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 114 | [2, 4987, 5300, 4850, 5050, 6172, 0, 0], # Exit with stoploss |
| 115 | [3, 4975, 5000, 4950, 4962, 6172, 0, 0], |
| 116 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 117 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 118 | stop_loss=-0.02, roi={"0": 0.05}, profit_perc=-0.02, |
| 119 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)] |
| 120 | ) |
| 121 | |
| 122 | # Test 7: 6% Positive / 1% Negative / Close 1% Positve, ROI Triggers 3% Gain |
| 123 | # stop-loss: 2% ROI: 3% |
| 124 | tc7 = BTContainer(data=[ |
| 125 | # D O H L C V B S |
| 126 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 127 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 128 | [2, 4987, 5300, 4950, 5050, 6172, 0, 0], |
| 129 | [3, 4975, 5000, 4950, 4962, 6172, 0, 0], |
| 130 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 131 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 132 | stop_loss=-0.02, roi={"0": 0.03}, profit_perc=0.03, |
| 133 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=2)] |
| 134 | ) |
| 135 | |
| 136 | |
| 137 | # Test 8: trailing_stop should raise so candle 3 causes a stoploss. |
| 138 | # stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted in candle 2 |
| 139 | tc8 = BTContainer(data=[ |
| 140 | # D O H L C V B S |
| 141 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 142 | [1, 5000, 5050, 4950, 5000, 6172, 0, 0], |
| 143 | [2, 5000, 5250, 4750, 4850, 6172, 0, 0], |
| 144 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 145 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 146 | stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.055, trailing_stop=True, |
| 147 | trades=[BTrade(sell_reason=SellType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)] |
| 148 | ) |
| 149 | |
| 150 | |
| 151 | # Test 9: trailing_stop should raise - high and low in same candle. |
| 152 | # stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted in candle 3 |
| 153 | tc9 = BTContainer(data=[ |
| 154 | # D O H L C V B S |
| 155 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 156 | [1, 5000, 5050, 4950, 5000, 6172, 0, 0], |
| 157 | [2, 5000, 5050, 4950, 5000, 6172, 0, 0], |
| 158 | [3, 5000, 5200, 4550, 4850, 6172, 0, 0], |
| 159 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 160 | stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.064, trailing_stop=True, |
| 161 | trades=[BTrade(sell_reason=SellType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)] |
| 162 | ) |
| 163 | |
| 164 | # Test 10: trailing_stop should raise so candle 3 causes a stoploss |
| 165 | # without applying trailing_stop_positive since stoploss_offset is at 10%. |
| 166 | # stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2 |
| 167 | tc10 = BTContainer(data=[ |
| 168 | # D O H L C V B S |
| 169 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 170 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 171 | [2, 5100, 5251, 5100, 5100, 6172, 0, 0], |
| 172 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 173 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 174 | stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.1, trailing_stop=True, |
| 175 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.10, |
| 176 | trailing_stop_positive=0.03, |
| 177 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=4)] |
| 178 | ) |
| 179 | |
| 180 | # Test 11: trailing_stop should raise so candle 3 causes a stoploss |
| 181 | # applying a positive trailing stop of 3% since stop_positive_offset is reached. |
| 182 | # stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2 |
| 183 | tc11 = BTContainer(data=[ |
| 184 | # D O H L C V B S |
| 185 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 186 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 187 | [2, 5100, 5251, 5100, 5100, 6172, 0, 0], |
| 188 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 189 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 190 | stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True, |
| 191 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05, |
| 192 | trailing_stop_positive=0.03, |
| 193 | trades=[BTrade(sell_reason=SellType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)] |
| 194 | ) |
| 195 | |
| 196 | # Test 12: trailing_stop should raise in candle 2 and cause a stoploss in the same candle |
| 197 | # applying a positive trailing stop of 3% since stop_positive_offset is reached. |
| 198 | # stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2 |
| 199 | tc12 = BTContainer(data=[ |
| 200 | # D O H L C V B S |
| 201 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 202 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 203 | [2, 5100, 5251, 4650, 5100, 6172, 0, 0], |
| 204 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 205 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 206 | stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True, |
| 207 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05, |
| 208 | trailing_stop_positive=0.03, |
| 209 | trades=[BTrade(sell_reason=SellType.TRAILING_STOP_LOSS, open_tick=1, close_tick=2)] |
| 210 | ) |
| 211 | |
| 212 | # Test 13: Buy and sell ROI on same candle |
| 213 | # stop-loss: 10% (should not apply), ROI: 1% |
| 214 | tc13 = BTContainer(data=[ |
| 215 | # D O H L C V B S |
| 216 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 217 | [1, 5000, 5100, 4950, 5100, 6172, 0, 0], |
| 218 | [2, 5100, 5251, 4850, 5100, 6172, 0, 0], |
| 219 | [3, 4850, 5050, 4850, 4750, 6172, 0, 0], |
| 220 | [4, 4750, 4950, 4850, 4750, 6172, 0, 0]], |
| 221 | stop_loss=-0.10, roi={"0": 0.01}, profit_perc=0.01, |
| 222 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=1)] |
| 223 | ) |
| 224 | |
| 225 | # Test 14 - Buy and Stoploss on same candle |
| 226 | # stop-loss: 5%, ROI: 10% (should not apply) |
| 227 | tc14 = BTContainer(data=[ |
| 228 | # D O H L C V B S |
| 229 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 230 | [1, 5000, 5100, 4600, 5100, 6172, 0, 0], |
| 231 | [2, 5100, 5251, 4850, 5100, 6172, 0, 0], |
| 232 | [3, 4850, 5050, 4850, 4750, 6172, 0, 0], |
| 233 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 234 | stop_loss=-0.05, roi={"0": 0.10}, profit_perc=-0.05, |
| 235 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=1)] |
| 236 | ) |
| 237 | |
| 238 | |
| 239 | # Test 15 - Buy and ROI on same candle, followed by buy and Stoploss on next candle |
| 240 | # stop-loss: 5%, ROI: 10% (should not apply) |
| 241 | tc15 = BTContainer(data=[ |
| 242 | # D O H L C V B S |
| 243 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 244 | [1, 5000, 5100, 4900, 5100, 6172, 1, 0], |
| 245 | [2, 5100, 5251, 4650, 5100, 6172, 0, 0], |
| 246 | [3, 4850, 5050, 4850, 4750, 6172, 0, 0], |
| 247 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 248 | stop_loss=-0.05, roi={"0": 0.01}, profit_perc=-0.04, |
| 249 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=1), |
| 250 | BTrade(sell_reason=SellType.STOP_LOSS, open_tick=2, close_tick=2)] |
| 251 | ) |
| 252 | |
| 253 | # Test 16: Buy, hold for 65 min, then forcesell using roi=-1 |
| 254 | # Causes negative profit even though sell-reason is ROI. |
| 255 | # stop-loss: 10%, ROI: 10% (should not apply), -100% after 65 minutes (limits trade duration) |
| 256 | tc16 = BTContainer(data=[ |
| 257 | # D O H L C V B S |
| 258 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 259 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 260 | [2, 4987, 5300, 4950, 5050, 6172, 0, 0], |
| 261 | [3, 4975, 5000, 4940, 4962, 6172, 0, 0], # ForceSell on ROI (roi=-1) |
| 262 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 263 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 264 | stop_loss=-0.10, roi={"0": 0.10, "65": -1}, profit_perc=-0.012, |
| 265 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 266 | ) |
| 267 | |
| 268 | # Test 17: Buy, hold for 120 mins, then forcesell using roi=-1 |
| 269 | # Causes negative profit even though sell-reason is ROI. |
| 270 | # stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration) |
| 271 | # Uses open as sell-rate (special case) - since the roi-time is a multiple of the timeframe. |
| 272 | tc17 = BTContainer(data=[ |
| 273 | # D O H L C V B S |
| 274 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 275 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 276 | [2, 4987, 5300, 4950, 5050, 6172, 0, 0], |
| 277 | [3, 4980, 5000, 4940, 4962, 6172, 0, 0], # ForceSell on ROI (roi=-1) |
| 278 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 279 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 280 | stop_loss=-0.10, roi={"0": 0.10, "120": -1}, profit_perc=-0.004, |
| 281 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 282 | ) |
| 283 | |
| 284 | |
| 285 | # Test 18: Buy, hold for 120 mins, then drop ROI to 1%, causing a sell in candle 3. |
| 286 | # stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration) |
| 287 | # uses open_rate as sell-price |
| 288 | tc18 = BTContainer(data=[ |
| 289 | # D O H L C V B S |
| 290 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 291 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 292 | [2, 4987, 5300, 4950, 5200, 6172, 0, 0], |
| 293 | [3, 5200, 5220, 4940, 4962, 6172, 0, 0], # Sell on ROI (sells on open) |
| 294 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 295 | [5, 4950, 4975, 4925, 4950, 6172, 0, 0]], |
| 296 | stop_loss=-0.10, roi={"0": 0.10, "120": 0.01}, profit_perc=0.04, |
| 297 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 298 | ) |
| 299 | |
| 300 | # Test 19: Buy, hold for 119 mins, then drop ROI to 1%, causing a sell in candle 3. |
| 301 | # stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration) |
| 302 | # uses calculated ROI (1%) as sell rate, otherwise identical to tc18 |
| 303 | tc19 = BTContainer(data=[ |
| 304 | # D O H L C V B S |
| 305 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 306 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 307 | [2, 4987, 5300, 4950, 5200, 6172, 0, 0], |
| 308 | [3, 5000, 5300, 4940, 4962, 6172, 0, 0], # Sell on ROI |
| 309 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 310 | [5, 4550, 4975, 4925, 4950, 6172, 0, 0]], |
| 311 | stop_loss=-0.10, roi={"0": 0.10, "120": 0.01}, profit_perc=0.01, |
| 312 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 313 | ) |
| 314 | |
| 315 | # Test 20: Buy, hold for 119 mins, then drop ROI to 1%, causing a sell in candle 3. |
| 316 | # stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration) |
| 317 | # uses calculated ROI (1%) as sell rate, otherwise identical to tc18 |
| 318 | tc20 = BTContainer(data=[ |
| 319 | # D O H L C V B S |
| 320 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 321 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], |
| 322 | [2, 4987, 5300, 4950, 5200, 6172, 0, 0], |
| 323 | [3, 5200, 5300, 4940, 4962, 6172, 0, 0], # Sell on ROI |
| 324 | [4, 4962, 4987, 4972, 4950, 6172, 0, 0], |
| 325 | [5, 4550, 4975, 4925, 4950, 6172, 0, 0]], |
| 326 | stop_loss=-0.10, roi={"0": 0.10, "119": 0.01}, profit_perc=0.01, |
| 327 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 328 | ) |
| 329 | |
| 330 | # Test 21: trailing_stop ROI collision. |
| 331 | # Roi should trigger before Trailing stop - otherwise Trailing stop profits can be > ROI |
| 332 | # which cannot happen in reality |
| 333 | # stop-loss: 10%, ROI: 4%, Trailing stop adjusted at the sell candle |
| 334 | tc21 = BTContainer(data=[ |
| 335 | # D O H L C V B S |
| 336 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 337 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 338 | [2, 5100, 5251, 4650, 5100, 6172, 0, 0], |
| 339 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 340 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 341 | stop_loss=-0.10, roi={"0": 0.04}, profit_perc=0.04, trailing_stop=True, |
| 342 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05, |
| 343 | trailing_stop_positive=0.03, |
| 344 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=2)] |
| 345 | ) |
| 346 | |
| 347 | # Test 22: trailing_stop Raises in candle 2 - but ROI applies at the same time. |
| 348 | # applying a positive trailing stop of 3% - ROI should apply before trailing stop. |
| 349 | # stop-loss: 10%, ROI: 4%, stoploss adjusted candle 2 |
| 350 | tc22 = BTContainer(data=[ |
| 351 | # D O H L C V B S |
| 352 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 353 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 354 | [2, 5100, 5251, 5100, 5100, 6172, 0, 0], |
| 355 | [3, 4850, 5050, 4650, 4750, 6172, 0, 0], |
| 356 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 357 | stop_loss=-0.10, roi={"0": 0.04}, profit_perc=0.04, trailing_stop=True, |
| 358 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05, |
| 359 | trailing_stop_positive=0.03, |
| 360 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=2)] |
| 361 | ) |
| 362 | |
| 363 | # Test 23: trailing_stop Raises in candle 2 (does not trigger) |
| 364 | # applying a positive trailing stop of 3% since stop_positive_offset is reached. |
| 365 | # ROI is changed after this to 4%, dropping ROI below trailing_stop_positive, causing a sell |
| 366 | # in the candle after the raised stoploss candle with ROI reason. |
| 367 | # Stoploss would trigger in this candle too, but it's no longer relevant. |
| 368 | # stop-loss: 10%, ROI: 4%, stoploss adjusted candle 2, ROI adjusted in candle 3 (causing the sell) |
| 369 | tc23 = BTContainer(data=[ |
| 370 | # D O H L C V B S |
| 371 | [0, 5000, 5050, 4950, 5000, 6172, 1, 0], |
| 372 | [1, 5000, 5050, 4950, 5100, 6172, 0, 0], |
| 373 | [2, 5100, 5251, 5100, 5100, 6172, 0, 0], |
| 374 | [3, 4850, 5251, 4650, 4750, 6172, 0, 0], |
| 375 | [4, 4750, 4950, 4350, 4750, 6172, 0, 0]], |
| 376 | stop_loss=-0.10, roi={"0": 0.1, "119": 0.03}, profit_perc=0.03, trailing_stop=True, |
| 377 | trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05, |
| 378 | trailing_stop_positive=0.03, |
| 379 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 380 | ) |
| 381 | |
| 382 | # Test 24: Sell with signal sell in candle 3 (stoploss also triggers on this candle) |
| 383 | # Stoploss at 1%. |
| 384 | # Stoploss wins over Sell-signal (because sell-signal is acted on in the next candle) |
| 385 | tc24 = BTContainer(data=[ |
| 386 | # D O H L C V B S |
| 387 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 388 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 389 | [2, 4987, 5012, 4986, 4600, 6172, 0, 0], |
| 390 | [3, 5010, 5000, 4855, 5010, 6172, 0, 1], # Triggers stoploss + sellsignal |
| 391 | [4, 5010, 4987, 4977, 4995, 6172, 0, 0], |
| 392 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 393 | stop_loss=-0.01, roi={"0": 1}, profit_perc=-0.01, use_sell_signal=True, |
| 394 | trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=3)] |
| 395 | ) |
| 396 | |
| 397 | # Test 25: Sell with signal sell in candle 3 (stoploss also triggers on this candle) |
| 398 | # Stoploss at 1%. |
| 399 | # Sell-signal wins over stoploss |
| 400 | tc25 = BTContainer(data=[ |
| 401 | # D O H L C V B S |
| 402 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 403 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 404 | [2, 4987, 5012, 4986, 4600, 6172, 0, 0], |
| 405 | [3, 5010, 5000, 4986, 5010, 6172, 0, 1], |
| 406 | [4, 5010, 4987, 4855, 4995, 6172, 0, 0], # Triggers stoploss + sellsignal acted on |
| 407 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 408 | stop_loss=-0.01, roi={"0": 1}, profit_perc=0.002, use_sell_signal=True, |
| 409 | trades=[BTrade(sell_reason=SellType.SELL_SIGNAL, open_tick=1, close_tick=4)] |
| 410 | ) |
| 411 | |
| 412 | # Test 26: Sell with signal sell in candle 3 (ROI at signal candle) |
| 413 | # Stoploss at 10% (irrelevant), ROI at 5% (will trigger) |
| 414 | # Sell-signal wins over stoploss |
| 415 | tc26 = BTContainer(data=[ |
| 416 | # D O H L C V B S |
| 417 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 418 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 419 | [2, 4987, 5012, 4986, 4600, 6172, 0, 0], |
| 420 | [3, 5010, 5251, 4986, 5010, 6172, 0, 1], # Triggers ROI, sell-signal |
| 421 | [4, 5010, 4987, 4855, 4995, 6172, 0, 0], |
| 422 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 423 | stop_loss=-0.10, roi={"0": 0.05}, profit_perc=0.05, use_sell_signal=True, |
| 424 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)] |
| 425 | ) |
| 426 | |
| 427 | # Test 27: Sell with signal sell in candle 3 (ROI at signal candle) |
| 428 | # Stoploss at 10% (irrelevant), ROI at 5% (will trigger) - Wins over Sell-signal |
| 429 | # TODO: figure out if sell-signal should win over ROI |
| 430 | # Sell-signal wins over stoploss |
| 431 | tc27 = BTContainer(data=[ |
| 432 | # D O H L C V B S |
| 433 | [0, 5000, 5025, 4975, 4987, 6172, 1, 0], |
| 434 | [1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle) |
| 435 | [2, 4987, 5012, 4986, 4600, 6172, 0, 0], |
| 436 | [3, 5010, 5012, 4986, 5010, 6172, 0, 1], # sell-signal |
| 437 | [4, 5010, 5251, 4855, 4995, 6172, 0, 0], # Triggers ROI, sell-signal acted on |
| 438 | [5, 4995, 4995, 4995, 4950, 6172, 0, 0]], |
| 439 | stop_loss=-0.10, roi={"0": 0.05}, profit_perc=0.05, use_sell_signal=True, |
| 440 | trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=4)] |
| 441 | ) |
| 442 | |
| 443 | TESTS = [ |
| 444 | tc0, |
| 445 | tc1, |
| 446 | tc2, |
| 447 | tc3, |
| 448 | tc4, |
| 449 | tc5, |
| 450 | tc6, |
| 451 | tc7, |
| 452 | tc8, |
| 453 | tc9, |
| 454 | tc10, |
| 455 | tc11, |
| 456 | tc12, |
| 457 | tc13, |
| 458 | tc14, |
| 459 | tc15, |
| 460 | tc16, |
| 461 | tc17, |
| 462 | tc18, |
| 463 | tc19, |
| 464 | tc20, |
| 465 | tc21, |
| 466 | tc22, |
| 467 | tc23, |
| 468 | tc24, |
| 469 | tc25, |
| 470 | tc26, |
| 471 | tc27, |
| 472 | ] |
| 473 | |
| 474 | |
| 475 | @pytest.mark.parametrize("data", TESTS) |
| 476 | def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None: |
| 477 | """ |
| 478 | run functional tests |
| 479 | """ |
| 480 | default_conf["stoploss"] = data.stop_loss |
| 481 | default_conf["minimal_roi"] = data.roi |
| 482 | default_conf["timeframe"] = tests_timeframe |
| 483 | default_conf["trailing_stop"] = data.trailing_stop |
| 484 | default_conf["trailing_only_offset_is_reached"] = data.trailing_only_offset_is_reached |
| 485 | # Only add this to configuration If it's necessary |
| 486 | if data.trailing_stop_positive is not None: |
| 487 | default_conf["trailing_stop_positive"] = data.trailing_stop_positive |
| 488 | default_conf["trailing_stop_positive_offset"] = data.trailing_stop_positive_offset |
| 489 | default_conf["ask_strategy"] = {"use_sell_signal": data.use_sell_signal} |
| 490 | |
| 491 | mocker.patch("freqtrade.exchange.Exchange.get_fee", return_value=0.0) |
| 492 | mocker.patch("freqtrade.exchange.Exchange.get_min_pair_stake_amount", return_value=0.00001) |
| 493 | patch_exchange(mocker) |
| 494 | frame = _build_backtest_dataframe(data.data) |
| 495 | backtesting = Backtesting(default_conf) |
| 496 | backtesting.strategy.advise_buy = lambda a, m: frame |
| 497 | backtesting.strategy.advise_sell = lambda a, m: frame |
| 498 | caplog.set_level(logging.DEBUG) |
| 499 | |
| 500 | pair = "UNITTEST/BTC" |
| 501 | # Dummy data as we mock the analyze functions |
| 502 | data_processed = {pair: frame.copy()} |
| 503 | min_date, max_date = get_timerange({pair: frame}) |
| 504 | results = backtesting.backtest( |
| 505 | processed=data_processed, |
| 506 | start_date=min_date, |
| 507 | end_date=max_date, |
| 508 | max_open_trades=10, |
| 509 | ) |
| 510 | |
| 511 | assert len(results) == len(data.trades) |
| 512 | assert round(results["profit_ratio"].sum(), 3) == round(data.profit_perc, 3) |
| 513 | |
| 514 | for c, trade in enumerate(data.trades): |
| 515 | res = results.iloc[c] |
| 516 | assert res.sell_reason == trade.sell_reason.value |
| 517 | assert res.open_date == _get_frame_time_from_offset(trade.open_tick) |
| 518 | assert res.close_date == _get_frame_time_from_offset(trade.close_tick) |