2023-05-13 20:40:11 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, W0212, line-too-long, C0103, unused-argument
|
2023-05-20 09:51:46 +00:00
|
|
|
from copy import deepcopy
|
2023-05-26 10:55:54 +00:00
|
|
|
from pathlib import Path, PurePosixPath
|
2023-05-20 09:28:52 +00:00
|
|
|
from unittest.mock import MagicMock, PropertyMock
|
2023-05-13 20:40:11 +00:00
|
|
|
|
2023-05-20 09:14:13 +00:00
|
|
|
import pytest
|
2023-05-13 20:40:11 +00:00
|
|
|
|
2023-05-20 09:28:52 +00:00
|
|
|
from freqtrade.commands.optimize_commands import start_lookahead_analysis
|
2023-05-13 20:40:11 +00:00
|
|
|
from freqtrade.data.history import get_timerange
|
2023-05-20 09:28:52 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2023-05-26 10:55:54 +00:00
|
|
|
from freqtrade.optimize.lookahead_analysis import Analysis, LookaheadAnalysis
|
2023-05-20 09:51:46 +00:00
|
|
|
from freqtrade.optimize.lookahead_analysis_helpers import LookaheadAnalysisSubFunctions
|
2023-05-20 17:57:26 +00:00
|
|
|
from tests.conftest import EXMS, get_args, log_has_re, patch_exchange
|
2023-05-20 09:14:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def lookahead_conf(default_conf_usdt):
|
|
|
|
default_conf_usdt['minimum_trade_amount'] = 10
|
|
|
|
default_conf_usdt['targeted_trade_amount'] = 20
|
2023-05-20 13:39:21 +00:00
|
|
|
default_conf_usdt['strategy_path'] = str(
|
|
|
|
Path(__file__).parent.parent / "strategy/strats/lookahead_bias")
|
2023-05-21 06:21:08 +00:00
|
|
|
default_conf_usdt['strategy'] = 'strategy_test_v3_with_lookahead_bias'
|
2023-05-20 09:14:13 +00:00
|
|
|
|
|
|
|
return default_conf_usdt
|
2023-05-13 20:40:11 +00:00
|
|
|
|
|
|
|
|
2023-05-20 09:36:58 +00:00
|
|
|
def test_start_lookahead_analysis(mocker):
|
2023-05-20 09:28:52 +00:00
|
|
|
single_mock = MagicMock()
|
2023-05-20 09:36:58 +00:00
|
|
|
text_table_mock = MagicMock()
|
2023-05-20 09:28:52 +00:00
|
|
|
mocker.patch.multiple(
|
2023-05-20 09:36:58 +00:00
|
|
|
'freqtrade.optimize.lookahead_analysis_helpers.LookaheadAnalysisSubFunctions',
|
2023-05-20 09:28:52 +00:00
|
|
|
initialize_single_lookahead_analysis=single_mock,
|
2023-05-20 09:36:58 +00:00
|
|
|
text_table_lookahead_analysis_instances=text_table_mock,
|
2023-05-26 10:55:54 +00:00
|
|
|
)
|
2023-05-20 09:28:52 +00:00
|
|
|
args = [
|
|
|
|
"lookahead-analysis",
|
|
|
|
"--strategy",
|
2023-05-20 13:39:21 +00:00
|
|
|
"strategy_test_v3_with_lookahead_bias",
|
2023-05-20 09:28:52 +00:00
|
|
|
"--strategy-path",
|
2023-05-21 06:21:08 +00:00
|
|
|
str(Path(__file__).parent.parent / "strategy/strats/lookahead_bias"),
|
2023-05-20 09:28:52 +00:00
|
|
|
]
|
|
|
|
pargs = get_args(args)
|
|
|
|
pargs['config'] = None
|
|
|
|
|
|
|
|
start_lookahead_analysis(pargs)
|
|
|
|
assert single_mock.call_count == 1
|
2023-05-20 09:36:58 +00:00
|
|
|
assert text_table_mock.call_count == 1
|
2023-05-20 09:28:52 +00:00
|
|
|
|
|
|
|
single_mock.reset_mock()
|
|
|
|
|
|
|
|
# Test invalid config
|
|
|
|
args = [
|
|
|
|
"lookahead-analysis",
|
|
|
|
"--strategy",
|
2023-05-20 13:39:21 +00:00
|
|
|
"strategy_test_v3_with_lookahead_bias",
|
2023-05-20 09:28:52 +00:00
|
|
|
"--strategy-path",
|
2023-05-21 06:21:08 +00:00
|
|
|
str(Path(__file__).parent.parent / "strategy/strats/lookahead_bias"),
|
2023-05-20 09:28:52 +00:00
|
|
|
"--targeted-trade-amount",
|
|
|
|
"10",
|
|
|
|
"--minimum-trade-amount",
|
|
|
|
"20",
|
2023-05-26 10:55:54 +00:00
|
|
|
]
|
2023-05-20 09:28:52 +00:00
|
|
|
pargs = get_args(args)
|
|
|
|
pargs['config'] = None
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"targeted trade amount can't be smaller than .*"):
|
|
|
|
start_lookahead_analysis(pargs)
|
|
|
|
|
2023-05-13 20:40:11 +00:00
|
|
|
|
2023-05-20 09:51:46 +00:00
|
|
|
def test_lookahead_helper_invalid_config(lookahead_conf, mocker, caplog) -> None:
|
|
|
|
conf = deepcopy(lookahead_conf)
|
|
|
|
conf['targeted_trade_amount'] = 10
|
|
|
|
conf['minimum_trade_amount'] = 40
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"targeted trade amount can't be smaller than .*"):
|
|
|
|
LookaheadAnalysisSubFunctions.start(conf)
|
|
|
|
|
|
|
|
conf = deepcopy(lookahead_conf)
|
|
|
|
del conf['strategy']
|
|
|
|
with pytest.raises(OperationalException,
|
|
|
|
match=r"No Strategy specified"):
|
|
|
|
LookaheadAnalysisSubFunctions.start(conf)
|
|
|
|
|
|
|
|
|
|
|
|
def test_lookahead_helper_start(lookahead_conf, mocker, caplog) -> None:
|
|
|
|
single_mock = MagicMock()
|
|
|
|
text_table_mock = MagicMock()
|
|
|
|
mocker.patch.multiple(
|
2023-05-26 10:55:54 +00:00
|
|
|
'freqtrade.optimize.lookahead_analysis_helpers.LookaheadAnalysisSubFunctions',
|
|
|
|
initialize_single_lookahead_analysis=single_mock,
|
|
|
|
text_table_lookahead_analysis_instances=text_table_mock,
|
|
|
|
)
|
2023-05-20 09:51:46 +00:00
|
|
|
LookaheadAnalysisSubFunctions.start(lookahead_conf)
|
|
|
|
assert single_mock.call_count == 1
|
|
|
|
assert text_table_mock.call_count == 1
|
|
|
|
|
|
|
|
single_mock.reset_mock()
|
|
|
|
text_table_mock.reset_mock()
|
|
|
|
|
|
|
|
|
2023-05-26 10:55:54 +00:00
|
|
|
def test_lookahead_helper_text_table_lookahead_analysis_instances(lookahead_conf, caplog):
|
|
|
|
analysis = Analysis()
|
|
|
|
analysis.has_bias = True
|
2023-05-27 17:15:35 +00:00
|
|
|
analysis.total_signals = 5
|
2023-05-26 10:55:54 +00:00
|
|
|
analysis.false_entry_signals = 4
|
|
|
|
analysis.false_exit_signals = 3
|
|
|
|
|
|
|
|
strategy_obj = \
|
|
|
|
{
|
|
|
|
'name': "strategy_test_v3_with_lookahead_bias",
|
|
|
|
'location': PurePosixPath(lookahead_conf['strategy_path'],
|
|
|
|
f"{lookahead_conf['strategy']}.py")
|
|
|
|
}
|
|
|
|
|
|
|
|
instance = LookaheadAnalysis(lookahead_conf, strategy_obj)
|
|
|
|
instance.current_analysis = analysis
|
|
|
|
table, headers, data = (LookaheadAnalysisSubFunctions.
|
|
|
|
text_table_lookahead_analysis_instances([instance]))
|
|
|
|
|
|
|
|
# check row contents for a try that errored out
|
|
|
|
assert data[0][0] == 'strategy_test_v3_with_lookahead_bias.py'
|
|
|
|
assert data[0][1] == 'strategy_test_v3_with_lookahead_bias'
|
|
|
|
assert data[0][2].__contains__('error')
|
|
|
|
assert len(data[0]) == 3
|
|
|
|
|
|
|
|
# edit it into not showing an error
|
|
|
|
instance.failed_bias_check = False
|
|
|
|
table, headers, data = (LookaheadAnalysisSubFunctions.
|
|
|
|
text_table_lookahead_analysis_instances([instance]))
|
|
|
|
assert data[0][0] == 'strategy_test_v3_with_lookahead_bias.py'
|
|
|
|
assert data[0][1] == 'strategy_test_v3_with_lookahead_bias'
|
|
|
|
assert data[0][2] # True
|
|
|
|
assert data[0][3] == 5
|
|
|
|
assert data[0][4] == 4
|
|
|
|
assert data[0][5] == 3
|
|
|
|
assert data[0][6] == ''
|
|
|
|
|
|
|
|
analysis.false_indicators.append('falseIndicator1')
|
|
|
|
analysis.false_indicators.append('falseIndicator2')
|
|
|
|
table, headers, data = (LookaheadAnalysisSubFunctions.
|
|
|
|
text_table_lookahead_analysis_instances([instance]))
|
|
|
|
|
|
|
|
assert data[0][6] == 'falseIndicator1, falseIndicator2'
|
2023-05-20 10:03:06 +00:00
|
|
|
|
2023-05-27 17:15:35 +00:00
|
|
|
# check amount of returning rows
|
|
|
|
assert len(data) == 1
|
2023-05-20 10:03:06 +00:00
|
|
|
|
2023-05-27 17:15:35 +00:00
|
|
|
# check amount of multiple rows
|
|
|
|
table, headers, data = (LookaheadAnalysisSubFunctions.
|
|
|
|
text_table_lookahead_analysis_instances([instance, instance, instance]))
|
|
|
|
assert len(data) == 3
|
|
|
|
|
|
|
|
|
|
|
|
def test_lookahead_helper_export_to_csv(lookahead_conf):
|
|
|
|
import pandas as pd
|
|
|
|
lookahead_conf['lookahead_analysis_exportfilename'] = "temp_csv_lookahead_analysis.csv"
|
|
|
|
|
|
|
|
# just to be sure the test won't fail: remove file if exists for some reason
|
|
|
|
# (repeat this at the end once again to clean up)
|
|
|
|
if Path(lookahead_conf['lookahead_analysis_exportfilename']).exists():
|
|
|
|
Path(lookahead_conf['lookahead_analysis_exportfilename']).unlink()
|
|
|
|
|
|
|
|
# before we can start we have to delete the
|
|
|
|
|
|
|
|
# 1st check: create a new file and verify its contents
|
|
|
|
analysis1 = Analysis()
|
|
|
|
analysis1.has_bias = True
|
|
|
|
analysis1.total_signals = 5
|
|
|
|
analysis1.false_entry_signals = 4
|
|
|
|
analysis1.false_exit_signals = 3
|
|
|
|
analysis1.false_indicators.append('falseIndicator1')
|
|
|
|
analysis1.false_indicators.append('falseIndicator2')
|
|
|
|
lookahead_conf['lookahead_analysis_exportfilename'] = "temp_csv_lookahead_analysis.csv"
|
|
|
|
|
|
|
|
strategy_obj1 = {
|
2023-05-27 18:35:45 +00:00
|
|
|
'name': "strat1",
|
|
|
|
'location': PurePosixPath("file1.py"),
|
|
|
|
}
|
2023-05-27 17:15:35 +00:00
|
|
|
|
|
|
|
instance1 = LookaheadAnalysis(lookahead_conf, strategy_obj1)
|
|
|
|
instance1.current_analysis = analysis1
|
|
|
|
|
|
|
|
LookaheadAnalysisSubFunctions.export_to_csv(lookahead_conf, [instance1])
|
|
|
|
saved_data1 = pd.read_csv(lookahead_conf['lookahead_analysis_exportfilename'])
|
|
|
|
|
|
|
|
expected_values1 = [
|
|
|
|
[
|
|
|
|
'file1.py', 'strat1', True,
|
|
|
|
5, 4, 3,
|
|
|
|
"falseIndicator1,falseIndicator2"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
expected_columns = ['filename', 'strategy', 'has_bias',
|
|
|
|
'total_signals', 'biased_entry_signals', 'biased_exit_signals',
|
|
|
|
'biased_indicators']
|
|
|
|
expected_data1 = pd.DataFrame(expected_values1, columns=expected_columns)
|
|
|
|
|
|
|
|
assert Path(lookahead_conf['lookahead_analysis_exportfilename']).exists()
|
|
|
|
assert expected_data1.equals(saved_data1)
|
|
|
|
|
|
|
|
# 2nd check: update the same strategy (which internally changed or is being retested)
|
|
|
|
expected_values2 = [
|
|
|
|
[
|
|
|
|
'file1.py', 'strat1', False,
|
|
|
|
10, 11, 12,
|
|
|
|
"falseIndicator3,falseIndicator4"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
expected_data2 = pd.DataFrame(expected_values2, columns=expected_columns)
|
|
|
|
|
|
|
|
analysis2 = Analysis()
|
|
|
|
analysis2.has_bias = False
|
|
|
|
analysis2.total_signals = 10
|
|
|
|
analysis2.false_entry_signals = 11
|
|
|
|
analysis2.false_exit_signals = 12
|
|
|
|
analysis2.false_indicators.append('falseIndicator3')
|
|
|
|
analysis2.false_indicators.append('falseIndicator4')
|
|
|
|
|
|
|
|
strategy_obj2 = {
|
|
|
|
'name': "strat1",
|
|
|
|
'location': PurePosixPath("file1.py"),
|
|
|
|
}
|
|
|
|
|
|
|
|
instance2 = LookaheadAnalysis(lookahead_conf, strategy_obj2)
|
|
|
|
instance2.current_analysis = analysis2
|
|
|
|
|
|
|
|
LookaheadAnalysisSubFunctions.export_to_csv(lookahead_conf, [instance2])
|
|
|
|
saved_data2 = pd.read_csv(lookahead_conf['lookahead_analysis_exportfilename'])
|
|
|
|
|
|
|
|
assert expected_data2.equals(saved_data2)
|
|
|
|
|
|
|
|
# 3rd check: now we add a new row to an already existing file
|
|
|
|
expected_values3 = [
|
|
|
|
[
|
|
|
|
'file1.py', 'strat1', False,
|
|
|
|
10, 11, 12,
|
|
|
|
"falseIndicator3,falseIndicator4"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'file3.py', 'strat3', True,
|
|
|
|
20, 21, 22, "falseIndicator5,falseIndicator6"
|
|
|
|
],
|
|
|
|
]
|
|
|
|
|
|
|
|
expected_data3 = pd.DataFrame(expected_values3, columns=expected_columns)
|
|
|
|
|
|
|
|
analysis3 = Analysis()
|
|
|
|
analysis3.has_bias = True
|
|
|
|
analysis3.total_signals = 20
|
|
|
|
analysis3.false_entry_signals = 21
|
|
|
|
analysis3.false_exit_signals = 22
|
|
|
|
analysis3.false_indicators.append('falseIndicator5')
|
|
|
|
analysis3.false_indicators.append('falseIndicator6')
|
|
|
|
lookahead_conf['lookahead_analysis_exportfilename'] = "temp_csv_lookahead_analysis.csv"
|
|
|
|
|
|
|
|
strategy_obj3 = {
|
|
|
|
'name': "strat3",
|
|
|
|
'location': PurePosixPath("file3.py"),
|
|
|
|
}
|
|
|
|
|
|
|
|
instance3 = LookaheadAnalysis(lookahead_conf, strategy_obj3)
|
|
|
|
instance3.current_analysis = analysis3
|
|
|
|
|
|
|
|
LookaheadAnalysisSubFunctions.export_to_csv(lookahead_conf, [instance3])
|
|
|
|
saved_data3 = pd.read_csv(lookahead_conf['lookahead_analysis_exportfilename'])
|
|
|
|
assert expected_data3.equals(saved_data3)
|
|
|
|
|
|
|
|
# remove csv file after the test is done
|
|
|
|
if Path(lookahead_conf['lookahead_analysis_exportfilename']).exists():
|
|
|
|
Path(lookahead_conf['lookahead_analysis_exportfilename']).unlink()
|
2023-05-20 10:03:06 +00:00
|
|
|
|
|
|
|
|
2023-05-27 18:35:45 +00:00
|
|
|
def test_initialize_single_lookahead_analysis(lookahead_conf, mocker):
|
|
|
|
mocker.patch('freqtrade.data.history.get_timerange', get_timerange)
|
|
|
|
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'))
|
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
|
|
|
PropertyMock(return_value=['UNITTEST/BTC']))
|
|
|
|
lookahead_conf['pairs'] = ['UNITTEST/USDT']
|
|
|
|
|
|
|
|
lookahead_conf['timeframe'] = '5m'
|
|
|
|
lookahead_conf['timerange'] = '20180119-20180122'
|
2023-05-27 18:47:59 +00:00
|
|
|
strategy_obj = {'name': "strategy_test_v3_with_lookahead_bias"}
|
|
|
|
|
|
|
|
instance = LookaheadAnalysis(lookahead_conf, strategy_obj)
|
|
|
|
assert instance.strategy_obj['name'] == "strategy_test_v3_with_lookahead_bias"
|
2023-05-20 10:03:06 +00:00
|
|
|
|
|
|
|
|
2023-05-20 18:12:04 +00:00
|
|
|
@pytest.mark.parametrize('scenario', [
|
|
|
|
'no_bias', 'bias1'
|
|
|
|
])
|
|
|
|
def test_biased_strategy(lookahead_conf, mocker, caplog, scenario) -> None:
|
2023-05-13 20:40:11 +00:00
|
|
|
mocker.patch('freqtrade.data.history.get_timerange', get_timerange)
|
2023-05-20 13:39:21 +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-05-13 20:40:11 +00:00
|
|
|
patch_exchange(mocker)
|
|
|
|
mocker.patch('freqtrade.plugins.pairlistmanager.PairListManager.whitelist',
|
|
|
|
PropertyMock(return_value=['UNITTEST/BTC']))
|
2023-05-20 13:39:21 +00:00
|
|
|
lookahead_conf['pairs'] = ['UNITTEST/USDT']
|
2023-05-13 20:40:11 +00:00
|
|
|
|
2023-05-20 09:14:13 +00:00
|
|
|
lookahead_conf['timeframe'] = '5m'
|
2023-05-20 17:57:26 +00:00
|
|
|
lookahead_conf['timerange'] = '20180119-20180122'
|
2023-05-13 20:40:11 +00:00
|
|
|
|
2023-05-20 18:12:04 +00:00
|
|
|
# Patch scenario Parameter to allow for easy selection
|
|
|
|
mocker.patch('freqtrade.strategy.hyper.HyperStrategyMixin.load_params_from_file',
|
|
|
|
return_value={
|
|
|
|
'params': {
|
|
|
|
"buy": {
|
2023-05-26 10:55:54 +00:00
|
|
|
"scenario": scenario
|
2023-05-20 18:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-05-27 18:47:59 +00:00
|
|
|
strategy_obj = {'name': "strategy_test_v3_with_lookahead_bias"}
|
2023-05-20 13:39:21 +00:00
|
|
|
instance = LookaheadAnalysis(lookahead_conf, strategy_obj)
|
|
|
|
instance.start()
|
2023-05-20 18:12:04 +00:00
|
|
|
# Assert init correct
|
|
|
|
assert log_has_re(f"Strategy Parameter: scenario = {scenario}", caplog)
|
|
|
|
|
2023-05-27 18:35:45 +00:00
|
|
|
# check non-biased strategy
|
|
|
|
if scenario == "no_bias":
|
|
|
|
assert not instance.current_analysis.has_bias
|
|
|
|
# check biased strategy
|
|
|
|
elif scenario == "bias1":
|
|
|
|
assert instance.current_analysis.has_bias
|