Skip to content

Commit c1329da

Browse files
committed
Fix timeframe conversion
1 parent a3ed069 commit c1329da

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

investing_algorithm_framework/domain/models/time_frame.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ def from_string(value: str):
3636
if value == entry.value.replace("H", "h"):
3737
return entry
3838

39+
# For hour timeframes compare with and without H
40+
if "d" in entry.value:
41+
42+
if value == entry.value:
43+
return entry
44+
45+
if value == entry.value.replace("d", "D"):
46+
return entry
47+
3948
if value == entry.value:
4049
return entry
4150

investing_algorithm_framework/domain/services/market_data_sources.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,7 @@ def __init__(
243243
symbol=symbol,
244244
)
245245
self._window_size = window_size
246-
247-
if timeframe is not None:
248-
self._timeframe = TimeFrame.from_value(timeframe)
246+
self._timeframe = timeframe
249247

250248
@property
251249
def timeframe(self):
@@ -266,6 +264,16 @@ def create_end_date(self, start_date, timeframe, window_size):
266264
def window_size(self):
267265
return self._window_size
268266

267+
@window_size.setter
268+
def window_size(self, value):
269+
270+
if not isinstance(value, int):
271+
raise OperationalException(
272+
"Window size must be an integer"
273+
)
274+
275+
self._window_size = value
276+
269277
def get_date_ranges(
270278
self,
271279
start_date: datetime,

investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def prepare_data(
119119
market_service.config = config
120120
ohlcv = market_service.get_ohlcv(
121121
symbol=self.symbol,
122-
time_frame=self.timeframe.value,
122+
time_frame=self.timeframe,
123123
from_timestamp=backtest_data_start_date,
124124
to_timestamp=backtest_end_date,
125125
market=self.market
@@ -142,7 +142,7 @@ def _create_file_path(self):
142142
OHLCV_{symbol}_{market}_{timeframe}_{start_date}_{end_date}.csv
143143
"""
144144
symbol_string = self.symbol.replace("/", "-")
145-
time_frame_string = self.timeframe.value.replace("_", "")
145+
time_frame_string = self.timeframe.replace("_", "")
146146
backtest_data_start_date = \
147147
self.backtest_data_start_date.strftime(DATETIME_FORMAT_BACKTESTING)
148148
backtest_data_end_date = \

investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ def get_closed_orders(
356356
def get_ohlcv(
357357
self, symbol, time_frame, from_timestamp, market, to_timestamp=None
358358
) -> pl.DataFrame:
359-
time_frame = TimeFrame.from_value(time_frame).value
360359

361360
if self.config is not None and "DATETIME_FORMAT" in self.config:
362361
datetime_format = self.config["DATETIME_FORMAT"]
@@ -414,6 +413,10 @@ def get_ohlcv(
414413
# Combine the Series into a DataFrame with given column names
415414
df = pl.DataFrame(data)
416415

416+
# Check if width is 0
417+
if len(df) == 0:
418+
return df
419+
417420
# Assign column names after DataFrame creation
418421
df.columns = col_names
419422
return df
@@ -427,7 +430,6 @@ def get_ohlcvs(
427430
to_timestamp=None
428431
) -> Dict[str, pl.DataFrame]:
429432
ohlcvs = {}
430-
time_frame = TimeFrame.from_value(time_frame).value
431433

432434
for symbol in symbols:
433435

0 commit comments

Comments
 (0)