boxue
boxue

Reputation: 51

fetching stock ticker data from yahoo finance

I am trying to 'Adj Close' Data from yahoo finance using pdr.get_data_yahoo(ticker,start,end)

I am able to fetch data into a dataframe but unable to sort it by ticker symbol

I have created a list of the ticker symbols which comes from a csv file. and use the get_data_yahoo method to get the data for each symbol.

However I am unable to get the data into columns sorted by ticker, rather the data just concatenates into 1 column, or I am only able to pull 1 symbol from the list.

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)

Im expecting a dataframe with each column as the adj close prices sorted by ticker e.g. with date as the index column

 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   

What I am getting is............

[*********************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

I think I am getting a series of individual dataframes with the price data for each ticker

But I need it concatenated into a single data frame.

I believe something along the lines of..............

# 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)

I am unsure how to implement this correctly

Upvotes: 1

Views: 2536

Answers (1)

run-out
run-out

Reputation: 3184

I've had a lot of success using fix-yahoo-finance. Here is the 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

Upvotes: 3

Related Questions