Fix weekly resamples to ensure they're on monday.

This commit is contained in:
Matthias
2024-01-23 19:21:00 +01:00
parent 34ac2dc9ae
commit 1ae3b1e622
4 changed files with 8 additions and 5 deletions

View File

@@ -129,7 +129,9 @@ def timeframe_to_resample_freq(timeframe: str) -> str:
timeframe_seconds = timeframe_to_seconds(timeframe)
timeframe_minutes = timeframe_seconds // 60
resample_interval = f'{timeframe_seconds}s'
if timeframe_minutes >= 43200 and timeframe_minutes < 525600:
if 10000 < timeframe_minutes < 43200:
resample_interval = '1W-MON'
elif timeframe_minutes >= 43200 and timeframe_minutes < 525600:
# Monthly candles need special treatment to stick to the 1st of the month
resample_interval = f'{timeframe}S'
elif timeframe_minutes > 43200:

View File

@@ -116,7 +116,7 @@ def generate_test_data(timeframe: str, size: int, start: str = '2020-07-05'):
date = pd.date_range(start, periods=size, freq='1MS', tz='UTC')
elif timeframe == '3M':
date = pd.date_range(start, periods=size, freq='3MS', tz='UTC')
elif timeframe == '1w':
elif timeframe == '1w' or timeframe == '7d':
date = pd.date_range(start, periods=size, freq='1W-MON', tz='UTC')
else:
tf_mins = timeframe_to_minutes(timeframe)

View File

@@ -163,14 +163,15 @@ def test_ohlcv_to_dataframe_multi(timeframe):
if timeframe in ('1M', '3M', '1y'):
data1.loc[:, 'date'] = data1.loc[:, 'date'] + pd.to_timedelta('1w')
else:
# Shift by half a timeframe
data1.loc[:, 'date'] = data1.loc[:, 'date'] + (pd.to_timedelta(timeframe) / 2)
df2 = ohlcv_to_dataframe(data1, timeframe, 'UNITTEST/USDT')
assert len(df2) == len(data) - 1
tfs = timeframe_to_seconds(timeframe)
tfm = timeframe_to_minutes(timeframe)
if 1 <= tfm < 43200:
# minute based resampling does not work on timeframes >= 1 month
if 1 <= tfm < 10000:
# minute based resampling does not work on timeframes >= 1 week
ohlcv_dict = {
'open': 'first',
'high': 'max',

View File

@@ -131,7 +131,7 @@ def test_timeframe_to_msecs():
("10m", '600s'),
("1h", '3600s'),
("1d", '86400s'),
("1w", '604800s'),
("1w", '1W-MON'),
("1M", '1MS'),
("1y", '1YS'),
])