Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,12 +2,11 @@ import streamlit as st
|
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
-
import matplotlib.pyplot as plt
|
| 6 |
import ta
|
| 7 |
from datetime import datetime
|
| 8 |
|
| 9 |
# Streamlit Title
|
| 10 |
-
st.title("NIFTY 500 Turtle Strategy Scanner with Support
|
| 11 |
|
| 12 |
# Load the NIFTY 500 stock list
|
| 13 |
stock_list = pd.read_csv('ind_nifty500list.csv')
|
|
@@ -35,7 +34,7 @@ support_period = st.sidebar.slider("Support Lookback Period (days)", min_value=5
|
|
| 35 |
resistance_period = st.sidebar.slider("Resistance Lookback Period (days)", min_value=5, max_value=50, value=20, step=1)
|
| 36 |
|
| 37 |
# Option to limit the number of stocks to scan
|
| 38 |
-
limit_stocks = st.sidebar.slider("Number of Stocks to Scan", min_value=5, max_value=len(nifty_500_stocks), value=
|
| 39 |
|
| 40 |
# Initialize an empty DataFrame to store the filtered stocks
|
| 41 |
filtered_data = pd.DataFrame(columns=["Stock", "Latest Price", "RSI", "Volume", "Short MA", "Long MA", "Support", "Resistance", "Entry Signal", "Exit Signal"])
|
|
@@ -74,25 +73,11 @@ for symbol in nifty_500_stocks[:limit_stocks]:
|
|
| 74 |
data['Support'] = data['Low'].rolling(window=support_period).min()
|
| 75 |
data['Resistance'] = data['High'].rolling(window=resistance_period).max()
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
plt.plot(data.index, data['Support'], label='Support Level', linestyle='--', color='red')
|
| 83 |
-
plt.plot(data.index, data['Resistance'], label='Resistance Level', linestyle='--', color='orange')
|
| 84 |
-
|
| 85 |
-
# Mark the entry and exit points
|
| 86 |
-
entry_points = data[data['Long'] == 1].index
|
| 87 |
-
exit_points = data[data['Exit'] == 1].index
|
| 88 |
-
plt.scatter(entry_points, data.loc[entry_points]['Close'], marker='^', color='green', s=100, label='Entry Signal')
|
| 89 |
-
plt.scatter(exit_points, data.loc[exit_points]['Close'], marker='v', color='red', s=100, label='Exit Signal')
|
| 90 |
-
|
| 91 |
-
plt.title(f'Support, Resistance, and Moving Averages for {symbol}')
|
| 92 |
-
plt.legend()
|
| 93 |
-
plt.xlabel('Date')
|
| 94 |
-
plt.ylabel('Price')
|
| 95 |
-
st.pyplot(plt)
|
| 96 |
|
| 97 |
# Check for the latest entry or exit signal, RSI, Volume, and Support/Resistance
|
| 98 |
latest_entry = data['Long'].iloc[-1] # Last Long signal (Entry)
|
|
@@ -104,5 +89,17 @@ for symbol in nifty_500_stocks[:limit_stocks]:
|
|
| 104 |
latest_support = data['Support'].iloc[-1] # Latest Support Level
|
| 105 |
latest_resistance = data['Resistance'].iloc[-1] # Latest Resistance Level
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
except Exception as e:
|
| 108 |
st.write(f"Error processing {symbol}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
import ta
|
| 6 |
from datetime import datetime
|
| 7 |
|
| 8 |
# Streamlit Title
|
| 9 |
+
st.title("NIFTY 500 Turtle Strategy Scanner with Support, Resistance, RSI, and Volume Filters")
|
| 10 |
|
| 11 |
# Load the NIFTY 500 stock list
|
| 12 |
stock_list = pd.read_csv('ind_nifty500list.csv')
|
|
|
|
| 34 |
resistance_period = st.sidebar.slider("Resistance Lookback Period (days)", min_value=5, max_value=50, value=20, step=1)
|
| 35 |
|
| 36 |
# Option to limit the number of stocks to scan
|
| 37 |
+
limit_stocks = st.sidebar.slider("Number of Stocks to Scan", min_value=5, max_value=len(nifty_500_stocks), value=50, step=5)
|
| 38 |
|
| 39 |
# Initialize an empty DataFrame to store the filtered stocks
|
| 40 |
filtered_data = pd.DataFrame(columns=["Stock", "Latest Price", "RSI", "Volume", "Short MA", "Long MA", "Support", "Resistance", "Entry Signal", "Exit Signal"])
|
|
|
|
| 73 |
data['Support'] = data['Low'].rolling(window=support_period).min()
|
| 74 |
data['Resistance'] = data['High'].rolling(window=resistance_period).max()
|
| 75 |
|
| 76 |
+
# Position Management
|
| 77 |
+
data['Position'] = 0
|
| 78 |
+
data.loc[data['Long'] == 1, 'Position'] = 1
|
| 79 |
+
data.loc[data['Exit'] == 1, 'Position'] = 0
|
| 80 |
+
data['Position'] = data['Position'].ffill().shift(1).fillna(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
# Check for the latest entry or exit signal, RSI, Volume, and Support/Resistance
|
| 83 |
latest_entry = data['Long'].iloc[-1] # Last Long signal (Entry)
|
|
|
|
| 89 |
latest_support = data['Support'].iloc[-1] # Latest Support Level
|
| 90 |
latest_resistance = data['Resistance'].iloc[-1] # Latest Resistance Level
|
| 91 |
|
| 92 |
+
# Filter based on Support/Resistance, RSI, Volume, and Moving Averages
|
| 93 |
+
if (latest_entry == 1 or latest_exit == 1) and ((latest_rsi <= lower_rsi_threshold or latest_rsi >= upper_rsi_threshold)) and latest_volume > volume_threshold and latest_short_ma > latest_long_ma:
|
| 94 |
+
latest_price = data['Close'].iloc[-1]
|
| 95 |
+
filtered_data = pd.concat([filtered_data, pd.DataFrame([[symbol, latest_price, latest_rsi, latest_volume, latest_short_ma, latest_long_ma, latest_support, latest_resistance, latest_entry, latest_exit]], columns=filtered_data.columns)], ignore_index=True)
|
| 96 |
+
|
| 97 |
except Exception as e:
|
| 98 |
st.write(f"Error processing {symbol}: {e}")
|
| 99 |
+
|
| 100 |
+
# Display the filtered stocks that meet the criteria
|
| 101 |
+
if not filtered_data.empty:
|
| 102 |
+
st.subheader(f"Filtered Stocks with Support/Resistance, Moving Average, Custom RSI, and Volume Thresholds:")
|
| 103 |
+
st.dataframe(filtered_data)
|
| 104 |
+
else:
|
| 105 |
+
st.write("No stocks meeting the criteria for entry/exit signals, Support/Resistance, RSI, MA, or volume thresholds.")
|