Example #1
0
def test_edge_results(edge_conf, mocker, caplog, data) -> None:
    """
    run functional tests
    """
    freqtrade = get_patched_freqtradebot(mocker, edge_conf)
    edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)
    frame = _build_backtest_dataframe(data.data)
    caplog.set_level(logging.DEBUG)
    edge.fee = 0

    trades = edge._find_trades_for_stoploss_range(frame, 'TEST/BTC',
                                                  [data.stop_loss])
    results = edge._fill_calculable_fields(
        DataFrame(trades)) if trades else DataFrame()

    print(results)

    assert len(trades) == len(data.trades)

    if not results.empty:
        assert round(results["profit_percent"].sum(),
                     3) == round(data.profit_perc, 3)

    for c, trade in enumerate(data.trades):
        res = results.iloc[c]
        assert res.exit_type == trade.sell_reason
        assert res.open_time == _get_frame_time_from_offset(trade.open_tick)
        assert res.close_time == _get_frame_time_from_offset(trade.close_tick)
Example #2
0
def test_backtest_results(default_conf, fee, mocker, caplog, data) -> None:
    """
    run functional tests
    """
    default_conf["stoploss"] = data.stop_loss
    default_conf["minimal_roi"] = {"0": data.roi}
    default_conf["ticker_interval"] = tests_ticker_interval
    default_conf["trailing_stop"] = data.trailing_stop
    default_conf[
        "trailing_only_offset_is_reached"] = data.trailing_only_offset_is_reached
    # Only add this to configuration If it's necessary
    if data.trailing_stop_positive:
        default_conf["trailing_stop_positive"] = data.trailing_stop_positive
    default_conf[
        "trailing_stop_positive_offset"] = data.trailing_stop_positive_offset
    default_conf["experimental"] = {"use_sell_signal": data.use_sell_signal}

    mocker.patch("freqtrade.exchange.Exchange.get_fee",
                 MagicMock(return_value=0.0))
    patch_exchange(mocker)
    frame = _build_backtest_dataframe(data.data)
    backtesting = Backtesting(default_conf)
    backtesting.advise_buy = lambda a, m: frame
    backtesting.advise_sell = lambda a, m: frame
    caplog.set_level(logging.DEBUG)

    pair = "UNITTEST/BTC"
    # Dummy data as we mock the analyze functions
    data_processed = {pair: DataFrame()}
    min_date, max_date = get_timeframe({pair: frame})
    results = backtesting.backtest({
        'stake_amount': default_conf['stake_amount'],
        'processed': data_processed,
        'max_open_trades': 10,
        'start_date': min_date,
        'end_date': max_date,
    })
    print(results.T)

    assert len(results) == len(data.trades)
    assert round(results["profit_percent"].sum(),
                 3) == round(data.profit_perc, 3)

    for c, trade in enumerate(data.trades):
        res = results.iloc[c]
        assert res.sell_reason == trade.sell_reason
        assert res.open_time == _get_frame_time_from_offset(trade.open_tick)
        assert res.close_time == _get_frame_time_from_offset(trade.close_tick)