Skip to content
Advertisement

Td Ameritrade Api returning empty json reponse for historical API call

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)

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement