2

I can't figure out how to get data for a given day. Using the annual line in my code, I know the milisecond value of give date.

1612159200000.00 AAPL 2/1/2021 6:00

1612418400000.00 AAPL 2/4/2021 6:00

But putting these value in the code doesn't work

data=get_price_history(symbol=i, endDate=1612418400000 , startDate=1612159200000,  frequency=1, frequencyType='daily')
import requests
import pandas as pd
import time
import datetime

# tickers_list= ['AAPL', 'AMGN', 'AXP']
# print(len(tickers_list))

key = '****'

def get_price_history(**kwargs):

    url = 'https://api.tdameritrade.com/v1/marketdata/{}/pricehistory'.format(kwargs.get('symbol'))
    params = {}
    params.update({'apikey': key})

    for arg in kwargs:
        parameter = {arg: kwargs.get(arg)}
        params.update(parameter)

    return requests.get(url, params=params).json()

tickers_list= ['AAPL', 'AMGN','WMT']
for i in tickers_list:

    # get data 1 year 1 day frequency -- good
    # data=get_price_history(symbol=i, period=1, periodType='year', frequency=1, frequencyType='daily')
    
    data=get_price_history(symbol=i, endDate=1612418400000 , startDate=1612159200000,  frequency=1, frequencyType='daily') 

    historical['date'] = pd.to_datetime(historical['datetime'], unit='ms')
    info=pd.DataFrame(data['candles'])

    historical=pd.concat([historical,info])

historical
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
HongNg
  • 53
  • 5
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/284236. If something "doesn't work", please explain: *what happens* when you use the code? If you get some kind of error message, make sure to show it [completely](https://meta.stackoverflow.com/questions/359146). If the output is wrong, explain *why* it is wrong - what should happen instead? How is that different? Also, please note that this is *not a discussion forum*, so [don't](https://meta.stackoverflow.com/questions/343721/kind-reminder-to-remove-noise) sign off on posts, offer thanks etc. – Karl Knechtel Feb 01 '22 at 17:55

2 Answers2

0

From the Ameritrade Price History API documentation:

6 Months / 1 Day, including today's data:

https://api.tdameritrade.com/v1/marketdata/XYZ/pricehistory?periodType=month&frequencyType=daily&endDate=1464825600000

Note that periodType=month is specified because the default periodType is day which is not compatible with the frequencyType daily

So it seems that this line in your code:

data=get_price_history(symbol=i, endDate=1612418400000 , startDate=1612159200000,  frequency=1, frequencyType='daily')

is missing a valid periodType parameter. Try:

data=get_price_history(symbol=i, endDate=1612418400000 , startDate=1612159200000, frequency=1, periodType='month', frequencyType='daily')
atrocia6
  • 319
  • 1
  • 8
  • You are right. it is the periodType. ``` data=get_price_history(symbol=i, startDate=1643522400000, endDate=1643695200000 , frequency=1,periodType='month', frequencyType='daily') ``` Also, the time need to be 6:00AM. Many thanks, – HongNg Feb 01 '22 at 22:36
0

step1: you need a valid session. step2: you could use the tda api funcion get_price_history() see example that I successfully used to get daily data given a start and end date

from tda.auth import easy_client
 
# need a valid refresh token to use easy_client
Client = easy_client(
        api_key='APIKEY',
        redirect_uri='https://localhost',
        token_path='/tmp/token.json')

# getting the daily data given a a date 

# get daily data given start and end dat 
resp = Client.get_price_history('AAPL',
        period_type=Client.PriceHistory.PeriodType.YEAR,     
        start_datetime= datetime(2019,9,30),
        end_datetime= datetime(2019,10,30)  , 
        frequency_type=Client.PriceHistory.FrequencyType.DAILY,
        frequency=Client.PriceHistory.Frequency.DAILY)

assert resp.status_code == httpx.codes.OK
history = resp.json()

aapl = pd.DataFrame(history)