| 1 | from math import isclose |
| 2 | |
| 3 | import numpy as np |
| 4 | import pandas as pd |
| 5 | import pytest |
| 6 | |
| 7 | from freqtrade.strategy import merge_informative_pair, stoploss_from_open, timeframe_to_minutes |
| 8 | |
| 9 | |
| 10 | def generate_test_data(timeframe: str, size: int): |
| 11 | np.random.seed(42) |
| 12 | tf_mins = timeframe_to_minutes(timeframe) |
| 13 | |
| 14 | base = np.random.normal(20, 2, size=size) |
| 15 | |
| 16 | date = pd.period_range('2020-07-05', periods=size, freq=f'{tf_mins}min').to_timestamp() |
| 17 | df = pd.DataFrame({ |
| 18 | 'date': date, |
| 19 | 'open': base, |
| 20 | 'high': base + np.random.normal(2, 1, size=size), |
| 21 | 'low': base - np.random.normal(2, 1, size=size), |
| 22 | 'close': base + np.random.normal(0, 1, size=size), |
| 23 | 'volume': np.random.normal(200, size=size) |
| 24 | } |
| 25 | ) |
| 26 | df = df.dropna() |
| 27 | return df |
| 28 | |
| 29 | |
| 30 | def test_merge_informative_pair(): |
| 31 | data = generate_test_data('15m', 40) |
| 32 | informative = generate_test_data('1h', 40) |
| 33 | |
| 34 | result = merge_informative_pair(data, informative, '15m', '1h', ffill=True) |
| 35 | assert isinstance(result, pd.DataFrame) |
| 36 | assert len(result) == len(data) |
| 37 | assert 'date' in result.columns |
| 38 | assert result['date'].equals(data['date']) |
| 39 | assert 'date_1h' in result.columns |
| 40 | |
| 41 | assert 'open' in result.columns |
| 42 | assert 'open_1h' in result.columns |
| 43 | assert result['open'].equals(data['open']) |
| 44 | |
| 45 | assert 'close' in result.columns |
| 46 | assert 'close_1h' in result.columns |
| 47 | assert result['close'].equals(data['close']) |
| 48 | |
| 49 | assert 'volume' in result.columns |
| 50 | assert 'volume_1h' in result.columns |
| 51 | assert result['volume'].equals(data['volume']) |
| 52 | |
| 53 | # First 3 rows are empty |
| 54 | assert result.iloc[0]['date_1h'] is pd.NaT |
| 55 | assert result.iloc[1]['date_1h'] is pd.NaT |
| 56 | assert result.iloc[2]['date_1h'] is pd.NaT |
| 57 | # Next 4 rows contain the starting date (0:00) |
| 58 | assert result.iloc[3]['date_1h'] == result.iloc[0]['date'] |
| 59 | assert result.iloc[4]['date_1h'] == result.iloc[0]['date'] |
| 60 | assert result.iloc[5]['date_1h'] == result.iloc[0]['date'] |
| 61 | assert result.iloc[6]['date_1h'] == result.iloc[0]['date'] |
| 62 | # Next 4 rows contain the next Hourly date original date row 4 |
| 63 | assert result.iloc[7]['date_1h'] == result.iloc[4]['date'] |
| 64 | assert result.iloc[8]['date_1h'] == result.iloc[4]['date'] |
| 65 | |
| 66 | |
| 67 | def test_merge_informative_pair_same(): |
| 68 | data = generate_test_data('15m', 40) |
| 69 | informative = generate_test_data('15m', 40) |
| 70 | |
| 71 | result = merge_informative_pair(data, informative, '15m', '15m', ffill=True) |
| 72 | assert isinstance(result, pd.DataFrame) |
| 73 | assert len(result) == len(data) |
| 74 | assert 'date' in result.columns |
| 75 | assert result['date'].equals(data['date']) |
| 76 | assert 'date_15m' in result.columns |
| 77 | |
| 78 | assert 'open' in result.columns |
| 79 | assert 'open_15m' in result.columns |
| 80 | assert result['open'].equals(data['open']) |
| 81 | |
| 82 | assert 'close' in result.columns |
| 83 | assert 'close_15m' in result.columns |
| 84 | assert result['close'].equals(data['close']) |
| 85 | |
| 86 | assert 'volume' in result.columns |
| 87 | assert 'volume_15m' in result.columns |
| 88 | assert result['volume'].equals(data['volume']) |
| 89 | |
| 90 | # Dates match 1:1 |
| 91 | assert result['date_15m'].equals(result['date']) |
| 92 | |
| 93 | |
| 94 | def test_merge_informative_pair_lower(): |
| 95 | data = generate_test_data('1h', 40) |
| 96 | informative = generate_test_data('15m', 40) |
| 97 | |
| 98 | with pytest.raises(ValueError, match=r"Tried to merge a faster timeframe .*"): |
| 99 | merge_informative_pair(data, informative, '1h', '15m', ffill=True) |
| 100 | |
| 101 | |
| 102 | def test_stoploss_from_open(): |
| 103 | open_price_ranges = [ |
| 104 | [0.01, 1.00, 30], |
| 105 | [1, 100, 30], |
| 106 | [100, 10000, 30], |
| 107 | ] |
| 108 | current_profit_range = [-0.99, 2, 30] |
| 109 | desired_stop_range = [-0.50, 0.50, 30] |
| 110 | |
| 111 | for open_range in open_price_ranges: |
| 112 | for open_price in np.linspace(*open_range): |
| 113 | for desired_stop in np.linspace(*desired_stop_range): |
| 114 | |
| 115 | # -1 is not a valid current_profit, should return 1 |
| 116 | assert stoploss_from_open(desired_stop, -1) == 1 |
| 117 | |
| 118 | for current_profit in np.linspace(*current_profit_range): |
| 119 | current_price = open_price * (1 + current_profit) |
| 120 | expected_stop_price = open_price * (1 + desired_stop) |
| 121 | |
| 122 | stoploss = stoploss_from_open(desired_stop, current_profit) |
| 123 | |
| 124 | assert stoploss >= 0 |
| 125 | assert stoploss <= 1 |
| 126 | |
| 127 | stop_price = current_price * (1 - stoploss) |
| 128 | |
| 129 | # there is no correct answer if the expected stop price is above |
| 130 | # the current price |
| 131 | if expected_stop_price > current_price: |
| 132 | assert stoploss == 0 |
| 133 | else: |
| 134 | assert isclose(stop_price, expected_stop_price, rel_tol=0.00001) |