How to fix this error in Python related to web.DataReader - python

I am having an error related to web.datareader. For some reason I can't figure out what is causing it. Any ideas?
!pip install mplfinance
!pip install seaborn
!pip install matplotlib
import numpy as np
import pandas as pd
import mplfinance as mpf
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
currency = "USD"
metric = "Close"
start = dt.datetime(2018,1,1)
end = dt.datetime.now()
crypto = ['BTC', 'ETH', 'LTC', 'XRP', 'DASH', 'SC']
colnames = []
first = True
for ticker in crypto:
data = web.DataReader(f"{ticker}-{currency}", "yahoo", start, end)
if first:
combined = data[[metric]].copy()
colnames.append(ticker)
combined.columns = colnames
first = False
else:
combined = combined.join(data[metric])
colnames.append(ticker)
combined.columns = colnames
plt.yscale('log') # first show linear
for ticker in crypto:
plt.plot(combined[ticker], label=ticker)
plt.legend(loc="upper right")
plt.show()
# # Correlation Heat Map
print(combined)
combined = combined.pct_change().corr(method='pearson')
sns.heatmap(combined, annot=True, cmap="coolwarm")
plt.show()

You are missing two lines:
!pip install pandas-datareader
import pandas_datareader as web

Related

Trying to plot Earnings and Stock price in the same graph

I'm trying to plot the stock price and the earnings on the graph but for some reason I'm getting this:
Graph1
Please see my code below:
import matplotlib.pyplot as plt
import yfinance as yf
import pandas
import pandas_datareader
import matplotlib
t = yf.Ticker("T")
df1 = t.earnings
df1['Earnings'].plot(label = 'earnings', figsize = (15,7), color='green')
print(df1)
df2 = t.history(start = '2018-01-01', end = '2021-01-01', actions = False, rounding = True)
df2['Close'].plot(label = 'price', figsize = (15,7),color = 'blue')
plt.show()
Could someone help me?
Thanks in advance.
Plotting in pandas is easy to create graphs, but if you try to overlay them with time series data, as in this example, you will encounter problems. There are many approaches, but the method that I find easiest is to convert the data level to the gregorian calendar managed by matplotlib and create the graph. Finally, you can either convert it to your preferred formatting, etc., or use the automatic formatter and locator.
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import yfinance as yf
import pandas as pd
t = yf.Ticker("T")
df1 = t.earnings
df1.index = pd.to_datetime(df1.index, format='%Y')
df1.index = mdates.date2num(df1.index)
ax = df1['Earnings'].plot(label='earnings', figsize=(15, 7), color='green')
df2 = t.history(start='2018-01-01', end='2021-01-01', actions=False, rounding=True)
df2.index = mdates.date2num(df2.index)
df2['Close'].plot(label='price', ax=ax,color='blue', secondary_y=True)
#ax.set_xticklabels([x.strftime('%Y-%m') for x in mdates.num2date(df2.index)][::125])
locator = mdates.AutoDateLocator()
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.show()

Interpreting Multiindex datetime

I have the following code:
import pandas as pd
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import warnings
warnings.filterwarnings("ignore")
start = datetime.date(2020,1,1)
end = datetime.date.today()
stock = 'fb'
data = web.DataReader(stock, 'yahoo', start, end)
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d')
data = data[~data.index.duplicated(keep='first')]
data['year'] = data.index.year
data['month'] = data.index.month
data['week'] = data.index.week
data['day'] = data.index.day
data.set_index('year', append=True, inplace =True)
data.set_index('month',append=True,inplace=True)
data.set_index('week',append=True,inplace=True)
data.set_index('day',append=True,inplace=True)
fig, ax = plt.subplots(dpi=300, figsize =(30,4))
data.plot(y='Close', ax=ax, xlabel= 'Date')
plt.show()
What can I do to interpret the multiindex dates as the x axis in more readable year and month format? Such as in a format like strftime('%y -%m'). A similar question was asked here: Renaming months from number to name in pandas
But I am unable to see how I can use this to rename the x axis. Any help would be appreciated.
You can use the dates from matplotlib. See the following link for more details:
https://matplotlib.org/stable/api/dates_api.html#matplotlib.dates.ConciseDateFormatter
Here is the modified code:
import pandas as pd
from pandas import DataFrame as df
import matplotlib
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime
import warnings
warnings.filterwarnings("ignore")
from matplotlib import dates as mdates
start = datetime.date(2020,1,1)
end = datetime.date.today()
stock = 'fb'
data = web.DataReader(stock, 'yahoo', start, end)
data.index = pd.to_datetime(data.index, format ='%Y-%m-%d')
data = data[~data.index.duplicated(keep='first')]
data['year'] = data.index.year
data['month'] = data.index.month
data['week'] = data.index.week
data['day'] = data.index.day
data.set_index('year', append=True, inplace =True)
data.set_index('month',append=True,inplace=True)
data.set_index('week',append=True,inplace=True)
data.set_index('day',append=True,inplace=True)
fig, ax = plt.subplots(dpi=300, figsize =(15,4))
plt.plot(data.index.get_level_values('Date'), data['Close'])
#--------------------------------------
#Feel free to try different options
#--------------------------------------
#locator = mdates.AutoDateLocator()
locator = mdates.MonthLocator()
formatter = mdates.ConciseDateFormatter(locator)
ax.xaxis.set_major_locator(locator)
ax.xaxis.set_major_formatter(formatter)
plt.show()
Here is the
output.

