From 4e250ef57fcbcf546f60bcfc6d461675ae74a5b0 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Thu, 15 Feb 2024 06:03:50 -0800 Subject: [PATCH] Ensure that provided dates are set to midnight in order to include all the data for the requested dates --- backtesting/backtest_iron_condor.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backtesting/backtest_iron_condor.py b/backtesting/backtest_iron_condor.py index 34e11f4..fe0d869 100644 --- a/backtesting/backtest_iron_condor.py +++ b/backtesting/backtest_iron_condor.py @@ -3,7 +3,7 @@ import os import pandas as pd from concurrent.futures import ProcessPoolExecutor -from datetime import datetime +from datetime import date, datetime, time from dotenv import load_dotenv from typing import List @@ -278,12 +278,13 @@ def backtest_iron_condor( filters: List[BacktestFilter] = [] ) -> pd.DataFrame: - start_year = start_date.year - end_year = end_date.year + # Setting dates to midnight to ensure all the data between them is included. + start_date = datetime.combine(start_date, time()) + end_date = datetime.combine(end_date, time()) futures = [] with ProcessPoolExecutor(max_workers = 10) as executor: - for year in range(start_year, end_year + 1): + for year in range(start_date.year, end_date.year + 1): year_directory = os.path.join(OPTION_DATA_DIRECTORY, str(year)) for file in os.listdir(year_directory): historical_data_file = os.path.join(year_directory, file) @@ -292,8 +293,6 @@ def backtest_iron_condor( # Assuming file format 'YYYY-MM-DD.csv'. current_date = datetime.strptime(os.path.splitext(file)[0], '%Y-%m-%d') - - # TODO: This doesn't work as expected when the start date is not set to midnight. if current_date < start_date or current_date > end_date: continue