Example #1
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)
    mocker.patch('freqtrade.configuration.open', mocker.mock_open(
        read_data=json.dumps(default_conf)
    ))

    args = [
        '--config', 'config.json',
        '--datadir', 'freqtrade/tests/testdata',
        'backtesting',
        '--ticker-interval', '1m',
        '--live',
        '--timerange', '-100',
        '--enable-position-stacking',
        '--disable-max-market-positions',
        '--strategy-list',
        'DefaultStrategy',
        'TestStrategy',
    ]
    args = get_args(args)
    start(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 ...',
        '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 ...',
        'Downloading data for all pairs in whitelist ...',
        'Measuring 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 ...',
        'Running backtesting for Strategy DefaultStrategy',
        'Running backtesting for Strategy TestStrategy',
    ]

    for line in exists:
        assert log_has(line, caplog.record_tuples)
Example #2
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)
    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(args)
    assert log_has('Starting freqtrade in Backtesting mode',
                   caplog.record_tuples)
    assert start_mock.call_count == 1
Example #3
0
def test_backtest_start_live(default_conf, mocker, caplog):
    conf = deepcopy(default_conf)
    conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
    mocker.patch('freqtrade.exchange.Exchange.get_ticker_history',
                 new=lambda s, n, i: _load_pair_as_ticks(n, i))
    patch_exchange(mocker)
    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(conf)))

    args = MagicMock()
    args.ticker_interval = 1
    args.level = 10
    args.live = True
    args.datadir = None
    args.export = None
    args.strategy = 'DefaultStrategy'
    args.timerange = '-100'  # needed due to MagicMock malleability

    args = [
        '--config', 'config.json', '--strategy', 'DefaultStrategy',
        '--datadir', 'freqtrade/tests/testdata', 'backtesting',
        '--ticker-interval', '1m', '--live', '--timerange', '-100',
        '--realistic-simulation'
    ]
    args = get_args(args)
    start(args)
    # check the logs, that will contain the backtest result
    exists = [
        'Parameter -i/--ticker-interval detected ...',
        'Using ticker_interval: 1m ...', 'Parameter -l/--live detected ...',
        'Using max_open_trades: 1 ...',
        'Parameter --timerange detected: -100 ...',
        'Using data folder: freqtrade/tests/testdata ...',
        'Using stake_currency: BTC ...', 'Using stake_amount: 0.001 ...',
        'Downloading data for all pairs in whitelist ...',
        'Measuring data from 2017-11-14T19:31:00+00:00 up to 2017-11-14T22:58:00+00:00 (0 days)..',
        'Parameter --realistic-simulation detected ...'
    ]

    for line in exists:
        assert log_has(line, caplog.record_tuples)
Example #4
0
def test_backtest_start(default_conf, mocker, caplog):
    default_conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
    mocker.patch.dict('freqtrade.main._CONF', default_conf)
    mocker.patch('freqtrade.misc.load_config', new=lambda s: default_conf)
    mocker.patch.multiple('freqtrade.optimize', load_data=mocked_load_data)
    args = MagicMock()
    args.ticker_interval = 1
    args.level = 10
    args.live = False
    args.datadir = None
    backtesting.start(args)
    # check the logs, that will contain the backtest result
    exists = [
        'Using max_open_trades: 1 ...', 'Using stake_amount: 0.001 ...',
        'Measuring data from 2017-11-14T21:17:00+00:00 up to 2017-11-14T22:59:00+00:00 ...'
    ]
    for line in exists:
        assert ('freqtrade.optimize.backtesting', logging.INFO,
                line) in caplog.record_tuples
Example #5
0
def test_backtest_start(default_conf, mocker, caplog):
    default_conf['exchange']['pair_whitelist'] = ['BTC_UNITEST']
    mocker.patch.dict('freqtrade.main._CONF', default_conf)
    mocker.patch('freqtrade.misc.load_config', new=lambda s: default_conf)
    mocker.patch.multiple('freqtrade.optimize',
                          load_data=mocked_load_data)
    args = MagicMock()
    args.ticker_interval = 1
    args.level = 10
    args.live = False
    args.datadir = None
    backtesting.start(args)
    # check the logs, that will contain the backtest result
    exists = ['Using max_open_trades: 1 ...',
              'Using stake_amount: 0.001 ...',
              'Measuring data from 2017-11-14T21:17:00+00:00 up to 2017-11-14T22:59:00+00:00 ...']
    for line in exists:
        assert ('freqtrade.optimize.backtesting',
                logging.INFO,
                line) in caplog.record_tuples
Example #6
0
def test_backtest_start_live(default_conf, mocker, caplog):
    default_conf['exchange']['pair_whitelist'] = ['UNITTEST/BTC']
    mocker.patch('freqtrade.exchange.Exchange.get_candle_history',
                 new=lambda s, n, i: _load_pair_as_ticks(n, i))
    patch_exchange(mocker)
    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(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 ...',
        'Downloading data for all pairs in whitelist ...',
        'Measuring 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)