| 1 | # pragma pylint: disable=missing-docstring, C0103 |
| 2 | import logging |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from freqtrade.configuration.timerange import TimeRange |
| 7 | from freqtrade.data.converter import (convert_ohlcv_format, convert_trades_format, |
| 8 | ohlcv_fill_up_missing_data, ohlcv_to_dataframe, |
| 9 | trades_dict_to_list, trades_remove_duplicates, |
| 10 | trades_to_ohlcv, trim_dataframe) |
| 11 | from freqtrade.data.history import (get_timerange, load_data, load_pair_history, |
| 12 | validate_backtest_data) |
| 13 | from tests.conftest import log_has, log_has_re |
| 14 | from tests.data.test_history import _backup_file, _clean_test_file |
| 15 | |
| 16 | |
| 17 | def test_dataframe_correct_columns(result): |
| 18 | assert result.columns.tolist() == ['date', 'open', 'high', 'low', 'close', 'volume'] |
| 19 | |
| 20 | |
| 21 | def test_ohlcv_to_dataframe(ohlcv_history_list, caplog): |
| 22 | columns = ['date', 'open', 'high', 'low', 'close', 'volume'] |
| 23 | |
| 24 | caplog.set_level(logging.DEBUG) |
| 25 | # Test file with BV data |
| 26 | dataframe = ohlcv_to_dataframe(ohlcv_history_list, '5m', pair="UNITTEST/BTC", |
| 27 | fill_missing=True) |
| 28 | assert dataframe.columns.tolist() == columns |
| 29 | assert log_has('Converting candle (OHLCV) data to dataframe for pair UNITTEST/BTC.', caplog) |
| 30 | |
| 31 | |
| 32 | def test_trades_to_ohlcv(ohlcv_history_list, caplog): |
| 33 | |
| 34 | caplog.set_level(logging.DEBUG) |
| 35 | with pytest.raises(ValueError, match="Trade-list empty."): |
| 36 | trades_to_ohlcv([], '1m') |
| 37 | |
| 38 | trades = [ |
| 39 | [1570752011620, "13519807", None, "sell", 0.00141342, 23.0, 0.03250866], |
| 40 | [1570752011620, "13519808", None, "sell", 0.00141266, 54.0, 0.07628364], |
| 41 | [1570752017964, "13519809", None, "sell", 0.00141266, 8.0, 0.01130128]] |
| 42 | |
| 43 | df = trades_to_ohlcv(trades, '1m') |
| 44 | assert not df.empty |
| 45 | assert len(df) == 1 |
| 46 | assert 'open' in df.columns |
| 47 | assert 'high' in df.columns |
| 48 | assert 'low' in df.columns |
| 49 | assert 'close' in df.columns |
| 50 | assert df.loc[:, 'high'][0] == 0.00141342 |
| 51 | assert df.loc[:, 'low'][0] == 0.00141266 |
| 52 | |
| 53 | |
| 54 | def test_ohlcv_fill_up_missing_data(testdatadir, caplog): |
| 55 | data = load_pair_history(datadir=testdatadir, |
| 56 | timeframe='1m', |
| 57 | pair='UNITTEST/BTC', |
| 58 | fill_up_missing=False) |
| 59 | caplog.set_level(logging.DEBUG) |
| 60 | data2 = ohlcv_fill_up_missing_data(data, '1m', 'UNITTEST/BTC') |
| 61 | assert len(data2) > len(data) |
| 62 | # Column names should not change |
| 63 | assert (data.columns == data2.columns).all() |
| 64 | |
| 65 | assert log_has_re(f"Missing data fillup for UNITTEST/BTC: before: " |
| 66 | f"{len(data)} - after: {len(data2)}.*", caplog) |
| 67 | |
| 68 | # Test fillup actually fixes invalid backtest data |
| 69 | min_date, max_date = get_timerange({'UNITTEST/BTC': data}) |
| 70 | assert validate_backtest_data(data, 'UNITTEST/BTC', min_date, max_date, 1) |
| 71 | assert not validate_backtest_data(data2, 'UNITTEST/BTC', min_date, max_date, 1) |
| 72 | |
| 73 | |
| 74 | def test_ohlcv_fill_up_missing_data2(caplog): |
| 75 | timeframe = '5m' |
| 76 | ticks = [[ |
| 77 | 1511686200000, # 8:50:00 |
| 78 | 8.794e-05, # open |
| 79 | 8.948e-05, # high |
| 80 | 8.794e-05, # low |
| 81 | 8.88e-05, # close |
| 82 | 2255, # volume (in quote currency) |
| 83 | ], |
| 84 | [ |
| 85 | 1511686500000, # 8:55:00 |
| 86 | 8.88e-05, |
| 87 | 8.942e-05, |
| 88 | 8.88e-05, |
| 89 | 8.893e-05, |
| 90 | 9911, |
| 91 | ], |
| 92 | [ |
| 93 | 1511687100000, # 9:05:00 |
| 94 | 8.891e-05, |
| 95 | 8.893e-05, |
| 96 | 8.875e-05, |
| 97 | 8.877e-05, |
| 98 | 2251 |
| 99 | ], |
| 100 | [ |
| 101 | 1511687400000, # 9:10:00 |
| 102 | 8.877e-05, |
| 103 | 8.883e-05, |
| 104 | 8.895e-05, |
| 105 | 8.817e-05, |
| 106 | 123551 |
| 107 | ] |
| 108 | ] |
| 109 | |
| 110 | # Generate test-data without filling missing |
| 111 | data = ohlcv_to_dataframe(ticks, timeframe, pair="UNITTEST/BTC", |
| 112 | fill_missing=False) |
| 113 | assert len(data) == 3 |
| 114 | caplog.set_level(logging.DEBUG) |
| 115 | data2 = ohlcv_fill_up_missing_data(data, timeframe, "UNITTEST/BTC") |
| 116 | assert len(data2) == 4 |
| 117 | # 3rd candle has been filled |
| 118 | row = data2.loc[2, :] |
| 119 | assert row['volume'] == 0 |
| 120 | # close shoult match close of previous candle |
| 121 | assert row['close'] == data.loc[1, 'close'] |
| 122 | assert row['open'] == row['close'] |
| 123 | assert row['high'] == row['close'] |
| 124 | assert row['low'] == row['close'] |
| 125 | # Column names should not change |
| 126 | assert (data.columns == data2.columns).all() |
| 127 | |
| 128 | assert log_has_re(f"Missing data fillup for UNITTEST/BTC: before: " |
| 129 | f"{len(data)} - after: {len(data2)}.*", caplog) |
| 130 | |
| 131 | |
| 132 | def test_ohlcv_drop_incomplete(caplog): |
| 133 | timeframe = '1d' |
| 134 | ticks = [[ |
| 135 | 1559750400000, # 2019-06-04 |
| 136 | 8.794e-05, # open |
| 137 | 8.948e-05, # high |
| 138 | 8.794e-05, # low |
| 139 | 8.88e-05, # close |
| 140 | 2255, # volume (in quote currency) |
| 141 | ], |
| 142 | [ |
| 143 | 1559836800000, # 2019-06-05 |
| 144 | 8.88e-05, |
| 145 | 8.942e-05, |
| 146 | 8.88e-05, |
| 147 | 8.893e-05, |
| 148 | 9911, |
| 149 | ], |
| 150 | [ |
| 151 | 1559923200000, # 2019-06-06 |
| 152 | 8.891e-05, |
| 153 | 8.893e-05, |
| 154 | 8.875e-05, |
| 155 | 8.877e-05, |
| 156 | 2251 |
| 157 | ], |
| 158 | [ |
| 159 | 1560009600000, # 2019-06-07 |
| 160 | 8.877e-05, |
| 161 | 8.883e-05, |
| 162 | 8.895e-05, |
| 163 | 8.817e-05, |
| 164 | 123551 |
| 165 | ] |
| 166 | ] |
| 167 | caplog.set_level(logging.DEBUG) |
| 168 | data = ohlcv_to_dataframe(ticks, timeframe, pair="UNITTEST/BTC", |
| 169 | fill_missing=False, drop_incomplete=False) |
| 170 | assert len(data) == 4 |
| 171 | assert not log_has("Dropping last candle", caplog) |
| 172 | |
| 173 | # Drop last candle |
| 174 | data = ohlcv_to_dataframe(ticks, timeframe, pair="UNITTEST/BTC", |
| 175 | fill_missing=False, drop_incomplete=True) |
| 176 | assert len(data) == 3 |
| 177 | |
| 178 | assert log_has("Dropping last candle", caplog) |
| 179 | |
| 180 | |
| 181 | def test_trim_dataframe(testdatadir) -> None: |
| 182 | data = load_data( |
| 183 | datadir=testdatadir, |
| 184 | timeframe='1m', |
| 185 | pairs=['UNITTEST/BTC'] |
| 186 | )['UNITTEST/BTC'] |
| 187 | min_date = int(data.iloc[0]['date'].timestamp()) |
| 188 | max_date = int(data.iloc[-1]['date'].timestamp()) |
| 189 | data_modify = data.copy() |
| 190 | |
| 191 | # Remove first 30 minutes (1800 s) |
| 192 | tr = TimeRange('date', None, min_date + 1800, 0) |
| 193 | data_modify = trim_dataframe(data_modify, tr) |
| 194 | assert not data_modify.equals(data) |
| 195 | assert len(data_modify) < len(data) |
| 196 | assert len(data_modify) == len(data) - 30 |
| 197 | assert all(data_modify.iloc[-1] == data.iloc[-1]) |
| 198 | assert all(data_modify.iloc[0] == data.iloc[30]) |
| 199 | |
| 200 | data_modify = data.copy() |
| 201 | tr = TimeRange('date', None, min_date + 1800, 0) |
| 202 | # Remove first 20 candles - ignores min date |
| 203 | data_modify = trim_dataframe(data_modify, tr, startup_candles=20) |
| 204 | assert not data_modify.equals(data) |
| 205 | assert len(data_modify) < len(data) |
| 206 | assert len(data_modify) == len(data) - 20 |
| 207 | assert all(data_modify.iloc[-1] == data.iloc[-1]) |
| 208 | assert all(data_modify.iloc[0] == data.iloc[20]) |
| 209 | |
| 210 | data_modify = data.copy() |
| 211 | # Remove last 30 minutes (1800 s) |
| 212 | tr = TimeRange(None, 'date', 0, max_date - 1800) |
| 213 | data_modify = trim_dataframe(data_modify, tr) |
| 214 | assert not data_modify.equals(data) |
| 215 | assert len(data_modify) < len(data) |
| 216 | assert len(data_modify) == len(data) - 30 |
| 217 | assert all(data_modify.iloc[0] == data.iloc[0]) |
| 218 | assert all(data_modify.iloc[-1] == data.iloc[-31]) |
| 219 | |
| 220 | data_modify = data.copy() |
| 221 | # Remove first 25 and last 30 minutes (1800 s) |
| 222 | tr = TimeRange('date', 'date', min_date + 1500, max_date - 1800) |
| 223 | data_modify = trim_dataframe(data_modify, tr) |
| 224 | assert not data_modify.equals(data) |
| 225 | assert len(data_modify) < len(data) |
| 226 | assert len(data_modify) == len(data) - 55 |
| 227 | # first row matches 25th original row |
| 228 | assert all(data_modify.iloc[0] == data.iloc[25]) |
| 229 | |
| 230 | |
| 231 | def test_trades_remove_duplicates(trades_history): |
| 232 | trades_history1 = trades_history * 3 |
| 233 | assert len(trades_history1) == len(trades_history) * 3 |
| 234 | res = trades_remove_duplicates(trades_history1) |
| 235 | assert len(res) == len(trades_history) |
| 236 | for i, t in enumerate(res): |
| 237 | assert t == trades_history[i] |
| 238 | |
| 239 | |
| 240 | def test_trades_dict_to_list(fetch_trades_result): |
| 241 | res = trades_dict_to_list(fetch_trades_result) |
| 242 | assert isinstance(res, list) |
| 243 | assert isinstance(res[0], list) |
| 244 | for i, t in enumerate(res): |
| 245 | assert t[0] == fetch_trades_result[i]['timestamp'] |
| 246 | assert t[1] == fetch_trades_result[i]['id'] |
| 247 | assert t[2] == fetch_trades_result[i]['type'] |
| 248 | assert t[3] == fetch_trades_result[i]['side'] |
| 249 | assert t[4] == fetch_trades_result[i]['price'] |
| 250 | assert t[5] == fetch_trades_result[i]['amount'] |
| 251 | assert t[6] == fetch_trades_result[i]['cost'] |
| 252 | |
| 253 | |
| 254 | def test_convert_trades_format(mocker, default_conf, testdatadir): |
| 255 | files = [{'old': testdatadir / "XRP_ETH-trades.json.gz", |
| 256 | 'new': testdatadir / "XRP_ETH-trades.json"}, |
| 257 | {'old': testdatadir / "XRP_OLD-trades.json.gz", |
| 258 | 'new': testdatadir / "XRP_OLD-trades.json"}, |
| 259 | ] |
| 260 | for file in files: |
| 261 | _backup_file(file['old'], copy_file=True) |
| 262 | assert not file['new'].exists() |
| 263 | |
| 264 | default_conf['datadir'] = testdatadir |
| 265 | |
| 266 | convert_trades_format(default_conf, convert_from='jsongz', |
| 267 | convert_to='json', erase=False) |
| 268 | |
| 269 | for file in files: |
| 270 | assert file['new'].exists() |
| 271 | assert file['old'].exists() |
| 272 | |
| 273 | # Remove original file |
| 274 | file['old'].unlink() |
| 275 | # Convert back |
| 276 | convert_trades_format(default_conf, convert_from='json', |
| 277 | convert_to='jsongz', erase=True) |
| 278 | for file in files: |
| 279 | assert file['old'].exists() |
| 280 | assert not file['new'].exists() |
| 281 | |
| 282 | _clean_test_file(file['old']) |
| 283 | if file['new'].exists(): |
| 284 | file['new'].unlink() |
| 285 | |
| 286 | |
| 287 | def test_convert_ohlcv_format(mocker, default_conf, testdatadir): |
| 288 | file1 = testdatadir / "XRP_ETH-5m.json" |
| 289 | file1_new = testdatadir / "XRP_ETH-5m.json.gz" |
| 290 | file2 = testdatadir / "XRP_ETH-1m.json" |
| 291 | file2_new = testdatadir / "XRP_ETH-1m.json.gz" |
| 292 | _backup_file(file1, copy_file=True) |
| 293 | _backup_file(file2, copy_file=True) |
| 294 | default_conf['datadir'] = testdatadir |
| 295 | default_conf['pairs'] = ['XRP_ETH'] |
| 296 | default_conf['timeframes'] = ['1m', '5m'] |
| 297 | |
| 298 | assert not file1_new.exists() |
| 299 | assert not file2_new.exists() |
| 300 | |
| 301 | convert_ohlcv_format(default_conf, convert_from='json', |
| 302 | convert_to='jsongz', erase=False) |
| 303 | |
| 304 | assert file1_new.exists() |
| 305 | assert file2_new.exists() |
| 306 | assert file1.exists() |
| 307 | assert file2.exists() |
| 308 | |
| 309 | # Remove original files |
| 310 | file1.unlink() |
| 311 | file2.unlink() |
| 312 | # Convert back |
| 313 | convert_ohlcv_format(default_conf, convert_from='jsongz', |
| 314 | convert_to='json', erase=True) |
| 315 | |
| 316 | assert file1.exists() |
| 317 | assert file2.exists() |
| 318 | assert not file1_new.exists() |
| 319 | assert not file2_new.exists() |
| 320 | |
| 321 | _clean_test_file(file1) |
| 322 | _clean_test_file(file2) |
| 323 | if file1_new.exists(): |
| 324 | file1_new.unlink() |
| 325 | if file2_new.exists(): |
| 326 | file2_new.unlink() |