-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (52 loc) · 1.8 KB
/
main.py
File metadata and controls
66 lines (52 loc) · 1.8 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
import websocket
import json
import pandas as pd
from datetime import datetime
from config import API_TOKEN, APP_ID, MARKET
from indicators import compute_indicators, generate_signal
# Variables to hold price data
prices = []
timestamps = []
def on_open(ws):
print("🔌 Connected to Deriv...")
ws.send(json.dumps({"authorize": API_TOKEN}))
def on_message(ws, message):
global prices, timestamps
data = json.loads(message)
# Handle authorization
if "authorize" in data:
print("✅ Authorized. Subscribing to market ticks...")
ws.send(json.dumps({
"ticks": MARKET,
"subscribe": 1
}))
# Handle tick data
elif "tick" in data:
tick = data["tick"]
price = float(tick["quote"])
timestamp = tick["epoch"]
prices.append(price)
timestamps.append(datetime.fromtimestamp(timestamp))
if len(prices) >= 30:
df = pd.DataFrame({
"close": prices[-30:],
"timestamp": timestamps[-30:]
})
df = compute_indicators(df)
signal = generate_signal(df)
print(f"[{timestamp}] Signal: {signal} | Price: {price}")
# Log to CSV
with open("trades.csv", "a") as file:
file.write(f"{timestamp},{signal},{price}\n")
def on_error(ws, error):
print("❌ Error:", error)
def on_close(ws, close_status_code, close_msg):
print("🔌 Disconnected from Deriv")
# Run the WebSocket connection
socket = f"wss://ws.deriv.com/websockets/v3?app_id={APP_ID}"
ws = websocket.WebSocketApp(socket,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()