Exemple #1
0
def test_start(mocker, fee, default_conf, caplog) -> None:
    start_mock = MagicMock()
    mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
    patch_exchange(mocker)
    mocker.patch('freqtrade.optimize.backtesting.Backtesting.start',
                 start_mock)
    patched_configuration_load_config_file(mocker, default_conf)

    args = [
        '--config', 'config.json', '--strategy', 'DefaultStrategy',
        'backtesting'
    ]
    args = get_args(args)
    start_backtesting(args)
    assert log_has('Starting freqtrade in Backtesting mode', caplog)
    assert start_mock.call_count == 1
def test_start(mocker, fee, default_conf, caplog) -> None:
    start_mock = MagicMock()
    mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
    patch_exchange(mocker)
    mocker.patch('freqtrade.optimize.backtesting.Backtesting.start',
                 start_mock)
    mocker.patch('freqtrade.configuration.open',
                 mocker.mock_open(read_data=json.dumps(default_conf)))
    args = [
        '--config', 'config.json', '--strategy', 'DefaultStrategy',
        'backtesting'
    ]
    args = get_args(args)
    start_backtesting(args)
    assert log_has('Starting freqtrade in Backtesting mode',
                   caplog.record_tuples)
    assert start_mock.call_count == 1
def test_backtest_start_live(default_conf, mocker, caplog):
    default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']

    async def load_pairs(pair, timeframe, since):
        return _load_pair_as_ticks(pair, timeframe)

    api_mock = MagicMock()
    api_mock.fetch_ohlcv = load_pairs

    patch_exchange(mocker, api_mock)
    mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest',
                 MagicMock())
    mocker.patch(
        'freqtrade.optimize.backtesting.Backtesting._generate_text_table',
        MagicMock())
    mocker.patch('freqtrade.configuration.open',
                 mocker.mock_open(read_data=json.dumps(default_conf)))

    args = [
        '--config', 'config.json', '--strategy', 'DefaultStrategy',
        '--datadir', 'freqtrade/tests/testdata', 'backtesting',
        '--ticker-interval', '1m', '--live', '--timerange', '-100',
        '--enable-position-stacking', '--disable-max-market-positions'
    ]
    args = get_args(args)
    start_backtesting(args)
    # check the logs, that will contain the backtest result
    exists = [
        'Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...',
        'Parameter -l/--live detected ...',
        'Ignoring max_open_trades (--disable-max-market-positions was used) ...',
        'Parameter --timerange detected: -100 ...',
        'Using data folder: freqtrade/tests/testdata ...',
        'Using stake_currency: BTC ...', 'Using stake_amount: 0.001 ...',
        'Live: Downloading data for all defined pairs ...',
        'Backtesting with data from 2017-11-14T19:31:00+00:00 '
        'up to 2017-11-14T22:58:00+00:00 (0 days)..',
        'Parameter --enable-position-stacking detected ...'
    ]

    for line in exists:
        assert log_has(line, caplog.record_tuples)
Exemple #4
0
def test_backtest_start_timerange(default_conf, mocker, caplog, testdatadir):
    default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']

    async def load_pairs(pair, timeframe, since):
        return _load_pair_as_ticks(pair, timeframe)

    api_mock = MagicMock()
    api_mock.fetch_ohlcv = load_pairs

    patch_exchange(mocker, api_mock)
    mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest',
                 MagicMock())
    mocker.patch(
        'freqtrade.optimize.backtesting.Backtesting._generate_text_table',
        MagicMock())
    patched_configuration_load_config_file(mocker, default_conf)

    args = [
        'backtesting', '--config', 'config.json', '--strategy',
        'DefaultStrategy', '--datadir',
        str(testdatadir), '--ticker-interval', '1m', '--timerange',
        '1510694220-1510700340', '--enable-position-stacking',
        '--disable-max-market-positions'
    ]
    args = get_args(args)
    start_backtesting(args)
    # check the logs, that will contain the backtest result
    exists = [
        'Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...',
        'Ignoring max_open_trades (--disable-max-market-positions was used) ...',
        'Parameter --timerange detected: 1510694220-1510700340 ...',
        f'Using data directory: {testdatadir} ...',
        'Using stake_currency: BTC ...', 'Using stake_amount: 0.001 ...',
        'Loading data from 2017-11-14T20:57:00+00:00 '
        'up to 2017-11-14T22:58:00+00:00 (0 days)..',
        'Backtesting with data from 2017-11-14T21:17:00+00:00 '
        'up to 2017-11-14T22:58:00+00:00 (0 days)..',
        'Parameter --enable-position-stacking detected ...'
    ]

    for line in exists:
        assert log_has(line, caplog)
Exemple #5
0
def test_backtest_start_multi_strat(default_conf, mocker, caplog):
    default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']

    async def load_pairs(pair, timeframe, since):
        return _load_pair_as_ticks(pair, timeframe)

    api_mock = MagicMock()
    api_mock.fetch_ohlcv = load_pairs

    patch_exchange(mocker, api_mock)
    backtestmock = MagicMock()
    mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest',
                 backtestmock)
    gen_table_mock = MagicMock()
    mocker.patch(
        'freqtrade.optimize.backtesting.Backtesting._generate_text_table',
        gen_table_mock)
    gen_strattable_mock = MagicMock()
    mocker.patch(
        'freqtrade.optimize.backtesting.Backtesting._generate_text_table_strategy',
        gen_strattable_mock)
    patched_configuration_load_config_file(mocker, default_conf)

    args = [
        '--config',
        'config.json',
        '--datadir',
        'freqtrade/tests/testdata',
        'backtesting',
        '--ticker-interval',
        '1m',
        '--timerange',
        '-100',
        '--enable-position-stacking',
        '--disable-max-market-positions',
        '--strategy-list',
        'DefaultStrategy',
        'TestStrategy',
    ]
    args = get_args(args)
    start_backtesting(args)
    # 2 backtests, 4 tables
    assert backtestmock.call_count == 2
    assert gen_table_mock.call_count == 4
    assert gen_strattable_mock.call_count == 1

    # check the logs, that will contain the backtest result
    exists = [
        'Parameter -i/--ticker-interval detected ... Using ticker_interval: 1m ...',
        'Ignoring max_open_trades (--disable-max-market-positions was used) ...',
        'Parameter --timerange detected: -100 ...',
        'Using data directory: freqtrade/tests/testdata ...',
        'Using stake_currency: BTC ...',
        'Using stake_amount: 0.001 ...',
        'Backtesting with data from 2017-11-14T21:17:00+00:00 '
        'up to 2017-11-14T22:58:00+00:00 (0 days)..',
        'Parameter --enable-position-stacking detected ...',
        'Running backtesting for Strategy DefaultStrategy',
        'Running backtesting for Strategy TestStrategy',
    ]

    for line in exists:
        assert log_has(line, caplog)