Sorry if this is obvious, but I'm using yfinance to create a stock analysis program, but I can't get anything in this month, it's the start of the month (as of now August 3rd) but my program can't fetch data after July 31st
Here's my program recording a 5 day window:
from pandas_datareader import data as pdr
import yfinance as yf
import datetime
from dateutil.relativedelta import *
import calendar
yf.pdr_override()
today =datetime.date.today()
yesterday = today-datetime.timedelta(5)
a= pdr.get_data_yahoo('AAPL', start=yesterday,end=today)
print(a)
and the output is
Open High Low Close Adj Close Volume
Date
2020-07-31 411.540009 425.660004 403.299988 425.040009 425.040009 93584200
Specify the correct date range
Since today is a Monday, the data for today was probably not available yet.
from datetime import date, timedelta
import yfinance as yf
from pandas_datareader import data as pdr
start = date(2020, 7, 1)
end = date(2020, 7, 31)
a = yf.download('AAPL', start=start, end=end)
# also works, but you don't need both yf and pdr
a = pdr.data.get_data_yahoo('AAPL', start=start, end=end)
# display(a.head())
Open High Low Close Adj Close Volume
Date
2020-07-01 365.119995 367.359985 363.910004 364.109985 364.109985 27684300
2020-07-02 367.850006 370.470001 363.640015 364.109985 364.109985 28510400
2020-07-06 370.000000 375.779999 369.869995 373.850006 373.850006 29663900
2020-07-07 375.410004 378.619995 372.230011 372.690002 372.690002 28106100
2020-07-08 376.720001 381.500000 376.359985 381.369995 381.369995 29273000
With your date ranges
today = date.today()
yesterday = today - timedelta(5)
a = pdr.get_data_yahoo('AAPL', start=yesterday, end=today)
High Low Open Close Volume Adj Close
Date
2020-07-29 380.920013 374.850006 375.000000 380.160004 22582300 380.160004
2020-07-30 385.190002 375.070007 376.750000 384.760010 39532500 384.760010
2020-07-31 425.660004 403.299988 411.540009 425.040009 93584200 425.040009
2020-08-03 446.545685 431.579987 432.799988 435.750000 76237006 435.750000
Related
I use this code to get BTC value but the date is starting previous day which my selected.
INPUT:
tickers=['BTC-USD'] # Name of asset
tarih="02-06-2021"
tarih2="05-06-2021"
start=dt.datetime.strptime(tarih, '%d-%m-%Y')
end=dt.datetime.strptime(tarih2, '%d-%m-%Y')
returns=pd.DataFrame()
liste=[]
for ticker in tickers:
data=web.DataReader(ticker,'yahoo',start,end)
data=pd.DataFrame(data)
data[ticker]=data['Adj Close'] #can work with change percentage in order to get more accurate data
if returns.empty:
returns=data[[ticker]]
else:
returns = returns.join(data[[ticker]],how='outer')#add right column
for dt in daterange(start, end):
dates=dt.strftime("%d-%m-%Y")
with open("fng_value.txt", "r") as filestream:
for line in filestream:
date = line.split(",")[0]
if dates == date:
fng_value=line.split(",")[1]
liste.append(fng_value)
print(returns.head(25))
OUTPUT:
BTC-USD
Date
2021-06-01 37575.179688
2021-06-02 39208.765625
2021-06-03 36894.406250
2021-06-04 35551.957031
2021-06-05 35862.378906
DataReader accepts a start parameter as a string, date, or datetime. Apparently, sometimes using start date (e.g. 2021-06-02) retrieves data starting from the previous day on 2021-06-01. Try to use a datetime with timezone and an hour late in the day to hack the date if it doesn't return what you expect it to.
See if this works:
import pandas_datareader.data as web
import pandas as pd
from pytz import timezone
from datetime import datetime, date
tarih = "02-06-2021"
tarih2 = "05-06-2021"
# start/end can be date, datetime, or string
#start = date(2021, 6, 2)
#end = date(2021, 6, 5)
#start = 'JUN-02-2021'
#end = 'JUN-05-2021'
start = datetime.strptime(tarih, '%d-%m-%Y').replace(hour=23, tzinfo=timezone('EST'))
end = datetime.strptime(tarih2, '%d-%m-%Y').replace(tzinfo=timezone('EST'))
tickers=['BTC-USD'] # Name of asset
for ticker in tickers:
data = web.DataReader(ticker, 'yahoo', start, end)
data = pd.DataFrame(data)
print(data)
This returns the data from 6/2 to 6/5.
I try to write a small "program" that gets the stock prices from Quandl.
The user should be able to enter a startring date (yyyymmdd) and the the program should then get the data from that date and forward to current date.
import datetime as dt
import pandas_datareader as web
ticker = 'TSLA'
# Time variables for current and starting date as integers
current_year = int(dt.datetime.now().strftime("%Y"))
current_month = int(dt.datetime.now().strftime("%m"))
current_day = int(dt.datetime.now().strftime("%d"))
start_date = str(input("Starting date (yyyymmdd): "))
start_year = int(start_date[:4])
start_month = int(start_date[5:6])
start_day = int(start_date[7:8])
# Defines the total period
start = dt.datetime(start_year, start_month, start_day) # Starting date, yyyy mm dd
end = dt.datetime(current_year, current_month, current_day) # End date, yyyy mm dd
# Gets the data from start to end date for the stock definded as 'Ticker'
df = web.get_data_quandl(ticker, start, end)
print(start)
print(end)
print(df)
My problem is that I cannot seem to get any data newer than 2018-03-27.
The WIKI database is indeed no longer updated or supported starting from March 27, 2018. The closest alternative on Quandl is EOD, which is a value-priced end-of-day US stock price data feed.
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())
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
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