how to get dates from yahoo finance

I have a problem with getting the dates from yfinance into my matplotlib graph can somebody help/show me how to get the dates from yfinance into my matplotlib graph
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
# function getting data for the last x years with x weeks space from checking data and specific
observation.
def stock_data(ticker, period, interval, observation):
ticker = yf.Ticker(ticker)
ticker_history = ticker.history(period, interval)
print(ticker_history[observation]) #is here to show you the data that we get
date = pd.date_range(ticker_history[period])
x = date
y = np.array(ticker_history[observation])
plt.style.use('dark_background')
plt.plot(x,y)
plt.ylabel('Price($)')
plt.xlabel('Date', rotation=0)
plt.show()
stock_data('GOOGL', '6mo', '1wk', 'Open')
Tested ✅
🔰You could extract the date from ticker_history[observation]
🔰 It is a Pandas Series object, so here's how I'd do it:
import numpy as np
import yfinance as yf
import matplotlib.pyplot as plt
import pandas as pd
# function getting data for the last x years with x weeks space
# from checking data and specific observation.
def stock_data(ticker, period, interval, observation):
ticker = yf.Ticker(ticker)
ticker_history = ticker.history(period, interval)
print((ticker_history[observation]))
sf = ticker_history[observation]
df = pd.DataFrame({'Date':sf.index, 'Values':sf.values})
x = df['Date'].tolist()
y = df['Values'].tolist()
plt.style.use('dark_background')
plt.plot(x,y)
plt.ylabel('Price($)')
plt.xlabel('Date', rotation=0)
plt.show()
if __name__ == '__main__':
stock_data('GOOGL', '6mo', '1wk', 'Open')

Why does code stops running in halfway in Python

Here is my python sample for scraping some finance data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
import datetime as dt
from mplfinance.original_flavor import candlestick_ohlc
import warnings
warnings.filterwarnings("ignore")
# fix_yahoo_finance is used to fetch data
import yfinance as yf
yf.pdr_override()
# input
symbol = 'AAPL'
start = '2018-01-01'
end = '2020-12-24'
# Read data
df = yf.download(symbol,start,end)
# View Columns
df.head()
df['Absolute_Return'] = 100 * (df['Adj Close'] - df['Adj Close'].shift(1))/df['Adj Close'].shift(1)
df.head(20)
fig = plt.figure(figsize=(14,10))
ax1 = plt.subplot(2, 1, 1)
ax1.plot(df['Adj Close'])
ax1.set_title('Stock '+ symbol +' Closing Price')
ax1.set_ylabel('Price')
ax2 = plt.subplot(2, 1, 2)
ax2.plot(df['Absolute_Return'] , label='Absolute Return', color='red')
#ax2.axhline(y=0, color='blue', linestyle='--')
#ax2.axhline(y=0.5, color='darkblue')
#ax2.axhline(y=-0.5, color='darkblue')
ax2.grid()
ax2.set_ylabel('Absolute Return')
ax2.set_xlabel('Date')
ax2.legend(loc='best')
Problem is, if I put them in Jupyter notebook and run block by block, everything works fine. But when I put them in a normal .py file then run it, it stops in halfway, right at
df = yf.download(symbol,start,end)
Anyone please explain that for me?

Python: Trouble making separate plots using plot_drawdown_underwater from pyfolio and get_data_yahoo from pandas-datareader to plot stock drawdown

I have this code:
from pandas_datareader import data as pdr
import pyfolio as pf
myStartDate = "2019-1-1" # (Format: Year-Month-Day)
myEndDate = "2020-11-11" # (Format: Year-Month-Day)
myTickers = ["AAPL", "MSFT"]
myData = pdr.get_data_yahoo(myTickers, myStartDate, myEndDate)
myDailyAdjustedClose = myData[['Adj Close']]
returns = myDailyAdjustedClose.pct_change()
pf.plot_drawdown_underwater(returns)
The problem is that both Apple and Microsoft are plotted on the same plot. Instead, I want 2 separate plots.
Instead of "pf.plot_drawdown_underwater(returns)" I also tried:
for x in range(returns.shape[1]):
columnData = returns.iloc[:, x]
pf.plot_drawdown_underwater(columnData)
But I still got the same result.
This works:
from pandas_datareader import data as pdr
import pyfolio as pf
import matplotlib.pyplot as plt
myStartDate = "2019-1-1" # (Format: Year-Month-Day)
myEndDate = "2020-11-11" # (Format: Year-Month-Day)
myTickers = ["AAPL", "MSFT"]
myData = pdr.get_data_yahoo(myTickers, myStartDate, myEndDate)
myDailyAdjustedClose = myData[['Adj Close']]
returns = myDailyAdjustedClose.pct_change()
returnsColumnCount = returns.shape[1]
fig, ax = plt.subplots(returnsColumnCount, 1, figsize=(10, 10))
fig.tight_layout(pad=5)
for x in range(returnsColumnCount):
columnData = returns.iloc[:, x]
pf.plot_drawdown_underwater(columnData, ax=ax[x])
plt.show();

Categories