Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix polygon get historical prices #679

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lumibot/tools/polygon_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ 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))

# Find any dates with nan values in the df_all DataFrame. This happens for some infrequently traded assets, but
# it is difficult to know if the data is actually missing or if it is just infrequent trading, query for it again.
missing_dates += df_all[df_all.isnull().all(axis=1)].index.date.tolist()
Comment on lines +414 to +416
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Over-querying for legitimate no-trade days category Functionality

Tell me more
What is the issue?

The code treats all days with null values as missing data that needs to be re-queried, but for infrequently traded assets, these null values might legitimately represent days with no trading activity.

Why this matters

This could lead to unnecessary API calls to Polygon for dates that genuinely had no trading activity, potentially hitting rate limits and slowing down the data retrieval process without adding any valuable data.

Chat with Korbit by mentioning @korbit-ai, and give a 👍 or 👎 to help Korbit improve your reviews.


# make sure the dates are unique
missing_dates = list(set(missing_dates))
missing_dates.sort()

# finally, filter out any dates that are not in start/end range (inclusive)
missing_dates = [d for d in missing_dates if start.date() <= d <= end.date()]

return missing_dates


Expand Down
7 changes: 1 addition & 6 deletions tests/test_polygon_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,10 +480,5 @@ def test_polygon_missing_day_caching(self, mocker, tmpdir, timespan, force_cache
assert mock_polyclient.create().get_aggs.call_count == 3
assert expected_cachefile.exists()
assert len(df) == 7
df = ph.get_price_data_from_polygon(api_key, asset, start_date, end_date, timespan, force_cache_update=force_cache_update)
assert len(df) == 7
if force_cache_update:
assert mock_polyclient.create().get_aggs.call_count == 2 * 3
else:
assert mock_polyclient.create().get_aggs.call_count == 3

expected_cachefile.unlink()
Loading