Skip to content

Commit

Permalink
Fix ticker sources
Browse files Browse the repository at this point in the history
  • Loading branch information
MDUYN committed Apr 11, 2024
1 parent f98c403 commit 5d559bd
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import logging
import os
from datetime import timedelta
import pandas as pd
from dateutil import parser

import polars
from dateutil import parser
Expand Down Expand Up @@ -368,22 +370,24 @@ def get_data(self, **kwargs):
timeframe_minutes = TimeFrame.from_string(self.timeframe)\
.amount_of_minutes
backtest_index_date = kwargs["backtest_index_date"]
end_date = backtest_index_date + timedelta(minutes=timeframe_minutes)

# Filter the data based on the backtest index date and the end date
df = polars.read_csv(file_path)
df = df.filter(
filtered_df = df.filter(
(df['Datetime'] >= backtest_index_date.strftime(DATETIME_FORMAT))
)
first_row = df.head(1)[0]
first_row_datetime = parser.parse(first_row["Datetime"][0])

if first_row_datetime > end_date:
logger.warning(
f"No ticker data available for the given backtest "
f"index date {backtest_index_date} and symbol {self.symbol} "
f"and market {self.market}"
# If nothing is found, get all dates before the index date
if len(filtered_df) == 0:
filtered_df = df.filter(
(df['Datetime'] <= backtest_index_date.strftime(
DATETIME_FORMAT))
)
first_row = filtered_df.tail(1)[0]
else:
first_row = filtered_df.head(1)[0]

first_row_datetime = parser.parse(first_row["Datetime"][0])

# Calculate the bid and ask price based on the high and low price
return {
Expand All @@ -392,7 +396,7 @@ def get_data(self, **kwargs):
+ float(first_row["High"][0]))/2,
"ask": float((first_row["Low"][0])
+ float(first_row["High"][0]))/2,
"datetime": first_row["Datetime"][0],
"datetime": first_row_datetime,
}

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

0 comments on commit 5d559bd

Please sign in to comment.