Pandas: 52 week high from yahoo or google finance - python

Does anyone know if you can get the 52 week high in pandas from either yahoo or google finance? Thanks.

It is possible, please check out pandas documentation. Here's an example:
import pandas.io.data as web
import datetime
symbol = 'aapl'
end = datetime.datetime.now()
start = end - datetime.timedelta(weeks=52)
df = web.DataReader(symbol, 'yahoo', start, end)
highest_high = df['High'].max()

One can also use yfinance(from yahoo)
pip install finance
import yfinance as yf
stock = "JNJ"
dataframe = yf.download(stock, period="1y", auto_adjust=True, prepost=True, threads=True)
max = dataframe['High'].max()

You could also use other libraries such as yahoo_fin. This one works better sometimes, it would depend on what you want to do, but it's good to bear in mind other possibilities : )
import yfinance as yf
import yahoo_fin.stock_info as si
stock = 'AAPL'
df = yf.download(stock, period="1y")
print("$",round(df['High'].max(), 2))
df2 = si.get_data(stock, interval="1mo")
print("$",round(df2['high'].tail(12).max(), 2))
Output:
$ 182.94
$ 182.94

You can use the info keyword to return lots of aggregated data like P/E Ratio, 52-Week High,etc.
import yfinance as yf
data = yf.Ticker(ticker).info
print(data.fiftyTwoWeekHigh)

Related

Python-3, Pandas datareader and Yahoo Error RemoteDataError: Unable to read URL

I am trying to use yahoo finance for importing stock data
I am using the code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
plt.style.use("fivethirtyeight")
%matplotlib inline
# For reading stock data from yahoo
from pandas_datareader.data import DataReader
# For time stamps
from datetime import datetime
It is running fine.
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
# The tech stocks we'll use for this analysis
tech_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']
# Set up End and Start times for data grab
end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)
#For loop for grabing yahoo finance data and setting as a dataframe
for stock in tech_list:
# Set DataFrame as the Stock Ticker
globals()[stock] = pdr.get_data_yahoo(stock, start, end)
While running below mentioned code I am getting an error:
company_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']
company_name = ["Wipro", "Infosys", "Tata_Consultancy_Services", "Happiest_Minds_Technologies"]
for company, com_name in zip(company_list, company_name):
company["company_name"] = com_name
df = pd.concat(company_list, axis=0)
df.tail(10)
Error Message:
TypeError Traceback (most recent call last)
<ipython-input-6-4753fcd8a7a3> in <module>
3
4 for company, com_name in zip(company_list, company_name):
----> 5 company["company_name"] = com_name
6
7 df = pd.concat(company_list, axis=0)
TypeError: 'str' object does not support item assignment
Please help me in solving this.
Thanks a lot ^_^
ARIMA is used for forecasting univariate time-series data. Not sure which feature you want to forecast. Came up with this one below:(Upvote if it works for you!)
#For loop for grabing yahoo finance data and setting as a dataframe
lt=[]
for stock in tech_list:
# Set DataFrame as the Stock Ticker
temp_df = pdr.get_data_yahoo(stock, start, end)
temp_df = temp_df.reset_index()
lt.append(temp_df)
# Each element in the list is a DataFrame
df = pd.concat([lt[0],lt[1],lt[2],lt[3]], axis=0)
df = df.reset_index(drop=True)
print(df.head())
Output:
Date Open High Low Close Adj Close Volume
0 2020-07-09 224.850006 224.850006 219.800003 221.600006 221.103027 198245
1 2020-07-10 221.600006 223.449997 219.449997 222.000000 221.502121 109461
2 2020-07-13 224.000000 229.000000 222.750000 227.550003 227.039673 385205
3 2020-07-14 229.000000 231.600006 224.199997 225.050003 224.545288 449975
4 2020-07-15 237.000000 265.500000 233.800003 262.950012 262.360291 6313161
The name of the fix_yahoo_finance package has been changed to yfinance. So please try this code.
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
# The tech stocks we'll use for this analysis
tech_list = ['WIPRO.BO', 'INFY.BO', 'TCS.BO', 'HAPPSTMNDS.BO']
# Set up End and Start times for data grab
end = datetime.now()
start = datetime(end.year - 1, end.month, end.day)
#For loop for grabing yahoo finance data and setting as a dataframe
for stock in tech_list:
# Set DataFrame as the Stock Ticker
globals()[stock] = pdr.get_data_yahoo(stock, start, end)
The get_data_yahoo() method returns a pandas dataframe. So depending on what you want to do you can generate list of dataframes and concatenate the list together.

