-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
149 lines (121 loc) · 4.22 KB
/
main.py
File metadata and controls
149 lines (121 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# main.py
import websocket, json, pandas as pd, time
from config import APP_ID, API_TOKEN, MARKET
from logger import log_trade
import joblib
import ta
print(f"APP_ID: {APP_ID}, API_TOKEN: {API_TOKEN}, MARKET: {MARKET}")
data_window = []
authorized = False
current_contract_id = None
entry_price = None
entry_signal = None
trade_count = 0
MAX_DAILY_TRADES = 5
# ✅ Load ML Model
model = joblib.load("model.pkl")
def calculate_ml_signal(df: pd.DataFrame) -> str:
df['rsi'] = ta.momentum.RSIIndicator(df['close'], window=14).rsi()
df['ma50'] = ta.trend.SMAIndicator(df['close'], window=50).sma_indicator()
df = df.dropna()
# ✅ Safe guard: if df is empty after indicators
if df.empty or len(df) < 1:
return "HOLD"
# ✅ Extract last row cleanly with correct features
latest = df[['close', 'rsi', 'ma50']].iloc[-1]
latest_df = pd.DataFrame([latest], columns=['close', 'rsi', 'ma50'])
# ✅ Predict using trained model
prediction = model.predict(latest_df)[0]
if prediction == 1:
return "BUY"
elif prediction == -1:
return "SELL"
return "HOLD"
def send_buy_order(ws, direction, price):
global current_contract_id, entry_price, entry_signal, trade_count
if trade_count >= MAX_DAILY_TRADES:
print("🚫 Max trades reached for the day.")
return
contract_type = "CALL" if direction == "BUY" else "PUT"
print(f"🟢 Placing {direction} trade...")
ws.send(json.dumps({
"buy": 1,
"price": 1,
"parameters": {
"amount": 1,
"basis": "stake",
"contract_type": contract_type,
"currency": "USD",
"duration": 1,
"duration_unit": "m",
"symbol": MARKET
}
}))
entry_price = price
entry_signal = direction
trade_count += 1
def simulate_tp_sl(current_price):
global entry_price, entry_signal
if not entry_price or not entry_signal:
return
change = (current_price - entry_price) / entry_price * 100
if entry_signal == "BUY":
if change >= 1.5:
print("🎯 Take Profit Hit!")
entry_price = None
elif change <= -0.75:
print("🛑 Stop Loss Hit!")
entry_price = None
elif entry_signal == "SELL":
if change <= -1.5:
print("🎯 Take Profit Hit!")
entry_price = None
elif change >= 0.75:
print("🛑 Stop Loss Hit!")
entry_price = None
def on_message(ws, message):
global data_window, authorized, current_contract_id
msg = json.loads(message)
if "error" in msg:
print("❌ Error:", msg["error"]["message"])
return
if msg.get("msg_type") == "authorize":
authorized = True
print("🔐 Authorized successfully. Listening to live ticks...")
ws.send(json.dumps({
"ticks": MARKET,
"subscribe": 1
}))
elif msg.get("msg_type") == "tick":
tick = msg["tick"]
price = float(tick["quote"])
timestamp = tick["epoch"]
data_window.append({"timestamp": timestamp, "close": price})
if len(data_window) > 100:
df = pd.DataFrame(data_window[-100:])
signal = calculate_ml_signal(df)
print(f"[{timestamp}] ML Signal: {signal} | Price: {price}")
simulate_tp_sl(price)
if signal in ["BUY", "SELL"] and not current_contract_id:
send_buy_order(ws, signal, price)
elif msg.get("msg_type") == "buy":
current_contract_id = msg["buy"]["contract_id"]
print(f"✅ Trade placed. Contract ID: {current_contract_id}")
log_trade(entry_signal, entry_price, current_contract_id)
def on_open(ws):
print("🔄 Connecting and authorizing...")
ws.send(json.dumps({"authorize": APP_ID}))
def on_error(ws, error):
print("❌ WebSocket error:", error)
def on_close(ws, code, reason):
print("🔒 WebSocket closed:", code, reason)
if __name__ == "__main__":
socket_url = f"wss://ws.deriv.com/websockets/v3?app_id={APP_ID}"
ws = websocket.WebSocketApp(
socket_url,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws.run_forever()