Skip to content

Commit ab9b698

Browse files
committed
fix: historic precision must also workf or big numbers
1 parent 1f69caf commit ab9b698

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

freqtrade/data/btanalysis/historic_precision.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from numpy import format_float_positional
12
from pandas import DataFrame, Series
23

34

@@ -11,7 +12,10 @@ def get_tick_size_over_time(candles: DataFrame) -> Series:
1112
# count the number of significant digits for the open and close prices
1213
for col in ["open", "high", "low", "close"]:
1314
candles[f"{col}_count"] = (
14-
candles[col].round(14).apply("{:.15f}".format).str.extract(r"\.(\d*[1-9])")[0].str.len()
15+
candles[col]
16+
.apply(format_float_positional, precision=14, unique=False, fractional=False, trim="-")
17+
.str.extract(r"\.(\d*[1-9])")[0]
18+
.str.len()
1519
)
1620
candles["max_count"] = candles[["open_count", "close_count", "high_count", "low_count"]].max(
1721
axis=1

tests/data/test_historic_precision.py

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ def test_get_tick_size_over_time_small_numbers():
172172
# January should have 5 significant digits (based on 1.23456789 being the most precise value)
173173
# which should be converted to 0.00001
174174

175-
assert result.asof("2020-01-01 00:00:00+00:00") == 0.000000000001
176175
assert result.asof("2020-01-01 00:00:00+00:00") == 0.000000000001
177176
assert result.asof("2020-02-25 00:00:00+00:00") == 0.00000000001
178177
assert result.asof("2020-03-25 00:00:00+00:00") == 0.000000001
@@ -181,3 +180,93 @@ def test_get_tick_size_over_time_small_numbers():
181180
assert result.asof("2025-04-01 00:00:00+00:00") == 0.000000001
182181

183182
assert result.iloc[0] == 0.000000000001
183+
184+
185+
def test_get_tick_size_over_time_big_numbers():
186+
"""
187+
Test the get_tick_size_over_time function with predefined data
188+
"""
189+
# Create test dataframe with different levels of precision
190+
data = {
191+
"date": [
192+
Timestamp("2020-01-01 00:00:00", tz=UTC),
193+
Timestamp("2020-01-02 00:00:00", tz=UTC),
194+
Timestamp("2020-01-03 00:00:00", tz=UTC),
195+
Timestamp("2020-01-15 00:00:00", tz=UTC),
196+
Timestamp("2020-01-16 00:00:00", tz=UTC),
197+
Timestamp("2020-01-31 00:00:00", tz=UTC),
198+
Timestamp("2020-02-01 00:00:00", tz=UTC),
199+
Timestamp("2020-02-15 00:00:00", tz=UTC),
200+
Timestamp("2020-03-15 00:00:00", tz=UTC),
201+
],
202+
"open": [
203+
12345.123456,
204+
12345.1234,
205+
12345.123,
206+
12345.12,
207+
12345.123456,
208+
12345.1234,
209+
12345.23456,
210+
12345,
211+
12345.234,
212+
],
213+
"high": [
214+
12345.123457,
215+
12345.1235,
216+
12345.124,
217+
12345.13,
218+
12345.123456,
219+
12345.1235,
220+
12345.23457,
221+
12345,
222+
12345.234,
223+
],
224+
"low": [
225+
12345.123455,
226+
12345.1233,
227+
12345.122,
228+
12345.11,
229+
12345.123456,
230+
12345.1233,
231+
12345.23455,
232+
12345,
233+
12345.234,
234+
],
235+
"close": [
236+
12345.123456,
237+
12345.1234,
238+
12345.123,
239+
12345.12,
240+
12345.123456,
241+
12345.1234,
242+
12345.23456,
243+
12345,
244+
12345.234,
245+
],
246+
"volume": [100, 200, 300, 400, 500, 600, 700, 800, 900],
247+
}
248+
249+
candles = DataFrame(data)
250+
251+
# Calculate significant digits
252+
result = get_tick_size_over_time(candles)
253+
254+
# Check that the result is a pandas Series
255+
assert isinstance(result, pd.Series)
256+
257+
# Check that we have three months of data (Jan, Feb and March 2020 )
258+
assert len(result) == 3
259+
260+
# Before
261+
assert result.asof("2019-01-01 00:00:00+00:00") is nan
262+
# January should have 5 significant digits (based on 1.23456789 being the most precise value)
263+
# which should be converted to 0.00001
264+
265+
assert result.asof("2020-01-01 00:00:00+00:00") == 0.000001
266+
assert result.asof("2020-02-25 00:00:00+00:00") == 0.00001
267+
assert result.asof("2020-03-25 00:00:00+00:00") == 0.001
268+
assert result.asof("2020-04-01 00:00:00+00:00") == 0.001
269+
# Value far past the last date should be the last value
270+
assert result.asof("2025-04-01 00:00:00+00:00") == 0.001
271+
272+
assert result.iloc[0] == 0.000001

0 commit comments

Comments
 (0)