-
Notifications
You must be signed in to change notification settings - Fork 0
/
yfinance.py
92 lines (62 loc) · 2 KB
/
yfinance.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 4 15:51:43 2023
@author: ajdam
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf
#%% introduction to yfinance, dictionaries
# let's download prices of Microsoft from 1st January 2020 - 1st September 2023
msft_df = yf.download('MSFT', start='2020-01-01', end='2023-09-01')
msft_df.tail()
# plot adjusted close price
plt.figure(dpi=1080)
plt.figure(figsize=((12,8)))
msft_df['Adj Close'].plot(title='Microsoft adjusted close price',
color='steelblue', lw=1.2)
# repeat for Apple
aapl_df = yf.download('AAPL', start='2020-01-01', end='2023-09-01')
# create a dictionary
stock_data = {'MSFT':msft_df, 'AAPL': aapl_df}
# access an element of a dictionary by its key
stock_data['MSFT']
#%% download prices for multiple stocks from yahoo finance
# create an empty dictionary
stock_data = {}
# all the firms for which we want data
tickers = ['MSFT', 'AAPL', 'TSLA', 'NVDA', 'AMZN']
start_date = '2020-01-01'
end_date = '2023-09-01'
for ticker in tickers:
stock_df = yf.download(ticker, start_date, end_date)
stock_data[ticker] = stock_df
#%% plot prices
plt.figure(dpi=1080)
plt.figure(figsize=(12,8))
for stock, stock_df in stock_data.items():
stock_df['Adj Close'].plot(label=f'{stock} Adj Close')
plt.title('Adjusted close prices for multiple stocks')
plt.xlabel('Date')
plt.ylabel('Prices')
plt.legend()
plt.show()
#%% plot volumes
plt.figure(dpi=1080)
plt.figure(figsize=(12,8))
for stock, stock_df in stock_data.items():
stock_df['Volume'].plot(label=f'{stock} Volume')
plt.title('Volume for multiple stocks')
plt.xlabel('Date')
plt.ylabel('Volume')
plt.legend()
plt.show()
#%% download SP500
sp500_df = yf.download('SPY', start_date, end_date)
stock_data['SPY'] = sp500_df
#%% calculate average log returns
returns = pd.DataFrame()
for stock, stock_df in stock_data.items():
returns[stock] = np.log(stock_df['Adj Close']/stock_df['Adj Close'].shift(1)).dropna()
returns.mean()