From c1329dabb9ad4ee850961db7f9d8bf7b1b6f4205 Mon Sep 17 00:00:00 2001 From: marcvanduyn Date: Thu, 4 Jul 2024 10:55:58 +0200 Subject: [PATCH] Fix timeframe conversion --- .../domain/models/time_frame.py | 9 +++++++++ .../domain/services/market_data_sources.py | 14 +++++++++++--- .../models/market_data_sources/ccxt.py | 4 ++-- .../services/market_service/ccxt_market_service.py | 6 ++++-- 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/investing_algorithm_framework/domain/models/time_frame.py b/investing_algorithm_framework/domain/models/time_frame.py index 4bfac240..20d08692 100644 --- a/investing_algorithm_framework/domain/models/time_frame.py +++ b/investing_algorithm_framework/domain/models/time_frame.py @@ -36,6 +36,15 @@ def from_string(value: str): if value == entry.value.replace("H", "h"): return entry + # For hour timeframes compare with and without H + if "d" in entry.value: + + if value == entry.value: + return entry + + if value == entry.value.replace("d", "D"): + return entry + if value == entry.value: return entry diff --git a/investing_algorithm_framework/domain/services/market_data_sources.py b/investing_algorithm_framework/domain/services/market_data_sources.py index 605afc55..3aa41501 100644 --- a/investing_algorithm_framework/domain/services/market_data_sources.py +++ b/investing_algorithm_framework/domain/services/market_data_sources.py @@ -243,9 +243,7 @@ def __init__( symbol=symbol, ) self._window_size = window_size - - if timeframe is not None: - self._timeframe = TimeFrame.from_value(timeframe) + self._timeframe = timeframe @property def timeframe(self): @@ -266,6 +264,16 @@ def create_end_date(self, start_date, timeframe, window_size): def window_size(self): return self._window_size + @window_size.setter + def window_size(self, value): + + if not isinstance(value, int): + raise OperationalException( + "Window size must be an integer" + ) + + self._window_size = value + def get_date_ranges( self, start_date: datetime, diff --git a/investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py b/investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py index fd781e48..2877dfcb 100644 --- a/investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py +++ b/investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py @@ -119,7 +119,7 @@ def prepare_data( market_service.config = config ohlcv = market_service.get_ohlcv( symbol=self.symbol, - time_frame=self.timeframe.value, + time_frame=self.timeframe, from_timestamp=backtest_data_start_date, to_timestamp=backtest_end_date, market=self.market @@ -142,7 +142,7 @@ def _create_file_path(self): OHLCV_{symbol}_{market}_{timeframe}_{start_date}_{end_date}.csv """ symbol_string = self.symbol.replace("/", "-") - time_frame_string = self.timeframe.value.replace("_", "") + time_frame_string = self.timeframe.replace("_", "") backtest_data_start_date = \ self.backtest_data_start_date.strftime(DATETIME_FORMAT_BACKTESTING) backtest_data_end_date = \ diff --git a/investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py b/investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py index 5ff47dbd..1691bde2 100644 --- a/investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py +++ b/investing_algorithm_framework/infrastructure/services/market_service/ccxt_market_service.py @@ -356,7 +356,6 @@ def get_closed_orders( def get_ohlcv( self, symbol, time_frame, from_timestamp, market, to_timestamp=None ) -> pl.DataFrame: - time_frame = TimeFrame.from_value(time_frame).value if self.config is not None and "DATETIME_FORMAT" in self.config: datetime_format = self.config["DATETIME_FORMAT"] @@ -414,6 +413,10 @@ def get_ohlcv( # Combine the Series into a DataFrame with given column names df = pl.DataFrame(data) + # Check if width is 0 + if len(df) == 0: + return df + # Assign column names after DataFrame creation df.columns = col_names return df @@ -427,7 +430,6 @@ def get_ohlcvs( to_timestamp=None ) -> Dict[str, pl.DataFrame]: ohlcvs = {} - time_frame = TimeFrame.from_value(time_frame).value for symbol in symbols: