forked from ranaroussi/quantstats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrp_stocks2.py
33 lines (25 loc) · 918 Bytes
/
rp_stocks2.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
import pandas as pd
import yfinance as yf
import pandas_ta as ta
# List of tickers
tickers = ['GOOGL', 'BLK', 'NVO', 'HESAY']
# Download data for multiple tickers
df = yf.download(tickers=tickers, period='1y', group_by='ticker')
# Check the column structure
print("Columns in the downloaded DataFrame:", df.columns)
# Create an empty DataFrame to store RSI values
rsi_df = pd.DataFrame()
# Iterate through each ticker to calculate RSI
for ticker in tickers:
try:
# Access the Adjusted Close column for the specific ticker
adj_close = df.loc[:, (ticker, 'Adj Close')]
# Calculate RSI
rsi_values = ta.rsi(close=adj_close, length=14)
# Add the RSI values to the DataFrame
rsi_df[ticker] = rsi_values
except KeyError as e:
print(f"Data for {ticker} is missing 'Adj Close' column. Skipping.")
continue
# Display the RSI DataFrame
print(rsi_df)