0

我不知道如何获取给定日期的数据。使用我的代码中的年线,我知道给定日期的毫秒值。

1612159200000.00 AAPL 2/1/2021 6:00

1612418400000.00 AAPL 2/4/2021 6:00

但是将这些值放在代码中是行不通的

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
4

1 回答 1

0

来自Ameritrade 价格历史 API 文档

6 个月/1 天,包括今天的数据:

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

请注意,指定 periodType=month 是因为默认 periodType 是 day,它与 frequencyType daily 不兼容

因此,您的代码中的这一行似乎是:

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

缺少有效periodType参数。尝试:

data=get_price_history(symbol=i, endDate=1612418400000 , startDate=1612159200000, frequency=1, periodType='month', frequencyType='daily')
于 2022-02-01T17:50:12.543 回答