Spaces:
Sleeping
Sleeping
Create Binance WebSocket
Browse files- Binance WebSocket +25 -0
Binance WebSocket
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import websocket
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
def on_message(ws, message):
|
| 5 |
+
data = json.loads(message)
|
| 6 |
+
# Process real-time market data
|
| 7 |
+
price = float(data['k']['c']) # Close price
|
| 8 |
+
signal = get_signal_from_model(price)
|
| 9 |
+
print(f"Signal: {signal}")
|
| 10 |
+
|
| 11 |
+
def on_error(ws, error):
|
| 12 |
+
print(error)
|
| 13 |
+
|
| 14 |
+
def on_close(ws, close_status_code, close_msg):
|
| 15 |
+
print("Closed")
|
| 16 |
+
|
| 17 |
+
def on_open(ws):
|
| 18 |
+
print("Connection established")
|
| 19 |
+
|
| 20 |
+
ws = websocket.WebSocketApp("wss://stream.binance.com:9443/ws/btcusdt@kline_1s", # WebSocket for 1-second interval
|
| 21 |
+
on_message=on_message,
|
| 22 |
+
on_error=on_error,
|
| 23 |
+
on_close=on_close)
|
| 24 |
+
ws.on_open = on_open
|
| 25 |
+
ws.run_forever()
|