Spaces:
Sleeping
Sleeping
Upload srp.py
Browse files
srp.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import pandas as pd
|
4 |
+
from datetime import datetime, timedelta, time
|
5 |
+
|
6 |
+
# Load ticker symbols from the CSV file
|
7 |
+
@st.cache_data # Updated from st.cache to remove deprecation warning
|
8 |
+
def load_ticker_list(file_path):
|
9 |
+
try:
|
10 |
+
stock_df = pd.read_csv(file_path)
|
11 |
+
return stock_df['Symbol'].tolist()
|
12 |
+
except Exception as e:
|
13 |
+
st.error(f"Error loading ticker list: {str(e)}")
|
14 |
+
return []
|
15 |
+
|
16 |
+
# Function to check if the market is open
|
17 |
+
def is_market_open():
|
18 |
+
now = datetime.now().time()
|
19 |
+
market_open_time = time(9, 15) # NSE opens at 9:15 AM
|
20 |
+
market_close_time = time(15, 30) # NSE closes at 3:30 PM
|
21 |
+
return market_open_time <= now <= market_close_time
|
22 |
+
|
23 |
+
# Function to calculate pivot point and support/resistance levels
|
24 |
+
def calculate_pivot_levels(stock_data):
|
25 |
+
try:
|
26 |
+
# Fixed the float() deprecation warnings by using .iloc[0]
|
27 |
+
high = stock_data['High'].iloc[-1]
|
28 |
+
low = stock_data['Low'].iloc[-1]
|
29 |
+
close = stock_data['Close'].iloc[-1]
|
30 |
+
|
31 |
+
# Calculate pivot point
|
32 |
+
pivot = (high + low + close) / 3
|
33 |
+
|
34 |
+
# Calculate support and resistance levels
|
35 |
+
resistance1 = (2 * pivot) - low
|
36 |
+
support1 = (2 * pivot) - high
|
37 |
+
resistance2 = pivot + (high - low)
|
38 |
+
support2 = pivot - (high - low)
|
39 |
+
resistance3 = high + 2 * (pivot - low)
|
40 |
+
support3 = low - 2 * (high - pivot)
|
41 |
+
|
42 |
+
levels = {
|
43 |
+
'Pivot Point': pivot,
|
44 |
+
'Resistance 1': resistance1,
|
45 |
+
'Support 1': support1,
|
46 |
+
'Resistance 2': resistance2,
|
47 |
+
'Support 2': support2,
|
48 |
+
'Resistance 3': resistance3,
|
49 |
+
'Support 3': support3
|
50 |
+
}
|
51 |
+
return levels
|
52 |
+
except Exception as e:
|
53 |
+
st.error(f"Error calculating pivot levels: {str(e)}")
|
54 |
+
return None
|
55 |
+
|
56 |
+
# Function to fetch stock data with proper error handling
|
57 |
+
@st.cache_data(ttl=300) # Cache for 5 minutes
|
58 |
+
def get_stock_data(symbol):
|
59 |
+
try:
|
60 |
+
# Add .NS suffix for NSE stocks
|
61 |
+
nse_symbol = f"{symbol}.NS"
|
62 |
+
|
63 |
+
# Try to get today's data
|
64 |
+
today = datetime.today().date()
|
65 |
+
stock = yf.download(
|
66 |
+
nse_symbol,
|
67 |
+
start=today - timedelta(days=5), # Get more days of data
|
68 |
+
end=today + timedelta(days=1),
|
69 |
+
interval='1d',
|
70 |
+
progress=False # Disable progress bar
|
71 |
+
)
|
72 |
+
|
73 |
+
if not stock.empty:
|
74 |
+
return stock.tail(1) # Return only the latest day's data
|
75 |
+
else:
|
76 |
+
st.warning("No data available for the selected stock.")
|
77 |
+
return pd.DataFrame()
|
78 |
+
|
79 |
+
except Exception as e:
|
80 |
+
st.error(f"Error fetching data for {symbol}: {str(e)}")
|
81 |
+
return pd.DataFrame()
|
82 |
+
|
83 |
+
# Streamlit UI
|
84 |
+
def main():
|
85 |
+
st.title("Support and Resistance Level Calculator for NIFTY 500 Stocks")
|
86 |
+
|
87 |
+
# Load ticker symbols
|
88 |
+
file_path = "G:/RSI/ind_nifty500list (1) (1).csv"
|
89 |
+
ticker_list = load_ticker_list(file_path)
|
90 |
+
|
91 |
+
if not ticker_list:
|
92 |
+
st.error("Failed to load ticker list. Please check the file path.")
|
93 |
+
return
|
94 |
+
|
95 |
+
# Select a ticker from the list
|
96 |
+
symbol = st.selectbox("Select a Stock Symbol", ticker_list)
|
97 |
+
|
98 |
+
# Check market status
|
99 |
+
if is_market_open():
|
100 |
+
st.warning("Market is currently open. Data might not be finalized until after 3:30 PM.")
|
101 |
+
|
102 |
+
if symbol:
|
103 |
+
# Add a refresh button
|
104 |
+
col1, col2 = st.columns([1, 4])
|
105 |
+
with col1:
|
106 |
+
refresh = st.button("🔄 Refresh")
|
107 |
+
|
108 |
+
# Display loading spinner
|
109 |
+
with st.spinner('Fetching stock data...'):
|
110 |
+
if refresh:
|
111 |
+
# Clear cache for this stock
|
112 |
+
get_stock_data.clear()
|
113 |
+
|
114 |
+
stock_data = get_stock_data(symbol)
|
115 |
+
|
116 |
+
if not stock_data.empty:
|
117 |
+
# Calculate and display levels
|
118 |
+
levels = calculate_pivot_levels(stock_data)
|
119 |
+
|
120 |
+
if levels:
|
121 |
+
# Fixed: Get current price as a scalar value
|
122 |
+
current_price = float(stock_data['Close'].iloc[-1])
|
123 |
+
st.metric("Current Price", f"₹{current_price:.2f}")
|
124 |
+
|
125 |
+
# Create three columns for better organization
|
126 |
+
st.subheader(f"Support and Resistance Levels for {symbol}")
|
127 |
+
col1, col2, col3 = st.columns(3)
|
128 |
+
|
129 |
+
# Display resistance levels
|
130 |
+
with col1:
|
131 |
+
st.write("### Resistance Levels")
|
132 |
+
for i in range(3, 0, -1):
|
133 |
+
value = float(levels[f'Resistance {i}']) # Convert to float
|
134 |
+
st.write(f"R{i}: ₹{value:.2f}")
|
135 |
+
|
136 |
+
# Display pivot point
|
137 |
+
with col2:
|
138 |
+
st.write("### Pivot Point")
|
139 |
+
value = float(levels['Pivot Point']) # Convert to float
|
140 |
+
st.write(f"PP: ₹{value:.2f}")
|
141 |
+
|
142 |
+
# Display support levels
|
143 |
+
with col3:
|
144 |
+
st.write("### Support Levels")
|
145 |
+
for i in range(1, 4):
|
146 |
+
value = float(levels[f'Support {i}']) # Convert to float
|
147 |
+
st.write(f"S{i}: ₹{value:.2f}")
|
148 |
+
else:
|
149 |
+
st.error("Failed to fetch stock data. Please try again later.")
|
150 |
+
|
151 |
+
if __name__ == "__main__":
|
152 |
+
main()
|