Real-time data from yfinance

I am trying to get real rime data from yfinance with following code.
But I just get the same close/high/low/open results.
I am starting to think that it is impossible with yfinance. Is there any free and european alternatives?
Thanks a lot in advance!!
#import libraries
import schedule
import time
import alpaca_trade_api as tradeapi
import yfinance as yf
import pandas as pd
# Ask what stocks you want to check
pd = pd.DataFrame()
n = int(input("Enter the size of the list "))
print("\n")
numList = list(num for num in input("Enter the list numbers separated by space ").strip().split())[:n]
print("User List: ", numList)
# Get info for every stock chosen.
def get_data():
for ticker in numList:
ticker_yahoo = yf.Ticker(ticker)
data = ticker_yahoo.history(period = "1d", interval="1m")
data = (data.tail(1).iloc[0])
pd[ticker] = data
print(pd)
get_data()
It is faster to do it with Pandas Datareader by using get_quote_yahoo() method
import pandas_datareader as web
tickers = ["MSFT", "XOM", "KKR"]
current_price = web.get_quote_yahoo(tickers)["regularMarketPrice"]
If you insist on using yfinance, you can use the following code, but beware that it will be MUCH SLOWER This is because 1) instantiating Ticker object and pulling the info property takes time, and 2) it is inconvenient to use Tickers class (as opposed to Ticker), so you have to use a for a loop.
import yfinance as yf
tickers = ["MSFT", "XOM", "KKR"]
current_price = list()
for i in range(len(tickers)):
company = yf.Ticker(ticker[i])
current_price[i] = company.info["regularMarketPrice"]
Obviously, these solutions apply to other types of securities as well. Note though that using this (or any other free) data source for algorithmic trading with real money is impossible at best due to requests limit, high latency, stability issues etc.

Work with data in python and numpy/pandas

so I started learning how to work with data in python. I wanted to load multiple securities. But I have an error that I can not fix for some reason. Could someone tell me what is the problem?
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
tickers = ['PG', 'MSFT', 'F', 'GE']
mydata = pd.DataFrame()
for t in tickers:
mydata[t] = wb.DataReader(t, data_source='yahoo', start = '1955-1-1')
you need 2 fixes here:
1) 1955 is too early for this data source, try 1971 or later.
2) your data from wb.DataReader(t, data_source='yahoo', start = '1971-1-1') comes as dataframe with multiple series, so you can not save it to mydata[t] as single series. Use a dictionary as in the other answer or save only closing prices:
mydata[t] = pdr.data.DataReader(t, data_source='yahoo', start = '2010-1-1')['Close']
First of all please do not share information as images unless absolutely necessary.
See: this link
Now here is a solution to your problem. You are using year '1955' but there is a possibility that data is not available for this year or there may be some other issues. But when you select the right year it will work. Another thing it returns data as dataframe so you can not assign it like a dictionary so instead of making a DataFram you should make a dictionary and store all dataframes into it.
Here is improved code choose year carefully
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
from datetime import datetime as dt
tickers = ['PG', 'MSFT', 'F', 'GE']
mydata = {}
for t in tickers:
mydata[t] = wb.DataReader(t, data_source='yahoo',start=dt(2019, 1, 1), end=dt.now())
Output
mydata['PG']
High Low Open Close Volume Adj Close
Date
2018-12-31 92.180000 91.150002 91.629997 91.919998 7239500.0 88.877655
2019-01-02 91.389999 89.930000 91.029999 91.279999 9843900.0 88.258835
2019-01-03 92.500000 90.379997 90.940002 90.639999 9820200.0 87.640022
2019-01-04 92.489998 90.370003 90.839996 92.489998 10565700.0 89.428787

Downloading mutliple stocks at once from yahoo finance python

