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

Get bars fixes #619

Merged
merged 5 commits into from
Nov 11, 2024
Merged

Get bars fixes #619

merged 5 commits into from
Nov 11, 2024

Conversation

brettelliot
Copy link
Collaborator

@brettelliot brettelliot commented Nov 8, 2024

  1. When asked for daily bars, tradier now returns bars with a Timestamp index just like all the other datasources (it was previously returning a date).
  2. When asked to get daily bars and given a length of N and a timeshift of none, tradier now returns the last N bars (instead of what it was doing before which was getting the bars for the last N days unlike the other datasources).

The following datasources now are tested for this stuff:
pandas, yahoo, alpaca, polygon, tradier.
(sorry i dont have any ibkr or thetadata accounts to test with)

Description by Korbit AI

What change is being made?

Modify the get_historical_prices function to ensure it fetches the last n bars instead of bars from the past n days, and refactor test cases for clarity and correctness in tradier_data.py.

Why are these changes being made?

The change ensures accurate fetching of historical bars by adjusting the start date to include only the required number of trading days, addressing inaccuracies with the previous method. Additionally, test cases are revised to enhance legibility and ensure that they effectively validate the data sources and the outputs, thus increasing the maintainability of the codebase.

Is this description stale? Ask me to generate a new description by commenting /korbit-generate-pr-description

Copy link
Contributor

@korbit-ai korbit-ai bot left a comment

Choose a reason for hiding this comment

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

Review Summary by Korbit AI

Code Execution Comments

  • Ensure correct number of bars by modifying start_date calculation for accurate trading days array slicing.

Code Health Comments

  • Handle date and datetime objects for consistent timezone-aware index conversion in DataFrame.
Files scanned
File Path Reviewed
lumibot/data_sources/tradier_data.py

Explore our documentation to understand the languages and file types we support and the files we ignore.

Need a new review? Comment /korbit-review on this PR and I'll review your latest changes.

Korbit Guide: Usage and Customization

Interacting with Korbit

  • You can manually ask Korbit to review your PR using the /korbit-review command in a comment at the root of your PR.
  • You can ask Korbit to generate a new PR description using the /korbit-generate-pr-description command in any comment on your PR.
  • Too many Korbit comments? I can resolve all my comment threads if you use the /korbit-resolve command in any comment on your PR.
  • Chat with Korbit on issues we post by tagging @korbit-ai in your reply.
  • Help train Korbit to improve your reviews by giving a 👍 or 👎 on the comments Korbit posts.

Customizing Korbit

  • Check out our docs on how you can make Korbit work best for you and your team.
  • Customize Korbit for your organization through the Korbit Console.

Current Korbit Configuration

General Settings
Setting Value
Review Schedule Automatic excluding drafts
Max Issue Count 10
Automatic PR Descriptions
Issue Categories
Category Enabled
Naming
Database Operations
Documentation
Logging
Error Handling
Systems and Environment
Objects and Data Structures
Readability and Maintainability
Asynchronous Processing
Design Patterns
Third-Party Libraries
Performance
Security
Functionality

Feedback and Support

Comment on lines +241 to +243
# if type of index is date, convert it to timestamp with timezone info of "America/New_York"
if isinstance(df.index[0], date):
df.index = pd.to_datetime(df.index, utc=True).tz_convert("America/New_York")
Copy link
Contributor

Choose a reason for hiding this comment

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

category Functionality

Enhance index handling for date and datetime objects.

Tell me more

The current implementation only checks if the index is a date object before converting it to a timezone-aware timestamp. However, it's possible that the index could already be a datetime object. To ensure consistent behavior, consider modifying the code to handle both date and datetime objects. You could update the condition as follows:

if isinstance(df.index[0], (date, datetime)) and df.index.tzinfo is None:
    df.index = pd.to_datetime(df.index, utc=True).tz_convert("America/New_York")

This change ensures that the index is always converted to a timezone-aware timestamp, regardless of whether it's originally a date or datetime object.

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

Comment on lines +205 to +211
if timestep == 'day' and timeshift is None:
# What we really want is the last n bars, not the bars from the last n days.
# get twice as many days as we need to ensure we get enough bars
tcal_start_date = end_date - (td * length * 2)
trading_days = get_trading_days(market='NYSE', start_date=tcal_start_date, end_date=end_date)
# Now, start_date is the length bars before the last trading day
start_date = trading_days.index[-length]
Copy link
Contributor

Choose a reason for hiding this comment

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

category Functionality

Incorrect start_date calculation with timestep 'day' and timeshift None.

Tell me more

The current implementation for calculating the start_date when timestep is 'day' and timeshift is None might not always return the correct number of bars. Consider modifying the code to use the length parameter to slice the trading_days array, ensuring we always get the exact number of bars requested. For example, you could change:

start_date = trading_days.index[-length]

to:

start_date = trading_days.index[-length:][0]

This change ensures that we always get the correct number of bars, even if there are gaps in the trading days.

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

@@ -230,6 +238,10 @@ def get_historical_prices(
if "timestamp" in df.columns:
df = df.drop(columns=["timestamp"])

# if type of index is date, convert it to timestamp with timezone info of "America/New_York"
if isinstance(df.index[0], date):
df.index = pd.to_datetime(df.index, utc=True).tz_convert("America/New_York")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not hard code New York. This is a variable in lumibot so people can change it if they're international

@grzesir grzesir merged commit 65880bc into dev Nov 11, 2024
1 check passed
@brettelliot brettelliot deleted the get-bars-fixes branch November 11, 2024 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants