feat: add timeframe_to_DateOffset

This commit is contained in:
Matthias
2024-12-06 20:42:04 +01:00
parent cc7b1a5642
commit e8376d5b79
2 changed files with 39 additions and 0 deletions

View File

@@ -50,6 +50,33 @@ def _init_dataframe_with_trades_columns(dataframe: pd.DataFrame):
dataframe[column] = dataframe[column].astype(object)
def timeframe_to_DateOffset(timeframe: str) -> pd.DateOffset:
"""
Translates the timeframe interval value written in the human readable
form ('1m', '5m', '1h', '1d', '1w', etc.) to the number
of seconds for one timeframe interval.
"""
from freqtrade.exchange import (
timeframe_to_seconds,
)
timeframe_seconds = timeframe_to_seconds(timeframe)
timeframe_minutes = timeframe_seconds // 60
if timeframe_minutes < 1:
return pd.DateOffset(seconds=timeframe_seconds)
elif 59 < timeframe_minutes < 1440:
return pd.DateOffset(hours=timeframe_minutes // 60)
elif 1440 <= timeframe_minutes < 10080:
return pd.DateOffset(days=timeframe_minutes // 1440)
elif 10000 < timeframe_minutes < 43200:
return pd.DateOffset(weeks=1)
elif timeframe_minutes >= 43200 and timeframe_minutes < 525600:
return pd.DateOffset(months=1)
elif timeframe == "1y":
return pd.DateOffset(years=1)
else:
return pd.DateOffset(minutes=timeframe_minutes)
def _calculate_ohlcv_candle_start_and_end(df: pd.DataFrame, timeframe: str):
from freqtrade.exchange import timeframe_to_next_date, timeframe_to_resample_freq

View File

@@ -6,6 +6,7 @@ from freqtrade.constants import DEFAULT_TRADES_COLUMNS
from freqtrade.data.converter import populate_dataframe_with_trades
from freqtrade.data.converter.orderflow import (
ORDERFLOW_ADDED_COLUMNS,
timeframe_to_DateOffset,
trades_to_volumeprofile_with_total_delta_bid_ask,
)
from freqtrade.data.converter.trade_converter import trades_list_to_df
@@ -564,3 +565,14 @@ def test_analyze_with_orderflow(
lastval_of2 = df2.at[len(df2) - 1, "orderflow"]
assert isinstance(lastval_of2, dict)
def test_timeframe_to_DateOffset():
assert timeframe_to_DateOffset("1s") == pd.DateOffset(seconds=1)
assert timeframe_to_DateOffset("1m") == pd.DateOffset(minutes=1)
assert timeframe_to_DateOffset("5m") == pd.DateOffset(minutes=5)
assert timeframe_to_DateOffset("1h") == pd.DateOffset(hours=1)
assert timeframe_to_DateOffset("1d") == pd.DateOffset(days=1)
assert timeframe_to_DateOffset("1w") == pd.DateOffset(weeks=1)
assert timeframe_to_DateOffset("1M") == pd.DateOffset(months=1)
assert timeframe_to_DateOffset("1y") == pd.DateOffset(years=1)