| 1 | import re |
| 2 | from io import BytesIO |
| 3 | from pathlib import Path |
| 4 | from unittest.mock import MagicMock, PropertyMock |
| 5 | from zipfile import ZipFile |
| 6 | |
| 7 | import arrow |
| 8 | import pytest |
| 9 | |
| 10 | from freqtrade.commands import (start_convert_data, start_create_userdir, start_download_data, |
| 11 | start_hyperopt_list, start_hyperopt_show, start_install_ui, |
| 12 | start_list_data, start_list_exchanges, start_list_hyperopts, |
| 13 | start_list_markets, start_list_strategies, start_list_timeframes, |
| 14 | start_new_hyperopt, start_new_strategy, start_show_trades, |
| 15 | start_test_pairlist, start_trading) |
| 16 | from freqtrade.commands.deploy_commands import (clean_ui_subdir, download_and_install_ui, |
| 17 | get_ui_download_url, read_ui_version) |
| 18 | from freqtrade.configuration import setup_utils_configuration |
| 19 | from freqtrade.exceptions import OperationalException |
| 20 | from freqtrade.state import RunMode |
| 21 | from tests.conftest import (create_mock_trades, get_args, log_has, log_has_re, patch_exchange, |
| 22 | patched_configuration_load_config_file) |
| 23 | from tests.conftest_trades import MOCK_TRADE_COUNT |
| 24 | |
| 25 | |
| 26 | def test_setup_utils_configuration(): |
| 27 | args = [ |
| 28 | 'list-exchanges', '--config', 'config_bittrex.json.example', |
| 29 | ] |
| 30 | |
| 31 | config = setup_utils_configuration(get_args(args), RunMode.OTHER) |
| 32 | assert "exchange" in config |
| 33 | assert config['dry_run'] is True |
| 34 | assert config['exchange']['key'] == '' |
| 35 | assert config['exchange']['secret'] == '' |
| 36 | |
| 37 | |
| 38 | def test_start_trading_fail(mocker, caplog): |
| 39 | |
| 40 | mocker.patch("freqtrade.worker.Worker.run", MagicMock(side_effect=OperationalException)) |
| 41 | |
| 42 | mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(return_value=None)) |
| 43 | |
| 44 | exitmock = mocker.patch("freqtrade.worker.Worker.exit", MagicMock()) |
| 45 | args = [ |
| 46 | 'trade', |
| 47 | '-c', 'config_bittrex.json.example' |
| 48 | ] |
| 49 | start_trading(get_args(args)) |
| 50 | assert exitmock.call_count == 1 |
| 51 | |
| 52 | exitmock.reset_mock() |
| 53 | caplog.clear() |
| 54 | mocker.patch("freqtrade.worker.Worker.__init__", MagicMock(side_effect=OperationalException)) |
| 55 | start_trading(get_args(args)) |
| 56 | assert exitmock.call_count == 0 |
| 57 | assert log_has('Fatal exception!', caplog) |
| 58 | |
| 59 | |
| 60 | def test_list_exchanges(capsys): |
| 61 | |
| 62 | args = [ |
| 63 | "list-exchanges", |
| 64 | ] |
| 65 | |
| 66 | start_list_exchanges(get_args(args)) |
| 67 | captured = capsys.readouterr() |
| 68 | assert re.match(r"Exchanges available for Freqtrade.*", captured.out) |
| 69 | assert re.search(r".*binance.*", captured.out) |
| 70 | assert re.search(r".*bittrex.*", captured.out) |
| 71 | |
| 72 | # Test with --one-column |
| 73 | args = [ |
| 74 | "list-exchanges", |
| 75 | "--one-column", |
| 76 | ] |
| 77 | |
| 78 | start_list_exchanges(get_args(args)) |
| 79 | captured = capsys.readouterr() |
| 80 | assert re.search(r"^binance$", captured.out, re.MULTILINE) |
| 81 | assert re.search(r"^bittrex$", captured.out, re.MULTILINE) |
| 82 | |
| 83 | # Test with --all |
| 84 | args = [ |
| 85 | "list-exchanges", |
| 86 | "--all", |
| 87 | ] |
| 88 | |
| 89 | start_list_exchanges(get_args(args)) |
| 90 | captured = capsys.readouterr() |
| 91 | assert re.match(r"All exchanges supported by the ccxt library.*", captured.out) |
| 92 | assert re.search(r".*binance.*", captured.out) |
| 93 | assert re.search(r".*bittrex.*", captured.out) |
| 94 | assert re.search(r".*bitmex.*", captured.out) |
| 95 | |
| 96 | # Test with --one-column --all |
| 97 | args = [ |
| 98 | "list-exchanges", |
| 99 | "--one-column", |
| 100 | "--all", |
| 101 | ] |
| 102 | |
| 103 | start_list_exchanges(get_args(args)) |
| 104 | captured = capsys.readouterr() |
| 105 | assert re.search(r"^binance$", captured.out, re.MULTILINE) |
| 106 | assert re.search(r"^bittrex$", captured.out, re.MULTILINE) |
| 107 | assert re.search(r"^bitmex$", captured.out, re.MULTILINE) |
| 108 | |
| 109 | |
| 110 | def test_list_timeframes(mocker, capsys): |
| 111 | |
| 112 | api_mock = MagicMock() |
| 113 | api_mock.timeframes = {'1m': 'oneMin', |
| 114 | '5m': 'fiveMin', |
| 115 | '30m': 'thirtyMin', |
| 116 | '1h': 'hour', |
| 117 | '1d': 'day', |
| 118 | } |
| 119 | patch_exchange(mocker, api_mock=api_mock, id='bittrex') |
| 120 | args = [ |
| 121 | "list-timeframes", |
| 122 | ] |
| 123 | pargs = get_args(args) |
| 124 | pargs['config'] = None |
| 125 | with pytest.raises(OperationalException, |
| 126 | match=r"This command requires a configured exchange.*"): |
| 127 | start_list_timeframes(pargs) |
| 128 | |
| 129 | # Test with --config config_bittrex.json.example |
| 130 | args = [ |
| 131 | "list-timeframes", |
| 132 | '--config', 'config_bittrex.json.example', |
| 133 | ] |
| 134 | start_list_timeframes(get_args(args)) |
| 135 | captured = capsys.readouterr() |
| 136 | assert re.match("Timeframes available for the exchange `Bittrex`: " |
| 137 | "1m, 5m, 30m, 1h, 1d", |
| 138 | captured.out) |
| 139 | |
| 140 | # Test with --exchange bittrex |
| 141 | args = [ |
| 142 | "list-timeframes", |
| 143 | "--exchange", "bittrex", |
| 144 | ] |
| 145 | start_list_timeframes(get_args(args)) |
| 146 | captured = capsys.readouterr() |
| 147 | assert re.match("Timeframes available for the exchange `Bittrex`: " |
| 148 | "1m, 5m, 30m, 1h, 1d", |
| 149 | captured.out) |
| 150 | |
| 151 | api_mock.timeframes = {'1m': '1m', |
| 152 | '5m': '5m', |
| 153 | '15m': '15m', |
| 154 | '30m': '30m', |
| 155 | '1h': '1h', |
| 156 | '6h': '6h', |
| 157 | '12h': '12h', |
| 158 | '1d': '1d', |
| 159 | '3d': '3d', |
| 160 | } |
| 161 | patch_exchange(mocker, api_mock=api_mock, id='binance') |
| 162 | # Test with --exchange binance |
| 163 | args = [ |
| 164 | "list-timeframes", |
| 165 | "--exchange", "binance", |
| 166 | ] |
| 167 | start_list_timeframes(get_args(args)) |
| 168 | captured = capsys.readouterr() |
| 169 | assert re.match("Timeframes available for the exchange `Binance`: " |
| 170 | "1m, 5m, 15m, 30m, 1h, 6h, 12h, 1d, 3d", |
| 171 | captured.out) |
| 172 | |
| 173 | # Test with --one-column |
| 174 | args = [ |
| 175 | "list-timeframes", |
| 176 | '--config', 'config_bittrex.json.example', |
| 177 | "--one-column", |
| 178 | ] |
| 179 | start_list_timeframes(get_args(args)) |
| 180 | captured = capsys.readouterr() |
| 181 | assert re.search(r"^1m$", captured.out, re.MULTILINE) |
| 182 | assert re.search(r"^5m$", captured.out, re.MULTILINE) |
| 183 | assert re.search(r"^1h$", captured.out, re.MULTILINE) |
| 184 | assert re.search(r"^1d$", captured.out, re.MULTILINE) |
| 185 | |
| 186 | # Test with --exchange binance --one-column |
| 187 | args = [ |
| 188 | "list-timeframes", |
| 189 | "--exchange", "binance", |
| 190 | "--one-column", |
| 191 | ] |
| 192 | start_list_timeframes(get_args(args)) |
| 193 | captured = capsys.readouterr() |
| 194 | assert re.search(r"^1m$", captured.out, re.MULTILINE) |
| 195 | assert re.search(r"^5m$", captured.out, re.MULTILINE) |
| 196 | assert re.search(r"^1h$", captured.out, re.MULTILINE) |
| 197 | assert re.search(r"^1d$", captured.out, re.MULTILINE) |
| 198 | |
| 199 | |
| 200 | def test_list_markets(mocker, markets, capsys): |
| 201 | |
| 202 | api_mock = MagicMock() |
| 203 | api_mock.markets = markets |
| 204 | patch_exchange(mocker, api_mock=api_mock, id='bittrex') |
| 205 | |
| 206 | # Test with no --config |
| 207 | args = [ |
| 208 | "list-markets", |
| 209 | ] |
| 210 | pargs = get_args(args) |
| 211 | pargs['config'] = None |
| 212 | with pytest.raises(OperationalException, |
| 213 | match=r"This command requires a configured exchange.*"): |
| 214 | start_list_markets(pargs, False) |
| 215 | |
| 216 | # Test with --config config_bittrex.json.example |
| 217 | args = [ |
| 218 | "list-markets", |
| 219 | '--config', 'config_bittrex.json.example', |
| 220 | "--print-list", |
| 221 | ] |
| 222 | start_list_markets(get_args(args), False) |
| 223 | captured = capsys.readouterr() |
| 224 | assert ("Exchange Bittrex has 10 active markets: " |
| 225 | "BLK/BTC, ETH/BTC, ETH/USDT, LTC/BTC, LTC/ETH, LTC/USD, NEO/BTC, " |
| 226 | "TKN/BTC, XLTCUSDT, XRP/BTC.\n" |
| 227 | in captured.out) |
| 228 | |
| 229 | patch_exchange(mocker, api_mock=api_mock, id="binance") |
| 230 | # Test with --exchange |
| 231 | args = [ |
| 232 | "list-markets", |
| 233 | "--exchange", "binance" |
| 234 | ] |
| 235 | pargs = get_args(args) |
| 236 | pargs['config'] = None |
| 237 | start_list_markets(pargs, False) |
| 238 | captured = capsys.readouterr() |
| 239 | assert re.match("\nExchange Binance has 10 active markets:\n", |
| 240 | captured.out) |
| 241 | |
| 242 | patch_exchange(mocker, api_mock=api_mock, id="bittrex") |
| 243 | # Test with --all: all markets |
| 244 | args = [ |
| 245 | "list-markets", "--all", |
| 246 | '--config', 'config_bittrex.json.example', |
| 247 | "--print-list", |
| 248 | ] |
| 249 | start_list_markets(get_args(args), False) |
| 250 | captured = capsys.readouterr() |
| 251 | assert ("Exchange Bittrex has 12 markets: " |
| 252 | "BLK/BTC, BTT/BTC, ETH/BTC, ETH/USDT, LTC/BTC, LTC/ETH, LTC/USD, LTC/USDT, NEO/BTC, " |
| 253 | "TKN/BTC, XLTCUSDT, XRP/BTC.\n" |
| 254 | in captured.out) |
| 255 | |
| 256 | # Test list-pairs subcommand: active pairs |
| 257 | args = [ |
| 258 | "list-pairs", |
| 259 | '--config', 'config_bittrex.json.example', |
| 260 | "--print-list", |
| 261 | ] |
| 262 | start_list_markets(get_args(args), True) |
| 263 | captured = capsys.readouterr() |
| 264 | assert ("Exchange Bittrex has 9 active pairs: " |
| 265 | "BLK/BTC, ETH/BTC, ETH/USDT, LTC/BTC, LTC/ETH, LTC/USD, NEO/BTC, TKN/BTC, XRP/BTC.\n" |
| 266 | in captured.out) |
| 267 | |
| 268 | # Test list-pairs subcommand with --all: all pairs |
| 269 | args = [ |
| 270 | "list-pairs", "--all", |
| 271 | '--config', 'config_bittrex.json.example', |
| 272 | "--print-list", |
| 273 | ] |
| 274 | start_list_markets(get_args(args), True) |
| 275 | captured = capsys.readouterr() |
| 276 | assert ("Exchange Bittrex has 11 pairs: " |
| 277 | "BLK/BTC, BTT/BTC, ETH/BTC, ETH/USDT, LTC/BTC, LTC/ETH, LTC/USD, LTC/USDT, NEO/BTC, " |
| 278 | "TKN/BTC, XRP/BTC.\n" |
| 279 | in captured.out) |
| 280 | |
| 281 | # active markets, base=ETH, LTC |
| 282 | args = [ |
| 283 | "list-markets", |
| 284 | '--config', 'config_bittrex.json.example', |
| 285 | "--base", "ETH", "LTC", |
| 286 | "--print-list", |
| 287 | ] |
| 288 | start_list_markets(get_args(args), False) |
| 289 | captured = capsys.readouterr() |
| 290 | assert ("Exchange Bittrex has 6 active markets with ETH, LTC as base currencies: " |
| 291 | "ETH/BTC, ETH/USDT, LTC/BTC, LTC/ETH, LTC/USD, XLTCUSDT.\n" |
| 292 | in captured.out) |
| 293 | |
| 294 | # active markets, base=LTC |
| 295 | args = [ |
| 296 | "list-markets", |
| 297 | '--config', 'config_bittrex.json.example', |
| 298 | "--base", "LTC", |
| 299 | "--print-list", |
| 300 | ] |
| 301 | start_list_markets(get_args(args), False) |
| 302 | captured = capsys.readouterr() |
| 303 | assert ("Exchange Bittrex has 4 active markets with LTC as base currency: " |
| 304 | "LTC/BTC, LTC/ETH, LTC/USD, XLTCUSDT.\n" |
| 305 | in captured.out) |
| 306 | |
| 307 | # active markets, quote=USDT, USD |
| 308 | args = [ |
| 309 | "list-markets", |
| 310 | '--config', 'config_bittrex.json.example', |
| 311 | "--quote", "USDT", "USD", |
| 312 | "--print-list", |
| 313 | ] |
| 314 | start_list_markets(get_args(args), False) |
| 315 | captured = capsys.readouterr() |
| 316 | assert ("Exchange Bittrex has 3 active markets with USDT, USD as quote currencies: " |
| 317 | "ETH/USDT, LTC/USD, XLTCUSDT.\n" |
| 318 | in captured.out) |
| 319 | |
| 320 | # active markets, quote=USDT |
| 321 | args = [ |
| 322 | "list-markets", |
| 323 | '--config', 'config_bittrex.json.example', |
| 324 | "--quote", "USDT", |
| 325 | "--print-list", |
| 326 | ] |
| 327 | start_list_markets(get_args(args), False) |
| 328 | captured = capsys.readouterr() |
| 329 | assert ("Exchange Bittrex has 2 active markets with USDT as quote currency: " |
| 330 | "ETH/USDT, XLTCUSDT.\n" |
| 331 | in captured.out) |
| 332 | |
| 333 | # active markets, base=LTC, quote=USDT |
| 334 | args = [ |
| 335 | "list-markets", |
| 336 | '--config', 'config_bittrex.json.example', |
| 337 | "--base", "LTC", "--quote", "USDT", |
| 338 | "--print-list", |
| 339 | ] |
| 340 | start_list_markets(get_args(args), False) |
| 341 | captured = capsys.readouterr() |
| 342 | assert ("Exchange Bittrex has 1 active market with LTC as base currency and " |
| 343 | "with USDT as quote currency: XLTCUSDT.\n" |
| 344 | in captured.out) |
| 345 | |
| 346 | # active pairs, base=LTC, quote=USDT |
| 347 | args = [ |
| 348 | "list-pairs", |
| 349 | '--config', 'config_bittrex.json.example', |
| 350 | "--base", "LTC", "--quote", "USD", |
| 351 | "--print-list", |
| 352 | ] |
| 353 | start_list_markets(get_args(args), True) |
| 354 | captured = capsys.readouterr() |
| 355 | assert ("Exchange Bittrex has 1 active pair with LTC as base currency and " |
| 356 | "with USD as quote currency: LTC/USD.\n" |
| 357 | in captured.out) |
| 358 | |
| 359 | # active markets, base=LTC, quote=USDT, NONEXISTENT |
| 360 | args = [ |
| 361 | "list-markets", |
| 362 | '--config', 'config_bittrex.json.example', |
| 363 | "--base", "LTC", "--quote", "USDT", "NONEXISTENT", |
| 364 | "--print-list", |
| 365 | ] |
| 366 | start_list_markets(get_args(args), False) |
| 367 | captured = capsys.readouterr() |
| 368 | assert ("Exchange Bittrex has 1 active market with LTC as base currency and " |
| 369 | "with USDT, NONEXISTENT as quote currencies: XLTCUSDT.\n" |
| 370 | in captured.out) |
| 371 | |
| 372 | # active markets, base=LTC, quote=NONEXISTENT |
| 373 | args = [ |
| 374 | "list-markets", |
| 375 | '--config', 'config_bittrex.json.example', |
| 376 | "--base", "LTC", "--quote", "NONEXISTENT", |
| 377 | "--print-list", |
| 378 | ] |
| 379 | start_list_markets(get_args(args), False) |
| 380 | captured = capsys.readouterr() |
| 381 | assert ("Exchange Bittrex has 0 active markets with LTC as base currency and " |
| 382 | "with NONEXISTENT as quote currency.\n" |
| 383 | in captured.out) |
| 384 | |
| 385 | # Test tabular output |
| 386 | args = [ |
| 387 | "list-markets", |
| 388 | '--config', 'config_bittrex.json.example', |
| 389 | ] |
| 390 | start_list_markets(get_args(args), False) |
| 391 | captured = capsys.readouterr() |
| 392 | assert ("Exchange Bittrex has 10 active markets:\n" |
| 393 | in captured.out) |
| 394 | |
| 395 | # Test tabular output, no markets found |
| 396 | args = [ |
| 397 | "list-markets", |
| 398 | '--config', 'config_bittrex.json.example', |
| 399 | "--base", "LTC", "--quote", "NONEXISTENT", |
| 400 | ] |
| 401 | start_list_markets(get_args(args), False) |
| 402 | captured = capsys.readouterr() |
| 403 | assert ("Exchange Bittrex has 0 active markets with LTC as base currency and " |
| 404 | "with NONEXISTENT as quote currency.\n" |
| 405 | in captured.out) |
| 406 | |
| 407 | # Test --print-json |
| 408 | args = [ |
| 409 | "list-markets", |
| 410 | '--config', 'config_bittrex.json.example', |
| 411 | "--print-json" |
| 412 | ] |
| 413 | start_list_markets(get_args(args), False) |
| 414 | captured = capsys.readouterr() |
| 415 | assert ('["BLK/BTC","ETH/BTC","ETH/USDT","LTC/BTC","LTC/ETH","LTC/USD","NEO/BTC",' |
| 416 | '"TKN/BTC","XLTCUSDT","XRP/BTC"]' |
| 417 | in captured.out) |
| 418 | |
| 419 | # Test --print-csv |
| 420 | args = [ |
| 421 | "list-markets", |
| 422 | '--config', 'config_bittrex.json.example', |
| 423 | "--print-csv" |
| 424 | ] |
| 425 | start_list_markets(get_args(args), False) |
| 426 | captured = capsys.readouterr() |
| 427 | assert ("Id,Symbol,Base,Quote,Active,Is pair" in captured.out) |
| 428 | assert ("blkbtc,BLK/BTC,BLK,BTC,True,True" in captured.out) |
| 429 | assert ("USD-LTC,LTC/USD,LTC,USD,True,True" in captured.out) |
| 430 | |
| 431 | # Test --one-column |
| 432 | args = [ |
| 433 | "list-markets", |
| 434 | '--config', 'config_bittrex.json.example', |
| 435 | "--one-column" |
| 436 | ] |
| 437 | start_list_markets(get_args(args), False) |
| 438 | captured = capsys.readouterr() |
| 439 | assert re.search(r"^BLK/BTC$", captured.out, re.MULTILINE) |
| 440 | assert re.search(r"^LTC/USD$", captured.out, re.MULTILINE) |
| 441 | |
| 442 | mocker.patch('freqtrade.exchange.Exchange.markets', PropertyMock(side_effect=ValueError)) |
| 443 | # Test --one-column |
| 444 | args = [ |
| 445 | "list-markets", |
| 446 | '--config', 'config_bittrex.json.example', |
| 447 | "--one-column" |
| 448 | ] |
| 449 | with pytest.raises(OperationalException, match=r"Cannot get markets.*"): |
| 450 | start_list_markets(get_args(args), False) |
| 451 | |
| 452 | |
| 453 | def test_create_datadir_failed(caplog): |
| 454 | |
| 455 | args = [ |
| 456 | "create-userdir", |
| 457 | ] |
| 458 | with pytest.raises(SystemExit): |
| 459 | start_create_userdir(get_args(args)) |
| 460 | assert log_has("`create-userdir` requires --userdir to be set.", caplog) |
| 461 | |
| 462 | |
| 463 | def test_create_datadir(caplog, mocker): |
| 464 | |
| 465 | cud = mocker.patch("freqtrade.commands.deploy_commands.create_userdata_dir", MagicMock()) |
| 466 | csf = mocker.patch("freqtrade.commands.deploy_commands.copy_sample_files", MagicMock()) |
| 467 | args = [ |
| 468 | "create-userdir", |
| 469 | "--userdir", |
| 470 | "/temp/freqtrade/test" |
| 471 | ] |
| 472 | start_create_userdir(get_args(args)) |
| 473 | |
| 474 | assert cud.call_count == 1 |
| 475 | assert csf.call_count == 1 |
| 476 | |
| 477 | |
| 478 | def test_start_new_strategy(mocker, caplog): |
| 479 | wt_mock = mocker.patch.object(Path, "write_text", MagicMock()) |
| 480 | mocker.patch.object(Path, "exists", MagicMock(return_value=False)) |
| 481 | |
| 482 | args = [ |
| 483 | "new-strategy", |
| 484 | "--strategy", |
| 485 | "CoolNewStrategy" |
| 486 | ] |
| 487 | start_new_strategy(get_args(args)) |
| 488 | |
| 489 | assert wt_mock.call_count == 1 |
| 490 | assert "CoolNewStrategy" in wt_mock.call_args_list[0][0][0] |
| 491 | assert log_has_re("Writing strategy to .*", caplog) |
| 492 | |
| 493 | mocker.patch('freqtrade.commands.deploy_commands.setup_utils_configuration') |
| 494 | mocker.patch.object(Path, "exists", MagicMock(return_value=True)) |
| 495 | with pytest.raises(OperationalException, |
| 496 | match=r".* already exists. Please choose another Strategy Name\."): |
| 497 | start_new_strategy(get_args(args)) |
| 498 | |
| 499 | |
| 500 | def test_start_new_strategy_DefaultStrat(mocker, caplog): |
| 501 | args = [ |
| 502 | "new-strategy", |
| 503 | "--strategy", |
| 504 | "DefaultStrategy" |
| 505 | ] |
| 506 | with pytest.raises(OperationalException, |
| 507 | match=r"DefaultStrategy is not allowed as name\."): |
| 508 | start_new_strategy(get_args(args)) |
| 509 | |
| 510 | |
| 511 | def test_start_new_strategy_no_arg(mocker, caplog): |
| 512 | args = [ |
| 513 | "new-strategy", |
| 514 | ] |
| 515 | with pytest.raises(OperationalException, |
| 516 | match="`new-strategy` requires --strategy to be set."): |
| 517 | start_new_strategy(get_args(args)) |
| 518 | |
| 519 | |
| 520 | def test_start_new_hyperopt(mocker, caplog): |
| 521 | wt_mock = mocker.patch.object(Path, "write_text", MagicMock()) |
| 522 | mocker.patch.object(Path, "exists", MagicMock(return_value=False)) |
| 523 | |
| 524 | args = [ |
| 525 | "new-hyperopt", |
| 526 | "--hyperopt", |
| 527 | "CoolNewhyperopt" |
| 528 | ] |
| 529 | start_new_hyperopt(get_args(args)) |
| 530 | |
| 531 | assert wt_mock.call_count == 1 |
| 532 | assert "CoolNewhyperopt" in wt_mock.call_args_list[0][0][0] |
| 533 | assert log_has_re("Writing hyperopt to .*", caplog) |
| 534 | |
| 535 | mocker.patch('freqtrade.commands.deploy_commands.setup_utils_configuration') |
| 536 | mocker.patch.object(Path, "exists", MagicMock(return_value=True)) |
| 537 | with pytest.raises(OperationalException, |
| 538 | match=r".* already exists. Please choose another Hyperopt Name\."): |
| 539 | start_new_hyperopt(get_args(args)) |
| 540 | |
| 541 | |
| 542 | def test_start_new_hyperopt_DefaultHyperopt(mocker, caplog): |
| 543 | args = [ |
| 544 | "new-hyperopt", |
| 545 | "--hyperopt", |
| 546 | "DefaultHyperopt" |
| 547 | ] |
| 548 | with pytest.raises(OperationalException, |
| 549 | match=r"DefaultHyperopt is not allowed as name\."): |
| 550 | start_new_hyperopt(get_args(args)) |
| 551 | |
| 552 | |
| 553 | def test_start_new_hyperopt_no_arg(mocker): |
| 554 | args = [ |
| 555 | "new-hyperopt", |
| 556 | ] |
| 557 | with pytest.raises(OperationalException, |
| 558 | match="`new-hyperopt` requires --hyperopt to be set."): |
| 559 | start_new_hyperopt(get_args(args)) |
| 560 | |
| 561 | |
| 562 | def test_start_install_ui(mocker): |
| 563 | clean_mock = mocker.patch('freqtrade.commands.deploy_commands.clean_ui_subdir') |
| 564 | get_url_mock = mocker.patch('freqtrade.commands.deploy_commands.get_ui_download_url', |
| 565 | return_value=('https://example.com/whatever', '0.0.1')) |
| 566 | download_mock = mocker.patch('freqtrade.commands.deploy_commands.download_and_install_ui') |
| 567 | mocker.patch('freqtrade.commands.deploy_commands.read_ui_version', return_value=None) |
| 568 | args = [ |
| 569 | "install-ui", |
| 570 | ] |
| 571 | start_install_ui(get_args(args)) |
| 572 | assert clean_mock.call_count == 1 |
| 573 | assert get_url_mock.call_count == 1 |
| 574 | assert download_mock.call_count == 1 |
| 575 | |
| 576 | clean_mock.reset_mock() |
| 577 | get_url_mock.reset_mock() |
| 578 | download_mock.reset_mock() |
| 579 | |
| 580 | args = [ |
| 581 | "install-ui", |
| 582 | "--erase", |
| 583 | ] |
| 584 | start_install_ui(get_args(args)) |
| 585 | assert clean_mock.call_count == 1 |
| 586 | assert get_url_mock.call_count == 1 |
| 587 | assert download_mock.call_count == 0 |
| 588 | |
| 589 | |
| 590 | def test_clean_ui_subdir(mocker, tmpdir, caplog): |
| 591 | mocker.patch("freqtrade.commands.deploy_commands.Path.is_dir", |
| 592 | side_effect=[True, True]) |
| 593 | mocker.patch("freqtrade.commands.deploy_commands.Path.is_file", |
| 594 | side_effect=[False, True]) |
| 595 | rd_mock = mocker.patch("freqtrade.commands.deploy_commands.Path.rmdir") |
| 596 | ul_mock = mocker.patch("freqtrade.commands.deploy_commands.Path.unlink") |
| 597 | |
| 598 | mocker.patch("freqtrade.commands.deploy_commands.Path.glob", |
| 599 | return_value=[Path('test1'), Path('test2'), Path('.gitkeep')]) |
| 600 | folder = Path(tmpdir) / "uitests" |
| 601 | clean_ui_subdir(folder) |
| 602 | assert log_has("Removing UI directory content.", caplog) |
| 603 | assert rd_mock.call_count == 1 |
| 604 | assert ul_mock.call_count == 1 |
| 605 | |
| 606 | |
| 607 | def test_download_and_install_ui(mocker, tmpdir): |
| 608 | # Create zipfile |
| 609 | requests_mock = MagicMock() |
| 610 | file_like_object = BytesIO() |
| 611 | with ZipFile(file_like_object, mode='w') as zipfile: |
| 612 | for file in ('test1.txt', 'hello/', 'test2.txt'): |
| 613 | zipfile.writestr(file, file) |
| 614 | file_like_object.seek(0) |
| 615 | requests_mock.content = file_like_object.read() |
| 616 | |
| 617 | mocker.patch("freqtrade.commands.deploy_commands.requests.get", return_value=requests_mock) |
| 618 | |
| 619 | mocker.patch("freqtrade.commands.deploy_commands.Path.is_dir", |
| 620 | side_effect=[True, False]) |
| 621 | wb_mock = mocker.patch("freqtrade.commands.deploy_commands.Path.write_bytes") |
| 622 | |
| 623 | folder = Path(tmpdir) / "uitests_dl" |
| 624 | folder.mkdir(exist_ok=True) |
| 625 | |
| 626 | assert read_ui_version(folder) is None |
| 627 | |
| 628 | download_and_install_ui(folder, 'http://whatever.xxx/download/file.zip', '22') |
| 629 | |
| 630 | assert wb_mock.call_count == 2 |
| 631 | |
| 632 | assert read_ui_version(folder) == '22' |
| 633 | |
| 634 | |
| 635 | def test_get_ui_download_url(mocker): |
| 636 | response = MagicMock() |
| 637 | response.json = MagicMock( |
| 638 | side_effect=[[{'assets_url': 'http://whatever.json', 'name': '0.0.1'}], |
| 639 | [{'browser_download_url': 'http://download.zip'}]]) |
| 640 | get_mock = mocker.patch("freqtrade.commands.deploy_commands.requests.get", |
| 641 | return_value=response) |
| 642 | x, last_version = get_ui_download_url() |
| 643 | assert get_mock.call_count == 2 |
| 644 | assert last_version == '0.0.1' |
| 645 | assert x == 'http://download.zip' |
| 646 | |
| 647 | |
| 648 | def test_get_ui_download_url_direct(mocker): |
| 649 | response = MagicMock() |
| 650 | response.json = MagicMock( |
| 651 | side_effect=[[{ |
| 652 | 'assets_url': 'http://whatever.json', |
| 653 | 'name': '0.0.1', |
| 654 | 'assets': [{'browser_download_url': 'http://download11.zip'}]}]]) |
| 655 | get_mock = mocker.patch("freqtrade.commands.deploy_commands.requests.get", |
| 656 | return_value=response) |
| 657 | x, last_version = get_ui_download_url() |
| 658 | assert get_mock.call_count == 1 |
| 659 | assert last_version == '0.0.1' |
| 660 | assert x == 'http://download11.zip' |
| 661 | |
| 662 | |
| 663 | def test_download_data_keyboardInterrupt(mocker, caplog, markets): |
| 664 | dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', |
| 665 | MagicMock(side_effect=KeyboardInterrupt)) |
| 666 | patch_exchange(mocker) |
| 667 | mocker.patch( |
| 668 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) |
| 669 | ) |
| 670 | args = [ |
| 671 | "download-data", |
| 672 | "--exchange", "binance", |
| 673 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 674 | ] |
| 675 | with pytest.raises(SystemExit): |
| 676 | start_download_data(get_args(args)) |
| 677 | |
| 678 | assert dl_mock.call_count == 1 |
| 679 | |
| 680 | |
| 681 | def test_download_data_timerange(mocker, caplog, markets): |
| 682 | dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', |
| 683 | MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) |
| 684 | patch_exchange(mocker) |
| 685 | mocker.patch( |
| 686 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value=markets) |
| 687 | ) |
| 688 | args = [ |
| 689 | "download-data", |
| 690 | "--exchange", "binance", |
| 691 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 692 | "--days", "20", |
| 693 | "--timerange", "20200101-" |
| 694 | ] |
| 695 | with pytest.raises(OperationalException, |
| 696 | match=r"--days and --timerange are mutually.*"): |
| 697 | start_download_data(get_args(args)) |
| 698 | assert dl_mock.call_count == 0 |
| 699 | |
| 700 | args = [ |
| 701 | "download-data", |
| 702 | "--exchange", "binance", |
| 703 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 704 | "--days", "20", |
| 705 | ] |
| 706 | start_download_data(get_args(args)) |
| 707 | assert dl_mock.call_count == 1 |
| 708 | # 20days ago |
| 709 | days_ago = arrow.get(arrow.now().shift(days=-20).date()).int_timestamp |
| 710 | assert dl_mock.call_args_list[0][1]['timerange'].startts == days_ago |
| 711 | |
| 712 | dl_mock.reset_mock() |
| 713 | args = [ |
| 714 | "download-data", |
| 715 | "--exchange", "binance", |
| 716 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 717 | "--timerange", "20200101-" |
| 718 | ] |
| 719 | start_download_data(get_args(args)) |
| 720 | assert dl_mock.call_count == 1 |
| 721 | |
| 722 | assert dl_mock.call_args_list[0][1]['timerange'].startts == arrow.Arrow( |
| 723 | 2020, 1, 1).int_timestamp |
| 724 | |
| 725 | |
| 726 | def test_download_data_no_markets(mocker, caplog): |
| 727 | dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', |
| 728 | MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) |
| 729 | patch_exchange(mocker, id='binance') |
| 730 | mocker.patch( |
| 731 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) |
| 732 | ) |
| 733 | args = [ |
| 734 | "download-data", |
| 735 | "--exchange", "binance", |
| 736 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 737 | "--days", "20" |
| 738 | ] |
| 739 | start_download_data(get_args(args)) |
| 740 | assert dl_mock.call_args[1]['timerange'].starttype == "date" |
| 741 | assert log_has("Pairs [ETH/BTC,XRP/BTC] not available on exchange Binance.", caplog) |
| 742 | |
| 743 | |
| 744 | def test_download_data_no_exchange(mocker, caplog): |
| 745 | mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', |
| 746 | MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) |
| 747 | patch_exchange(mocker) |
| 748 | mocker.patch( |
| 749 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) |
| 750 | ) |
| 751 | args = [ |
| 752 | "download-data", |
| 753 | ] |
| 754 | pargs = get_args(args) |
| 755 | pargs['config'] = None |
| 756 | with pytest.raises(OperationalException, |
| 757 | match=r"This command requires a configured exchange.*"): |
| 758 | start_download_data(pargs) |
| 759 | |
| 760 | |
| 761 | def test_download_data_no_pairs(mocker, caplog): |
| 762 | |
| 763 | mocker.patch.object(Path, "exists", MagicMock(return_value=False)) |
| 764 | |
| 765 | mocker.patch('freqtrade.commands.data_commands.refresh_backtest_ohlcv_data', |
| 766 | MagicMock(return_value=["ETH/BTC", "XRP/BTC"])) |
| 767 | patch_exchange(mocker) |
| 768 | mocker.patch( |
| 769 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) |
| 770 | ) |
| 771 | args = [ |
| 772 | "download-data", |
| 773 | "--exchange", |
| 774 | "binance", |
| 775 | ] |
| 776 | pargs = get_args(args) |
| 777 | pargs['config'] = None |
| 778 | with pytest.raises(OperationalException, |
| 779 | match=r"Downloading data requires a list of pairs\..*"): |
| 780 | start_download_data(pargs) |
| 781 | |
| 782 | |
| 783 | def test_download_data_trades(mocker, caplog): |
| 784 | dl_mock = mocker.patch('freqtrade.commands.data_commands.refresh_backtest_trades_data', |
| 785 | MagicMock(return_value=[])) |
| 786 | convert_mock = mocker.patch('freqtrade.commands.data_commands.convert_trades_to_ohlcv', |
| 787 | MagicMock(return_value=[])) |
| 788 | patch_exchange(mocker) |
| 789 | mocker.patch( |
| 790 | 'freqtrade.exchange.Exchange.markets', PropertyMock(return_value={}) |
| 791 | ) |
| 792 | args = [ |
| 793 | "download-data", |
| 794 | "--exchange", "kraken", |
| 795 | "--pairs", "ETH/BTC", "XRP/BTC", |
| 796 | "--days", "20", |
| 797 | "--dl-trades" |
| 798 | ] |
| 799 | start_download_data(get_args(args)) |
| 800 | assert dl_mock.call_args[1]['timerange'].starttype == "date" |
| 801 | assert dl_mock.call_count == 1 |
| 802 | assert convert_mock.call_count == 1 |
| 803 | |
| 804 | |
| 805 | def test_start_list_strategies(mocker, caplog, capsys): |
| 806 | |
| 807 | args = [ |
| 808 | "list-strategies", |
| 809 | "--strategy-path", |
| 810 | str(Path(__file__).parent.parent / "strategy" / "strats"), |
| 811 | "-1" |
| 812 | ] |
| 813 | pargs = get_args(args) |
| 814 | # pargs['config'] = None |
| 815 | start_list_strategies(pargs) |
| 816 | captured = capsys.readouterr() |
| 817 | assert "TestStrategyLegacy" in captured.out |
| 818 | assert "legacy_strategy.py" not in captured.out |
| 819 | assert "DefaultStrategy" in captured.out |
| 820 | |
| 821 | # Test regular output |
| 822 | args = [ |
| 823 | "list-strategies", |
| 824 | "--strategy-path", |
| 825 | str(Path(__file__).parent.parent / "strategy" / "strats"), |
| 826 | '--no-color', |
| 827 | ] |
| 828 | pargs = get_args(args) |
| 829 | # pargs['config'] = None |
| 830 | start_list_strategies(pargs) |
| 831 | captured = capsys.readouterr() |
| 832 | assert "TestStrategyLegacy" in captured.out |
| 833 | assert "legacy_strategy.py" in captured.out |
| 834 | assert "DefaultStrategy" in captured.out |
| 835 | |
| 836 | |
| 837 | def test_start_list_hyperopts(mocker, caplog, capsys): |
| 838 | |
| 839 | args = [ |
| 840 | "list-hyperopts", |
| 841 | "--hyperopt-path", |
| 842 | str(Path(__file__).parent.parent / "optimize" / "hyperopts"), |
| 843 | "-1" |
| 844 | ] |
| 845 | pargs = get_args(args) |
| 846 | # pargs['config'] = None |
| 847 | start_list_hyperopts(pargs) |
| 848 | captured = capsys.readouterr() |
| 849 | assert "TestHyperoptLegacy" not in captured.out |
| 850 | assert "legacy_hyperopt.py" not in captured.out |
| 851 | assert "DefaultHyperOpt" in captured.out |
| 852 | assert "test_hyperopt.py" not in captured.out |
| 853 | |
| 854 | # Test regular output |
| 855 | args = [ |
| 856 | "list-hyperopts", |
| 857 | "--hyperopt-path", |
| 858 | str(Path(__file__).parent.parent / "optimize" / "hyperopts"), |
| 859 | ] |
| 860 | pargs = get_args(args) |
| 861 | # pargs['config'] = None |
| 862 | start_list_hyperopts(pargs) |
| 863 | captured = capsys.readouterr() |
| 864 | assert "TestHyperoptLegacy" not in captured.out |
| 865 | assert "legacy_hyperopt.py" not in captured.out |
| 866 | assert "DefaultHyperOpt" in captured.out |
| 867 | |
| 868 | |
| 869 | def test_start_test_pairlist(mocker, caplog, tickers, default_conf, capsys): |
| 870 | patch_exchange(mocker, mock_markets=True) |
| 871 | mocker.patch.multiple('freqtrade.exchange.Exchange', |
| 872 | exchange_has=MagicMock(return_value=True), |
| 873 | get_tickers=tickers, |
| 874 | ) |
| 875 | |
| 876 | default_conf['pairlists'] = [ |
| 877 | { |
| 878 | "method": "VolumePairList", |
| 879 | "number_assets": 5, |
| 880 | "sort_key": "quoteVolume", |
| 881 | }, |
| 882 | {"method": "PrecisionFilter"}, |
| 883 | {"method": "PriceFilter", "low_price_ratio": 0.02}, |
| 884 | ] |
| 885 | |
| 886 | patched_configuration_load_config_file(mocker, default_conf) |
| 887 | args = [ |
| 888 | 'test-pairlist', |
| 889 | '-c', 'config_bittrex.json.example' |
| 890 | ] |
| 891 | |
| 892 | start_test_pairlist(get_args(args)) |
| 893 | |
| 894 | assert log_has_re(r"^Using resolved pairlist VolumePairList.*", caplog) |
| 895 | assert log_has_re(r"^Using resolved pairlist PrecisionFilter.*", caplog) |
| 896 | assert log_has_re(r"^Using resolved pairlist PriceFilter.*", caplog) |
| 897 | captured = capsys.readouterr() |
| 898 | assert re.match(r"Pairs for .*", captured.out) |
| 899 | assert re.match("['ETH/BTC', 'TKN/BTC', 'BLK/BTC', 'LTC/BTC', 'XRP/BTC']", captured.out) |
| 900 | |
| 901 | args = [ |
| 902 | 'test-pairlist', |
| 903 | '-c', 'config_bittrex.json.example', |
| 904 | '--one-column', |
| 905 | ] |
| 906 | start_test_pairlist(get_args(args)) |
| 907 | captured = capsys.readouterr() |
| 908 | assert re.match(r"ETH/BTC\nTKN/BTC\nBLK/BTC\nLTC/BTC\nXRP/BTC\n", captured.out) |
| 909 | |
| 910 | args = [ |
| 911 | 'test-pairlist', |
| 912 | '-c', 'config_bittrex.json.example', |
| 913 | '--print-json', |
| 914 | ] |
| 915 | start_test_pairlist(get_args(args)) |
| 916 | captured = capsys.readouterr() |
| 917 | assert re.match(r'Pairs for BTC: \n\["ETH/BTC","TKN/BTC","BLK/BTC","LTC/BTC","XRP/BTC"\]\n', |
| 918 | captured.out) |
| 919 | |
| 920 | |
| 921 | def test_hyperopt_list(mocker, capsys, caplog, hyperopt_results): |
| 922 | mocker.patch( |
| 923 | 'freqtrade.optimize.hyperopt_tools.HyperoptTools.load_previous_results', |
| 924 | MagicMock(return_value=hyperopt_results) |
| 925 | ) |
| 926 | |
| 927 | args = [ |
| 928 | "hyperopt-list", |
| 929 | "--no-details", |
| 930 | "--no-color", |
| 931 | ] |
| 932 | pargs = get_args(args) |
| 933 | pargs['config'] = None |
| 934 | start_hyperopt_list(pargs) |
| 935 | captured = capsys.readouterr() |
| 936 | assert all(x in captured.out |
| 937 | for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", |
| 938 | " 6/12", " 7/12", " 8/12", " 9/12", " 10/12", |
| 939 | " 11/12", " 12/12"]) |
| 940 | args = [ |
| 941 | "hyperopt-list", |
| 942 | "--best", |
| 943 | "--no-details", |
| 944 | "--no-color", |
| 945 | ] |
| 946 | pargs = get_args(args) |
| 947 | pargs['config'] = None |
| 948 | start_hyperopt_list(pargs) |
| 949 | captured = capsys.readouterr() |
| 950 | assert all(x in captured.out |
| 951 | for x in [" 1/12", " 5/12", " 10/12"]) |
| 952 | assert all(x not in captured.out |
| 953 | for x in [" 2/12", " 3/12", " 4/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 954 | " 11/12", " 12/12"]) |
| 955 | args = [ |
| 956 | "hyperopt-list", |
| 957 | "--profitable", |
| 958 | "--no-details", |
| 959 | "--no-color", |
| 960 | ] |
| 961 | pargs = get_args(args) |
| 962 | pargs['config'] = None |
| 963 | start_hyperopt_list(pargs) |
| 964 | captured = capsys.readouterr() |
| 965 | assert all(x in captured.out |
| 966 | for x in [" 2/12", " 10/12"]) |
| 967 | assert all(x not in captured.out |
| 968 | for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 969 | " 11/12", " 12/12"]) |
| 970 | args = [ |
| 971 | "hyperopt-list", |
| 972 | "--profitable", |
| 973 | "--no-color", |
| 974 | ] |
| 975 | pargs = get_args(args) |
| 976 | pargs['config'] = None |
| 977 | start_hyperopt_list(pargs) |
| 978 | captured = capsys.readouterr() |
| 979 | assert all(x in captured.out |
| 980 | for x in [" 2/12", " 10/12", "Best result:", "Buy hyperspace params", |
| 981 | "Sell hyperspace params", "ROI table", "Stoploss"]) |
| 982 | assert all(x not in captured.out |
| 983 | for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 984 | " 11/12", " 12/12"]) |
| 985 | args = [ |
| 986 | "hyperopt-list", |
| 987 | "--no-details", |
| 988 | "--no-color", |
| 989 | "--min-trades", "20", |
| 990 | ] |
| 991 | pargs = get_args(args) |
| 992 | pargs['config'] = None |
| 993 | start_hyperopt_list(pargs) |
| 994 | captured = capsys.readouterr() |
| 995 | assert all(x in captured.out |
| 996 | for x in [" 3/12", " 6/12", " 7/12", " 9/12", " 11/12"]) |
| 997 | assert all(x not in captured.out |
| 998 | for x in [" 1/12", " 2/12", " 4/12", " 5/12", " 8/12", " 10/12", " 12/12"]) |
| 999 | args = [ |
| 1000 | "hyperopt-list", |
| 1001 | "--profitable", |
| 1002 | "--no-details", |
| 1003 | "--no-color", |
| 1004 | "--max-trades", "20", |
| 1005 | ] |
| 1006 | pargs = get_args(args) |
| 1007 | pargs['config'] = None |
| 1008 | start_hyperopt_list(pargs) |
| 1009 | captured = capsys.readouterr() |
| 1010 | assert all(x in captured.out |
| 1011 | for x in [" 2/12", " 10/12"]) |
| 1012 | assert all(x not in captured.out |
| 1013 | for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 1014 | " 11/12", " 12/12"]) |
| 1015 | args = [ |
| 1016 | "hyperopt-list", |
| 1017 | "--profitable", |
| 1018 | "--no-details", |
| 1019 | "--no-color", |
| 1020 | "--min-avg-profit", "0.11", |
| 1021 | ] |
| 1022 | pargs = get_args(args) |
| 1023 | pargs['config'] = None |
| 1024 | start_hyperopt_list(pargs) |
| 1025 | captured = capsys.readouterr() |
| 1026 | assert all(x in captured.out |
| 1027 | for x in [" 2/12"]) |
| 1028 | assert all(x not in captured.out |
| 1029 | for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 1030 | " 10/12", " 11/12", " 12/12"]) |
| 1031 | args = [ |
| 1032 | "hyperopt-list", |
| 1033 | "--no-details", |
| 1034 | "--no-color", |
| 1035 | "--max-avg-profit", "0.10", |
| 1036 | ] |
| 1037 | pargs = get_args(args) |
| 1038 | pargs['config'] = None |
| 1039 | start_hyperopt_list(pargs) |
| 1040 | captured = capsys.readouterr() |
| 1041 | assert all(x in captured.out |
| 1042 | for x in [" 1/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12", " 9/12", |
| 1043 | " 11/12"]) |
| 1044 | assert all(x not in captured.out |
| 1045 | for x in [" 2/12", " 4/12", " 10/12", " 12/12"]) |
| 1046 | args = [ |
| 1047 | "hyperopt-list", |
| 1048 | "--no-details", |
| 1049 | "--no-color", |
| 1050 | "--min-total-profit", "0.4", |
| 1051 | ] |
| 1052 | pargs = get_args(args) |
| 1053 | pargs['config'] = None |
| 1054 | start_hyperopt_list(pargs) |
| 1055 | captured = capsys.readouterr() |
| 1056 | assert all(x in captured.out |
| 1057 | for x in [" 10/12"]) |
| 1058 | assert all(x not in captured.out |
| 1059 | for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", |
| 1060 | " 9/12", " 11/12", " 12/12"]) |
| 1061 | args = [ |
| 1062 | "hyperopt-list", |
| 1063 | "--no-details", |
| 1064 | "--no-color", |
| 1065 | "--max-total-profit", "0.4", |
| 1066 | ] |
| 1067 | pargs = get_args(args) |
| 1068 | pargs['config'] = None |
| 1069 | start_hyperopt_list(pargs) |
| 1070 | captured = capsys.readouterr() |
| 1071 | assert all(x in captured.out |
| 1072 | for x in [" 1/12", " 2/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12", |
| 1073 | " 9/12", " 11/12"]) |
| 1074 | assert all(x not in captured.out |
| 1075 | for x in [" 4/12", " 10/12", " 12/12"]) |
| 1076 | args = [ |
| 1077 | "hyperopt-list", |
| 1078 | "--no-details", |
| 1079 | "--no-color", |
| 1080 | "--min-objective", "0.1", |
| 1081 | ] |
| 1082 | pargs = get_args(args) |
| 1083 | pargs['config'] = None |
| 1084 | start_hyperopt_list(pargs) |
| 1085 | captured = capsys.readouterr() |
| 1086 | assert all(x in captured.out |
| 1087 | for x in [" 10/12"]) |
| 1088 | assert all(x not in captured.out |
| 1089 | for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", " 8/12", |
| 1090 | " 9/12", " 11/12", " 12/12"]) |
| 1091 | args = [ |
| 1092 | "hyperopt-list", |
| 1093 | "--no-details", |
| 1094 | "--max-objective", "0.1", |
| 1095 | ] |
| 1096 | pargs = get_args(args) |
| 1097 | pargs['config'] = None |
| 1098 | start_hyperopt_list(pargs) |
| 1099 | captured = capsys.readouterr() |
| 1100 | assert all(x in captured.out |
| 1101 | for x in [" 1/12", " 2/12", " 3/12", " 5/12", " 6/12", " 7/12", " 8/12", |
| 1102 | " 9/12", " 11/12"]) |
| 1103 | assert all(x not in captured.out |
| 1104 | for x in [" 4/12", " 10/12", " 12/12"]) |
| 1105 | args = [ |
| 1106 | "hyperopt-list", |
| 1107 | "--profitable", |
| 1108 | "--no-details", |
| 1109 | "--no-color", |
| 1110 | "--min-avg-time", "2000", |
| 1111 | ] |
| 1112 | pargs = get_args(args) |
| 1113 | pargs['config'] = None |
| 1114 | start_hyperopt_list(pargs) |
| 1115 | captured = capsys.readouterr() |
| 1116 | assert all(x in captured.out |
| 1117 | for x in [" 10/12"]) |
| 1118 | assert all(x not in captured.out |
| 1119 | for x in [" 1/12", " 2/12", " 3/12", " 4/12", " 5/12", " 6/12", " 7/12", |
| 1120 | " 8/12", " 9/12", " 11/12", " 12/12"]) |
| 1121 | args = [ |
| 1122 | "hyperopt-list", |
| 1123 | "--no-details", |
| 1124 | "--no-color", |
| 1125 | "--max-avg-time", "1500", |
| 1126 | ] |
| 1127 | pargs = get_args(args) |
| 1128 | pargs['config'] = None |
| 1129 | start_hyperopt_list(pargs) |
| 1130 | captured = capsys.readouterr() |
| 1131 | assert all(x in captured.out |
| 1132 | for x in [" 2/12", " 6/12"]) |
| 1133 | assert all(x not in captured.out |
| 1134 | for x in [" 1/12", " 3/12", " 4/12", " 5/12", " 7/12", " 8/12" |
| 1135 | " 9/12", " 10/12", " 11/12", " 12/12"]) |
| 1136 | args = [ |
| 1137 | "hyperopt-list", |
| 1138 | "--no-details", |
| 1139 | "--no-color", |
| 1140 | "--export-csv", "test_file.csv", |
| 1141 | ] |
| 1142 | pargs = get_args(args) |
| 1143 | pargs['config'] = None |
| 1144 | start_hyperopt_list(pargs) |
| 1145 | captured = capsys.readouterr() |
| 1146 | log_has("CSV file created: test_file.csv", caplog) |
| 1147 | f = Path("test_file.csv") |
| 1148 | assert 'Best,1,2,-1.25%,-1.2222,-0.00125625,,-2.51,"3,930.0 m",0.43662' in f.read_text() |
| 1149 | assert f.is_file() |
| 1150 | f.unlink() |
| 1151 | |
| 1152 | |
| 1153 | def test_hyperopt_show(mocker, capsys, hyperopt_results): |
| 1154 | mocker.patch( |
| 1155 | 'freqtrade.optimize.hyperopt_tools.HyperoptTools.load_previous_results', |
| 1156 | MagicMock(return_value=hyperopt_results) |
| 1157 | ) |
| 1158 | |
| 1159 | args = [ |
| 1160 | "hyperopt-show", |
| 1161 | ] |
| 1162 | pargs = get_args(args) |
| 1163 | pargs['config'] = None |
| 1164 | start_hyperopt_show(pargs) |
| 1165 | captured = capsys.readouterr() |
| 1166 | assert " 12/12" in captured.out |
| 1167 | |
| 1168 | args = [ |
| 1169 | "hyperopt-show", |
| 1170 | "--best" |
| 1171 | ] |
| 1172 | pargs = get_args(args) |
| 1173 | pargs['config'] = None |
| 1174 | start_hyperopt_show(pargs) |
| 1175 | captured = capsys.readouterr() |
| 1176 | assert " 10/12" in captured.out |
| 1177 | |
| 1178 | args = [ |
| 1179 | "hyperopt-show", |
| 1180 | "-n", "1" |
| 1181 | ] |
| 1182 | pargs = get_args(args) |
| 1183 | pargs['config'] = None |
| 1184 | start_hyperopt_show(pargs) |
| 1185 | captured = capsys.readouterr() |
| 1186 | assert " 1/12" in captured.out |
| 1187 | |
| 1188 | args = [ |
| 1189 | "hyperopt-show", |
| 1190 | "--best", |
| 1191 | "-n", "2" |
| 1192 | ] |
| 1193 | pargs = get_args(args) |
| 1194 | pargs['config'] = None |
| 1195 | start_hyperopt_show(pargs) |
| 1196 | captured = capsys.readouterr() |
| 1197 | assert " 5/12" in captured.out |
| 1198 | |
| 1199 | args = [ |
| 1200 | "hyperopt-show", |
| 1201 | "--best", |
| 1202 | "-n", "-1" |
| 1203 | ] |
| 1204 | pargs = get_args(args) |
| 1205 | pargs['config'] = None |
| 1206 | start_hyperopt_show(pargs) |
| 1207 | captured = capsys.readouterr() |
| 1208 | assert " 10/12" in captured.out |
| 1209 | |
| 1210 | args = [ |
| 1211 | "hyperopt-show", |
| 1212 | "--best", |
| 1213 | "-n", "-4" |
| 1214 | ] |
| 1215 | pargs = get_args(args) |
| 1216 | pargs['config'] = None |
| 1217 | with pytest.raises(OperationalException, |
| 1218 | match="The index of the epoch to show should be greater than -4."): |
| 1219 | start_hyperopt_show(pargs) |
| 1220 | |
| 1221 | args = [ |
| 1222 | "hyperopt-show", |
| 1223 | "--best", |
| 1224 | "-n", "4" |
| 1225 | ] |
| 1226 | pargs = get_args(args) |
| 1227 | pargs['config'] = None |
| 1228 | with pytest.raises(OperationalException, |
| 1229 | match="The index of the epoch to show should be less than 4."): |
| 1230 | start_hyperopt_show(pargs) |
| 1231 | |
| 1232 | |
| 1233 | def test_convert_data(mocker, testdatadir): |
| 1234 | ohlcv_mock = mocker.patch("freqtrade.commands.data_commands.convert_ohlcv_format") |
| 1235 | trades_mock = mocker.patch("freqtrade.commands.data_commands.convert_trades_format") |
| 1236 | args = [ |
| 1237 | "convert-data", |
| 1238 | "--format-from", |
| 1239 | "json", |
| 1240 | "--format-to", |
| 1241 | "jsongz", |
| 1242 | "--datadir", |
| 1243 | str(testdatadir), |
| 1244 | ] |
| 1245 | pargs = get_args(args) |
| 1246 | pargs['config'] = None |
| 1247 | start_convert_data(pargs, True) |
| 1248 | assert trades_mock.call_count == 0 |
| 1249 | assert ohlcv_mock.call_count == 1 |
| 1250 | assert ohlcv_mock.call_args[1]['convert_from'] == 'json' |
| 1251 | assert ohlcv_mock.call_args[1]['convert_to'] == 'jsongz' |
| 1252 | assert ohlcv_mock.call_args[1]['erase'] is False |
| 1253 | |
| 1254 | |
| 1255 | def test_convert_data_trades(mocker, testdatadir): |
| 1256 | ohlcv_mock = mocker.patch("freqtrade.commands.data_commands.convert_ohlcv_format") |
| 1257 | trades_mock = mocker.patch("freqtrade.commands.data_commands.convert_trades_format") |
| 1258 | args = [ |
| 1259 | "convert-trade-data", |
| 1260 | "--format-from", |
| 1261 | "jsongz", |
| 1262 | "--format-to", |
| 1263 | "json", |
| 1264 | "--datadir", |
| 1265 | str(testdatadir), |
| 1266 | ] |
| 1267 | pargs = get_args(args) |
| 1268 | pargs['config'] = None |
| 1269 | start_convert_data(pargs, False) |
| 1270 | assert ohlcv_mock.call_count == 0 |
| 1271 | assert trades_mock.call_count == 1 |
| 1272 | assert trades_mock.call_args[1]['convert_from'] == 'jsongz' |
| 1273 | assert trades_mock.call_args[1]['convert_to'] == 'json' |
| 1274 | assert trades_mock.call_args[1]['erase'] is False |
| 1275 | |
| 1276 | |
| 1277 | def test_start_list_data(testdatadir, capsys): |
| 1278 | args = [ |
| 1279 | "list-data", |
| 1280 | "--data-format-ohlcv", |
| 1281 | "json", |
| 1282 | "--datadir", |
| 1283 | str(testdatadir), |
| 1284 | ] |
| 1285 | pargs = get_args(args) |
| 1286 | pargs['config'] = None |
| 1287 | start_list_data(pargs) |
| 1288 | captured = capsys.readouterr() |
| 1289 | assert "Found 17 pair / timeframe combinations." in captured.out |
| 1290 | assert "\n| Pair | Timeframe |\n" in captured.out |
| 1291 | assert "\n| UNITTEST/BTC | 1m, 5m, 8m, 30m |\n" in captured.out |
| 1292 | |
| 1293 | args = [ |
| 1294 | "list-data", |
| 1295 | "--data-format-ohlcv", |
| 1296 | "json", |
| 1297 | "--pairs", "XRP/ETH", |
| 1298 | "--datadir", |
| 1299 | str(testdatadir), |
| 1300 | ] |
| 1301 | pargs = get_args(args) |
| 1302 | pargs['config'] = None |
| 1303 | start_list_data(pargs) |
| 1304 | captured = capsys.readouterr() |
| 1305 | assert "Found 2 pair / timeframe combinations." in captured.out |
| 1306 | assert "\n| Pair | Timeframe |\n" in captured.out |
| 1307 | assert "UNITTEST/BTC" not in captured.out |
| 1308 | assert "\n| XRP/ETH | 1m, 5m |\n" in captured.out |
| 1309 | |
| 1310 | |
| 1311 | @pytest.mark.usefixtures("init_persistence") |
| 1312 | def test_show_trades(mocker, fee, capsys, caplog): |
| 1313 | mocker.patch("freqtrade.persistence.init_db") |
| 1314 | create_mock_trades(fee) |
| 1315 | args = [ |
| 1316 | "show-trades", |
| 1317 | "--db-url", |
| 1318 | "sqlite:///" |
| 1319 | ] |
| 1320 | pargs = get_args(args) |
| 1321 | pargs['config'] = None |
| 1322 | start_show_trades(pargs) |
| 1323 | assert log_has(f"Printing {MOCK_TRADE_COUNT} Trades: ", caplog) |
| 1324 | captured = capsys.readouterr() |
| 1325 | assert "Trade(id=1" in captured.out |
| 1326 | assert "Trade(id=2" in captured.out |
| 1327 | assert "Trade(id=3" in captured.out |
| 1328 | args = [ |
| 1329 | "show-trades", |
| 1330 | "--db-url", |
| 1331 | "sqlite:///", |
| 1332 | "--print-json", |
| 1333 | "--trade-ids", "1", "2" |
| 1334 | ] |
| 1335 | pargs = get_args(args) |
| 1336 | pargs['config'] = None |
| 1337 | start_show_trades(pargs) |
| 1338 | |
| 1339 | captured = capsys.readouterr() |
| 1340 | assert log_has("Printing 2 Trades: ", caplog) |
| 1341 | assert '"trade_id": 1' in captured.out |
| 1342 | assert '"trade_id": 2' in captured.out |
| 1343 | assert '"trade_id": 3' not in captured.out |
| 1344 | args = [ |
| 1345 | "show-trades", |
| 1346 | ] |
| 1347 | pargs = get_args(args) |
| 1348 | pargs['config'] = None |
| 1349 | |
| 1350 | with pytest.raises(OperationalException, match=r"--db-url is required for this command."): |
| 1351 | start_show_trades(pargs) |