Beispiel #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()

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

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

    for c, trade in enumerate(data.trades):
        res = results.iloc[c]
        assert res.exit_type == trade.exit_reason
        assert res.open_date == _get_frame_time_from_offset(
            trade.open_tick).replace(tzinfo=None)
        assert res.close_date == _get_frame_time_from_offset(
            trade.close_tick).replace(tzinfo=None)
Beispiel #2
0
def test_process_expectancy_only_wins(mocker, edge_conf, fee,):
    edge_conf['edge']['min_trade_number'] = 2
    freqtrade = get_patched_freqtradebot(mocker, edge_conf)

    freqtrade.exchange.get_fee = fee
    edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)

    trades = [
        {'pair': 'TEST/BTC',
         'stoploss': -0.9,
         'profit_percent': '',
         'profit_abs': '',
         'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
         'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
         'open_index': 1,
         'close_index': 1,
         'trade_duration': '',
         'open_rate': 15,
         'close_rate': 17,
         'exit_type': 'sell_signal'},
        {'pair': 'TEST/BTC',
         'stoploss': -0.9,
         'profit_percent': '',
         'profit_abs': '',
         'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
         'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
         'open_index': 4,
         'close_index': 4,
         'trade_duration': '',
         'open_rate': 10,
         'close_rate': 20,
         'exit_type': 'sell_signal'},
        {'pair': 'TEST/BTC',
         'stoploss': -0.9,
         'profit_percent': '',
         'profit_abs': '',
         'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
         'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
         'open_index': 6,
         'close_index': 7,
         'trade_duration': '',
         'open_rate': 26,
         'close_rate': 134,
         'exit_type': 'sell_signal'}
    ]

    trades_df = DataFrame(trades)
    trades_df = edge._fill_calculable_fields(trades_df)
    final = edge._process_expectancy(trades_df)

    assert 'TEST/BTC' in final
    assert final['TEST/BTC'].stoploss == -0.9
    assert final['TEST/BTC'].nb_trades == len(trades_df)
    assert round(final['TEST/BTC'].winrate, 10) == 1.0
    assert round(final['TEST/BTC'].risk_reward_ratio, 10) == float('inf')
    assert round(final['TEST/BTC'].expectancy, 10) == float('inf')
Beispiel #3
0
def test_process_expectancy(mocker, edge_conf, fee, risk_reward_ratio,
                            expectancy):
    edge_conf['edge']['min_trade_number'] = 2
    freqtrade = get_patched_freqtradebot(mocker, edge_conf)

    def get_fee(*args, **kwargs):
        return fee

    freqtrade.exchange.get_fee = get_fee
    edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)

    trades = [{
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_date': np.datetime64('2018-10-03T00:05:00.000000000'),
        'close_date': np.datetime64('2018-10-03T00:10:00.000000000'),
        'trade_duration': '',
        'open_rate': 17,
        'close_rate': 17,
        'exit_type': 'exit_signal'
    }, {
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_date': np.datetime64('2018-10-03T00:20:00.000000000'),
        'close_date': np.datetime64('2018-10-03T00:25:00.000000000'),
        'trade_duration': '',
        'open_rate': 20,
        'close_rate': 20,
        'exit_type': 'exit_signal'
    }, {
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_date': np.datetime64('2018-10-03T00:30:00.000000000'),
        'close_date': np.datetime64('2018-10-03T00:40:00.000000000'),
        'trade_duration': '',
        'open_rate': 26,
        'close_rate': 34,
        'exit_type': 'exit_signal'
    }]

    trades_df = DataFrame(trades)
    trades_df = edge._fill_calculable_fields(trades_df)
    final = edge._process_expectancy(trades_df)
    assert len(final) == 1

    assert 'TEST/BTC' in final
    assert final['TEST/BTC'].stoploss == -0.9
    assert round(final['TEST/BTC'].winrate, 10) == 0.3333333333
    assert round(final['TEST/BTC'].risk_reward_ratio, 10) == risk_reward_ratio
    assert round(final['TEST/BTC'].required_risk_reward, 10) == 2.0
    assert round(final['TEST/BTC'].expectancy, 10) == expectancy

    # Pop last item so no trade is profitable
    trades.pop()
    trades_df = DataFrame(trades)
    trades_df = edge._fill_calculable_fields(trades_df)
    final = edge._process_expectancy(trades_df)
    assert len(final) == 0
    assert isinstance(final, dict)
Beispiel #4
0
def test_process_expectancy(mocker, edge_conf):
    edge_conf['edge']['min_trade_number'] = 2
    freqtrade = get_patched_freqtradebot(mocker, edge_conf)

    def get_fee():
        return 0.001

    freqtrade.exchange.get_fee = get_fee
    edge = Edge(edge_conf, freqtrade.exchange, freqtrade.strategy)

    trades = [{
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_time': np.datetime64('2018-10-03T00:05:00.000000000'),
        'close_time': np.datetime64('2018-10-03T00:10:00.000000000'),
        'open_index': 1,
        'close_index': 1,
        'trade_duration': '',
        'open_rate': 17,
        'close_rate': 17,
        'exit_type': 'sell_signal'
    }, {
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_time': np.datetime64('2018-10-03T00:20:00.000000000'),
        'close_time': np.datetime64('2018-10-03T00:25:00.000000000'),
        'open_index': 4,
        'close_index': 4,
        'trade_duration': '',
        'open_rate': 20,
        'close_rate': 20,
        'exit_type': 'sell_signal'
    }, {
        'pair': 'TEST/BTC',
        'stoploss': -0.9,
        'profit_percent': '',
        'profit_abs': '',
        'open_time': np.datetime64('2018-10-03T00:30:00.000000000'),
        'close_time': np.datetime64('2018-10-03T00:40:00.000000000'),
        'open_index': 6,
        'close_index': 7,
        'trade_duration': '',
        'open_rate': 26,
        'close_rate': 34,
        'exit_type': 'sell_signal'
    }]

    trades_df = DataFrame(trades)
    trades_df = edge._fill_calculable_fields(trades_df)
    final = edge._process_expectancy(trades_df)
    assert len(final) == 1

    assert 'TEST/BTC' in final
    assert final['TEST/BTC'].stoploss == -0.9
    assert round(final['TEST/BTC'].winrate, 10) == 0.3333333333
    assert round(final['TEST/BTC'].risk_reward_ratio, 10) == 306.5384615384
    assert round(final['TEST/BTC'].required_risk_reward, 10) == 2.0
    assert round(final['TEST/BTC'].expectancy, 10) == 101.5128205128