I have a question about the function of yahoo finance using the pandas data reader. I'm using for months now a list with stock tickers and execute it in the following lines:
import pandas_datareader as pdr
import datetime
stocks = ["stock1","stock2",....]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)
f = pdr.DataReader(stocks, 'yahoo',start,end)
Since yesterday i get the error "IndexError: list index out of range", which appears only if I try to get multiple stocks.
Has anything changed in recent days which I have to consider or do you have a better solution for my problem?
Updated as of 2021-01-19
At this time, the implementation in the OP works without issue, to download multiple stocks.
Version: 0.9.0 Date: July 10, 2020
GitHub: pydata / pandas-datareader
tickers = ['msft', 'aapl', 'twtr', 'intc', 'tsm', 'goog', 'amzn', 'fb', 'nvda']
df = pdr.DataReader(tickers, data_source='yahoo', start='2017-01-01', end='2020-09-28')
Original Answer
If you read through Pandas DataReader's documentation, they issued an immediate depreciation on multiple data source API's, one of which is Yahoo! Finance.
v0.6.0 (January 24, 2018)
Immediate deprecation of Yahoo!, Google Options and Quotes and EDGAR.
The end points behind these APIs have radically changed and the
existing readers require complete rewrites. In the case of most Yahoo!
data the endpoints have been removed. PDR would like to restore these
features, and pull requests are welcome.
This could be the culprit to why you been getting IndexError's (or any other normally none-existant errors).
However, there is another Python package whose goal is to fix the support for Yahoo! Finance for Pandas DataReader, you can find that package here:
https://pypi.python.org/pypi/fix-yahoo-finance
According to their documentation:
Yahoo! finance has decommissioned their historical data API, causing many programs that relied on it to stop working.
fix-yahoo-finance offers a temporary fix to the problem by scraping the data from Yahoo! finance using and return a Pandas
DataFrame/Panel in the same format as pandas_datareader’s
get_data_yahoo().
By basically “hijacking” pandas_datareader.data.get_data_yahoo()
method, fix-yahoo-finance’s implantation is easy and only requires
to import fix_yahoo_finance into your code.
All you need to add is this:
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
yf.pdr_override()
stocks = ["stock1","stock2", ...]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)
f = pdr.get_data_yahoo(stocks, start=start, end=end)
Or even without the need of Pandas DataReader:
import fix_yahoo_finance as yf
stocks = ["stock1","stock2", ...]
start = datetime.datetime(2012,5,31)
end = datetime.datetime(2018,3,1)
data = yf.download(stocks, start=start, end=end)
You can use the new Python YahooFinancials module with pandas to do this. YahooFinancials is well built and gets it's data by hashing out the datastore object present in each Yahoo Finance Web page, so it's fast and doesn't rely on the old discontinued api nor a web driver like a scraper does. Data is returned as JSON and you can pull as many stocks as you want at once by passing in a list of stock/index tickers to initialize the YahooFinancials Class with.
$ pip install yahoofinancials
Usage Example:
from yahoofinancials import YahooFinancials
import pandas as pd
# Select Tickers and stock history dates
ticker = 'AAPL'
ticker2 = 'MSFT'
ticker3 = 'INTC'
index = '^NDX'
freq = 'daily'
start_date = '2012-10-01'
end_date = '2017-10-01'
# Function to clean data extracts
def clean_stock_data(stock_data_list):
new_list = []
for rec in stock_data_list:
if 'type' not in rec.keys():
new_list.append(rec)
return new_list
# Construct yahoo financials objects for data extraction
aapl_financials = YahooFinancials(ticker)
mfst_financials = YahooFinancials(ticker2)
intl_financials = YahooFinancials(ticker3)
index_financials = YahooFinancials(index)
# Clean returned stock history data and remove dividend events from price history
daily_aapl_data = clean_stock_data(aapl_financials
.get_historical_stock_data(start_date, end_date, freq)[ticker]['prices'])
daily_msft_data = clean_stock_data(mfst_financials
.get_historical_stock_data(start_date, end_date, freq)[ticker2]['prices'])
daily_intl_data = clean_stock_data(intl_financials
.get_historical_stock_data(start_date, end_date, freq)[ticker3]['prices'])
daily_index_data = index_financials.get_historical_stock_data(start_date, end_date, freq)[index]['prices']
stock_hist_data_list = [{'NDX': daily_index_data}, {'AAPL': daily_aapl_data}, {'MSFT': daily_msft_data},
{'INTL': daily_intl_data}]
# Function to construct data frame based on a stock and it's market index
def build_data_frame(data_list1, data_list2, data_list3, data_list4):
data_dict = {}
i = 0
for list_item in data_list2:
if 'type' not in list_item.keys():
data_dict.update({list_item['formatted_date']: {'NDX': data_list1[i]['close'], 'AAPL': list_item['close'],
'MSFT': data_list3[i]['close'],
'INTL': data_list4[i]['close']}})
i += 1
tseries = pd.to_datetime(list(data_dict.keys()))
df = pd.DataFrame(data=list(data_dict.values()), index=tseries,
columns=['NDX', 'AAPL', 'MSFT', 'INTL']).sort_index()
return df
Multiple stocks data at once example (returns list of JSON objects for each ticker):
from yahoofinancials import YahooFinancials
tech_stocks = ['AAPL', 'MSFT', 'INTC']
bank_stocks = ['WFC', 'BAC', 'C']
yahoo_financials_tech = YahooFinancials(tech_stocks)
yahoo_financials_banks = YahooFinancials(bank_stocks)
tech_cash_flow_data_an = yahoo_financials_tech.get_financial_stmts('annual', 'cash')
bank_cash_flow_data_an = yahoo_financials_banks.get_financial_stmts('annual', 'cash')
banks_net_ebit = yahoo_financials_banks.get_ebit()
tech_stock_price_data = tech_cash_flow_data.get_stock_price_data()
daily_bank_stock_prices = yahoo_financials_banks.get_historical_stock_data('2008-09-15', '2017-09-15', 'daily')
JSON Output Example:
Code:
yahoo_financials = YahooFinancials('WFC')
print(yahoo_financials.get_historical_stock_data("2017-09-10", "2017-10-10", "monthly"))
JSON Return:
{
"WFC": {
"prices": [
{
"volume": 260271600,
"formatted_date": "2017-09-30",
"high": 55.77000045776367,
"adjclose": 54.91999816894531,
"low": 52.84000015258789,
"date": 1506830400,
"close": 54.91999816894531,
"open": 55.15999984741211
}
],
"eventsData": [],
"firstTradeDate": {
"date": 76233600,
"formatted_date": "1972-06-01"
},
"isPending": false,
"timeZone": {
"gmtOffset": -14400
},
"id": "1mo15050196001507611600"
}
}
Try this simple code
watchlist=["stock1","stock2".......]
closing_price=pd.DataFrame()
symbols=[]
for i in watchlist:
Result=wb.DataReader(i,start='05-1-20', end='05-20-20',data_source='yahoo')
closing_price=closing_price.append(Result)
symbols.append(i)
print("Generating Closing price for",i)
closing_price["SYMBOL"]=symbols
print("closing_price"
yahoo_finance is no longer able to use since Yahoo has changed the format, fix_yahoo_finance is good enough to download data. However to parse you'll need other libraries, here's the simple working example:
import numpy as np #python library for scientific computing
import pandas as pd #python library for data manipulation and analysis
import matplotlib.pyplot as plt #python library for charting
import fix_yahoo_finance as yf #python library to scrape data from yahoo finance
from pandas_datareader import data as pdr #extract data from internet sources into pandas data frame
yf.pdr_override()
data = pdr.get_data_yahoo(‘^DJI’, start=”2006–01–01")
data2 = pdr.get_data_yahoo(“MSFT”, start=”2006–01–01")
data3 = pdr.get_data_yahoo(“AAPL”, start=”2006–01–01")
data4 = pdr.get_data_yahoo(“BB.TO”, start=”2006–01–01")
ax = (data[‘Close’] / data[‘Close’].iloc[0] * 100).plot(figsize=(15, 6))
(data2[‘Close’] / data2[‘Close’].iloc[0] * 100).plot(ax=ax, figsize=(15,6))
(data3[‘Close’] / data3[‘Close’].iloc[0] * 100).plot(ax=ax, figsize=(15,6))
(data4[‘Close’] / data5[‘Close’].iloc[0] * 100).plot(ax=ax, figsize=(15,6))
plt.legend([‘Dow Jones’, ‘Microsoft’, ‘Apple’, ‘Blackberry’], loc=’upper left’)
plt.show()
for the explanation of the code you can visit at https://medium.com/#gerrysabar/charting-stocks-price-from-yahoo-finance-using-fix-yahoo-finance-library-6b690cac5447
This works for me.
assets = ['TSLA', 'MSFT', 'FB']
yahoo_financials = YahooFinancials(assets)
data = yahoo_financials.get_historical_price_data(start_date='2019-01-01',
end_date='2019-12-31',
time_interval='weekly')
prices_df = pd.DataFrame({
a: {x['formatted_date']: x['adjclose'] for x in data[a]['prices']} for a in assets})
prices_df
Result:

Yahoo Finance API / URL not working: Python fix for Pandas DataReader

Yahoo Finance URL has not been accessible using the Pandas DataReader's "yahoo" method since 16 May 2017. I have yet to test this fix-yahoo-finance: https://pypi.python.org/pypi/fix-yahoo-finance that was posted just yesterday, with the statement: "Yahoo! finance has decommissioned their historical data API".
EDIT August 2, 2017: I have since followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir, upgraded pandas_datareader to work with "fix-yahoo-finance 0.0.6", and amended codes:
from pandas_datareader import data as pdr
import fix_yahoo_finance
data = pdr.get_data_yahoo('AAPL', start='2017-04-23', end='2017-05-24')
Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. not the previous format. For my purpose, they are simply reset to the original format:
cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
data.reindex(columns=cols)
I would recommend using Quandl. I'm not sure if Yahoo is going to be reliable after their acquisition.
In Quandl if you have multiple symbols you have to loop. Read the docs and do something like this:
import quandl as qdl
start_date = '2016-01-01'
end_date = '2017-05-22'
for symbol in symbols:
quandldata = qdl.get_table("WIKI/PRICES",qopts={"columns":["date", "adj_close"]},
ticker=symbol, date = {'gte': start_date,'lte' : end_date})
# specify that the quandldata df has index col = 'date'
quandldata = quandldata.set_index(["date"], drop=True)
# rename col adj close to the respective symbol to prevent clash w/ same name for all cols
quandldata = quandldata.rename(columns={'adj_close': symbol})
df = df.join(quandldata)
import pandas_datareader.data as pdweb
from pandas_datareader import data as pdr
import fix_yahoo_finance # must pip install first
data = pdr.get_data_yahoo('SPY','2017-05-20','2017-05-23')
data = pdr.get_data_yahoo(['SPY','QQQ'],'2017-05-01','2017-05-23', as_panel=False,group_by = 'ticker')
I have followed the steps in https://pypi.python.org/pypi/fix-yahoo-finance to: $ pip3 install fix_yahoo_finance --upgrade --no-cache-dir and also upgraded pandas_datareader to be sure.
The "fix-yahoo-finance 0.0.6" worked well, for example BHP.AX:
from pandas_datareader import data as pdr
import fix_yahoo_finance
data = pdr.get_data_yahoo('BHP.AX', start='2017-04-23', end='2017-05-24')
Note that the order of the last 2 data columns are 'Adj Close' and 'Volume' ie. not the previous format. For my purpose, they are reset to the original format:
cols = ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Close']
data.reindex(columns=cols)
I like user3443068 answer as it's simple.
I also would suggest looking at using Google as your source as the yahoo instance is probably going to go through many deprecated versions given where the company's going.
def get_ret(tickers_ls, start_dt, end_dt):
#create dataframe
df_ret=pd.DataFrame()
#get prices for all tickers
for tk in tickers:
p = wb.DataReader(tk, "google", start_date, end_date).Close
df_ret_tmp = p.to_frame()['Close'].reset_index()
df_ret_tmp['Ticker']=tk
## append
df_ret=df_ret.append(df_ret_tmp)
#pivot and get into single dataframe
pivoted = df_ret.pivot(index='Date', columns='Ticker')
pivoted.columns = pivoted.columns.droplevel(0)
return pivoted

Categories

Resources