Skip to content

Commit

Permalink
Fix timeframe conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Jul 4, 2024
1 parent a3ed069 commit c1329da
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
9 changes: 9 additions & 0 deletions investing_algorithm_framework/domain/models/time_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand All @@ -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:

Expand Down

0 comments on commit c1329da

Please sign in to comment.