如何使用Python批量下载Quandl上所有股票的过去100天的EOD数据

时间:2019-02-11 23:22:39

标签: python api stock algorithmic-trading quandl

使用Quandl API和Quandl Python库,我试图批量下载过去100天的EOD数据。

批量下载使用此调用来下载最后一个收集日的所有股票的所有EOD数据。删除download_type = partial参数将下载所有历史EOD数据: https://www.quandl.com/api/v3/databases/EOD/data?download_type=partial

此呼叫将为单个报价下载最后n天的EOD: https://www.quandl.com/api/v3/datasets/EOD/AAPL?start_date=2019-02-07

是否可以将这些结合起来并一次下载所有股票的最近n天的EOD数据? 此时,看来我唯一的选择是:

  1. 对所有8,000个股票进行单独的API调用
  2. 下载每只股票的所有历史数据

1 个答案:

答案 0 :(得分:1)

Quandle不再免费工作。曾经是过去。 如果需要,可以使用IEX。请查看下面的示例,该示例将为您提供每日收益:

from datetime import datetime
from iexfinance.stocks import get_historical_data
from pandas_datareader import data
import pandas as pd
start =  '2014-01-01'
end = datetime.today().utcnow()

datasets_original_test = ['AAPL', 'MSFT','NFLX','FB','GS','TSLA','BAC','TWTR','COF','TOL','EA','PFE','MS','C','SKX','GLD','SPY','EEM','XLF','GDX','EWZ','QQQ','FXI','XOP','EFA','VXXB','HYG','XLI','XLU','JNK','USO','IWM','XLP','XLE','EWJ','XLK','KRE','XLV','VNQ','MBB','OIH','FEZ','RSX','EWG','SMH','TLT','IBB','SLV','IYR','XRT','XLB','EMB','AGG','INDA','EWW','DBO','SPLV','KBE','VGK','XLY','EWH','EWT','DIA','IVV','XLRE','EPI','IJR','IEF']
dataset_names_test = ['AAPL', 'MSFT','NFLX','FB','GS','TSLA','BAC','TWTR','COF','TOL','EA','PFE','MS','C','SKX','GLD','SPY','EEM','XLF','GDX','EWZ','QQQ','FXI','XOP','EFA','VXXB','HYG','XLI','XLU','JNK','USO','IWM','XLP','XLE','EWJ','XLK','KRE','XLV','VNQ','MBB','OIH','FEZ','RSX','EWG','SMH','TLT','IBB','SLV','IYR','XRT','XLB','EMB','AGG','INDA','EWW','DBO','SPLV','KBE','VGK','XLY','EWH','EWT','DIA','IVV','XLRE','EPI','IJR','IEF']

datasets_test = []
for d in datasets_original_test:
    data_original = data.DataReader(d, 'iex', start, end)
    data_original.index = pd.to_datetime(data_original.index, format='%Y/%m/%d')
    data_ch = data_original['close'].pct_change()
    datasets_test.append(data_ch)
df_returns = pd.concat(datasets_test, axis=1, join_axes=[datasets_test[0].index])
df_returns.columns = dataset_names_test