从雅虎财经获取股票行情数据

fetching stock ticker data from yahoo finance

我正在尝试 'Adj Close' 使用 pdr.get_data_yahoo(ticker,start,end)

来自 yahoo finance 的数据

我能够将数据提取到数据框中,但无法按股票代码对其进行排序

我创建了一个来自 csv 文件的股票代码列表。 并使用 get_data_yahoo 方法获取每个交易品种的数据。

但是我无法将数据放入按代码排序的列中,而是数据只是连接成 1 列,或者我只能从列表中提取 1 个符号。

import fix_yahoo_finance as fyf
from pandas_datareader import data as pdr
import csv
import datetime
import pandas as pd
import numpy as np
import pandas_datareader as dr

#Stock tickers from csv
#Csv file data looks like this.........................
  Symbol
0  MOQ.AX
1  ONT.AX
2  14D.AX
3  1ST.AX
4  T3D.AX

#----------------------------
Asx_Stocks = pd.read_csv('ASXListedCompaniesAX1.csv',usecols = ['Symbol'], 
squeeze=True)
df_symbol = pd.DataFrame(Asx_Stocks)
print(df_symbol.head())


#df looks like this--------
 Symbol
0  MOQ.AX
1  ONT.AX
2  14D.AX
3  1ST.AX
4  T3D.AX
#-----------------------------
#set symbols to list
symbols = df_symbol['Symbol'].tolist()
symbol is a list of the tickers formatted for australian stock exchange 
(ASX)
e.g....

['MOQ.AX', 'ONT.AX', '14D.AX', '1ST.AX', 'T3D.AX', 'TGP.AX'........]

#set start and end parameters
start = datetime.datetime(2018, 1, 1)
end = datetime.datetime(2019, 1, 1);
#loop through symbols from list and fetch adj close data
for symbol in symbols:
    f = pdr.get_data_yahoo(symbol, start, end)['Adj Close']

    print(f)

我期待一个数据框,每一列都是按股票代码排序的调整收盘价,例如以日期为索引列

 AAA   DJRE    GOLD     IJR    NDQ  PMGOLD   QCB    VAP    VAS    VCF  \
0  50.15  20.63  170.50  105.25  16.34   18.13  8.27  79.74  72.13  47.25   
1  50.14  20.62  170.86  104.57  16.07   18.27  8.27  81.00  72.03  47.25   
2  50.15  20.53  169.29  104.66  16.11   18.22  8.08  80.39  71.19  47.21   

我得到的是......

[*********************100%***********************]  1 of 1 downloaded
Date
2018-01-01    0.230
2018-01-02    0.230
2018-01-03    0.230
2018-01-04    0.230
2018-01-07    0.230
2018-01-08    0.230
2018-01-09    0.220
2018-01-10    0.220
2018-01-11    0.230
[*********************100%***********************]  1 of 1 downloaded
etc.... continues on many times so data is downloading

我想我得到了一系列单独的数据框,其中包含每个代码的价格数据

但我需要将它连接成一个数据框。

我相信…………

# concatenate all dataframes into one final dataframe  
dfs = [out put from looping through each ticker] 
#perform the joins on DATE column as key and drop null values
df = reduce(lambda left,right: pd.merge(left,right,on='DATE', 
how='outer'), dfs).dropna() df.head(5)

我不确定如何正确实施

我使用 fix-yahoo-finance 取得了很大的成功。这里是github page

import fix_yahoo_finance as fyf
from pandas_datareader import data as web

fyf.pdr_override()

stock_list = sorted(['MOQ.AX', 'ONT.AX', '14D.AX', '1ST.AX', 'T3D.AX', 'TGP.AX'])
goog = web.get_data_yahoo(stock_list, start = '2018-01-01', end = '2019-12-31')['Adj Close']


          14D.AX 1ST.AX MOQ.AX  ONT.AX    T3D.AX    TGP.AX
Date                        
2018-01-01  NaN 0.049   0.23    6.594936    0.005   0.980018
2018-01-02  NaN 0.049   0.23    6.604564    0.005   0.970595
2018-01-03  NaN 0.053   0.23    6.546799    0.005   0.970595
2018-01-04  NaN 0.059   0.23    6.489032    0.005   0.998865
2018-01-07  NaN 0.060   0.23    6.450522    0.005   0.980018