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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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
"""
WebSocket Server for Real-time Trading Data

Provides real-time updates for:
- Market data streaming
- Trading signals
- Portfolio updates
- System alerts
"""

import asyncio
import websockets
import json
import logging
import threading
import time
from datetime import datetime, timedelta
from typing import Dict, Any, List, Optional
import pandas as pd
import os
import sys

# Add project root to path
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from agentic_ai_system.main import load_config
from agentic_ai_system.data_ingestion import load_data, add_technical_indicators
from agentic_ai_system.alpaca_broker import AlpacaBroker
from agentic_ai_system.finrl_agent import FinRLAgent, FinRLConfig

class TradingWebSocketServer:
    def __init__(self, host="localhost", port=8765):
        self.host = host
        self.port = port
        self.clients = set()
        self.config = None
        self.alpaca_broker = None
        self.finrl_agent = None
        self.trading_active = False
        self.market_data = None
        self.portfolio_data = {}
        
        # Setup logging
        logging.basicConfig(level=logging.INFO)
        self.logger = logging.getLogger(__name__)
    
    async def register(self, websocket):
        """Register a new client"""
        self.clients.add(websocket)
        self.logger.info(f"Client connected. Total clients: {len(self.clients)}")
        
        # Send initial data
        await self.send_initial_data(websocket)
    
    async def unregister(self, websocket):
        """Unregister a client"""
        self.clients.remove(websocket)
        self.logger.info(f"Client disconnected. Total clients: {len(self.clients)}")
    
    async def send_initial_data(self, websocket):
        """Send initial data to new client"""
        initial_data = {
            "type": "initial_data",
            "timestamp": datetime.now().isoformat(),
            "config": self.config,
            "portfolio": self.portfolio_data,
            "trading_status": self.trading_active
        }
        await websocket.send(json.dumps(initial_data))
    
    async def broadcast(self, message):
        """Broadcast message to all connected clients"""
        if self.clients:
            message_str = json.dumps(message)
            await asyncio.gather(
                *[client.send(message_str) for client in self.clients],
                return_exceptions=True
            )
    
    async def handle_market_data(self):
        """Handle real-time market data updates"""
        while True:
            try:
                if self.config and self.alpaca_broker:
                    # Get real-time market data
                    symbol = self.config['trading']['symbol']
                    
                    # Get current price
                    current_price = await self.get_current_price(symbol)
                    
                    if current_price:
                        market_update = {
                            "type": "market_data",
                            "timestamp": datetime.now().isoformat(),
                            "symbol": symbol,
                            "price": current_price,
                            "volume": await self.get_current_volume(symbol)
                        }
                        
                        await self.broadcast(market_update)
                        self.logger.info(f"Broadcasted market data for {symbol}: ${current_price}")
                
                await asyncio.sleep(1)  # Update every second
                
            except Exception as e:
                self.logger.error(f"Error in market data handler: {e}")
                await asyncio.sleep(5)  # Wait before retrying
    
    async def handle_portfolio_updates(self):
        """Handle portfolio updates"""
        while True:
            try:
                if self.alpaca_broker:
                    # Get portfolio information
                    account_info = self.alpaca_broker.get_account_info()
                    positions = self.alpaca_broker.get_positions()
                    
                    if account_info:
                        portfolio_update = {
                            "type": "portfolio_update",
                            "timestamp": datetime.now().isoformat(),
                            "account": {
                                "buying_power": float(account_info['buying_power']),
                                "portfolio_value": float(account_info['portfolio_value']),
                                "equity": float(account_info['equity']),
                                "cash": float(account_info['cash'])
                            },
                            "positions": positions if positions else []
                        }
                        
                        await self.broadcast(portfolio_update)
                        self.portfolio_data = portfolio_update
                
                await asyncio.sleep(5)  # Update every 5 seconds
                
            except Exception as e:
                self.logger.error(f"Error in portfolio updates: {e}")
                await asyncio.sleep(10)  # Wait before retrying
    
    async def handle_trading_signals(self):
        """Handle trading signals from FinRL agent"""
        while True:
            try:
                if self.trading_active and self.finrl_agent and self.market_data is not None:
                    # Generate trading signals
                    signal = await self.generate_trading_signal()
                    
                    if signal:
                        signal_update = {
                            "type": "trading_signal",
                            "timestamp": datetime.now().isoformat(),
                            "signal": signal
                        }
                        
                        await self.broadcast(signal_update)
                        self.logger.info(f"Broadcasted trading signal: {signal}")
                
                await asyncio.sleep(10)  # Generate signals every 10 seconds
                
            except Exception as e:
                self.logger.error(f"Error in trading signals: {e}")
                await asyncio.sleep(30)  # Wait before retrying
    
    async def get_current_price(self, symbol):
        """Get current price for symbol"""
        try:
            if self.alpaca_broker:
                # Get latest price from Alpaca
                latest_trade = self.alpaca_broker.get_latest_trade(symbol)
                if latest_trade:
                    return float(latest_trade['p'])
            return None
        except Exception as e:
            self.logger.error(f"Error getting current price: {e}")
            return None
    
    async def get_current_volume(self, symbol):
        """Get current volume for symbol"""
        try:
            if self.alpaca_broker:
                # Get latest trade volume
                latest_trade = self.alpaca_broker.get_latest_trade(symbol)
                if latest_trade:
                    return int(latest_trade['s'])
            return None
        except Exception as e:
            self.logger.error(f"Error getting current volume: {e}")
            return None
    
    async def generate_trading_signal(self):
        """Generate trading signal using FinRL agent"""
        try:
            if self.finrl_agent and self.market_data is not None:
                # Use recent data for prediction
                recent_data = self.market_data.tail(100)
                
                prediction_result = self.finrl_agent.predict(
                    data=recent_data,
                    config=self.config,
                    use_real_broker=False
                )
                
                if prediction_result['success']:
                    # Generate signal based on prediction
                    current_price = await self.get_current_price(self.config['trading']['symbol'])
                    
                    if current_price:
                        signal = {
                            "action": "HOLD",  # Default action
                            "confidence": 0.5,
                            "price": current_price,
                            "reasoning": "Model prediction"
                        }
                        
                        # Determine action based on prediction
                        if prediction_result['total_return'] > 0.02:  # 2% positive return
                            signal["action"] = "BUY"
                            signal["confidence"] = min(0.9, 0.5 + abs(prediction_result['total_return']))
                        elif prediction_result['total_return'] < -0.02:  # 2% negative return
                            signal["action"] = "SELL"
                            signal["confidence"] = min(0.9, 0.5 + abs(prediction_result['total_return']))
                        
                        return signal
            
            return None
        except Exception as e:
            self.logger.error(f"Error generating trading signal: {e}")
            return None
    
    async def handle_client_message(self, websocket, message):
        """Handle incoming client messages"""
        try:
            data = json.loads(message)
            message_type = data.get("type")
            
            if message_type == "load_config":
                # Load configuration
                config_file = data.get("config_file", "config.yaml")
                self.config = load_config(config_file)
                
                response = {
                    "type": "config_loaded",
                    "success": True,
                    "config": self.config
                }
                await websocket.send(json.dumps(response))
            
            elif message_type == "connect_alpaca":
                # Connect to Alpaca
                api_key = data.get("api_key")
                secret_key = data.get("secret_key")
                
                if api_key and secret_key:
                    self.config['alpaca']['api_key'] = api_key
                    self.config['alpaca']['secret_key'] = secret_key
                    self.config['execution']['broker_api'] = 'alpaca_paper'
                    
                    self.alpaca_broker = AlpacaBroker(self.config)
                    
                    response = {
                        "type": "alpaca_connected",
                        "success": True
                    }
                    await websocket.send(json.dumps(response))
                else:
                    response = {
                        "type": "alpaca_connected",
                        "success": False,
                        "error": "Missing API credentials"
                    }
                    await websocket.send(json.dumps(response))
            
            elif message_type == "start_trading":
                # Start trading
                self.trading_active = True
                
                response = {
                    "type": "trading_started",
                    "success": True
                }
                await websocket.send(json.dumps(response))
                
                # Broadcast to all clients
                await self.broadcast({
                    "type": "trading_status",
                    "active": True,
                    "timestamp": datetime.now().isoformat()
                })
            
            elif message_type == "stop_trading":
                # Stop trading
                self.trading_active = False
                
                response = {
                    "type": "trading_stopped",
                    "success": True
                }
                await websocket.send(json.dumps(response))
                
                # Broadcast to all clients
                await self.broadcast({
                    "type": "trading_status",
                    "active": False,
                    "timestamp": datetime.now().isoformat()
                })
            
            elif message_type == "load_data":
                # Load market data
                if self.config:
                    self.market_data = load_data(self.config)
                    if self.market_data is not None:
                        self.market_data = add_technical_indicators(self.market_data)
                        
                        response = {
                            "type": "data_loaded",
                            "success": True,
                            "data_points": len(self.market_data)
                        }
                    else:
                        response = {
                            "type": "data_loaded",
                            "success": False,
                            "error": "Failed to load data"
                        }
                else:
                    response = {
                        "type": "data_loaded",
                        "success": False,
                        "error": "Configuration not loaded"
                    }
                
                await websocket.send(json.dumps(response))
            
            elif message_type == "train_model":
                # Train FinRL model
                if self.market_data is not None:
                    algorithm = data.get("algorithm", "PPO")
                    learning_rate = data.get("learning_rate", 0.0003)
                    training_steps = data.get("training_steps", 100000)
                    
                    finrl_config = FinRLConfig(
                        algorithm=algorithm,
                        learning_rate=learning_rate,
                        batch_size=64,
                        buffer_size=1000000,
                        learning_starts=100,
                        gamma=0.99,
                        tau=0.005,
                        train_freq=1,
                        gradient_steps=1,
                        verbose=1,
                        tensorboard_log='logs/finrl_tensorboard'
                    )
                    
                    self.finrl_agent = FinRLAgent(finrl_config)
                    
                    # Train in background thread
                    def train_model():
                        try:
                            result = self.finrl_agent.train(
                                data=self.market_data,
                                config=self.config,
                                total_timesteps=training_steps,
                                use_real_broker=False
                            )
                            
                            # Broadcast training completion
                            asyncio.create_task(self.broadcast({
                                "type": "training_completed",
                                "success": result['success'],
                                "result": result
                            }))
                        except Exception as e:
                            asyncio.create_task(self.broadcast({
                                "type": "training_completed",
                                "success": False,
                                "error": str(e)
                            }))
                    
                    training_thread = threading.Thread(target=train_model)
                    training_thread.daemon = True
                    training_thread.start()
                    
                    response = {
                        "type": "training_started",
                        "success": True
                    }
                else:
                    response = {
                        "type": "training_started",
                        "success": False,
                        "error": "Market data not loaded"
                    }
                
                await websocket.send(json.dumps(response))
            
            else:
                # Unknown message type
                response = {
                    "type": "error",
                    "message": f"Unknown message type: {message_type}"
                }
                await websocket.send(json.dumps(response))
                
        except json.JSONDecodeError:
            response = {
                "type": "error",
                "message": "Invalid JSON message"
            }
            await websocket.send(json.dumps(response))
        except Exception as e:
            response = {
                "type": "error",
                "message": f"Server error: {str(e)}"
            }
            await websocket.send(json.dumps(response))
    
    async def websocket_handler(self, websocket, path):
        """Main WebSocket handler"""
        await self.register(websocket)
        try:
            async for message in websocket:
                await self.handle_client_message(websocket, message)
        except websockets.exceptions.ConnectionClosed:
            pass
        finally:
            await self.unregister(websocket)
    
    async def start_server(self):
        """Start the WebSocket server"""
        # Start background tasks
        asyncio.create_task(self.handle_market_data())
        asyncio.create_task(self.handle_portfolio_updates())
        asyncio.create_task(self.handle_trading_signals())
        
        # Start WebSocket server
        server = await websockets.serve(
            self.websocket_handler,
            self.host,
            self.port
        )
        
        self.logger.info(f"WebSocket server started on ws://{self.host}:{self.port}")
        
        # Keep server running
        await server.wait_closed()
    
    def run_server(self):
        """Run the server in a separate thread"""
        def run():
            asyncio.run(self.start_server())
        
        server_thread = threading.Thread(target=run)
        server_thread.daemon = True
        server_thread.start()
        
        return server_thread

def create_websocket_server(host="localhost", port=8765):
    """Create and return a WebSocket server instance"""
    return TradingWebSocketServer(host=host, port=port)