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)
Asked By: Santa

||

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.

Answered By: Santa

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

Answered By: Pete Micalizzi
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.