From 8b06000f0fd6ed65d4b7c675a20c6ccf0ee156de Mon Sep 17 00:00:00 2001 From: Matthias Date: Thu, 5 Jul 2018 20:20:52 +0200 Subject: [PATCH 1/2] Use open-rates for backtesting --- freqtrade/optimize/backtesting.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/freqtrade/optimize/backtesting.py b/freqtrade/optimize/backtesting.py index 16c21258f..16c46480b 100644 --- a/freqtrade/optimize/backtesting.py +++ b/freqtrade/optimize/backtesting.py @@ -134,7 +134,7 @@ class Backtesting(object): stake_amount = args['stake_amount'] max_open_trades = args.get('max_open_trades', 0) trade = Trade( - open_rate=buy_row.close, + open_rate=buy_row.open, open_date=buy_row.date, stake_amount=stake_amount, amount=stake_amount / buy_row.open, @@ -149,35 +149,35 @@ class Backtesting(object): trade_count_lock[sell_row.date] = trade_count_lock.get(sell_row.date, 0) + 1 buy_signal = sell_row.buy - if self.analyze.should_sell(trade, sell_row.close, sell_row.date, buy_signal, + if self.analyze.should_sell(trade, sell_row.open, sell_row.date, buy_signal, sell_row.sell): return BacktestResult(pair=pair, - profit_percent=trade.calc_profit_percent(rate=sell_row.close), - profit_abs=trade.calc_profit(rate=sell_row.close), + profit_percent=trade.calc_profit_percent(rate=sell_row.open), + profit_abs=trade.calc_profit(rate=sell_row.open), open_time=buy_row.date, close_time=sell_row.date, trade_duration=(sell_row.date - buy_row.date).seconds // 60, open_index=buy_row.Index, close_index=sell_row.Index, open_at_end=False, - open_rate=buy_row.close, - close_rate=sell_row.close + open_rate=buy_row.open, + close_rate=sell_row.open ) if partial_ticker: # no sell condition found - trade stil open at end of backtest period sell_row = partial_ticker[-1] btr = BacktestResult(pair=pair, - profit_percent=trade.calc_profit_percent(rate=sell_row.close), - profit_abs=trade.calc_profit(rate=sell_row.close), + profit_percent=trade.calc_profit_percent(rate=sell_row.open), + profit_abs=trade.calc_profit(rate=sell_row.open), open_time=buy_row.date, close_time=sell_row.date, trade_duration=(sell_row.date - buy_row.date).seconds // 60, open_index=buy_row.Index, close_index=sell_row.Index, open_at_end=True, - open_rate=buy_row.close, - close_rate=sell_row.close + open_rate=buy_row.open, + close_rate=sell_row.open ) logger.debug('Force_selling still open trade %s with %s perc - %s', btr.pair, btr.profit_percent, btr.profit_abs) From 750d737b7d6d010901cec0477819148185cca28a Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 8 Jul 2018 20:18:34 +0200 Subject: [PATCH 2/2] Add tests for change to open_rate --- freqtrade/tests/optimize/test_backtesting.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/freqtrade/tests/optimize/test_backtesting.py b/freqtrade/tests/optimize/test_backtesting.py index 7b71c0da6..467c50f81 100644 --- a/freqtrade/tests/optimize/test_backtesting.py +++ b/freqtrade/tests/optimize/test_backtesting.py @@ -502,28 +502,28 @@ def test_backtest(default_conf, fee, mocker) -> None: expected = pd.DataFrame( {'pair': [pair, pair], - 'profit_percent': [0.00148826, 0.00075313], + 'profit_percent': [0.00029975, 0.00056708], 'profit_abs': [1.49e-06, 7.6e-07], 'open_time': [Arrow(2018, 1, 29, 18, 40, 0).datetime, Arrow(2018, 1, 30, 3, 30, 0).datetime], - 'close_time': [Arrow(2018, 1, 29, 23, 15, 0).datetime, + 'close_time': [Arrow(2018, 1, 29, 22, 40, 0).datetime, Arrow(2018, 1, 30, 4, 20, 0).datetime], 'open_index': [77, 183], - 'close_index': [132, 193], - 'trade_duration': [275, 50], + 'close_index': [125, 193], + 'trade_duration': [240, 50], 'open_at_end': [False, False], - 'open_rate': [0.10432, 0.103364], - 'close_rate': [0.104999, 0.10396]}) + 'open_rate': [0.104445, 0.10302485], + 'close_rate': [0.105, 0.10359999]}) pd.testing.assert_frame_equal(results, expected) data_pair = data_processed[pair] for _, t in results.iterrows(): ln = data_pair.loc[data_pair["date"] == t["open_time"]] - # Check open trade + # Check open trade rate alignes to open rate assert ln is not None - assert round(ln.iloc[0]["close"], 6) == round(t["open_rate"], 6) - # check close trade + assert round(ln.iloc[0]["open"], 6) == round(t["open_rate"], 6) + # check close trade rate alignes to close rate ln = data_pair.loc[data_pair["date"] == t["close_time"]] - assert round(ln.iloc[0]["close"], 6) == round(t["close_rate"], 6) + assert round(ln.iloc[0]["open"], 6) == round(t["close_rate"], 6) def test_backtest_1min_ticker_interval(default_conf, fee, mocker) -> None: