areed1192 / td-ameritrade-api

The unofficial Python API client library for TD Ameritrade. This library allows for easy access of the Standard REST API and Streaming API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

start_date of get_price_history does not actually change anything

dillondriskill opened this issue · comments

I have this code to get a price history

start = int((time.time()*1000)-1)  # For some reason this value doesn't actually change the output?
end = int(time.time()*1000)

def get_prices(symbol=str):  # as the program goes on the list gets longer and longer. No idea why. Maybe ext hours?
     raw_prices = td_client.get_price_history(
          symbol=symbol,
          start_date=start,
          end_date=end,
          frequency_type='minute',
          frequency='1',
          extended_hours='True')
     prices = pd.DataFrame(data=raw_prices['candles'], columns=['close'])
     return prices

The start date does not affect the output. If i use SPY (a 24 hour stock) as the symbol it gets prices starting at 7 am today no matter what. I Figure this is a bug somewhere

I believe it's the size of the change between the two times. For example, the API will only except up to milliseconds, but that doesn't mean the data is coming back at millisecond level changes. The lowest level you can go is one minute.

I see what youre saying, however no matter the number i put in, as long as its lower than the end time, then it just gets me the price history starting from 7 am that day. I have been testing this on SPY, which is 24 hour, so im not sure if this is the issue or not.

I was able to get it to work with the above code and output. Note it's possible to get 1440 1 min candles in 24 hours but SPY doesn't necessarily always have trades in that period.

Grab the Price History, custom time frame.

price_history = price_history_service.get_price_history(
symbol='SPY',
frequency_type=FrequencyType.Minute,
frequency=1,
start_date=start_date,
end_date=end_date,
extended_hours_needed=True
)

import pandas as pd

df = pd.DataFrame.from_records(price_history['candles'])
df.sort_values(by="datetime", ascending=True, inplace=True)

df["date"] = pd.to_datetime(df['datetime'], unit='ms')

print(df['date'].dt.normalize().value_counts().sort_index())

print(24*60)

2022-04-06 727
2022-04-07 946
2022-04-08 863
2022-04-11 1048
2022-04-12 1027
2022-04-13 904
2022-04-14 879
2022-04-18 973
2022-04-19 965
2022-04-20 921
2022-04-21 896
2022-04-22 971
2022-04-25 1119
2022-04-26 980
2022-04-27 1093
2022-04-28 1012
2022-04-29 966
2022-05-02 1036
2022-05-03 984
2022-05-04 914
2022-05-05 1007
2022-05-06 972
Name: date, dtype: int64
1440

It seems as if this issue is actually with the much older td-ameritrade-python api, which is what i have been referencing. I will consider this issue closed

ya i feel the same it has to be more than a day diff between start and end