-
Notifications
You must be signed in to change notification settings - Fork 187
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
Get bars fixes #619
Conversation
There was a problem hiding this 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
# 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") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -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") |
There was a problem hiding this comment.
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
0cc30db
to
057b675
Compare
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 lastn
bars instead of bars from the pastn
days, and refactor test cases for clarity and correctness intradier_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.