freqtrade_origin/tests/optimize/test_edge_cli.py

134 lines
4.1 KiB
Python
Raw Normal View History

2018-11-14 18:14:34 +00:00
# pragma pylint: disable=missing-docstring, C0103, C0330
# pragma pylint: disable=protected-access, too-many-lines, invalid-name, too-many-arguments
2019-04-24 19:24:00 +00:00
from unittest.mock import MagicMock
2020-01-26 12:33:13 +00:00
from freqtrade.commands.optimize_commands import setup_optimize_configuration, start_edge
2021-06-08 19:20:35 +00:00
from freqtrade.enums import RunMode
2019-05-25 18:06:18 +00:00
from freqtrade.optimize.edge_cli import EdgeCli
2024-05-12 13:08:40 +00:00
from tests.conftest import (
CURRENT_TEST_STRATEGY,
EXMS,
get_args,
log_has,
patch_exchange,
patched_configuration_load_config_file,
)
2018-11-14 18:14:34 +00:00
2020-01-26 12:33:13 +00:00
def test_setup_optimize_configuration_without_arguments(mocker, default_conf, caplog) -> None:
patched_configuration_load_config_file(mocker, default_conf)
2018-11-14 18:14:34 +00:00
args = [
2024-05-12 13:38:09 +00:00
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
2018-11-14 18:14:34 +00:00
]
2020-01-26 12:33:13 +00:00
config = setup_optimize_configuration(get_args(args), RunMode.EDGE)
2024-05-12 13:38:09 +00:00
assert config["runmode"] == RunMode.EDGE
2018-12-25 13:35:48 +00:00
2024-05-12 13:38:09 +00:00
assert "max_open_trades" in config
assert "stake_currency" in config
assert "stake_amount" in config
assert "exchange" in config
assert "pair_whitelist" in config["exchange"]
assert "datadir" in config
assert log_has("Using data directory: {} ...".format(config["datadir"]), caplog)
assert "timeframe" in config
2018-11-14 18:14:34 +00:00
2024-05-12 13:38:09 +00:00
assert "timerange" not in config
assert "stoploss_range" not in config
2018-11-14 18:14:34 +00:00
2019-01-01 13:07:40 +00:00
def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> None:
patched_configuration_load_config_file(mocker, edge_conf)
2024-05-12 13:38:09 +00:00
mocker.patch("freqtrade.configuration.configuration.create_datadir", lambda c, x: x)
2018-11-14 18:14:34 +00:00
args = [
2024-05-12 13:38:09 +00:00
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
"--datadir",
"/foo/bar",
"--timeframe",
"1m",
"--timerange",
":100",
"--stoplosses=-0.01,-0.10,-0.001",
2018-11-14 18:14:34 +00:00
]
2020-01-26 12:33:13 +00:00
config = setup_optimize_configuration(get_args(args), RunMode.EDGE)
2024-05-12 13:38:09 +00:00
assert "max_open_trades" in config
assert "stake_currency" in config
assert "stake_amount" in config
assert "exchange" in config
assert "pair_whitelist" in config["exchange"]
assert "datadir" in config
assert config["runmode"] == RunMode.EDGE
assert log_has("Using data directory: {} ...".format(config["datadir"]), caplog)
assert "timeframe" in config
assert log_has("Parameter -i/--timeframe detected ... Using timeframe: 1m ...", caplog)
assert "timerange" in config
assert log_has("Parameter --timerange detected: {} ...".format(config["timerange"]), caplog)
2018-11-14 18:14:34 +00:00
def test_start(mocker, fee, edge_conf, caplog) -> None:
start_mock = MagicMock()
2024-05-12 13:38:09 +00:00
mocker.patch(f"{EXMS}.get_fee", fee)
2018-11-14 18:14:34 +00:00
patch_exchange(mocker)
2024-05-12 13:38:09 +00:00
mocker.patch("freqtrade.optimize.edge_cli.EdgeCli.start", start_mock)
patched_configuration_load_config_file(mocker, edge_conf)
2019-07-11 21:39:42 +00:00
2018-11-14 18:14:34 +00:00
args = [
2024-05-12 13:38:09 +00:00
"edge",
"--config",
"config.json",
"--strategy",
CURRENT_TEST_STRATEGY,
2018-11-14 18:14:34 +00:00
]
2020-02-10 09:35:48 +00:00
pargs = get_args(args)
start_edge(pargs)
2024-05-12 13:38:09 +00:00
assert log_has("Starting freqtrade in Edge mode", caplog)
2018-11-14 18:14:34 +00:00
assert start_mock.call_count == 1
def test_edge_init(mocker, edge_conf) -> None:
patch_exchange(mocker)
2024-05-12 13:38:09 +00:00
edge_conf["stake_amount"] = 20
2018-11-14 18:14:34 +00:00
edge_cli = EdgeCli(edge_conf)
assert edge_cli.config == edge_conf
2024-05-12 13:38:09 +00:00
assert edge_cli.config["stake_amount"] == "unlimited"
2018-11-14 18:14:34 +00:00
assert callable(edge_cli.edge.calculate)
assert edge_cli.strategy.bot_started is True
2018-11-14 18:14:34 +00:00
2019-10-05 13:34:31 +00:00
def test_edge_init_fee(mocker, edge_conf) -> None:
patch_exchange(mocker)
2024-05-12 13:38:09 +00:00
edge_conf["fee"] = 0.01234
edge_conf["stake_amount"] = 20
fee_mock = mocker.patch(f"{EXMS}.get_fee", return_value=0.5)
2019-10-05 13:34:31 +00:00
edge_cli = EdgeCli(edge_conf)
2024-04-27 17:52:48 +00:00
assert edge_cli.edge.fee == 0.01234
2019-10-05 13:34:31 +00:00
assert fee_mock.call_count == 0
2020-08-06 07:22:41 +00:00
def test_edge_start(mocker, edge_conf) -> None:
2024-05-12 13:38:09 +00:00
mock_calculate = mocker.patch(
"freqtrade.edge.edge_positioning.Edge.calculate", return_value=True
)
table_mock = mocker.patch("freqtrade.optimize.edge_cli.generate_edge_table")
2020-08-06 07:22:41 +00:00
patch_exchange(mocker)
2024-05-12 13:38:09 +00:00
edge_conf["stake_amount"] = 20
2020-08-06 07:22:41 +00:00
edge_cli = EdgeCli(edge_conf)
edge_cli.start()
assert mock_calculate.call_count == 1
assert table_mock.call_count == 1