Skip to content

Commit 4849d54

Browse files
committed
feat: add function to count "significant digits over time".
1 parent 8b8bf6f commit 4849d54

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

freqtrade/data/btanalysis/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
trade_list_to_dataframe,
2626
update_backtest_metadata,
2727
)
28+
from .historic_precision import get_significant_digits_over_time
2829
from .trade_parallelism import (
2930
analyze_trade_parallelism,
3031
evaluate_result_multi,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from pandas import DataFrame, Series
2+
3+
4+
def get_significant_digits_over_time(candles: DataFrame) -> Series:
5+
"""
6+
Calculate the number of significant digits for candles over time.
7+
It's using the Monthly maximum of the number of significant digits for each month.
8+
:param candles: DataFrame with OHLCV data
9+
:return: Series with the average number of significant digits for each month
10+
"""
11+
# count the number of significant digits for the open and close prices
12+
for col in ["open", "high", "low", "close"]:
13+
candles[f"{col}_count"] = (
14+
candles[col].round(14).astype(str).str.extract(r"\.(\d*[1-9])")[0].str.len()
15+
)
16+
candles["max_count"] = candles[["open_count", "close_count", "high_count", "low_count"]].max(
17+
axis=1
18+
)
19+
20+
candles1 = candles.set_index("date", drop=True)
21+
# Group by month and calculate the average number of significant digits
22+
monthly_count_avg1 = candles1["max_count"].resample("ME").max()
23+
# monthly_open_count_avg
24+
# convert monthly_open_count_avg from 5.0 to 0.00001, 4.0 to 0.0001, ...
25+
monthly_open_count_avg = 1 / 10**monthly_count_avg1
26+
27+
return monthly_open_count_avg

0 commit comments

Comments
 (0)