alexgolec / schwab-py

Unofficial API wrapper for the Schwab HTTP API
MIT License
181 stars 43 forks source link

get_price_history doesn't honor start_datetime and end_datetime #148

Open s-maheshbabu opened 2 months ago

s-maheshbabu commented 2 months ago

Description of Bug get_price_history_every_minute and similar methods don't honor the start_datatime and end_datetime fields. Regardless of what the time bounds are, we return data for the entire day.

The API should either filter out data outside the provided bounds or just not accept bounds at all as an input.

Code to Reproduce

start_datetime = datetime(2024, 7, 3, 10, 0, 0, tzinfo=ZoneInfo("US/Eastern"))
end_datetime = datetime(2024, 7, 3, 11, 0, 0, tzinfo=ZoneInfo("US/Eastern"))
response = self.schwab_client.get_price_history_every_minute(
    "$SPX",
    start_datetime=start_datetime,
    end_datetime=end_datetime,
    need_extended_hours_data=False,
)

data = response.json()

data_for_the_hour = pd.json_normalize(data["candles"]).drop(columns=["volume"])

print(f"Number of datapoints returned: {len(data_for_the_hour)}")
print(data_for_the_hour)

Expected Behavior Data returned only for the requested hour i.e. 10 AM to 11 AM Eastern time. Number of candles returned should be 60.

Actual Behavior 224 one minute candles were returned. The earliest and latest timestamps are clearly beyond the provided time bounds in the API call.

Number of datapoints returned: 224
      open   high    low  close       datetime
0    12.06  12.06  12.05  12.05  1720013520000
1    12.04  12.05  12.04  12.05  1720013580000
2    12.06  12.07  12.06  12.07  1720013640000
3    12.08  12.09  12.08  12.09  1720013700000
4    12.07  12.07  12.05  12.05  1720013760000
..     ...    ...    ...    ...            ...
219  12.06  12.07  12.06  12.06  1720026660000
220  12.06  12.07  12.06  12.07  1720026720000
221  12.07  12.07  12.07  12.07  1720026780000
222  12.07  12.10  12.07  12.09  1720026840000
223  12.09  12.09  12.09  12.09  1720026900000
s-maheshbabu commented 1 month ago

I reached out to Schwab about this issue.

They acknowledged that their API does not return intra-day snippets but instead returns the entire day when the given start/end times are within the same day. They said the data needs to be filtered down to the given time bounds at the client level. Weird limitation for no good reason!

Even though the underlying API defines this as expected behavior, I still think schwab-py should make developers life easier by always honoring the time bounds.