-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_streaming.py
More file actions
334 lines (268 loc) · 11.1 KB
/
websocket_streaming.py
File metadata and controls
334 lines (268 loc) · 11.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
WebSocket Streaming Example
Demonstrates real-time financial data streaming using WebSocket connections.
This example shows:
1. Connecting to the WebSocket endpoint
2. Subscribing to price streams
3. Receiving real-time updates
4. Managing multiple subscriptions
5. Handling errors and reconnection
"""
import asyncio
import json
from datetime import datetime
import websockets
async def stream_prices_simple():
"""
Simple example: Stream prices for multiple symbols
Uses the simplified /ws/prices/{symbols} endpoint
"""
print("=== Simple Price Streaming ===\n")
uri = "ws://localhost:8000/ws/prices/AAPL,GOOGL,MSFT"
try:
async with websockets.connect(uri) as websocket:
print(f"Connected to {uri}")
# Receive subscription acknowledgment
ack = await websocket.recv()
ack_data = json.loads(ack)
print(f"Subscribed: {ack_data}")
print()
# Receive price updates
print("Receiving price updates (Ctrl+C to stop):\n")
while True:
message = await websocket.recv()
data = json.loads(message)
if data.get("type") == "data":
# Print price updates
for update in data["data"]:
print(
f"[{update['timestamp']}] {update['symbol']}: "
f"${update['price']:.2f} ({update['change_percent']:+.2f}%) "
f"[{update['provider']}]"
)
elif data.get("type") == "heartbeat":
print(f"❤️ Heartbeat - {data['active_subscriptions']} active subscriptions")
except websockets.exceptions.ConnectionClosed:
print("Connection closed")
except KeyboardInterrupt:
print("\nStopped by user")
async def stream_with_full_control():
"""
Advanced example: Full control over subscriptions
Uses the main /ws/stream endpoint with subscription messages
"""
print("\n=== Advanced Streaming with Full Control ===\n")
uri = "ws://localhost:8000/ws/stream"
try:
async with websockets.connect(uri) as websocket:
print(f"Connected to {uri}")
# Subscribe to price stream for equities
equity_subscription = {
"type": "subscribe",
"stream_type": "price",
"symbols": ["AAPL", "TSLA", "NVDA"],
"asset_type": "equity",
"market": "US",
"interval_ms": 2000, # Update every 2 seconds
"data_type": "price"
}
await websocket.send(json.dumps(equity_subscription))
ack1 = json.loads(await websocket.recv())
print(f"Equity subscription created: {ack1['subscription_id']}")
# Subscribe to OHLCV stream for crypto
crypto_subscription = {
"type": "subscribe",
"stream_type": "ohlcv",
"symbols": ["BTC/USDT"],
"asset_type": "crypto",
"market": "CRYPTO",
"interval_ms": 5000, # Update every 5 seconds
"data_type": "ohlcv"
}
await websocket.send(json.dumps(crypto_subscription))
ack2 = json.loads(await websocket.recv())
print(f"Crypto subscription created: {ack2['subscription_id']}")
print()
# Receive updates
print("Receiving updates from multiple streams:\n")
update_count = 0
while update_count < 20: # Receive 20 updates then stop
message = await websocket.recv()
data = json.loads(message)
if data.get("type") == "data":
stream_type = data["stream_type"]
if stream_type == "price":
for update in data["data"]:
print(
f"💵 PRICE: {update['symbol']} = ${update['price']:.2f} "
f"({update['change_percent']:+.2f}%)"
)
elif stream_type == "ohlcv":
for update in data["data"]:
print(
f"📊 OHLCV: {update['symbol']} - "
f"O:{update['open']:.2f} H:{update['high']:.2f} "
f"L:{update['low']:.2f} C:{update['close']:.2f} "
f"V:{update['volume']:.0f}"
)
update_count += 1
elif data.get("type") == "heartbeat":
print("❤️ Heartbeat")
elif data.get("type") == "error":
print(f"❌ Error: {data['message']}")
# Unsubscribe from equity stream
print("\nUnsubscribing from equity stream...")
unsubscribe = {
"type": "unsubscribe",
"stream_type": "price",
"symbols": None # Unsubscribe from all price streams
}
await websocket.send(json.dumps(unsubscribe))
unsub_ack = json.loads(await websocket.recv())
print(f"Unsubscribed: {unsub_ack}")
except websockets.exceptions.ConnectionClosed:
print("Connection closed")
except KeyboardInterrupt:
print("\nStopped by user")
async def stream_with_error_handling():
"""
Robust example: Automatic reconnection and error handling
"""
print("\n=== Streaming with Auto-Reconnection ===\n")
uri = "ws://localhost:8000/ws/prices/AAPL,GOOGL"
max_retries = 3
retry_delay = 5
for attempt in range(max_retries):
try:
print(f"Connection attempt {attempt + 1}/{max_retries}")
async with websockets.connect(uri) as websocket:
print("✅ Connected successfully")
# Receive subscription ACK
ack = json.loads(await websocket.recv())
print(f"Subscribed to: {', '.join(ack['symbols'])}\n")
# Stream data
while True:
try:
message = await asyncio.wait_for(websocket.recv(), timeout=60)
data = json.loads(message)
if data.get("type") == "data":
timestamp = datetime.fromisoformat(
data["timestamp"].replace("Z", "+00:00")
)
for update in data["data"]:
print(
f"[{timestamp.strftime('%H:%M:%S')}] "
f"{update['symbol']}: ${update['price']:.2f}"
)
except asyncio.TimeoutError:
print("⚠️ No data received in 60s, checking connection...")
# Send a ping to check if connection is alive
await websocket.ping()
except websockets.exceptions.ConnectionClosed as e:
print(f"❌ Connection closed: {e}")
if attempt < max_retries - 1:
print(f"Reconnecting in {retry_delay} seconds...")
await asyncio.sleep(retry_delay)
else:
print("Max retries reached, giving up")
break
except KeyboardInterrupt:
print("\n👋 Stopped by user")
break
async def monitor_multiple_assets():
"""
Practical example: Monitor a portfolio of assets
"""
print("\n=== Portfolio Monitoring ===\n")
# Portfolio to monitor
portfolio = {
"Tech Stocks": ["AAPL", "GOOGL", "MSFT", "NVDA"],
"Crypto": ["BTC/USDT", "ETH/USDT"],
}
uri = "ws://localhost:8000/ws/stream"
try:
async with websockets.connect(uri) as websocket:
print("📊 Starting portfolio monitor...\n")
# Subscribe to tech stocks
tech_sub = {
"type": "subscribe",
"stream_type": "price",
"symbols": portfolio["Tech Stocks"],
"asset_type": "equity",
"market": "US",
"interval_ms": 3000,
"data_type": "price"
}
await websocket.send(json.dumps(tech_sub))
json.loads(await websocket.recv())
print(f"✅ Monitoring tech stocks: {portfolio['Tech Stocks']}")
# Subscribe to crypto
crypto_sub = {
"type": "subscribe",
"stream_type": "price",
"symbols": portfolio["Crypto"],
"asset_type": "crypto",
"market": "CRYPTO",
"interval_ms": 3000,
"data_type": "price"
}
await websocket.send(json.dumps(crypto_sub))
json.loads(await websocket.recv())
print(f"✅ Monitoring crypto: {portfolio['Crypto']}\n")
# Track portfolio value
prices = {}
while True:
message = await websocket.recv()
data = json.loads(message)
if data.get("type") == "data":
for update in data["data"]:
symbol = update["symbol"]
price = update["price"]
change_pct = update["change_percent"]
prices[symbol] = price
# Color code by change
color = "🟢" if change_pct >= 0 else "🔴"
print(
f"{color} {symbol:12} ${price:>10.2f} "
f"({change_pct:>+6.2f}%) "
f"[{update['provider']}]"
)
print(f"\nTracking {len(prices)} assets\n")
except KeyboardInterrupt:
print("\n👋 Portfolio monitor stopped")
async def main():
"""
Run examples
"""
print("=" * 60)
print("FIML WebSocket Streaming Examples")
print("=" * 60)
print("\nMake sure the FIML server is running:")
print(" uvicorn fiml.server:app --reload")
print("\n" + "=" * 60 + "\n")
# Choose which example to run
examples = {
"1": ("Simple Price Streaming", stream_prices_simple),
"2": ("Advanced Multi-Stream", stream_with_full_control),
"3": ("Auto-Reconnection", stream_with_error_handling),
"4": ("Portfolio Monitor", monitor_multiple_assets),
}
print("Available examples:")
for key, (name, _) in examples.items():
print(f" {key}. {name}")
choice = input("\nSelect example (1-4, or 'all'): ").strip()
if choice == "all":
# Run all examples sequentially
for name, func in examples.values():
print(f"\n{'=' * 60}")
print(f"Running: {name}")
print(f"{'=' * 60}\n")
await func()
await asyncio.sleep(2)
elif choice in examples:
name, func = examples[choice]
await func()
else:
print("Invalid choice")
if __name__ == "__main__":
asyncio.run(main())