Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yfinance as yf
|
| 4 |
+
import talib
|
| 5 |
+
|
| 6 |
+
# Streamlit Title
|
| 7 |
+
st.title("NIFTY 500 RSI Divergence Scanner")
|
| 8 |
+
|
| 9 |
+
# Load NIFTY 500 Stock List
|
| 10 |
+
stock_list = pd.read_csv('ind_nifty500list.csv')
|
| 11 |
+
tickers = stock_list['Symbol'].tolist()
|
| 12 |
+
|
| 13 |
+
# Multi-select Dropdown for Stock Selection
|
| 14 |
+
selected_stocks = st.multiselect('Select Stocks for Screening', tickers, default=tickers[:10])
|
| 15 |
+
|
| 16 |
+
# RSI Parameters
|
| 17 |
+
rsi_period = st.sidebar.slider('RSI Period', min_value=7, max_value=28, value=14, step=1)
|
| 18 |
+
rsi_lower_bound = st.sidebar.slider('RSI Lower Bound', min_value=10, max_value=50, value=30, step=5)
|
| 19 |
+
rsi_upper_bound = st.sidebar.slider('RSI Upper Bound', min_value=50, max_value=90, value=70, step=5)
|
| 20 |
+
|
| 21 |
+
# Divergence Results
|
| 22 |
+
divergence_data = []
|
| 23 |
+
|
| 24 |
+
# Loop through selected stocks
|
| 25 |
+
for ticker in selected_stocks:
|
| 26 |
+
try:
|
| 27 |
+
# Fetch historical data for the selected stock
|
| 28 |
+
data = yf.download(ticker + ".NS", period="6mo", interval="1d")
|
| 29 |
+
data['RSI'] = talib.RSI(data['Close'], timeperiod=rsi_period)
|
| 30 |
+
|
| 31 |
+
# Identify RSI Divergence
|
| 32 |
+
data['Price Change'] = data['Close'].diff()
|
| 33 |
+
data['RSI Change'] = data['RSI'].diff()
|
| 34 |
+
|
| 35 |
+
# Divergence Condition: Price and RSI moving in opposite directions
|
| 36 |
+
data['Divergence'] = (data['Price Change'] * data['RSI Change'] < 0).astype(int)
|
| 37 |
+
|
| 38 |
+
# Filter for RSI levels and Divergence
|
| 39 |
+
divergence_days = data[(data['RSI'] < rsi_lower_bound) | (data['RSI'] > rsi_upper_bound) & (data['Divergence'] == 1)]
|
| 40 |
+
|
| 41 |
+
if not divergence_days.empty:
|
| 42 |
+
divergence_data.append({'Ticker': ticker, 'Divergence Dates': list(divergence_days.index)})
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
st.write(f"Error processing {ticker}: {e}")
|
| 46 |
+
|
| 47 |
+
# Display Results
|
| 48 |
+
if divergence_data:
|
| 49 |
+
st.write("RSI Divergence Found in Following Stocks:")
|
| 50 |
+
for item in divergence_data:
|
| 51 |
+
st.write(f"**{item['Ticker']}** - Divergence Dates: {item['Divergence Dates']}")
|
| 52 |
+
else:
|
| 53 |
+
st.write("No Divergences Found for the Selected Stocks.")
|
| 54 |
+
|
| 55 |
+
# Show Raw Data Option
|
| 56 |
+
if st.sidebar.checkbox('Show Stock Data'):
|
| 57 |
+
for ticker in selected_stocks:
|
| 58 |
+
st.write(f"Stock Data for: {ticker}")
|
| 59 |
+
st.write(data)
|