2018-07-09 19:38:49 +00:00
|
|
|
|
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument
|
|
|
|
|
import logging
|
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
2018-07-10 19:08:44 +00:00
|
|
|
|
from pandas import DataFrame
|
2018-07-09 19:38:49 +00:00
|
|
|
|
import pytest
|
|
|
|
|
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
2018-07-09 19:38:49 +00:00
|
|
|
|
from freqtrade.optimize.backtesting import Backtesting
|
2018-07-30 19:32:54 +00:00
|
|
|
|
from freqtrade.strategy.interface import SellType
|
2018-10-30 19:02:01 +00:00
|
|
|
|
from freqtrade.tests.optimize import (BTrade, BTContainer, _build_backtest_dataframe,
|
|
|
|
|
_get_frame_time_from_offset)
|
|
|
|
|
from freqtrade.tests.conftest import patch_exchange
|
2018-10-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
2018-10-30 18:36:19 +00:00
|
|
|
|
# Test 0 Minus 8% Close
|
2018-07-10 19:08:44 +00:00
|
|
|
|
# Test with Stop-loss at 1%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC1: Stop-Loss Triggered 1% loss
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc0 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000.0, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[2, 9975, 10025, 9200, 9200, 12345, 0, 0], # exit with stoploss hit
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[3, 9950, 10000, 9960, 9955, 12345, 0, 0],
|
|
|
|
|
[4, 9955, 9975, 9955, 9990, 12345, 0, 0],
|
|
|
|
|
[5, 9990, 9990, 9990, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.01, roi=1, profit_perc=-0.01,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-11 05:18:52 +00:00
|
|
|
|
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
2018-10-30 18:36:19 +00:00
|
|
|
|
# Test 1 Minus 4% Low, minus 1% close
|
2018-07-10 19:08:44 +00:00
|
|
|
|
# Test with Stop-Loss at 3%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC2: Stop-Loss Triggered 3% Loss
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc1 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[2, 9975, 10025, 9925, 9950, 12345, 0, 0],
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[3, 9950, 10000, 9600, 9925, 12345, 0, 0], # exit with stoploss hit
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[4, 9925, 9975, 9875, 9900, 12345, 0, 0],
|
|
|
|
|
[5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.03, roi=1, profit_perc=-0.03,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=3)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test 3 Candle drops 4%, Recovers 1%.
|
|
|
|
|
# Entry Criteria Met
|
|
|
|
|
# Candle drops 20%
|
|
|
|
|
# Candle Data for test 3
|
|
|
|
|
# Test with Stop-Loss at 2%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC3: Trade-A: Stop-Loss Triggered 2% Loss
|
|
|
|
|
# Trade-B: Stop-Loss Triggered 2% Loss
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc2 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[2, 9975, 10025, 9600, 9950, 12345, 0, 0], # exit with stoploss hit
|
2018-10-30 18:33:32 +00:00
|
|
|
|
[3, 9950, 10000, 9900, 9925, 12345, 1, 0],
|
|
|
|
|
[4, 9950, 10000, 9900, 9925, 12345, 0, 0], # enter trade 2 (signal on last candle)
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[5, 9925, 9975, 8000, 8000, 12345, 0, 0], # exit with stoploss hit
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[6, 9900, 9950, 9950, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.02, roi=1, profit_perc=-0.04,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2),
|
|
|
|
|
BTrade(sell_reason=SellType.STOP_LOSS, open_tick=4, close_tick=5)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +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
|
2018-07-10 19:08:44 +00:00
|
|
|
|
# Test with Stop-loss at 2% ROI 6%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC4: Stop-Loss Triggered 2% Loss
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc3 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[2, 9975, 11500, 9700, 11500, 12345, 0, 0], # Exit with stoploss hit
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[3, 9950, 10000, 9900, 9925, 12345, 0, 0],
|
2018-10-30 18:36:19 +00:00
|
|
|
|
[4, 9925, 9975, 9875, 9900, 12345, 0, 0],
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.02, roi=0.06, profit_perc=-0.02,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
2018-10-30 18:36:19 +00:00
|
|
|
|
# Test 4 / Drops 0.5% Closes +20%
|
2018-07-10 19:08:44 +00:00
|
|
|
|
# Set stop-loss at 1% ROI 3%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC5: ROI triggers 3% Gain
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc4 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9960, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9960, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[2, 9975, 10050, 9950, 9975, 12345, 0, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[3, 9950, 12000, 9950, 12000, 12345, 0, 0], # ROI
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[4, 9925, 9975, 9945, 9900, 12345, 0, 0],
|
|
|
|
|
[5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.01, roi=0.03, profit_perc=0.03,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=3)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
|
|
|
|
# Test 6 / Drops 3% / Recovers 6% Positive / Closes 1% positve
|
|
|
|
|
# Candle Data for test 6
|
|
|
|
|
# Set stop-loss at 2% ROI at 5%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC6: Stop-Loss triggers 2% Loss
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc5 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
|
|
|
|
[2, 9975, 10600, 9700, 10100, 12345, 0, 0], # Exit with stoploss
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[3, 9950, 10000, 9900, 9925, 12345, 0, 0],
|
|
|
|
|
[4, 9925, 9975, 9945, 9900, 12345, 0, 0],
|
|
|
|
|
[5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.02, roi=0.05, profit_perc=-0.02,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.STOP_LOSS, open_tick=1, close_tick=2)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
|
|
|
|
# Test 7 - 6% Positive / 1% Negative / Close 1% Positve
|
|
|
|
|
# Candle Data for test 7
|
|
|
|
|
# Set stop-loss at 2% ROI at 3%
|
2018-07-11 05:18:52 +00:00
|
|
|
|
# TC7: ROI Triggers 3% Gain
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc6 = BTContainer(data=[
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[0, 10000, 10050, 9950, 9975, 12345, 1, 0],
|
2018-10-29 19:33:27 +00:00
|
|
|
|
[1, 10000, 10050, 9950, 9975, 12345, 0, 0], # enter trade (signal on last candle)
|
|
|
|
|
[2, 9975, 10600, 9900, 10100, 12345, 0, 0], # ROI
|
2018-10-29 19:17:15 +00:00
|
|
|
|
[3, 9950, 10000, 9900, 9925, 12345, 0, 0],
|
|
|
|
|
[4, 9925, 9975, 9945, 9900, 12345, 0, 0],
|
|
|
|
|
[5, 9900, 9950, 9850, 9900, 12345, 0, 0]],
|
2018-10-30 18:33:32 +00:00
|
|
|
|
stop_loss=-0.02, roi=0.03, profit_perc=0.03,
|
2018-10-30 18:44:31 +00:00
|
|
|
|
trades=[BTrade(sell_reason=SellType.ROI, open_tick=1, close_tick=2)]
|
2018-10-30 18:33:32 +00:00
|
|
|
|
)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
|
|
|
|
|
TESTS = [
|
2018-10-30 18:33:32 +00:00
|
|
|
|
tc0,
|
2018-07-10 19:08:44 +00:00
|
|
|
|
tc1,
|
|
|
|
|
tc2,
|
|
|
|
|
tc3,
|
|
|
|
|
tc4,
|
|
|
|
|
tc5,
|
|
|
|
|
tc6,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("data", TESTS)
|
|
|
|
|
def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None:
|
2018-07-09 19:38:49 +00:00
|
|
|
|
"""
|
|
|
|
|
run functional tests
|
|
|
|
|
"""
|
2018-07-10 19:08:44 +00:00
|
|
|
|
default_conf["stoploss"] = data.stop_loss
|
|
|
|
|
default_conf["minimal_roi"] = {"0": data.roi}
|
2018-08-16 09:31:41 +00:00
|
|
|
|
mocker.patch('freqtrade.exchange.Exchange.get_fee', MagicMock(return_value=0.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)
|
2018-10-29 19:17:15 +00:00
|
|
|
|
backtesting.advise_buy = lambda a, m: frame
|
|
|
|
|
backtesting.advise_sell = lambda a, m: frame
|
2018-07-09 19:38:49 +00:00
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
|
|
pair = 'UNITTEST/BTC'
|
|
|
|
|
# Dummy data as we mock the analyze functions
|
2018-07-10 19:08:44 +00:00
|
|
|
|
data_processed = {pair: DataFrame()}
|
2018-07-09 19:38:49 +00:00
|
|
|
|
results = backtesting.backtest(
|
|
|
|
|
{
|
|
|
|
|
'stake_amount': default_conf['stake_amount'],
|
|
|
|
|
'processed': data_processed,
|
|
|
|
|
'max_open_trades': 10,
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
print(results.T)
|
|
|
|
|
|
2018-10-30 18:33:32 +00:00
|
|
|
|
assert len(results) == len(data.trades)
|
2018-07-10 19:08:44 +00:00
|
|
|
|
assert round(results["profit_percent"].sum(), 3) == round(data.profit_perc, 3)
|
2018-10-30 18:33:32 +00:00
|
|
|
|
# if data.sell_r == SellType.STOP_LOSS:
|
|
|
|
|
# assert log_has("Stop loss hit.", caplog.record_tuples)
|
|
|
|
|
# else:
|
|
|
|
|
# assert not log_has("Stop loss hit.", caplog.record_tuples)
|
|
|
|
|
# log_test = (f'Force_selling still open trade UNITTEST/BTC with '
|
|
|
|
|
# f'{results.iloc[-1].profit_percent} perc - {results.iloc[-1].profit_abs}')
|
|
|
|
|
# if data.sell_r == SellType.FORCE_SELL:
|
|
|
|
|
# assert log_has(log_test,
|
|
|
|
|
# caplog.record_tuples)
|
|
|
|
|
# else:
|
|
|
|
|
# assert not log_has(log_test,
|
|
|
|
|
# caplog.record_tuples)
|
|
|
|
|
for c, trade in enumerate(data.trades):
|
|
|
|
|
res = results.iloc[c]
|
2018-10-30 18:44:31 +00:00
|
|
|
|
assert res.sell_reason == trade.sell_reason
|
2018-10-30 18:58:06 +00:00
|
|
|
|
assert res.open_time == _get_frame_time_from_offset(trade.open_tick)
|
|
|
|
|
assert res.close_time == _get_frame_time_from_offset(trade.close_tick)
|