Td Ameritrade Api returning empty json reponse for historical API call
Question:
I am trying to create a function that returns a days historical data for a certain symbol in python and have run across an error where whenever I call this function it return to me:
[{'candles': [], 'symbol': 'snap', 'empty': True}, {'candles': [], 'symbol': 'fb', 'empty': True}]
My expected output would be one where the candles list is filled with data for that certain stock. Here is my code so far:
def historical(symbols, key=TD_KEY, periodType="day", period=1, frequency="minute", needExtended=False):
# Gets historical data
return_list = []
for symbol in symbols:
url = f"/marketdata/{symbol}/pricehistory"
endpoint = f"{TD_URL}{url}"
payload = {"apikey":key,
"periodType": periodType,
"period": period,
"frequencyType": frequency,
"needExtendedHoursData": needExtended}
response = requests.get(url=endpoint, params=payload)
if str(response) == "<Response [200]>":
return_list.append(response.json())
else:
return f"Could not seccusfully connect http code {response}"
time.sleep(0.5)
Answers:
The reason behind this strange behavior was because when I was requesting historical data I put my query in lower case and only I changed that to upper case it was able to pull down the data I needed.
I searched everywhere for why my pricehistory requests weren’t working. Only THIS little post has the right answer and it took me 45 minutes to find. Thank you! UPPERCASE FOR THE TICKER IN THE URL
I am trying to create a function that returns a days historical data for a certain symbol in python and have run across an error where whenever I call this function it return to me:
[{'candles': [], 'symbol': 'snap', 'empty': True}, {'candles': [], 'symbol': 'fb', 'empty': True}]
My expected output would be one where the candles list is filled with data for that certain stock. Here is my code so far:
def historical(symbols, key=TD_KEY, periodType="day", period=1, frequency="minute", needExtended=False):
# Gets historical data
return_list = []
for symbol in symbols:
url = f"/marketdata/{symbol}/pricehistory"
endpoint = f"{TD_URL}{url}"
payload = {"apikey":key,
"periodType": periodType,
"period": period,
"frequencyType": frequency,
"needExtendedHoursData": needExtended}
response = requests.get(url=endpoint, params=payload)
if str(response) == "<Response [200]>":
return_list.append(response.json())
else:
return f"Could not seccusfully connect http code {response}"
time.sleep(0.5)
The reason behind this strange behavior was because when I was requesting historical data I put my query in lower case and only I changed that to upper case it was able to pull down the data I needed.
I searched everywhere for why my pricehistory requests weren’t working. Only THIS little post has the right answer and it took me 45 minutes to find. Thank you! UPPERCASE FOR THE TICKER IN THE URL