From b57d9edda8b28722e382652a0ed329593a8d22ae Mon Sep 17 00:00:00 2001 From: misagh Date: Wed, 3 Oct 2018 14:23:10 +0200 Subject: [PATCH] Edge test expectancy function (round 1) --- freqtrade/tests/edge/test_edge.py | 107 +++++++++++++++++++++++++++--- 1 file changed, 96 insertions(+), 11 deletions(-) diff --git a/freqtrade/tests/edge/test_edge.py b/freqtrade/tests/edge/test_edge.py index 5714b1530..15fc38114 100644 --- a/freqtrade/tests/edge/test_edge.py +++ b/freqtrade/tests/edge/test_edge.py @@ -1,6 +1,8 @@ from freqtrade.tests.conftest import get_patched_exchange from freqtrade.edge import Edge -from pandas import DataFrame +from pandas import DataFrame, to_datetime +import arrow +import numpy as np # Cases to be tested: @@ -20,6 +22,10 @@ from pandas import DataFrame # 8) Stoploss and sell are hit. should sell on stoploss #################################################################### +ticker_start_time = arrow.get(2018, 10, 3) +ticker_interval_in_minute = 5 + + def test_filter(mocker, default_conf): exchange = get_patched_exchange(mocker, default_conf) edge = Edge(default_conf, exchange) @@ -45,11 +51,11 @@ def _validate_ohlc(buy_ohlc_sell_matrice): def _build_dataframe(buy_ohlc_sell_matrice): _validate_ohlc(buy_ohlc_sell_matrice) - tickers = [] for ohlc in buy_ohlc_sell_matrice: ticker = { - 'date': ohlc[0], + # ticker every 5 min + 'date': ticker_start_time.shift(minutes=(ohlc[0] * 5)).timestamp * 1000, 'buy': ohlc[1], 'open': ohlc[2], 'high': ohlc[3], @@ -58,7 +64,71 @@ def _build_dataframe(buy_ohlc_sell_matrice): 'sell': ohlc[6] } tickers.append(ticker) - return DataFrame(tickers) + + frame = DataFrame(tickers) + frame['date'] = to_datetime(frame['date'], + unit='ms', + utc=True, + infer_datetime_format=True) + + return frame + + +def test_process_expectancy(mocker, default_conf): + default_conf['edge']['min_trade_number'] = 2 + exchange = get_patched_exchange(mocker, default_conf) + + def get_fee(): + return 0.001 + + exchange.get_fee = get_fee + edge = Edge(default_conf, exchange) + + 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'}, # sdfsdf + + {'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 def test_three_complete_trades(mocker, default_conf): @@ -80,17 +150,32 @@ def test_three_complete_trades(mocker, default_conf): ticker_df = _build_dataframe(three_sell_points_hit) trades = edge._find_trades_for_stoploss_range(ticker_df, 'TEST/BTC', [stoploss]) - # Three trades must have happened + # Three trades must have occured assert len(trades) == 3 # First trade check - assert trades[0]['open_time'] == 1 - assert trades[0]['close_time'] == 2 + # open time should be on line 1 + assert trades[0]['open_time'] == np.datetime64(ticker_start_time.shift( + minutes=(1 * ticker_interval_in_minute)).timestamp * 1000, 'ms') + + # close time should be on line 2 + assert trades[0]['close_time'] == np.datetime64(ticker_start_time.shift( + minutes=(2 * ticker_interval_in_minute)).timestamp * 1000, 'ms') # Second trade check - assert trades[1]['open_time'] == 4 - assert trades[1]['close_time'] == 5 + # open time should be on line 4 + assert trades[1]['open_time'] == np.datetime64(ticker_start_time.shift( + minutes=(4 * ticker_interval_in_minute)).timestamp * 1000, 'ms') + + # close time should be on line 5 + assert trades[1]['close_time'] == np.datetime64(ticker_start_time.shift( + minutes=(5 * ticker_interval_in_minute)).timestamp * 1000, 'ms') # Third trade check - assert trades[2]['open_time'] == 6 - assert trades[2]['close_time'] == 7 + # open time should be on line 6 + assert trades[2]['open_time'] == np.datetime64(ticker_start_time.shift( + minutes=(6 * ticker_interval_in_minute)).timestamp * 1000, 'ms') + + # close time should be on line 7 + assert trades[2]['close_time'] == np.datetime64(ticker_start_time.shift( + minutes=(7 * ticker_interval_in_minute)).timestamp * 1000, 'ms')