#!/usr/bin/env python # coding: utf-8 # Open In Colab # In[4]: get_ipython().system('pip install yfinance') # In[5]: # Importing the modules import yfinance as yf import pandas as pd import numpy as np import math import matplotlib.pyplot as plt # In[8]: dataset = yf.download("TSLA", start="2017-01-01", end="2022-05-19") dataset # In[9]: dataset.info() # In[11]: (dataset['Adj Close'] / dataset['Adj Close'].iloc[0] * 100).plot(figsize = (15, 6)); plt.show() # #Let's do the multiple stocks. # In[17]: tickers = ['SBUX', 'WMT', 'AMZN', 'HD'] mydata = pd.DataFrame() for t in tickers: mydata[t] = yf.download(t, start="2000-01-01", end="2022-05-31")['Adj Close'] # In[18]: mydata.head() # In[ ]: # In[19]: mydata.plot(figsize=(10, 12), subplots=True); # In[20]: mydata.describe().round(2) # In[27]: mydata.pct_change().describe().round(4) # In[28]: mydata.pct_change().mean().plot(kind='bar', figsize=(10, 6)); # In[29]: mydata.pct_change().std().plot(kind='bar', figsize=(10, 6)); # In[33]: ret = np.log(mydata / mydata.shift(1)) ret.describe() # In[34]: ret.mean().plot(kind='bar', figsize=(10, 6)); # In[35]: ret.std().plot(kind='bar', figsize=(10, 6)); # In[36]: ret.cumsum().apply(np.exp).plot(figsize=(10, 6)); # In[37]: ret.dropna(inplace=True) # In[39]: ret.plot(subplots=True, figsize=(10, 12)); # In[41]: ret.plot(subplots=False, figsize=(15, 10)); # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # In[ ]: # ### Oops! AMZN was not a public firm in 1995. Let's choose another one instead. # In[42]: tickers = ['SBUX', 'WMT', 'ORCL', 'HD'] mydata = pd.DataFrame() for t in tickers: mydata[t] = yf.download(t, start="1995-01-01", end="2020-12-31")['Adj Close'] # In[43]: mydata # ### What if we invest $1 in 1995? # In[44]: (mydata / mydata.iloc[0] * 1) # ### What if we invest $100? # In[45]: (mydata / mydata.iloc[0] * 100).plot(figsize = (15, 6)); plt.show() # In[ ]: # In[ ]: # In[46]: tickers = ['SBUX', 'WMT', 'ORCL', 'HD'] mydata = pd.DataFrame() for t in tickers: mydata[t] = yf.download(t, start="2000-01-01", end="2020-12-31", interval="1wk")['Adj Close'] # In[47]: mydata["HD"].pct_change().plot.hist(alpha=0.9, bins=50) # In[ ]: # In[48]: tickers = ['SBUX', 'WMT', 'ORCL', 'HD'] mydata = pd.DataFrame() for t in tickers: mydata[t] = yf.download(t, start="2000-01-01", end="2020-12-31", interval="1d")['Adj Close'] # In[49]: mydata # ### resmaple of Pandas ### # https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases # In[50]: mydata_m=mydata.resample('BM').last() mydata_m # In[51]: returns=mydata_m.pct_change() # In[52]: returns # In[53]: returns2=returns.dropna() # In[54]: returns2 # In[55]: sbux_stock = returns2["SBUX"] # In[56]: sbux_stock # In[57]: plt.hist(sbux_stock,bins=25) # In[58]: p=np.percentile(sbux_stock, 50) p # In[59]: sbux_stock.median() # In[60]: p=np.percentile(sbux_stock, 95) p # In[ ]: