I'm looking to create a program that will take a user selected stock and
return information using the web.DataReader function in Pandas. Any suggestions or alternative solutions would be appreciated.
import pandas as pd
import pandas.io.data as web # Package and modules for importing data; this code may change depending on pandas version
import datetime
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
apple = web.DataReader(input(""), "yahoo", start, end)
type(apple)
apple.head()
Results with
input("") in web.DataReader statement
OSError: after 3 tries, Yahoo! did not return a 200 for url 'http://ichart.finance.yahoo.com/table.csv?s=appl&a=0&b=1&c=2016&d=11&e=8&f=2016&g=d&ignore=.csv'
Here's how you should really do it now that the data readers have been moved into their own package. You also need to provide a valid ticker, in case of apple it's AAPL and not appl
import pandas as pd
from pandas_datareader import data, wb
import datetime
start = datetime.datetime(2016,1,1)
end = datetime.date.today()
apple = data.DataReader(input("Please Input the name of the Ticker:\n"), "yahoo", start, end)
type(apple)
apple.head()
Please Input the name of the Ticker:
AAPL
Open High Low Close Volume Adj Close
Date
2016-01-04 102.610001 105.370003 102.000000 105.349998 67649400 103.057063
2016-01-05 105.750000 105.849998 102.410004 102.709999 55791000 100.474523
2016-01-06 100.559998 102.370003 99.870003 100.699997 68457400 98.508268
2016-01-07 98.680000 100.129997 96.430000 96.449997 81094400 94.350769
2016-01-08 98.550003 99.110001 96.760002 96.959999 70798000 94.849671
Related
I have searched for this topic and I found some packages that are useful. All what I am trying to get is the last price of any specific ticker such as "MSFT"
Here's a code that I found and it is good
import pandas_datareader as pdr
from datetime import datetime
ibm = pdr.get_data_yahoo(symbols='MSFT', start=datetime(2021, 3, 1), end=datetime(2021, 3, 12))
print(ibm['Adj Close'])
This works for range of dates. How can I get the last price only without hard-coding the start date or end date?
Just use tail keyword.
from datetime import datetime, date
ibm = pdr.get_data_yahoo(symbols='MSFT', start = date.today(), end = date.today())
print(ibm['Adj Close'].tail(1))
I have this code that works when manually entering a start date and an end date:
import datetime as dt
from datetime import date
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader as web
today = date.today()
style.use("ggplot")
start = dt.datetime(2020,1,1)
end = today
df = web.get_data_yahoo("TSLA", start, end)
df["Adj Close"].plot()
plt.xlabel("Date")
plt.ylabel("Price $")
plt.title("TSLA Stock Price ", )
plt.show()
It works when plotting the graph, but I want the start and end date to show up on the title as well. Is there anyway that I can import the "start" and "end" variables after "TSLA Stock Price "?
I think you can just use an f string in the plt.title().
plt.title(f"TSLA Stock Price {start.strftime('%x')} to {end.strftime('%x')}")
This code results in the title being formated like mm/dd/yy to mm/dd/yy
Things used to work great until several days ago. Now when I run the following:
from pandas_datareader import data
symbol = 'AMZN'
data_source='google'
start_date = '2010-01-01'
end_date = '2016-01-01'
df = data.DataReader(symbol, data_source, start_date, end_date)
I get only the most recent data of ONE year shown below, as if the start_data and end_data did not seem to matter. Change them to different dates yielded the same results below. Does anyone know why?
Results:
df.head()
Open High Low Close Volume
Date
2016-09-21 129.13 130.00 128.39 129.94 14068336
2016-09-22 130.50 130.73 129.56 130.08 15538307
2016-09-23 127.56 128.60 127.30 127.96 28326266
2016-09-26 127.37 128.16 126.80 127.31 15064940
2016-09-27 127.61 129.01 127.43 128.69 15637111
Use fix-yahoo-finance and then use yahoo rather than Google as your source. It looks like Google has been locking down a lot of its data lately.
First you'll need to install fix-yahoo-finance. Just use pip install fix-yahoo-finance.
Then use get_data_yahoo:
from pandas_datareader import data
import fix_yahoo_finance as yf
yf.pdr_override()
symbol = 'AMZN'
data_source='google'
start_date = '2010-01-01'
end_date = '2016-01-01'
df = data.get_data_yahoo(symbol, start_date, end_date)
df.head()
Open High Low Close Adj Close Volume
Date
2010-01-04 136.25000 136.61000 133.14000 133.89999 133.89999 7599900
2010-01-05 133.42999 135.48000 131.81000 134.69000 134.69000 8851900
2010-01-06 134.60001 134.73000 131.64999 132.25000 132.25000 7178800
2010-01-07 132.01000 132.32001 128.80000 130.00000 130.00000 11030200
2010-01-08 130.56000 133.67999 129.03000 133.52000 133.52000 9830500
Just replace google with yahoo. There are problem with google source right now. https://github.com/pydata/pandas-datareader/issues/394
from pandas_datareader import data
symbol = 'AMZN'
data_source='yahoo'
start_date = '2010-01-01'
end_date = '2016-01-01'
df = data.DataReader(symbol, data_source, start_date, end_date)
Yahoo working as of January 01, 2020:
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2015, 1, 1)
end = datetime.datetime(2018, 2, 8)
df = web.DataReader('TSLA', 'yahoo', start, end)
print(df.head())
Revised question with appropriate MCVE:
As part of a script I'm writing I need to have a loop that contains a different pair of dates during each iteration, these dates are the first and last available stock trading dates of each month. I have managed to find a calendar with the available dates in an index however despite my research I am not sure how to select the correct dates from this index so that they can be used in the DateTime variables start and end.
Here is as far as my research has got me and I will continue to search for and build my own solution which I will post if I manage to find one:
from __future__ import division
import numpy as np
import pandas as pd
import datetime
import pandas_market_calendars as mcal
from pandas_datareader import data as web
from datetime import date
'''
Full date range:
'''
startrange = datetime.date(2016, 1, 1)
endrange = datetime.date(2016, 12, 31)
'''
Tradable dates in the year:
'''
nyse = mcal.get_calendar('NYSE')
available = nyse.valid_days(start_date='2016-01-01', end_date='2016-12-31')
'''
The loop that needs to take first and last trading date of each month:
'''
dict1 = {}
for i in available:
start = datetime.date('''first available trade day of the month''')
end = datetime.date('''last available trade day of the month''')
diffdays = ((end - start).days)/365
dict1 [i] = diffdays
print (dict1)
That is probably because 1 January 2016 was not a trading day. To check if I am right, try giving it the date 4 January 2016, which was the following Monday. If that works, then you will have to be more sophisticated about the dates you ask for.
Look in the documentaion for dm.BbgDataManager(). It is possible that you can ask it what dates are available.
Python beginner here.
What I've done so far:
Imported price data from Yahoo Finance from a list of stocks.
Between the stocks (every combination), computed the 20 day rolling correlation into a dataframe.
I would like to:
1) Calculate the 200 day simple moving average for each of the 20 day rolling correlations.
2) Report the 200 day moving average results in a matrix.
How to do this in python/pandas? Thanks, this would help me out a ton!
Here is what I have so far...
import pandas as pd
from pandas import DataFrame
import datetime
import pandas.io.data as web
from pandas.io.data import DataReader
stocks = ['spy', 'gld', 'uso']
start = datetime.datetime(2014,1,1)
end = datetime.datetime(2015,1,1)
f = web.DataReader(stocks, 'yahoo', start, end)
adj_close_df = f['Adj Close']
correls = pd.rolling_corr(adj_close_df, 20)
means = pd.rolling_mean(correls, 200) #<---- I get an error message here!
This is a start which answers questions 1-3 (you should only have one question per post).
import pandas.io.data as web
import datetime as dt
import pandas as pd
end_date = dt.datetime.now().date()
start_date = end_date - pd.DateOffset(years=5)
symbols = ['AAPL', 'IBM', 'GM']
prices = web.get_data_yahoo(symbols=symbols, start=start_date, end=end_date)['Adj Close']
returns = prices.pct_change()
rolling_corr = pd.rolling_corr_pairwise(returns, window=20)
Getting the rolling mean of the rolling correlation is relatively simple for a single stock against all others. For example:
pd.rolling_mean(rolling_corr.major_xs('AAPL').T, 200).tail()
Out[34]:
AAPL GM IBM
Date
2015-05-08 1 0.313391 0.324728
2015-05-11 1 0.315561 0.327537
2015-05-12 1 0.317844 0.330375
2015-05-13 1 0.320137 0.333189
2015-05-14 1 0.322119 0.335659
To view the correlation matrix for the most recent 200 day window:
>>> rolling_corr.iloc[-200:].mean(axis=0)
AAPL GM IBM
AAPL 1.000000 0.322119 0.335659
GM 0.322119 1.000000 0.383672
IBM 0.335659 0.383672 1.000000