freqtrade_origin/tests/optimize/test_backtest_detail.py

974 lines
42 KiB
Python
Raw Normal View History

# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, C0330, unused-argument
2018-07-09 19:38:49 +00:00
import logging
2022-02-05 14:12:29 +00:00
from unittest.mock import MagicMock
2018-07-09 19:38:49 +00:00
import pytest
2019-12-17 22:06:03 +00:00
from freqtrade.data.history import get_timerange
from freqtrade.enums import ExitType, TradingMode
2018-07-09 19:38:49 +00:00
from freqtrade.optimize.backtesting import Backtesting
from freqtrade.persistence.trade_model import LocalTrade
from tests.conftest import EXMS, patch_exchange
2019-09-08 07:54:15 +00:00
from tests.optimize import (BTContainer, BTrade, _build_backtest_dataframe,
_get_frame_time_from_offset, tests_timeframe)
2018-10-29 19:17:15 +00:00
2020-09-28 17:43:15 +00:00
2019-08-05 18:19:19 +00:00
# Test 0: Sell with signal sell in candle 3
2019-06-13 18:00:56 +00:00
# Test with Stop-loss at 1%
tc0 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2019-06-13 18:00:56 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
2021-10-30 13:58:14 +00:00
[2, 4987, 5012, 4986, 4986, 6172, 0, 0], # exit with stoploss hit
2021-10-30 14:10:28 +00:00
[3, 5010, 5010, 4980, 5010, 6172, 0, 1],
[4, 5010, 5011, 4977, 4995, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.01, roi={"0": 1}, profit_perc=0.002, use_exit_signal=True,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4)]
2019-06-13 18:00:56 +00:00
)
2019-08-05 18:19:19 +00:00
# Test 1: Stop-Loss Triggered 1% loss
# Test with Stop-loss at 1%
2019-03-17 12:27:32 +00:00
tc1 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5012, 4600, 4600, 6172, 0, 0], # exit with stoploss hit
2021-10-30 14:10:28 +00:00
[3, 4975, 5000, 4975, 4977, 6172, 0, 0],
[4, 4977, 4995, 4977, 4995, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 1}, profit_perc=-0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2)]
)
2019-08-05 18:19:19 +00:00
# Test 2: Minus 4% Low, minus 1% close
# Test with Stop-Loss at 3%
2019-03-17 12:27:32 +00:00
tc2 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5012, 4962, 4975, 6172, 0, 0],
[3, 4975, 5000, 4800, 4962, 6172, 0, 0], # exit with stoploss hit
[4, 4962, 4987, 4937, 4950, 6172, 0, 0],
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.03, roi={"0": 1}, profit_perc=-0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=3)]
)
2019-08-05 18:19:19 +00:00
# Test 3: Multiple trades.
# Candle drops 4%, Recovers 1%.
# Entry Criteria Met
# Candle drops 20%
# Trade-A: Stop-Loss Triggered 2% Loss
# Trade-B: Stop-Loss Triggered 2% Loss
2019-03-17 12:27:32 +00:00
tc3 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5012, 4800, 4975, 6172, 0, 0], # exit with stoploss hit
[3, 4975, 5000, 4950, 4962, 6172, 1, 0],
[4, 4975, 5000, 4950, 4962, 6172, 0, 0], # enter trade 2 (signal on last candle)
[5, 4962, 4987, 4000, 4000, 6172, 0, 0], # exit with stoploss hit
2021-10-30 13:58:14 +00:00
[6, 4950, 4975, 4950, 4950, 6172, 0, 0]],
stop_loss=-0.02, roi={"0": 1}, profit_perc=-0.04,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2),
BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=4, close_tick=5)]
)
2019-08-05 18:19:19 +00:00
# Test 4: Minus 3% / recovery +15%
2018-10-30 18:36:19 +00:00
# Candle Data for test 3 Candle drops 3% Closed 15% up
# Test with Stop-loss at 2% ROI 6%
2019-08-05 18:19:19 +00:00
# Stop-Loss Triggered 2% Loss
2019-03-17 12:27:32 +00:00
tc4 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5750, 4850, 5750, 6172, 0, 0], # Exit with stoploss hit
[3, 4975, 5000, 4950, 4962, 6172, 0, 0],
[4, 4962, 4987, 4937, 4950, 6172, 0, 0],
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.02, roi={"0": 0.06}, profit_perc=-0.02,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2)]
)
2019-08-05 18:19:19 +00:00
# Test 5: Drops 0.5% Closes +20%, ROI triggers 3% Gain
# stop-loss: 1%, ROI: 3%
2019-03-17 12:27:32 +00:00
tc5 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4980, 4987, 6172, 1, 0],
[1, 5000, 5025, 4980, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5025, 4975, 4987, 6172, 0, 0],
[3, 4975, 6000, 4975, 6000, 6172, 0, 0], # ROI
2021-10-30 14:10:28 +00:00
[4, 4962, 4987, 4962, 4972, 6172, 0, 0],
2018-10-30 19:42:34 +00:00
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.03}, profit_perc=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
2019-08-05 18:19:19 +00:00
# Test 6: Drops 3% / Recovers 6% Positive / Closes 1% positve, Stop-Loss triggers 2% Loss
# stop-loss: 2% ROI: 5%
2019-03-17 12:27:32 +00:00
tc6 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5300, 4850, 5050, 6172, 0, 0], # Exit with stoploss
[3, 4975, 5000, 4950, 4962, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
2018-10-30 19:42:34 +00:00
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.02, roi={"0": 0.05}, profit_perc=-0.02,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2)]
)
2019-08-05 18:19:19 +00:00
# Test 7: 6% Positive / 1% Negative / Close 1% Positve, ROI Triggers 3% Gain
# stop-loss: 2% ROI: 3%
2019-03-17 12:27:32 +00:00
tc7 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2018-10-30 19:42:34 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5050, 6172, 0, 0],
[3, 4975, 5000, 4950, 4962, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
2018-10-30 19:42:34 +00:00
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.02, roi={"0": 0.03}, profit_perc=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2)]
)
2019-03-17 15:01:34 +00:00
2019-08-05 18:19:19 +00:00
# Test 8: trailing_stop should raise so candle 3 causes a stoploss.
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted in candle 2
2019-03-17 15:01:34 +00:00
tc8 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2019-03-17 15:01:34 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5050, 4950, 5000, 6172, 0, 0],
[2, 5000, 5250, 4750, 4850, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.055, trailing_stop=True,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)]
2019-03-17 15:01:34 +00:00
)
2019-03-17 15:26:38 +00:00
2019-08-05 18:19:19 +00:00
# Test 9: trailing_stop should raise - high and low in same candle.
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted in candle 3
2019-03-17 15:26:38 +00:00
tc9 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2019-03-17 15:26:38 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5050, 4950, 5000, 6172, 0, 0],
[2, 5000, 5050, 4950, 5000, 6172, 0, 0],
[3, 5000, 5200, 4550, 4850, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.064, trailing_stop=True,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)]
2019-03-17 15:26:38 +00:00
)
2019-08-05 18:19:19 +00:00
# Test 10: trailing_stop should raise so candle 3 causes a stoploss
# without applying trailing_stop_positive since stoploss_offset is at 10%.
2019-08-05 18:19:19 +00:00
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
tc10 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.1, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.10,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=4)]
)
2019-08-05 18:19:19 +00:00
# Test 11: trailing_stop should raise so candle 3 causes a stoploss
# applying a positive trailing stop of 3% since stop_positive_offset is reached.
2019-08-05 18:19:19 +00:00
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
tc11 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
[3, 5000, 5150, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)]
)
2019-08-05 18:19:19 +00:00
# Test 12: trailing_stop should raise in candle 2 and cause a stoploss in the same candle
# applying a positive trailing stop of 3% since stop_positive_offset is reached.
2019-08-05 18:19:19 +00:00
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
tc12 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 4650, 5100, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.019, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=2)]
)
2019-08-05 18:19:19 +00:00
# Test 13: Buy and sell ROI on same candle
# stop-loss: 10% (should not apply), ROI: 1%
tc13 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 4850, 5100, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[3, 4850, 5050, 4750, 4750, 6172, 0, 0],
[4, 4750, 4950, 4750, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.01}, profit_perc=0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=1)]
)
# Test 14 - Buy and Stoploss on same candle
2019-08-05 18:19:19 +00:00
# stop-loss: 5%, ROI: 10% (should not apply)
tc14 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5100, 4600, 5100, 6172, 0, 0],
[2, 5100, 5251, 4850, 5100, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[3, 4850, 5050, 4750, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.05, roi={"0": 0.10}, profit_perc=-0.05,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)]
)
# Test 15 - Buy and ROI on same candle, followed by buy and Stoploss on next candle
2019-08-05 18:19:19 +00:00
# stop-loss: 5%, ROI: 10% (should not apply)
tc15 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5100, 4900, 5100, 6172, 1, 0],
[2, 5100, 5251, 4650, 5100, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[3, 4850, 5050, 4750, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.05, roi={"0": 0.01}, profit_perc=-0.04,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=1),
BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=2, close_tick=2)]
)
2022-04-10 13:56:29 +00:00
# Test 16: Buy, hold for 65 min, then forceexit using roi=-1
# Causes negative profit even though sell-reason is ROI.
# stop-loss: 10%, ROI: 10% (should not apply), -100% after 65 minutes (limits trade duration)
tc16 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5050, 6172, 0, 0],
2022-04-10 13:56:29 +00:00
[3, 4975, 5000, 4940, 4962, 6172, 0, 0], # Forceexit on ROI (roi=-1)
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10, "65": -1}, profit_perc=-0.012,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
2022-04-10 13:56:29 +00:00
# Test 17: Buy, hold for 120 mins, then forceexit using roi=-1
# Causes negative profit even though sell-reason is ROI.
# stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration)
2021-04-03 14:54:47 +00:00
# Uses open as sell-rate (special case) - since the roi-time is a multiple of the timeframe.
tc17 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5050, 6172, 0, 0],
2022-04-10 13:56:29 +00:00
[3, 4980, 5000, 4940, 4962, 6172, 0, 0], # Forceexit on ROI (roi=-1)
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10, "120": -1}, profit_perc=-0.004,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
# Test 18: Buy, hold for 120 mins, then drop ROI to 1%, causing a sell in candle 3.
# stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration)
# uses open_rate as sell-price
tc18 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5200, 6172, 0, 0],
[3, 5200, 5220, 4940, 4962, 6172, 0, 0], # Sell on ROI (sells on open)
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
[5, 4950, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10, "120": 0.01}, profit_perc=0.04,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
# Test 19: Buy, hold for 119 mins, then drop ROI to 1%, causing a sell in candle 3.
# stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration)
# uses calculated ROI (1%) as sell rate, otherwise identical to tc18
tc19 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5200, 6172, 0, 0],
[3, 5000, 5300, 4940, 4962, 6172, 0, 0], # Sell on ROI
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
2021-10-30 14:10:28 +00:00
[5, 4550, 4975, 4550, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10, "120": 0.01}, profit_perc=0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
# Test 20: Buy, hold for 119 mins, then drop ROI to 1%, causing a sell in candle 3.
# stop-loss: 10%, ROI: 10% (should not apply), -100% after 100 minutes (limits trade duration)
# uses calculated ROI (1%) as sell rate, otherwise identical to tc18
tc20 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0],
[2, 4987, 5300, 4950, 5200, 6172, 0, 0],
[3, 5200, 5300, 4940, 4962, 6172, 0, 0], # Sell on ROI
2021-10-30 13:58:14 +00:00
[4, 4962, 4987, 4950, 4950, 6172, 0, 0],
2021-10-30 14:10:28 +00:00
[5, 4925, 4975, 4925, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10, "119": 0.01}, profit_perc=0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
# Test 21: trailing_stop ROI collision.
# Roi should trigger before Trailing stop - otherwise Trailing stop profits can be > ROI
# which cannot happen in reality
# stop-loss: 10%, ROI: 4%, Trailing stop adjusted at the sell candle
tc21 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 4650, 5100, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.04}, profit_perc=0.04, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2)]
)
# Test 22: trailing_stop Raises in candle 2 - but ROI applies at the same time.
# applying a positive trailing stop of 3% - ROI should apply before trailing stop.
# stop-loss: 10%, ROI: 4%, stoploss adjusted candle 2
tc22 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.04}, profit_perc=0.04, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2)]
)
2022-03-16 18:51:12 +00:00
2022-04-01 05:00:51 +00:00
# Test 23: trailing_stop Raises in candle 2 - but ROI applies at the same time.
2022-03-16 18:51:12 +00:00
# applying a positive trailing stop of 3% - ROI should apply before trailing stop.
# stop-loss: 10%, ROI: 4%, stoploss adjusted candle 2
2022-04-01 05:00:51 +00:00
tc23 = BTContainer(data=[
2022-03-16 18:51:12 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5050, 4900, 4900, 6172, 0, 0, 0, 0],
[2, 4900, 4900, 4749, 4900, 6172, 0, 0, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]],
stop_loss=-0.10, roi={"0": 0.04}, profit_perc=0.04, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2, is_short=True)]
2022-03-16 18:51:12 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 24: trailing_stop Raises in candle 2 (does not trigger)
# applying a positive trailing stop of 3% since stop_positive_offset is reached.
# ROI is changed after this to 4%, dropping ROI below trailing_stop_positive, causing a sell
# in the candle after the raised stoploss candle with ROI reason.
# Stoploss would trigger in this candle too, but it's no longer relevant.
# stop-loss: 10%, ROI: 4%, stoploss adjusted candle 2, ROI adjusted in candle 3 (causing the sell)
2022-04-01 05:00:51 +00:00
tc24 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
[3, 4850, 5251, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.1, "119": 0.03}, profit_perc=0.03, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
)
2022-04-01 05:00:51 +00:00
# Test 25: Sell with signal sell in candle 3 (stoploss also triggers on this candle)
2020-11-27 08:17:25 +00:00
# Stoploss at 1%.
# Stoploss wins over Sell-signal (because sell-signal is acted on in the next candle)
2022-04-01 05:00:51 +00:00
tc25 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2020-11-27 08:17:25 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
2021-10-30 13:58:14 +00:00
[2, 4987, 5012, 4986, 4986, 6172, 0, 0],
2021-10-30 14:10:28 +00:00
[3, 5010, 5010, 4855, 5010, 6172, 0, 1], # Triggers stoploss + sellsignal
[4, 5010, 5010, 4977, 4995, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.01, roi={"0": 1}, profit_perc=-0.01, use_exit_signal=True,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=3)]
2020-11-27 08:17:25 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 26: Sell with signal sell in candle 3 (stoploss also triggers on this candle)
2020-11-27 08:17:25 +00:00
# Stoploss at 1%.
# Sell-signal wins over stoploss
2022-04-01 05:00:51 +00:00
tc26 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2020-11-27 08:17:25 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
2021-10-30 13:58:14 +00:00
[2, 4987, 5012, 4986, 4986, 6172, 0, 0],
2021-10-30 14:10:28 +00:00
[3, 5010, 5010, 4986, 5010, 6172, 0, 1],
[4, 5010, 5010, 4855, 4995, 6172, 0, 0], # Triggers stoploss + sellsignal acted on
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.01, roi={"0": 1}, profit_perc=0.002, use_exit_signal=True,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4)]
2020-11-27 08:17:25 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 27: (copy of test26 with leverage)
2022-03-16 18:54:29 +00:00
# Sell with signal sell in candle 3 (stoploss also triggers on this candle)
# Stoploss at 1%.
# Sell-signal wins over stoploss
2022-04-01 05:00:51 +00:00
tc27 = BTContainer(data=[
2022-03-16 18:54:29 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5012, 4986, 4986, 6172, 0, 0],
[3, 5010, 5010, 4986, 5010, 6172, 0, 1],
[4, 5010, 5010, 4855, 4995, 6172, 0, 0], # Triggers stoploss + sellsignal acted on
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.05, roi={"0": 1}, profit_perc=0.002 * 5.0, use_exit_signal=True,
2022-03-16 18:54:29 +00:00
leverage=5.0,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4)]
2022-03-16 18:54:29 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 28: (copy of test26 with leverage and as short)
2022-03-16 18:54:29 +00:00
# Sell with signal sell in candle 3 (stoploss also triggers on this candle)
# Stoploss at 1%.
# Sell-signal wins over stoploss
2022-04-01 05:00:51 +00:00
tc28 = BTContainer(data=[
2022-03-16 18:54:29 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5025, 4975, 4987, 6172, 0, 0, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0, 0, 0], # enter trade (signal on last candle)
[2, 4987, 5012, 4986, 4986, 6172, 0, 0, 0, 0],
[3, 5010, 5010, 4986, 5010, 6172, 0, 0, 0, 1],
[4, 4990, 5010, 4855, 4995, 6172, 0, 0, 0, 0], # Triggers stoploss + sellsignal acted on
[5, 4995, 4995, 4950, 4950, 6172, 0, 0, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.05, roi={"0": 1}, profit_perc=0.002 * 5.0, use_exit_signal=True,
2022-03-16 18:54:29 +00:00
leverage=5.0,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4, is_short=True)]
2022-03-16 18:54:29 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 29: Sell with signal sell in candle 3 (ROI at signal candle)
2020-11-27 08:17:25 +00:00
# Stoploss at 10% (irrelevant), ROI at 5% (will trigger)
# Sell-signal wins over stoploss
2022-04-01 05:00:51 +00:00
tc29 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2020-11-27 08:17:25 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
2021-10-30 13:58:14 +00:00
[2, 4987, 5012, 4986, 4986, 6172, 0, 0],
2020-11-27 08:17:25 +00:00
[3, 5010, 5251, 4986, 5010, 6172, 0, 1], # Triggers ROI, sell-signal
2021-10-30 14:10:28 +00:00
[4, 5010, 5010, 4855, 4995, 6172, 0, 0],
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.10, roi={"0": 0.05}, profit_perc=0.05, use_exit_signal=True,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=3)]
2020-11-27 08:17:25 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 30: Sell with signal sell in candle 3 (ROI at signal candle)
2020-11-27 08:17:25 +00:00
# Stoploss at 10% (irrelevant), ROI at 5% (will trigger) - Wins over Sell-signal
2022-04-01 05:00:51 +00:00
tc30 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2020-11-27 08:17:25 +00:00
[0, 5000, 5025, 4975, 4987, 6172, 1, 0],
[1, 5000, 5025, 4975, 4987, 6172, 0, 0], # enter trade (signal on last candle)
2021-10-30 13:58:14 +00:00
[2, 4987, 5012, 4986, 4986, 6172, 0, 0],
2020-11-27 08:17:25 +00:00
[3, 5010, 5012, 4986, 5010, 6172, 0, 1], # sell-signal
[4, 5010, 5251, 4855, 4995, 6172, 0, 0], # Triggers ROI, sell-signal acted on
2021-10-30 13:58:14 +00:00
[5, 4995, 4995, 4950, 4950, 6172, 0, 0]],
2022-04-03 17:27:30 +00:00
stop_loss=-0.10, roi={"0": 0.05}, profit_perc=0.002, use_exit_signal=True,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4)]
2020-11-27 08:17:25 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 31: trailing_stop should raise so candle 3 causes a stoploss
# Same case than tc11 - but candle 3 "gaps down" - the stoploss will be above the candle,
# therefore "open" will be used
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
2022-04-01 05:00:51 +00:00
tc31 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 14:10:28 +00:00
[1, 5000, 5100, 4950, 5100, 6172, 0, 0],
[2, 5100, 5251, 5100, 5100, 6172, 0, 0],
[3, 4850, 5050, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.03, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3)]
)
2022-04-01 05:00:51 +00:00
# Test 32: (Short of test 31) trailing_stop should raise so candle 3 causes a stoploss
2022-03-16 20:28:24 +00:00
# Same case than tc11 - but candle 3 "gaps down" - the stoploss will be above the candle,
# therefore "open" will be used
# stop-loss: 10%, ROI: 10% (should not apply), stoploss adjusted candle 2
2022-04-01 05:00:51 +00:00
tc32 = BTContainer(data=[
2022-03-16 20:28:24 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5050, 4890, 4890, 6172, 0, 0, 0, 0],
[2, 4890, 4890, 4749, 4890, 6172, 0, 0, 0, 0],
[3, 5150, 5350, 4950, 4950, 6172, 0, 0, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.03, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.05,
trailing_stop_positive=0.03,
2022-03-16 20:31:34 +00:00
trades=[
2022-03-24 19:33:47 +00:00
BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=3, is_short=True)
]
2022-03-16 20:28:24 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 33: trailing_stop should be triggered by low of next candle, without adjusting stoploss using
# high of stoploss candle.
# stop-loss: 10%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc33 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 13:58:14 +00:00
[1, 5000, 5050, 5000, 5000, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4900, 5250, 4500, 5100, 6172, 0, 0], # Triggers trailing-stoploss
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.02, trailing_stop=True,
trailing_stop_positive=0.03,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=2)]
)
2022-04-01 05:00:51 +00:00
# Test 34: trailing_stop should be triggered immediately on trade open candle.
# stop-loss: 10%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc34 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 13:58:14 +00:00
[1, 5000, 5500, 4900, 4900, 6172, 0, 0], # enter trade (signal on last candle) and stop
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.01, trailing_stop=True,
trailing_stop_positive=0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=1)]
)
2022-04-01 05:00:51 +00:00
# Test 35: trailing_stop should be triggered immediately on trade open candle.
# stop-loss: 10%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc35 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-10-30 13:58:14 +00:00
[1, 5000, 5500, 4900, 4900, 6172, 0, 0], # enter trade (signal on last candle) and stop
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.01, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.02,
trailing_stop_positive=0.01,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=1)]
)
2022-04-01 05:00:51 +00:00
# Test 36: trailing_stop should be triggered immediately on trade open candle.
# stop-loss: 1%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc36 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2021-11-06 14:24:52 +00:00
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # enter trade and stop
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.02,
trailing_stop_positive=0.01, use_custom_stoploss=True,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.TRAILING_STOP_LOSS, open_tick=1, close_tick=1)]
)
2022-04-01 05:00:51 +00:00
# Test 37: trailing_stop should be triggered immediately on trade open candle.
2021-07-20 16:25:00 +00:00
# stop-loss: 1%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc37 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0, 0, 0, 'buy_signal_01'],
2021-11-06 14:24:52 +00:00
[1, 5000, 5500, 4951, 5000, 6172, 0, 0, 0, 0, None], # enter trade and stop
[2, 4900, 5250, 4500, 5100, 6172, 0, 0, 0, 0, None],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 0, None],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0, None]],
2021-07-20 16:25:00 +00:00
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.02,
trailing_stop_positive=0.01, use_custom_stoploss=True,
2021-07-20 16:56:03 +00:00
trades=[BTrade(
2022-03-24 19:33:47 +00:00
exit_reason=ExitType.TRAILING_STOP_LOSS,
2021-07-20 16:56:03 +00:00
open_tick=1,
close_tick=1,
2021-09-26 13:20:59 +00:00
enter_tag='buy_signal_01'
2021-07-20 16:56:03 +00:00
)]
2021-07-20 16:25:00 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 38: trailing_stop should be triggered immediately on trade open candle.
# copy of Test37 using shorts.
2022-03-16 18:26:08 +00:00
# stop-loss: 1%, ROI: 10% (should not apply)
2022-04-01 05:00:51 +00:00
tc38 = BTContainer(data=[
2022-03-16 18:26:08 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0, 'short_signal_01'],
[1, 5000, 5049, 4500, 5000, 6172, 0, 0, 0, 0, None], # enter trade and stop
[2, 4900, 5250, 4500, 5100, 6172, 0, 0, 0, 0, None],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 0, None],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0, None]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01, trailing_stop=True,
trailing_only_offset_is_reached=True, trailing_stop_positive_offset=0.02,
trailing_stop_positive=0.01, use_custom_stoploss=True,
trades=[BTrade(
2022-03-24 19:33:47 +00:00
exit_reason=ExitType.TRAILING_STOP_LOSS,
2022-03-16 18:26:08 +00:00
open_tick=1,
close_tick=1,
enter_tag='short_signal_01',
is_short=True,
)]
)
2021-07-20 16:25:00 +00:00
2022-04-01 05:00:51 +00:00
# Test 39: Custom-entry-price below all candles should timeout - so no trade happens.
tc39 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2022-02-05 14:12:29 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # timeout
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=0.0,
custom_entry_price=4200, trades=[]
)
2022-04-01 05:00:51 +00:00
# Test 40: Custom-entry-price above all candles should have rate adjusted to "entry candle high"
tc40 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2022-02-05 14:12:29 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # Timeout
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01,
custom_entry_price=7200, trades=[
2022-03-24 19:33:47 +00:00
BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1)
2022-03-16 19:11:32 +00:00
])
2022-04-01 05:00:51 +00:00
# Test 41: Custom-entry-price above all candles should have rate adjusted to "entry candle high"
tc41 = BTContainer(data=[
2022-03-16 19:11:32 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0, 0, 0], # Timeout
[2, 4900, 5250, 4500, 5100, 6172, 0, 0, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=-0.01,
custom_entry_price=4000,
trades=[
2022-03-24 19:33:47 +00:00
BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=1, is_short=True)
]
2022-02-05 14:12:29 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 42: Custom-entry-price around candle low
2022-02-14 19:02:38 +00:00
# Would cause immediate ROI exit, but since the trade was entered
# below open, we treat this as cheating, and delay the sell by 1 candle.
# details: https://github.com/freqtrade/freqtrade/issues/6261
2022-04-01 05:00:51 +00:00
tc42 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2022-02-05 14:12:29 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
2022-02-14 18:57:51 +00:00
[1, 5000, 5500, 4951, 4999, 6172, 0, 0], # Enter and immediate ROI
2022-02-05 14:12:29 +00:00
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
2022-02-14 18:57:51 +00:00
stop_loss=-0.10, roi={"0": 0.01}, profit_perc=0.01,
2022-02-05 14:12:29 +00:00
custom_entry_price=4952,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=2)]
2022-02-05 14:12:29 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 43: Custom-entry-price around candle low
# Would cause immediate ROI exit below close
# details: https://github.com/freqtrade/freqtrade/issues/6261
2022-04-01 05:00:51 +00:00
tc43 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5400, 5500, 4951, 5100, 6172, 0, 0], # Enter and immediate ROI
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.01}, profit_perc=0.01,
custom_entry_price=4952,
2022-03-24 19:33:47 +00:00
trades=[BTrade(exit_reason=ExitType.ROI, open_tick=1, close_tick=1)]
)
2022-02-05 14:12:29 +00:00
2022-04-01 05:00:51 +00:00
# Test 44: Custom exit price below all candles
# Price adjusted to candle Low.
2022-04-01 05:00:51 +00:00
tc44 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2022-02-05 14:20:05 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0],
[2, 4900, 5250, 4900, 5100, 6172, 0, 1], # exit - but timeout
[3, 5100, 5100, 4950, 4950, 6172, 0, 0],
[4, 5000, 5100, 4950, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=-0.01,
2022-04-03 17:27:30 +00:00
use_exit_signal=True,
2022-02-05 14:20:05 +00:00
custom_exit_price=4552,
2022-04-04 15:10:02 +00:00
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=3)]
2022-02-05 14:20:05 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 45: Custom exit price above all candles
2022-02-05 14:20:05 +00:00
# causes sell signal timeout
2022-04-01 05:00:51 +00:00
tc45 = BTContainer(data=[
2022-03-16 06:15:20 +00:00
# D O H L C V EL XL ES Xs BT
2022-02-05 14:20:05 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0],
2022-03-16 19:09:04 +00:00
[2, 4950, 5250, 4900, 5100, 6172, 0, 1], # exit - entry timeout
2022-02-05 14:20:05 +00:00
[3, 5100, 5100, 4950, 4950, 6172, 0, 0],
[4, 5000, 5100, 4950, 4950, 6172, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.0,
2022-04-03 17:27:30 +00:00
use_exit_signal=True,
2022-02-05 14:20:05 +00:00
custom_exit_price=6052,
2022-04-04 14:59:27 +00:00
trades=[BTrade(exit_reason=ExitType.FORCE_EXIT, open_tick=1, close_tick=4)]
2022-02-05 14:20:05 +00:00
)
2022-04-01 05:00:51 +00:00
# Test 46: (Short of tc45) Custom short exit price above below candles
2022-03-16 19:09:04 +00:00
# causes sell signal timeout
2022-04-01 05:00:51 +00:00
tc46 = BTContainer(data=[
2022-03-16 19:09:04 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5000, 4951, 5000, 6172, 0, 0, 0, 0],
[2, 4910, 5150, 4910, 5100, 6172, 0, 0, 0, 1], # exit - entry timeout
[3, 5100, 5100, 4950, 4950, 6172, 0, 0, 0, 0],
[4, 5000, 5100, 4950, 4950, 6172, 0, 0, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.0,
2022-04-03 17:27:30 +00:00
use_exit_signal=True,
2022-03-16 19:09:04 +00:00
custom_exit_price=4700,
2022-04-04 14:59:27 +00:00
trades=[BTrade(exit_reason=ExitType.FORCE_EXIT, open_tick=1, close_tick=4, is_short=True)]
2022-03-16 19:09:04 +00:00
)
2022-03-16 18:54:29 +00:00
2022-04-01 05:00:51 +00:00
# Test 47: Colliding long and short signal
tc47 = BTContainer(data=[
2022-03-16 19:09:04 +00:00
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0, 0, 0],
[2, 4900, 5250, 4900, 5100, 6172, 0, 0, 0, 0],
[3, 5100, 5100, 4950, 4950, 6172, 0, 0, 0, 0],
[4, 5000, 5100, 4950, 4950, 6172, 0, 0, 0, 0]],
stop_loss=-0.10, roi={"0": 0.10}, profit_perc=0.0,
2022-04-03 17:27:30 +00:00
use_exit_signal=True,
2022-03-16 19:09:04 +00:00
trades=[]
)
2021-11-30 19:42:18 +00:00
2022-05-04 05:13:02 +00:00
# Test 48: Custom-entry-price below all candles - readjust order
tc48 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # timeout
[2, 4900, 5250, 4500, 5100, 6172, 0, 0], # Order readjust
[3, 5100, 5100, 4650, 4750, 6172, 0, 1],
2022-05-04 05:13:02 +00:00
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
2022-05-14 13:39:27 +00:00
stop_loss=-0.2, roi={"0": 0.10}, profit_perc=-0.087,
use_exit_signal=True, timeout=1000,
custom_entry_price=4200, adjust_entry_price=5200,
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4, is_short=False)]
2022-05-04 05:13:02 +00:00
)
2022-02-05 14:20:05 +00:00
# Test 49: Custom-entry-price short above all candles - readjust order
tc49 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5200, 4951, 5000, 6172, 0, 0, 0, 0], # timeout
[2, 4900, 5250, 4900, 5100, 6172, 0, 0, 0, 0], # Order readjust
[3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 1],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]],
2022-05-14 13:39:27 +00:00
stop_loss=-0.2, roi={"0": 0.10}, profit_perc=0.05,
use_exit_signal=True, timeout=1000,
custom_entry_price=5300, adjust_entry_price=5000,
trades=[BTrade(exit_reason=ExitType.EXIT_SIGNAL, open_tick=1, close_tick=4, is_short=True)]
)
2022-05-06 04:45:22 +00:00
# Test 50: Custom-entry-price below all candles - readjust order cancels order
tc50 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
2022-05-07 14:31:56 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0], # Enter long - place order
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # Order readjust - cancel order
[2, 4900, 5250, 4500, 5100, 6172, 0, 0],
2022-05-06 04:45:22 +00:00
[3, 5100, 5100, 4650, 4750, 6172, 0, 0],
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=0.0,
use_exit_signal=True, timeout=1000,
custom_entry_price=4200, adjust_entry_price=None,
trades=[]
)
# Test 51: Custom-entry-price below all candles - readjust order leaves order in place and timeout.
tc51 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
2022-05-07 14:31:56 +00:00
[0, 5000, 5050, 4950, 5000, 6172, 1, 0], # Enter long - place order
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # Order readjust - replace order
[2, 4900, 5250, 4500, 5100, 6172, 0, 0], # Order readjust - maintain order
[3, 5100, 5100, 4650, 4750, 6172, 0, 0], # Timeout
2022-05-06 04:45:22 +00:00
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
stop_loss=-0.01, roi={"0": 0.10}, profit_perc=0.0,
2022-05-07 14:31:56 +00:00
use_exit_signal=True, timeout=60,
custom_entry_price=4200, adjust_entry_price=4100,
2022-05-06 04:45:22 +00:00
trades=[]
)
2022-05-14 13:39:27 +00:00
# Test 52: Custom-entry-price below all candles - readjust order - stoploss
tc52 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 1, 0],
[1, 5000, 5500, 4951, 5000, 6172, 0, 0], # enter trade (signal on last candle)
[2, 4900, 5250, 4500, 5100, 6172, 0, 0], # Order readjust
[3, 5100, 5100, 4650, 4750, 6172, 0, 0], # stoploss hit?
[4, 4750, 4950, 4350, 4750, 6172, 0, 0]],
2023-05-27 17:39:00 +00:00
stop_loss=-0.03, roi={}, profit_perc=-0.03,
2022-05-14 13:39:27 +00:00
use_exit_signal=True, timeout=1000,
custom_entry_price=4200, adjust_entry_price=5200,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2, is_short=False)]
)
# Test 53: Custom-entry-price short above all candles - readjust order - stoploss
tc53 = BTContainer(data=[
# D O H L C V EL XL ES Xs BT
[0, 5000, 5050, 4950, 5000, 6172, 0, 0, 1, 0],
[1, 5000, 5200, 4951, 5000, 6172, 0, 0, 0, 0], # enter trade (signal on last candle)
[2, 4900, 5250, 4900, 5100, 6172, 0, 0, 0, 0], # Order readjust
[3, 5100, 5100, 4650, 4750, 6172, 0, 0, 0, 1], # stoploss hit?
[4, 4750, 4950, 4350, 4750, 6172, 0, 0, 0, 0]],
stop_loss=-0.03, roi={"0": 0.10}, profit_perc=-0.03,
use_exit_signal=True, timeout=1000,
custom_entry_price=5300, adjust_entry_price=5000,
trades=[BTrade(exit_reason=ExitType.STOP_LOSS, open_tick=1, close_tick=2, is_short=True)]
)
TESTS = [
2019-06-13 18:00:56 +00:00
tc0,
tc1,
tc2,
tc3,
tc4,
tc5,
tc6,
2019-03-17 12:27:32 +00:00
tc7,
2019-03-17 15:01:34 +00:00
tc8,
2019-03-17 15:26:38 +00:00
tc9,
tc10,
tc11,
tc12,
tc13,
tc14,
tc15,
tc16,
tc17,
tc18,
tc19,
tc20,
tc21,
tc22,
tc23,
2020-11-27 08:17:25 +00:00
tc24,
tc25,
tc26,
tc27,
tc28,
tc29,
tc30,
tc31,
tc32,
2021-07-20 16:25:00 +00:00
tc33,
2021-11-30 19:42:18 +00:00
tc34,
2022-02-05 14:12:29 +00:00
tc35,
tc36,
2022-02-05 14:20:05 +00:00
tc37,
tc38,
2022-02-11 16:02:04 +00:00
tc39,
2022-03-16 19:09:04 +00:00
tc40,
2022-04-01 05:00:51 +00:00
tc41,
tc42,
tc43,
tc44,
tc45,
tc46,
tc47,
2022-05-04 05:13:02 +00:00
tc48,
tc49,
2022-05-06 04:45:22 +00:00
tc50,
tc51,
2022-05-14 13:39:27 +00:00
tc52,
tc53,
]
@pytest.mark.parametrize("data", TESTS)
2022-04-03 17:27:30 +00:00
def test_backtest_results(default_conf, fee, mocker, caplog, data: BTContainer) -> None:
2018-07-09 19:38:49 +00:00
"""
run functional tests
"""
default_conf["stoploss"] = data.stop_loss
default_conf["minimal_roi"] = data.roi
default_conf["timeframe"] = tests_timeframe
default_conf["trailing_stop"] = data.trailing_stop
2019-06-13 18:00:56 +00:00
default_conf["trailing_only_offset_is_reached"] = data.trailing_only_offset_is_reached
2022-05-04 05:13:02 +00:00
if data.timeout:
default_conf['unfilledtimeout'].update({
'entry': data.timeout,
'exit': data.timeout,
})
2019-06-13 18:00:56 +00:00
# Only add this to configuration If it's necessary
if data.trailing_stop_positive is not None:
2019-06-13 18:00:56 +00:00
default_conf["trailing_stop_positive"] = data.trailing_stop_positive
default_conf["trailing_stop_positive_offset"] = data.trailing_stop_positive_offset
2022-04-05 18:07:58 +00:00
default_conf["use_exit_signal"] = data.use_exit_signal
default_conf["max_open_trades"] = 10
2019-06-13 18:00:56 +00:00
mocker.patch(f"{EXMS}.get_fee", return_value=0.0)
mocker.patch(f"{EXMS}.get_min_pair_stake_amount", return_value=0.00001)
mocker.patch(f"{EXMS}.get_max_pair_stake_amount", return_value=float('inf'))
2023-03-08 06:05:59 +00:00
mocker.patch(f"{EXMS}.get_max_leverage", return_value=100)
mocker.patch(f"{EXMS}.calculate_funding_fees", return_value=0)
2018-07-09 19:38:49 +00:00
patch_exchange(mocker)
2018-10-30 18:58:06 +00:00
frame = _build_backtest_dataframe(data.data)
2018-07-09 19:38:49 +00:00
backtesting = Backtesting(default_conf)
2022-03-16 18:54:29 +00:00
# TODO: Should we initialize this properly??
backtesting.trading_mode = TradingMode.MARGIN
backtesting._set_strategy(backtesting.strategylist[0])
2023-03-26 16:21:21 +00:00
backtesting._can_short = True
backtesting.required_startup = 0
2021-09-22 18:42:31 +00:00
backtesting.strategy.advise_entry = lambda a, m: frame
backtesting.strategy.advise_exit = lambda a, m: frame
2022-02-05 14:12:29 +00:00
if data.custom_entry_price:
backtesting.strategy.custom_entry_price = MagicMock(return_value=data.custom_entry_price)
2022-02-05 14:20:05 +00:00
if data.custom_exit_price:
backtesting.strategy.custom_exit_price = MagicMock(return_value=data.custom_exit_price)
2022-05-07 14:31:56 +00:00
backtesting.strategy.adjust_entry_price = MagicMock(return_value=data.adjust_entry_price)
2022-05-04 05:13:02 +00:00
backtesting.strategy.use_custom_stoploss = data.use_custom_stoploss
2021-11-30 19:42:18 +00:00
backtesting.strategy.leverage = lambda **kwargs: data.leverage
2018-07-09 19:38:49 +00:00
caplog.set_level(logging.DEBUG)
pair = "UNITTEST/BTC"
2018-07-09 19:38:49 +00:00
# Dummy data as we mock the analyze functions
2019-11-03 09:38:21 +00:00
data_processed = {pair: frame.copy()}
2019-12-17 22:06:03 +00:00
min_date, max_date = get_timerange({pair: frame})
result = backtesting.backtest(
2019-12-13 23:12:44 +00:00
processed=data_processed,
start_date=min_date,
end_date=max_date,
2018-07-09 19:38:49 +00:00
)
results = result['results']
assert len(results) == len(data.trades)
assert round(results["profit_ratio"].sum(), 3) == round(data.profit_perc, 3)
2018-11-09 18:34:46 +00:00
for c, trade in enumerate(data.trades):
2022-03-16 18:26:08 +00:00
res: BTrade = results.iloc[c]
2022-03-24 19:33:47 +00:00
assert res.exit_reason == trade.exit_reason.value
2021-11-21 08:51:16 +00:00
assert res.enter_tag == trade.enter_tag
2020-06-26 07:21:28 +00:00
assert res.open_date == _get_frame_time_from_offset(trade.open_tick)
assert res.close_date == _get_frame_time_from_offset(trade.close_tick)
2022-03-16 18:26:08 +00:00
assert res.is_short == trade.is_short
assert len(LocalTrade.trades) == len(data.trades)
assert len(LocalTrade.trades_open) == 0
backtesting.cleanup()
del backtesting