|
| 1 | +# pragma pylint: disable=missing-docstring, C0103 |
| 2 | + |
| 3 | +from datetime import timezone |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +from numpy import nan |
| 7 | +from pandas import DataFrame, Timestamp |
| 8 | + |
| 9 | +from freqtrade.data.btanalysis.historic_precision import get_significant_digits_over_time |
| 10 | + |
| 11 | + |
| 12 | +def test_get_significant_digits_over_time(): |
| 13 | + """ |
| 14 | + Test the get_significant_digits_over_time function with predefined data |
| 15 | + """ |
| 16 | + # Create test dataframe with different levels of precision |
| 17 | + data = { |
| 18 | + "date": [ |
| 19 | + Timestamp("2020-01-01 00:00:00", tz=timezone.utc), |
| 20 | + Timestamp("2020-01-02 00:00:00", tz=timezone.utc), |
| 21 | + Timestamp("2020-01-03 00:00:00", tz=timezone.utc), |
| 22 | + Timestamp("2020-01-15 00:00:00", tz=timezone.utc), |
| 23 | + Timestamp("2020-01-16 00:00:00", tz=timezone.utc), |
| 24 | + Timestamp("2020-01-31 00:00:00", tz=timezone.utc), |
| 25 | + Timestamp("2020-02-01 00:00:00", tz=timezone.utc), |
| 26 | + Timestamp("2020-02-15 00:00:00", tz=timezone.utc), |
| 27 | + Timestamp("2020-03-15 00:00:00", tz=timezone.utc), |
| 28 | + ], |
| 29 | + "open": [1.23456, 1.234, 1.23, 1.2, 1.23456, 1.234, 2.3456, 2.34, 2.34], |
| 30 | + "high": [1.23457, 1.235, 1.24, 1.3, 1.23456, 1.235, 2.3457, 2.34, 2.34], |
| 31 | + "low": [1.23455, 1.233, 1.22, 1.1, 1.23456, 1.233, 2.3455, 2.34, 2.34], |
| 32 | + "close": [1.23456, 1.234, 1.23, 1.2, 1.23456, 1.234, 2.3456, 2.34, 2.34], |
| 33 | + "volume": [100, 200, 300, 400, 500, 600, 700, 800, 900], |
| 34 | + } |
| 35 | + |
| 36 | + candles = DataFrame(data) |
| 37 | + |
| 38 | + # Calculate significant digits |
| 39 | + result = get_significant_digits_over_time(candles) |
| 40 | + |
| 41 | + # Check that the result is a pandas Series |
| 42 | + assert isinstance(result, pd.Series) |
| 43 | + |
| 44 | + # Check that we have three months of data (Jan, Feb and March 2020 ) |
| 45 | + assert len(result) == 3 |
| 46 | + |
| 47 | + # Before |
| 48 | + assert result.asof("2019-01-01 00:00:00+00:00") is nan |
| 49 | + # January should have 5 significant digits (based on 1.23456789 being the most precise value) |
| 50 | + # which should be converted to 0.00001 |
| 51 | + |
| 52 | + assert result.asof("2020-01-01 00:00:00+00:00") == 0.00001 |
| 53 | + assert result.asof("2020-01-01 00:00:00+00:00") == 0.00001 |
| 54 | + assert result.asof("2020-02-25 00:00:00+00:00") == 0.0001 |
| 55 | + assert result.asof("2020-03-25 00:00:00+00:00") == 0.01 |
| 56 | + assert result.asof("2020-04-01 00:00:00+00:00") == 0.01 |
| 57 | + # Value far past the last date should be the last value |
| 58 | + assert result.asof("2025-04-01 00:00:00+00:00") == 0.01 |
| 59 | + |
| 60 | + assert result.iloc[0] == 0.00001 |
| 61 | + |
| 62 | + |
| 63 | +def test_get_significant_digits_over_time_real_data(testdatadir): |
| 64 | + """ |
| 65 | + Test the get_significant_digits_over_time function with real data from the testdatadir |
| 66 | + """ |
| 67 | + from freqtrade.data.history import load_pair_history |
| 68 | + |
| 69 | + # Load some test data from the testdata directory |
| 70 | + pair = "UNITTEST/BTC" |
| 71 | + timeframe = "1m" |
| 72 | + |
| 73 | + candles = load_pair_history( |
| 74 | + datadir=testdatadir, |
| 75 | + pair=pair, |
| 76 | + timeframe=timeframe, |
| 77 | + ) |
| 78 | + |
| 79 | + # Make sure we have test data |
| 80 | + assert not candles.empty, "No test data found, cannot run test" |
| 81 | + |
| 82 | + # Calculate significant digits |
| 83 | + result = get_significant_digits_over_time(candles) |
| 84 | + |
| 85 | + assert isinstance(result, pd.Series) |
| 86 | + |
| 87 | + # Verify that all values are between 0 and 1 (valid precision values) |
| 88 | + assert all(result > 0) |
| 89 | + assert all(result < 1) |
| 90 | + |
| 91 | + assert all(result <= 0.0001) |
| 92 | + assert all(result >= 0.00000001) |
0 commit comments