Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,116 +3,125 @@ import time
|
|
3 |
import gradio as gr
|
4 |
from datetime import datetime
|
5 |
import pytz
|
|
|
|
|
6 |
from quotexpy import Quotex
|
7 |
from quotexpy.utils.candles_period import CandlesPeriod
|
8 |
from quotexpy.utils.account_type import AccountType
|
9 |
|
10 |
-
# Initialize
|
11 |
state = []
|
12 |
trade_recommendations = []
|
13 |
client = Quotex(email="your_email@example.com", password="your_password", headless=True)
|
|
|
14 |
|
15 |
-
def
|
16 |
-
"""
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
else:
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
def background_worker(pair: str, timeframe: str):
|
34 |
try:
|
35 |
-
|
36 |
-
print("[ERROR] Failed to connect to Quotex")
|
37 |
-
return
|
38 |
-
|
39 |
-
client.change_account(AccountType.PRACTICE)
|
40 |
-
|
41 |
-
period_map = {
|
42 |
-
"5s": CandlesPeriod.SEC_5,
|
43 |
-
"15s": CandlesPeriod.SEC_15,
|
44 |
-
"1m": CandlesPeriod.ONE_MINUTE,
|
45 |
-
"5m": CandlesPeriod.FIVE_MINUTES,
|
46 |
-
}
|
47 |
-
period = period_map[timeframe]
|
48 |
-
client.start_candles_stream(pair, 10, period)
|
49 |
-
|
50 |
while True:
|
51 |
candles = client.get_realtime_candles(pair)
|
52 |
if candles:
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
timestamp = datetime.fromtimestamp(latest_ts / 1000).strftime("%H:%M:%S")
|
57 |
-
|
58 |
-
entry = f"{timestamp} | {direction} | Close: {candle_data['close']:.5f}"
|
59 |
-
state.append(entry)
|
60 |
-
|
61 |
-
# Generate new recommendation every 5 candles
|
62 |
-
if len(state) % 5 == 0:
|
63 |
-
recommendation = analyze_candle_pattern(candles)
|
64 |
-
trade_recommendations.append(f"β±οΈ {timestamp} | {recommendation}")
|
65 |
-
|
66 |
-
if len(state) > 100:
|
67 |
-
state.pop(0)
|
68 |
-
time.sleep(1)
|
69 |
except Exception as e:
|
70 |
-
print(f"
|
71 |
|
72 |
def start(pair, timeframe):
|
73 |
-
global state, trade_recommendations
|
74 |
state.clear()
|
75 |
trade_recommendations.clear()
|
76 |
-
threading.Thread(
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
return "
|
82 |
-
|
83 |
-
def get_latest_recommendations():
|
84 |
-
"""Display last 3 trade recommendations."""
|
85 |
-
return "\n".join(trade_recommendations[-3:][::-1]) or "Waiting for recommendations..."
|
86 |
|
87 |
-
with gr.Blocks(
|
88 |
-
gr.Markdown("##
|
|
|
89 |
|
90 |
with gr.Row():
|
91 |
-
pair = gr.Dropdown(["EURUSD", "USDJPY", "BTCUSD"], label="
|
92 |
-
timeframe = gr.Dropdown(["5s", "15s", "1m", "5m"], label="
|
93 |
-
start_btn = gr.Button("Start
|
94 |
-
refresh_btn = gr.Button("Refresh Data")
|
95 |
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
101 |
|
102 |
start_btn.click(
|
103 |
-
|
104 |
inputs=[pair, timeframe],
|
105 |
-
outputs=
|
106 |
)
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
)
|
112 |
-
|
113 |
-
refresh_btn.click(
|
114 |
-
fn=get_latest_recommendations,
|
115 |
-
outputs=recommendation_output
|
116 |
)
|
117 |
|
118 |
if __name__ == "__main__":
|
|
|
3 |
import gradio as gr
|
4 |
from datetime import datetime
|
5 |
import pytz
|
6 |
+
import requests
|
7 |
+
from groq import Groq
|
8 |
from quotexpy import Quotex
|
9 |
from quotexpy.utils.candles_period import CandlesPeriod
|
10 |
from quotexpy.utils.account_type import AccountType
|
11 |
|
12 |
+
# Initialize
|
13 |
state = []
|
14 |
trade_recommendations = []
|
15 |
client = Quotex(email="your_email@example.com", password="your_password", headless=True)
|
16 |
+
groq_client = Groq(api_key="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")
|
17 |
|
18 |
+
def get_ai_recommendation(market_data):
|
19 |
+
"""Get AI-powered recommendation using Groq API"""
|
20 |
+
try:
|
21 |
+
prompt = f"""
|
22 |
+
Analyze the latest candlestick patterns and market data:
|
23 |
+
|
24 |
+
{market_data}
|
25 |
|
26 |
+
Provide:
|
27 |
+
1. Trading Signal (BUY/SELL/HOLD)
|
28 |
+
2. Confidence (High/Medium/Low)
|
29 |
+
3. RSI Level
|
30 |
+
4. Key Price Levels
|
31 |
+
"""
|
32 |
+
|
33 |
+
chat_completion = groq_client.chat.completions.create(
|
34 |
+
messages=[{"role": "user", "content": prompt}],
|
35 |
+
model="mixtral-8x7b-32768",
|
36 |
+
temperature=0.3,
|
37 |
+
max_tokens=150
|
38 |
+
)
|
39 |
+
return chat_completion.choices[0].message.content
|
40 |
+
except Exception as e:
|
41 |
+
return f"AI analysis failed: {e}"
|
42 |
|
43 |
+
def analyze_candles(candles):
|
44 |
+
"""AI + Technical Analysis"""
|
45 |
+
if not candles or len(candles) < 3:
|
46 |
+
return "Waiting for more data..."
|
47 |
+
|
48 |
+
# Technical analysis
|
49 |
+
last_close = candles[max(candles.keys())]["close"]
|
50 |
+
prev_close = candles[sorted(candles.keys())[-2]]["close"]
|
51 |
+
|
52 |
+
if last_close > prev_close * 1.005: # Strong bullish
|
53 |
+
tech_signal = "π₯ STRONG UPTREND - BUY"
|
54 |
+
elif last_close < prev_close * 0.995: # Strong bearish
|
55 |
+
tech_signal = "π STRONG DOWNTREND - SELL"
|
56 |
else:
|
57 |
+
tech_signal = "π‘ WEAK TREND - HOLD"
|
58 |
+
|
59 |
+
# AI Input
|
60 |
+
market_data = f"""
|
61 |
+
Latest Candle:
|
62 |
+
- Open: {last_candle["open"]}
|
63 |
+
- Close: {last_candle["close"]}
|
64 |
+
- High: {last_candle["high"]}
|
65 |
+
- Low: {last_candle["low"]}
|
66 |
+
- Volume: {last_candle["vol"]}
|
67 |
+
"""
|
68 |
+
ai_signal = get_ai_recommendation(market_data)
|
69 |
+
|
70 |
+
return f"""\n
|
71 |
+
π **TECHNICAL ANALYSIS:**
|
72 |
+
{tech_signal}
|
73 |
+
π€ **AI RECOMMENDATION:**
|
74 |
+
{ai_signal}"""
|
75 |
|
76 |
def background_worker(pair: str, timeframe: str):
|
77 |
try:
|
78 |
+
client.start_candles_stream(pair, 10, period_map[timeframe])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
while True:
|
80 |
candles = client.get_realtime_candles(pair)
|
81 |
if candles:
|
82 |
+
analysis = analyze_candles(candles)
|
83 |
+
trade_recommendations.append(analysis)
|
84 |
+
time.sleep(5) # Refresh every 5 sec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
except Exception as e:
|
86 |
+
print(f"β Worker error: {e}")
|
87 |
|
88 |
def start(pair, timeframe):
|
|
|
89 |
state.clear()
|
90 |
trade_recommendations.clear()
|
91 |
+
threading.Thread(
|
92 |
+
target=background_worker,
|
93 |
+
args=(pair, timeframe),
|
94 |
+
daemon=True
|
95 |
+
).start()
|
96 |
+
return "π Analyzing market..."
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
with gr.Blocks() as demo:
|
99 |
+
gr.Markdown("## π€ AI Trading Assistant")
|
100 |
+
gr.Markdown("### Real-time Candle Analysis")
|
101 |
|
102 |
with gr.Row():
|
103 |
+
pair = gr.Dropdown(choices=["EURUSD", "USDJPY", "BTCUSD"], label="Pair")
|
104 |
+
timeframe = gr.Dropdown(choices=["5s", "15s", "1m", "5m"], label="Timeframe")
|
105 |
+
start_btn = gr.Button("Start AI Analysis")
|
|
|
106 |
|
107 |
+
gr.Markdown("---")
|
108 |
+
|
109 |
+
with gr.Column():
|
110 |
+
gr.Markdown("### π Last 10 Candles")
|
111 |
+
candle_output = gr.Textbox()
|
112 |
+
|
113 |
+
gr.Markdown("### ποΈ Trading Signals")
|
114 |
+
ai_output = gr.Textbox(label="AI + Technical Signals")
|
115 |
|
116 |
start_btn.click(
|
117 |
+
start,
|
118 |
inputs=[pair, timeframe],
|
119 |
+
outputs=[candle_output, ai_output]
|
120 |
)
|
121 |
+
demo.load(
|
122 |
+
get_latest_recommendations,
|
123 |
+
outputs=ai_output,
|
124 |
+
every=5 # Refresh signals every 5 seconds
|
|
|
|
|
|
|
|
|
|
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|