Last active
September 24, 2017 15:20
-
-
Save PyDataBlog/3badd3f8d7faab12e8fbff3dfd60a9a5 to your computer and use it in GitHub Desktop.
A script to help visualize the volatility of assets with a histogram
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| from pandas_datareader import data as web | |
| import matplotlib.pyplot as plt | |
| from datetime import datetime | |
| start = datetime(2016,1,1) | |
| end = datetime(2017,1,1) | |
| assets = ['AAPL', 'FB', 'TSLA'] | |
| df = pd.DataFrame() | |
| for stock in assets: | |
| df[stock] = web.DataReader(stock, data_source='yahoo', start=start, end=end)['Adj Close'] | |
| asset_returns_daily = df.pct_change() | |
| asset_volatility_daily = asset_returns_daily.std() | |
| asset_returns_daily.plot.hist(bins=50, figsize=(10,6)); | |
| plt.xlabel('Daily Returns') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment