-
Notifications
You must be signed in to change notification settings - Fork 0
/
prices from yahoo.py
29 lines (23 loc) · 998 Bytes
/
prices from yahoo.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
import yfinance as yf
import pandas as pd
def prices_yahoo(y, s=None, e=None):
p = pd.DataFrame() # Create an empty DataFrame
# Loop for data extraction & Set up statements for start and end dates
for ticker in y:
if s is None and e is None:
# When neither start date nor end date is defined
data = yf.download(ticker)
elif e is None:
data = yf.download(ticker, start=s) # Only start date is defined
elif s is None:
data = yf.download(ticker, end=e) # When only end date is defined
else:
# When both start date and end date are defined
data = yf.download(ticker, start=s, end=e)
# Extract the Adjusted Close prices and add to the DataFrame
if not data.empty:
p[ticker] = data['Adj Close']
p = p.dropna() # Drop rows with NA values
return p
result = prices_yahoo(y=["UNM", "AIG", "OMF", "MET", "HIG"], s="2022-01-01")
print(result) # Test