Skip to content

Commit

Permalink
speed optimization because of weekends
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed Dec 20, 2023
1 parent 047bf00 commit 8cb2901
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lumibot/backtesting/polygon_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import traceback
from datetime import date, timedelta

from polygon import RESTClient

from lumibot.data_sources import PandasData
from lumibot.entities import Asset, Data
from lumibot.tools import polygon_helper
from polygon import RESTClient

START_BUFFER = timedelta(days=5)

Expand Down Expand Up @@ -147,9 +148,7 @@ def _pull_source_symbol_bars(
):
# Get the current datetime and calculate the start datetime
current_dt = self.get_datetime()
start_dt, ts_unit = self.get_start_datetime_and_ts_unit(
length, timestep, current_dt, start_buffer=START_BUFFER
)
start_dt, ts_unit = self.get_start_datetime_and_ts_unit(length, timestep, current_dt, start_buffer=START_BUFFER)

# Get data from Polygon
pandas_data_update = self.update_pandas_data(asset, quote, length, timestep, start_dt)
Expand Down
14 changes: 10 additions & 4 deletions lumibot/tools/polygon_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ def get_missing_dates(df_all, asset, start, end):
A list of dates that we need to get data for
"""
trading_dates = get_trading_dates(asset, start, end)

# For options and stocks, exclude weekends
if asset.asset_type in ["option", "stock"]:
# Remove weekends from the missing dates
trading_dates = [x for x in trading_dates if x.weekday() < 5]

# For Options, don't need any dates passed the expiration date
if asset.asset_type == "option":
trading_dates = [x for x in trading_dates if x <= asset.expiration]

if df_all is None or not len(df_all):
return trading_dates

Expand All @@ -299,10 +309,6 @@ def get_missing_dates(df_all, asset, start, end):
dates = pd.Series(df_all.index.date).unique()
missing_dates = sorted(set(trading_dates) - set(dates))

# For Options, don't need any dates passed the expiration date
if asset.asset_type == "option":
missing_dates = [x for x in missing_dates if x <= asset.expiration]

return missing_dates


Expand Down

0 comments on commit 8cb2901

Please sign in to comment.