Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,10 @@ from threading import Thread
|
|
| 10 |
# Define IST timezone
|
| 11 |
IST = pytz.timezone('Asia/Kolkata')
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Define the list of stocks in Nifty 50 index
|
| 14 |
nifty50_list = [
|
| 15 |
'ADANIENT.NS', 'ADANIPORTS.NS', 'APOLLOHOSP.NS', 'ASIANPAINT.NS', 'AXISBANK.NS',
|
|
@@ -111,7 +115,7 @@ def check_triggered_stocks(recommendations, triggered_stocks, recommendation_typ
|
|
| 111 |
|
| 112 |
return recommendations, triggered_stocks
|
| 113 |
|
| 114 |
-
# Function to calculate the time to the next 5-minute interval
|
| 115 |
def time_to_next_interval():
|
| 116 |
now = datetime.datetime.now(IST)
|
| 117 |
next_minute = (now.minute // 5 + 1) * 5
|
|
@@ -123,7 +127,7 @@ def time_to_next_interval():
|
|
| 123 |
else:
|
| 124 |
next_hour = now.hour
|
| 125 |
next_time = now.replace(hour=next_hour, minute=next_minute, second=0, microsecond=0)
|
| 126 |
-
wait_time = (next_time - now).total_seconds()
|
| 127 |
return wait_time
|
| 128 |
|
| 129 |
# Function to keep the app alive
|
|
@@ -132,7 +136,7 @@ def keep_alive():
|
|
| 132 |
time.sleep(60)
|
| 133 |
|
| 134 |
# Start keep_alive function in a separate thread
|
| 135 |
-
Thread(target=keep_alive).start()
|
| 136 |
|
| 137 |
# Streamlit App Design
|
| 138 |
st.title("Stock Recommendation App")
|
|
@@ -140,7 +144,7 @@ st.sidebar.header("Select Recommendation Type")
|
|
| 140 |
option = st.sidebar.selectbox("Choose a recommendation type:", ["Intraday", "Short-Term", "Long-Term"])
|
| 141 |
|
| 142 |
long_recs_filename = "long_recommendations.csv"
|
| 143 |
-
short_recs_filename = "
|
| 144 |
triggered_recs_filename = "triggered_recommendations.csv"
|
| 145 |
|
| 146 |
# Load existing data
|
|
@@ -152,35 +156,42 @@ if option == "Intraday":
|
|
| 152 |
st.header("Intraday Stock Recommendations")
|
| 153 |
|
| 154 |
while True:
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
| 10 |
# Define IST timezone
|
| 11 |
IST = pytz.timezone('Asia/Kolkata')
|
| 12 |
|
| 13 |
+
# Market hours in IST (9:15 AM to 3:30 PM)
|
| 14 |
+
market_open_time = datetime.time(9, 15, 0)
|
| 15 |
+
market_close_time = datetime.time(15, 30, 0)
|
| 16 |
+
|
| 17 |
# Define the list of stocks in Nifty 50 index
|
| 18 |
nifty50_list = [
|
| 19 |
'ADANIENT.NS', 'ADANIPORTS.NS', 'APOLLOHOSP.NS', 'ASIANPAINT.NS', 'AXISBANK.NS',
|
|
|
|
| 115 |
|
| 116 |
return recommendations, triggered_stocks
|
| 117 |
|
| 118 |
+
# Function to calculate the time to the next exact minute after each 5-minute interval
|
| 119 |
def time_to_next_interval():
|
| 120 |
now = datetime.datetime.now(IST)
|
| 121 |
next_minute = (now.minute // 5 + 1) * 5
|
|
|
|
| 127 |
else:
|
| 128 |
next_hour = now.hour
|
| 129 |
next_time = now.replace(hour=next_hour, minute=next_minute, second=0, microsecond=0)
|
| 130 |
+
wait_time = (next_time - now).total_seconds() + 60 # Add 60 seconds to ensure checking one minute after the 5-minute interval
|
| 131 |
return wait_time
|
| 132 |
|
| 133 |
# Function to keep the app alive
|
|
|
|
| 136 |
time.sleep(60)
|
| 137 |
|
| 138 |
# Start keep_alive function in a separate thread
|
| 139 |
+
Thread(target=keep_alive, daemon=True).start()
|
| 140 |
|
| 141 |
# Streamlit App Design
|
| 142 |
st.title("Stock Recommendation App")
|
|
|
|
| 144 |
option = st.sidebar.selectbox("Choose a recommendation type:", ["Intraday", "Short-Term", "Long-Term"])
|
| 145 |
|
| 146 |
long_recs_filename = "long_recommendations.csv"
|
| 147 |
+
short_recs_filename = "short_recommendations.csv"
|
| 148 |
triggered_recs_filename = "triggered_recommendations.csv"
|
| 149 |
|
| 150 |
# Load existing data
|
|
|
|
| 156 |
st.header("Intraday Stock Recommendations")
|
| 157 |
|
| 158 |
while True:
|
| 159 |
+
now = datetime.datetime.now(IST)
|
| 160 |
+
if market_open_time <= now.time() <= market_close_time:
|
| 161 |
+
# Wait until the next exact minute after the 5-minute interval
|
| 162 |
+
wait_time = time_to_next_interval()
|
| 163 |
+
time.sleep(wait_time)
|
| 164 |
+
|
| 165 |
+
# Get recommendations
|
| 166 |
+
long_recs, short_recs = get_recommendations()
|
| 167 |
+
|
| 168 |
+
if long_recs:
|
| 169 |
+
long_recs_df = pd.concat([long_recs_df, pd.DataFrame(long_recs, columns=["Stock Name", "Current Price", "Recommended Price", "SL", "Target", "Status", "Timestamp"])])
|
| 170 |
+
save_to_csv(long_recs_df, long_recs_filename)
|
| 171 |
+
if not long_recs_df.empty:
|
| 172 |
+
long_recs_df, triggered_stocks_df = check_triggered_stocks(long_recs_df, triggered_stocks_df, "LONG")
|
| 173 |
+
save_to_csv(long_recs_df, long_recs_filename)
|
| 174 |
+
save_to_csv(triggered_stocks_df, triggered_recs_filename)
|
| 175 |
+
st.subheader("LONG Recommendations")
|
| 176 |
+
st.table(long_recs_df)
|
| 177 |
+
else:
|
| 178 |
+
st.write("No LONG recommendations at the moment.")
|
| 179 |
+
|
| 180 |
+
if short_recs:
|
| 181 |
+
short_recs_df = pd.concat([short_recs_df, pd.DataFrame(short_recs, columns=["Stock Name", "Current Price", "Recommended Price", "SL", "Target", "Status", "Timestamp"])])
|
| 182 |
+
save_to_csv(short_recs_df, short_recs_filename)
|
| 183 |
+
if not short_recs_df.empty:
|
| 184 |
+
short_recs_df, triggered_stocks_df = check_triggered_stocks(short_recs_df, triggered_stocks_df, "SHORT")
|
| 185 |
+
save_to_csv(short_recs_df, short_recs_filename)
|
| 186 |
+
save_to_csv(triggered_stocks_df, triggered_recs_filename)
|
| 187 |
+
st.subheader("SHORT Recommendations")
|
| 188 |
+
st.table(short_recs_df)
|
| 189 |
+
else:
|
| 190 |
+
st.write("No SHORT recommendations at the moment.")
|
| 191 |
+
|
| 192 |
+
st.subheader("Triggered Recommendations")
|
| 193 |
+
st.table(triggered_stocks_df)
|
| 194 |
|
| 195 |
+
else:
|
| 196 |
+
st.write("Market is closed. The app will resume checking recommendations at the next market open time.")
|
| 197 |
+
time.sleep(60) # Sleep for 1 minute before checking time again
|