Skip to content

Commit 5d559bd

Browse files
committed
Fix ticker sources
1 parent f98c403 commit 5d559bd

File tree

1 file changed

+14
-10
lines changed
  • investing_algorithm_framework/infrastructure/models/market_data_sources

1 file changed

+14
-10
lines changed

investing_algorithm_framework/infrastructure/models/market_data_sources/ccxt.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import logging
22
import os
33
from datetime import timedelta
4+
import pandas as pd
5+
from dateutil import parser
46

57
import polars
68
from dateutil import parser
@@ -368,22 +370,24 @@ def get_data(self, **kwargs):
368370
timeframe_minutes = TimeFrame.from_string(self.timeframe)\
369371
.amount_of_minutes
370372
backtest_index_date = kwargs["backtest_index_date"]
371-
end_date = backtest_index_date + timedelta(minutes=timeframe_minutes)
372373

373374
# Filter the data based on the backtest index date and the end date
374375
df = polars.read_csv(file_path)
375-
df = df.filter(
376+
filtered_df = df.filter(
376377
(df['Datetime'] >= backtest_index_date.strftime(DATETIME_FORMAT))
377378
)
378-
first_row = df.head(1)[0]
379-
first_row_datetime = parser.parse(first_row["Datetime"][0])
380379

381-
if first_row_datetime > end_date:
382-
logger.warning(
383-
f"No ticker data available for the given backtest "
384-
f"index date {backtest_index_date} and symbol {self.symbol} "
385-
f"and market {self.market}"
380+
# If nothing is found, get all dates before the index date
381+
if len(filtered_df) == 0:
382+
filtered_df = df.filter(
383+
(df['Datetime'] <= backtest_index_date.strftime(
384+
DATETIME_FORMAT))
386385
)
386+
first_row = filtered_df.tail(1)[0]
387+
else:
388+
first_row = filtered_df.head(1)[0]
389+
390+
first_row_datetime = parser.parse(first_row["Datetime"][0])
387391

388392
# Calculate the bid and ask price based on the high and low price
389393
return {
@@ -392,7 +396,7 @@ def get_data(self, **kwargs):
392396
+ float(first_row["High"][0]))/2,
393397
"ask": float((first_row["Low"][0])
394398
+ float(first_row["High"][0]))/2,
395-
"datetime": first_row["Datetime"][0],
399+
"datetime": first_row_datetime,
396400
}
397401

398402
def write_data_to_file_path(self, data_file, data: polars.DataFrame):

0 commit comments

Comments
 (0)