feat: add function to count "significant digits over time".

This commit is contained in:
Matthias
2025-05-03 20:14:37 +02:00
parent 8b8bf6f97d
commit 4849d5413f
2 changed files with 28 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ from .bt_fileutils import (
trade_list_to_dataframe,
update_backtest_metadata,
)
from .historic_precision import get_significant_digits_over_time
from .trade_parallelism import (
analyze_trade_parallelism,
evaluate_result_multi,

View File

@@ -0,0 +1,27 @@
from pandas import DataFrame, Series
def get_significant_digits_over_time(candles: DataFrame) -> Series:
"""
Calculate the number of significant digits for candles over time.
It's using the Monthly maximum of the number of significant digits for each month.
:param candles: DataFrame with OHLCV data
:return: Series with the average number of significant digits for each month
"""
# count the number of significant digits for the open and close prices
for col in ["open", "high", "low", "close"]:
candles[f"{col}_count"] = (
candles[col].round(14).astype(str).str.extract(r"\.(\d*[1-9])")[0].str.len()
)
candles["max_count"] = candles[["open_count", "close_count", "high_count", "low_count"]].max(
axis=1
)
candles1 = candles.set_index("date", drop=True)
# Group by month and calculate the average number of significant digits
monthly_count_avg1 = candles1["max_count"].resample("ME").max()
# monthly_open_count_avg
# convert monthly_open_count_avg from 5.0 to 0.00001, 4.0 to 0.0001, ...
monthly_open_count_avg = 1 / 10**monthly_count_avg1
return monthly_open_count_avg