riteshcp commited on
Commit
28e094d
·
verified ·
1 Parent(s): 6970bbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -30
app.py CHANGED
@@ -2,11 +2,11 @@ import streamlit as st
2
  import yfinance as yf
3
  import pandas as pd
4
  import numpy as np
5
- import talib
6
  from datetime import datetime
7
 
8
  # Streamlit Title
9
- st.title("NIFTY 500 Turtle Strategy Scanner with Entry, Exit, RSI, and Volume")
10
 
11
  # Load the NIFTY 500 stock list
12
  stock_list = pd.read_csv('ind_nifty500list.csv')
@@ -49,8 +49,8 @@ for symbol in nifty_500_stocks[:limit_stocks]:
49
  data['Long'] = np.where(data['Close'] > data['20D_High'].shift(1), 1, 0)
50
  data['Exit'] = np.where(data['Close'] < data['10D_Low'].shift(1), 1, 0) # Exit Signal as 1 if true
51
 
52
- # Calculate RSI using TA-Lib
53
- data['RSI'] = talib.RSI(data['Close'], timeperiod=14)
54
 
55
  # Position Management
56
  data['Position'] = 0
@@ -75,32 +75,6 @@ for symbol in nifty_500_stocks[:limit_stocks]:
75
  # Display the filtered stocks that meet the criteria
76
  if not filtered_data.empty:
77
  st.subheader("Filtered Stocks with Entry or Exit Signals, RSI, and Volume:")
78
- # Display as a table
79
  st.dataframe(filtered_data)
80
  else:
81
  st.write("No stocks meeting the criteria for entry/exit signals, RSI, or volume thresholds.")
82
-
83
- # Optional: Show performance for a single stock
84
- selected_stock = st.selectbox("Select a Stock to View Detailed Performance", nifty_500_stocks)
85
- if selected_stock:
86
- st.write(f"### Detailed Analysis for {selected_stock}")
87
-
88
- # Fetch detailed data for the selected stock
89
- detailed_data = yf.download(selected_stock, start='2022-01-01', end=end_date)
90
-
91
- # Turtle Strategy Calculations
92
- detailed_data['20D_High'] = detailed_data['High'].rolling(window=entry_days).max()
93
- detailed_data['10D_Low'] = detailed_data['Low'].rolling(window=exit_days).min()
94
- detailed_data['Long'] = np.where(detailed_data['Close'] > detailed_data['20D_High'].shift(1), 1, 0)
95
- detailed_data['Exit'] = np.where(detailed_data['Close'] < detailed_data['10D_Low'].shift(1), 1, 0)
96
- detailed_data['Position'] = 0
97
- detailed_data.loc[detailed_data['Long'] == 1, 'Position'] = 1
98
- detailed_data.loc[detailed_data['Exit'] == 1, 'Position'] = 0
99
- detailed_data['Position'] = detailed_data['Position'].ffill().shift(1).fillna(0)
100
-
101
- # Calculate RSI
102
- detailed_data['RSI'] = talib.RSI(detailed_data['Close'], timeperiod=14)
103
-
104
- # Show Raw Data
105
- st.subheader("Strategy Data Preview")
106
- st.write(detailed_data.tail(20))
 
2
  import yfinance as yf
3
  import pandas as pd
4
  import numpy as np
5
+ import ta # Use 'ta' library instead of 'talib'
6
  from datetime import datetime
7
 
8
  # Streamlit Title
9
+ st.title("NIFTY 500 Turtle Strategy Scanner with RSI and Volume using 'ta'")
10
 
11
  # Load the NIFTY 500 stock list
12
  stock_list = pd.read_csv('ind_nifty500list.csv')
 
49
  data['Long'] = np.where(data['Close'] > data['20D_High'].shift(1), 1, 0)
50
  data['Exit'] = np.where(data['Close'] < data['10D_Low'].shift(1), 1, 0) # Exit Signal as 1 if true
51
 
52
+ # Calculate RSI using 'ta' library
53
+ data['RSI'] = ta.momentum.RSIIndicator(data['Close'], window=14).rsi()
54
 
55
  # Position Management
56
  data['Position'] = 0
 
75
  # Display the filtered stocks that meet the criteria
76
  if not filtered_data.empty:
77
  st.subheader("Filtered Stocks with Entry or Exit Signals, RSI, and Volume:")
 
78
  st.dataframe(filtered_data)
79
  else:
80
  st.write("No stocks meeting the criteria for entry/exit signals, RSI, or volume thresholds.")