Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -58,45 +58,34 @@ def get_mistral_analysis(df):
|
|
58 |
current_price = last_10_days.iloc[-1]['Close']
|
59 |
|
60 |
# Format data for prompt
|
61 |
-
data_str = f"""Based on the following NIFTY 50 price data for the last 10 days,
|
62 |
-
1. Key support levels (2-3 levels)
|
63 |
-
2. Key resistance levels (2-3 levels)
|
64 |
-
3. Potential target price for today
|
65 |
-
4. Trading setup recommendation (bullish/bearish)
|
66 |
|
67 |
-
Current price: {current_price}
|
68 |
-
|
69 |
-
Last 10 days data (Open, High, Low, Close):
|
70 |
-
"""
|
71 |
-
# Add data in a readable format
|
72 |
-
for _, row in last_10_days.iterrows():
|
73 |
-
data_str += f"Date: {row['Date']} - Open: {row['Open']}, High: {row['High']}, Low: {row['Low']}, Close: {row['Close']}\n"
|
74 |
-
|
75 |
-
data_str += """
|
76 |
-
Please provide your analysis in the following format:
|
77 |
Support Levels:
|
78 |
-
- S1:
|
79 |
-
- S2:
|
80 |
-
- S3:
|
81 |
|
82 |
Resistance Levels:
|
83 |
-
- R1:
|
84 |
-
- R2:
|
85 |
-
- R3:
|
86 |
|
87 |
Today's Target Price:
|
88 |
-
- Target:
|
89 |
|
90 |
Trading Setup:
|
91 |
-
- Direction:
|
92 |
-
- Entry Price Range:
|
93 |
-
- Stop Loss:
|
94 |
-
- Risk/Reward Ratio:
|
95 |
|
96 |
-
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
|
99 |
-
# Mistral API endpoint
|
100 |
url = "https://api.mistral.ai/v1/chat/completions"
|
101 |
|
102 |
headers = {
|
@@ -107,11 +96,11 @@ Analysis Explanation:
|
|
107 |
data = {
|
108 |
"model": "mistral-small",
|
109 |
"messages": [
|
110 |
-
{"role": "system", "content": "You are a
|
111 |
{"role": "user", "content": data_str}
|
112 |
],
|
113 |
"temperature": 0.7,
|
114 |
-
"max_tokens":
|
115 |
}
|
116 |
|
117 |
response = requests.post(url, headers=headers, json=data)
|
@@ -125,7 +114,6 @@ Analysis Explanation:
|
|
125 |
return f"Error getting analysis: {str(e)}"
|
126 |
|
127 |
def show_nifty_data_and_analysis():
|
128 |
-
# First verify API key
|
129 |
api_status = verify_api_key()
|
130 |
if api_status.startswith("ERROR"):
|
131 |
return pd.DataFrame(), api_status
|
@@ -134,49 +122,31 @@ def show_nifty_data_and_analysis():
|
|
134 |
analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
|
135 |
return df, analysis
|
136 |
|
137 |
-
# Create Gradio interface with custom CSS
|
138 |
-
custom_css = """
|
139 |
-
.container {max-width: 1200px; margin: auto;}
|
140 |
-
.analysis {white-space: pre-wrap; font-family: monospace;}
|
141 |
-
"""
|
142 |
-
|
143 |
# Create Gradio interface
|
144 |
-
with gr.Blocks(
|
145 |
-
with gr.Column(
|
146 |
-
gr.Markdown("# NIFTY 50
|
147 |
-
gr.Markdown("Displays the last 15 days of trading data and AI analysis for NIFTY 50 index")
|
148 |
-
|
149 |
-
# Add API key status
|
150 |
-
api_status = gr.Markdown(f"API Status: {verify_api_key()}")
|
151 |
|
152 |
-
# Add refresh button
|
153 |
-
refresh_btn = gr.Button(
|
154 |
-
"Refresh Data and Analysis",
|
155 |
-
variant="primary",
|
156 |
-
size="lg"
|
157 |
-
)
|
158 |
|
159 |
-
# Add outputs
|
160 |
with gr.Row():
|
161 |
with gr.Column(scale=1):
|
162 |
-
gr.Markdown("###
|
163 |
output_table = gr.Dataframe(
|
164 |
headers=["Date", "Open", "High", "Low", "Close", "Volume"],
|
165 |
wrap=True
|
166 |
)
|
167 |
|
168 |
with gr.Column(scale=1):
|
169 |
-
gr.Markdown("###
|
170 |
analysis_output = gr.Textbox(
|
171 |
-
label="
|
172 |
lines=15,
|
173 |
elem_classes="analysis"
|
174 |
)
|
175 |
-
|
176 |
-
# Add footer
|
177 |
-
gr.Markdown("---")
|
178 |
-
gr.Markdown("Updated every minute • Data source: Yahoo Finance • Analysis: Mistral AI")
|
179 |
-
|
180 |
# Set up refresh button click event
|
181 |
refresh_btn.click(
|
182 |
fn=show_nifty_data_and_analysis,
|
|
|
58 |
current_price = last_10_days.iloc[-1]['Close']
|
59 |
|
60 |
# Format data for prompt
|
61 |
+
data_str = f"""Based on the following NIFTY 50 price data for the last 10 days, provide only support levels, resistance levels, target price, and trading setup. Format exactly as shown below:
|
|
|
|
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
Support Levels:
|
64 |
+
- S1: [price] (brief reason)
|
65 |
+
- S2: [price] (brief reason)
|
66 |
+
- S3: [price] (brief reason)
|
67 |
|
68 |
Resistance Levels:
|
69 |
+
- R1: [price] (brief reason)
|
70 |
+
- R2: [price] (brief reason)
|
71 |
+
- R3: [price] (brief reason)
|
72 |
|
73 |
Today's Target Price:
|
74 |
+
- Target ([direction]): [price] (brief reason)
|
75 |
|
76 |
Trading Setup:
|
77 |
+
- Direction: [Bullish/Bearish]
|
78 |
+
- Entry Price Range: [range]
|
79 |
+
- Stop Loss: [price]
|
80 |
+
- Risk/Reward Ratio: [ratio]
|
81 |
|
82 |
+
Current price: {current_price}
|
83 |
+
|
84 |
+
Data:
|
85 |
+
"""
|
86 |
+
for _, row in last_10_days.iterrows():
|
87 |
+
data_str += f"{row['Date']}: O:{row['Open']} H:{row['High']} L:{row['Low']} C:{row['Close']}\n"
|
88 |
|
|
|
89 |
url = "https://api.mistral.ai/v1/chat/completions"
|
90 |
|
91 |
headers = {
|
|
|
96 |
data = {
|
97 |
"model": "mistral-small",
|
98 |
"messages": [
|
99 |
+
{"role": "system", "content": "You are a price action trader. Provide only the requested levels and setup information in the exact format specified. Do not add any additional analysis or explanation."},
|
100 |
{"role": "user", "content": data_str}
|
101 |
],
|
102 |
"temperature": 0.7,
|
103 |
+
"max_tokens": 500
|
104 |
}
|
105 |
|
106 |
response = requests.post(url, headers=headers, json=data)
|
|
|
114 |
return f"Error getting analysis: {str(e)}"
|
115 |
|
116 |
def show_nifty_data_and_analysis():
|
|
|
117 |
api_status = verify_api_key()
|
118 |
if api_status.startswith("ERROR"):
|
119 |
return pd.DataFrame(), api_status
|
|
|
122 |
analysis = get_mistral_analysis(df) if not df.empty else "Unable to fetch data"
|
123 |
return df, analysis
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
# Create Gradio interface
|
126 |
+
with gr.Blocks() as demo:
|
127 |
+
with gr.Column():
|
128 |
+
gr.Markdown("# NIFTY 50 Levels and Setup")
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
# Add refresh button
|
131 |
+
refresh_btn = gr.Button("Refresh Data", variant="primary")
|
|
|
|
|
|
|
|
|
132 |
|
133 |
+
# Add outputs
|
134 |
with gr.Row():
|
135 |
with gr.Column(scale=1):
|
136 |
+
gr.Markdown("### Market Data")
|
137 |
output_table = gr.Dataframe(
|
138 |
headers=["Date", "Open", "High", "Low", "Close", "Volume"],
|
139 |
wrap=True
|
140 |
)
|
141 |
|
142 |
with gr.Column(scale=1):
|
143 |
+
gr.Markdown("### Trading Levels")
|
144 |
analysis_output = gr.Textbox(
|
145 |
+
label="Levels and Setup",
|
146 |
lines=15,
|
147 |
elem_classes="analysis"
|
148 |
)
|
149 |
+
|
|
|
|
|
|
|
|
|
150 |
# Set up refresh button click event
|
151 |
refresh_btn.click(
|
152 |
fn=show_nifty_data_and_analysis,
|