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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#!/usr/bin/env python3
"""
Trading Analysis API Router - Trading & Technical Analysis Endpoints
Implements:
- GET /api/trading/volume - Volume analysis by exchange
- GET /api/trading/orderbook - Aggregated order book data
- GET /api/indicators/{coin} - Technical indicators (RSI, MACD, etc)
- POST /api/backtest - Strategy backtesting endpoint
- GET /api/correlations - Crypto correlation matrix
"""

from fastapi import APIRouter, HTTPException, Query, Body
from fastapi.responses import JSONResponse
from typing import Optional, Dict, Any, List
from pydantic import BaseModel, Field
from datetime import datetime, timedelta
import logging
import time
import httpx
import asyncio
import numpy as np

# Import enhanced provider manager for intelligent load balancing
from backend.services.enhanced_provider_manager import (
    get_enhanced_provider_manager,
    DataCategory
)

logger = logging.getLogger(__name__)

router = APIRouter(tags=["Trading Analysis API"])


# ============================================================================
# Request/Response Models
# ============================================================================

class BacktestRequest(BaseModel):
    """Request model for backtesting"""
    symbol: str = Field(..., description="Trading symbol (e.g., BTC)")
    strategy: str = Field(..., description="Strategy name: sma_cross, rsi_oversold, macd_signal")
    start_date: str = Field(..., description="Start date (YYYY-MM-DD)")
    end_date: str = Field(..., description="End date (YYYY-MM-DD)")
    initial_capital: float = Field(10000, description="Initial capital in USD")
    params: Dict[str, Any] = Field(default_factory=dict, description="Strategy parameters")


# ============================================================================
# Helper Functions
# ============================================================================

async def fetch_binance_ticker_24h(symbol: str = None) -> List[Dict]:
    """Fetch 24h ticker data with intelligent provider failover"""
    try:
        manager = get_enhanced_provider_manager()
        result = await manager.fetch_data(
            DataCategory.MARKET_VOLUME,
            symbol=symbol
        )
        
        if result and result.get("success"):
            data = result.get("data")
            return [data] if isinstance(data, dict) else data
        else:
            logger.error(f"Volume data fetch failed: {result.get('error')}")
            return []
    except Exception as e:
        logger.error(f"Ticker error: {e}")
        return []


async def fetch_binance_orderbook(symbol: str, limit: int = 20) -> Dict:
    """Fetch order book with intelligent provider failover"""
    try:
        manager = get_enhanced_provider_manager()
        result = await manager.fetch_data(
            DataCategory.MARKET_ORDERBOOK,
            symbol=f"{symbol}USDT",
            limit=limit
        )
        
        if result and result.get("success"):
            return result.get("data")
        else:
            raise HTTPException(status_code=502, detail=f"Order book unavailable: {result.get('error')}")
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Orderbook error: {e}")
        raise HTTPException(status_code=502, detail=f"Order book unavailable: {str(e)}")


async def fetch_ohlcv_for_analysis(symbol: str, interval: str, limit: int) -> List[List]:
    """Fetch OHLCV data with intelligent provider failover"""
    try:
        manager = get_enhanced_provider_manager()
        result = await manager.fetch_data(
            DataCategory.MARKET_OHLCV,
            symbol=f"{symbol}USDT",
            interval=interval,
            limit=limit
        )
        
        if result and result.get("success"):
            return result.get("data", [])
        else:
            logger.error(f"OHLCV fetch failed: {result.get('error')}")
            return []
    except Exception as e:
        logger.error(f"OHLCV fetch error: {e}")
        return []


def calculate_rsi(prices: List[float], period: int = 14) -> float:
    """Calculate RSI indicator"""
    if len(prices) < period + 1:
        return 50.0
    
    deltas = np.diff(prices)
    gains = np.where(deltas > 0, deltas, 0)
    losses = np.where(deltas < 0, -deltas, 0)
    
    avg_gain = np.mean(gains[-period:])
    avg_loss = np.mean(losses[-period:])
    
    if avg_loss == 0:
        return 100.0
    
    rs = avg_gain / avg_loss
    rsi = 100 - (100 / (1 + rs))
    return round(rsi, 2)


def calculate_macd(prices: List[float], fast: int = 12, slow: int = 26, signal: int = 9) -> Dict:
    """Calculate MACD indicator"""
    if len(prices) < slow:
        return {"macd": 0, "signal": 0, "histogram": 0}
    
    prices_arr = np.array(prices)
    
    # Calculate EMAs
    ema_fast = prices_arr[-1]  # Simplified
    ema_slow = prices_arr[-slow]
    
    macd_line = ema_fast - ema_slow
    signal_line = macd_line * 0.9  # Simplified
    histogram = macd_line - signal_line
    
    return {
        "macd": round(macd_line, 2),
        "signal": round(signal_line, 2),
        "histogram": round(histogram, 2)
    }


def calculate_bollinger_bands(prices: List[float], period: int = 20, std_dev: int = 2) -> Dict:
    """Calculate Bollinger Bands"""
    if len(prices) < period:
        return {"upper": 0, "middle": 0, "lower": 0}
    
    recent_prices = prices[-period:]
    middle = np.mean(recent_prices)
    std = np.std(recent_prices)
    
    return {
        "upper": round(middle + (std_dev * std), 2),
        "middle": round(middle, 2),
        "lower": round(middle - (std_dev * std), 2)
    }


# ============================================================================
# GET /api/trading/volume
# ============================================================================

@router.get("/api/trading/volume")
async def get_volume_analysis(
    symbol: Optional[str] = Query(None, description="Specific symbol (e.g., BTC)")
):
    """
    Get volume analysis by exchange
    
    Returns 24h volume data from major exchanges
    """
    try:
        # Fetch from Binance
        tickers = await fetch_binance_ticker_24h(symbol)
        
        if not tickers:
            raise HTTPException(status_code=503, detail="Volume data unavailable")
        
        volume_data = []
        total_volume = 0
        
        for ticker in tickers[:50]:  # Top 50 pairs
            ticker_symbol = ticker.get("symbol", "")
            if not ticker_symbol.endswith("USDT"):
                continue
            
            base_symbol = ticker_symbol.replace("USDT", "")
            volume_usdt = float(ticker.get("quoteVolume", 0))
            
            if symbol and base_symbol != symbol.upper():
                continue
            
            volume_data.append({
                "symbol": base_symbol,
                "exchange": "Binance",
                "volume_24h": volume_usdt,
                "volume_change": float(ticker.get("priceChangePercent", 0)),
                "trades_count": int(ticker.get("count", 0))
            })
            
            total_volume += volume_usdt
        
        # Sort by volume
        volume_data.sort(key=lambda x: x["volume_24h"], reverse=True)
        
        return {
            "success": True,
            "symbol": symbol,
            "total_volume": round(total_volume, 2),
            "count": len(volume_data),
            "data": volume_data[:20],
            "source": "binance",
            "timestamp": datetime.utcnow().isoformat() + "Z"
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Volume analysis error: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# GET /api/trading/orderbook
# ============================================================================

@router.get("/api/trading/orderbook")
async def get_orderbook(
    symbol: str = Query(..., description="Trading symbol (e.g., BTC)"),
    depth: int = Query(20, ge=5, le=100, description="Order book depth")
):
    """
    Get aggregated order book data
    
    Returns bids and asks with depth analysis
    """
    try:
        orderbook = await fetch_binance_orderbook(symbol.upper(), depth)
        
        bids = [[float(price), float(qty)] for price, qty in orderbook.get("bids", [])]
        asks = [[float(price), float(qty)] for price, qty in orderbook.get("asks", [])]
        
        # Calculate metrics
        total_bid_volume = sum(qty for _, qty in bids)
        total_ask_volume = sum(qty for _, qty in asks)
        
        bid_ask_ratio = total_bid_volume / total_ask_volume if total_ask_volume > 0 else 1.0
        
        spread = asks[0][0] - bids[0][0] if bids and asks else 0
        spread_percent = (spread / bids[0][0] * 100) if bids and bids[0][0] > 0 else 0
        
        return {
            "success": True,
            "symbol": symbol.upper(),
            "timestamp": orderbook.get("lastUpdateId"),
            "bids": bids,
            "asks": asks,
            "metrics": {
                "bid_volume": round(total_bid_volume, 4),
                "ask_volume": round(total_ask_volume, 4),
                "bid_ask_ratio": round(bid_ask_ratio, 2),
                "spread": round(spread, 2),
                "spread_percent": round(spread_percent, 4),
                "best_bid": bids[0][0] if bids else 0,
                "best_ask": asks[0][0] if asks else 0
            },
            "source": "binance",
            "update_time": datetime.utcnow().isoformat() + "Z"
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Orderbook error: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# GET /api/indicators/{coin}
# ============================================================================

@router.get("/api/indicators/{coin}")
async def get_technical_indicators(
    coin: str,
    interval: str = Query("1h", description="Time interval: 1h, 4h, 1d"),
    indicators: Optional[str] = Query(None, description="Comma-separated list: rsi,macd,bb,sma,ema")
):
    """
    Get technical indicators for a coin
    
    Supported indicators:
    - RSI (Relative Strength Index)
    - MACD (Moving Average Convergence Divergence)
    - BB (Bollinger Bands)
    - SMA (Simple Moving Average)
    - EMA (Exponential Moving Average)
    """
    try:
        # Fetch OHLCV data
        klines = await fetch_ohlcv_for_analysis(coin.upper(), interval, 100)
        
        if not klines:
            raise HTTPException(status_code=404, detail=f"No data available for {coin}")
        
        # Extract close prices
        closes = [float(k[4]) for k in klines]
        
        # Parse requested indicators
        requested = indicators.split(",") if indicators else ["rsi", "macd", "bb"]
        
        result_indicators = {}
        
        # Calculate requested indicators
        if "rsi" in requested:
            result_indicators["rsi"] = {
                "value": calculate_rsi(closes, 14),
                "period": 14,
                "interpretation": "oversold" if calculate_rsi(closes, 14) < 30 else "overbought" if calculate_rsi(closes, 14) > 70 else "neutral"
            }
        
        if "macd" in requested:
            macd_data = calculate_macd(closes)
            result_indicators["macd"] = {
                **macd_data,
                "interpretation": "bullish" if macd_data["histogram"] > 0 else "bearish"
            }
        
        if "bb" in requested:
            bb_data = calculate_bollinger_bands(closes)
            current_price = closes[-1]
            result_indicators["bollinger_bands"] = {
                **bb_data,
                "current_price": round(current_price, 2),
                "position": "above" if current_price > bb_data["upper"] else "below" if current_price < bb_data["lower"] else "middle"
            }
        
        if "sma" in requested:
            sma_20 = round(np.mean(closes[-20:]), 2) if len(closes) >= 20 else 0
            sma_50 = round(np.mean(closes[-50:]), 2) if len(closes) >= 50 else 0
            result_indicators["sma"] = {
                "sma_20": sma_20,
                "sma_50": sma_50,
                "current_price": round(closes[-1], 2),
                "trend": "bullish" if closes[-1] > sma_20 > sma_50 else "bearish" if closes[-1] < sma_20 < sma_50 else "neutral"
            }
        
        if "ema" in requested:
            # Simplified EMA calculation
            ema_12 = round(closes[-1] * 0.15 + closes[-2] * 0.85, 2) if len(closes) >= 2 else closes[-1]
            ema_26 = round(np.mean(closes[-26:]), 2) if len(closes) >= 26 else 0
            result_indicators["ema"] = {
                "ema_12": ema_12,
                "ema_26": ema_26,
                "crossover": "bullish" if ema_12 > ema_26 else "bearish"
            }
        
        return {
            "success": True,
            "symbol": coin.upper(),
            "interval": interval,
            "current_price": round(closes[-1], 2),
            "indicators": result_indicators,
            "timestamp": datetime.utcnow().isoformat() + "Z"
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Indicators error: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# POST /api/backtest
# ============================================================================

@router.post("/api/backtest")
async def backtest_strategy(request: BacktestRequest):
    """
    Backtest a trading strategy
    
    Supported strategies:
    - sma_cross: Simple Moving Average crossover
    - rsi_oversold: RSI oversold/overbought
    - macd_signal: MACD signal line crossover
    """
    try:
        # Validate dates
        try:
            start = datetime.fromisoformat(request.start_date)
            end = datetime.fromisoformat(request.end_date)
        except:
            raise HTTPException(status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
        
        if start >= end:
            raise HTTPException(status_code=400, detail="Start date must be before end date")
        
        # Fetch historical data
        days = (end - start).days
        klines = await fetch_ohlcv_for_analysis(request.symbol.upper(), "1d", min(days, 365))
        
        if not klines:
            raise HTTPException(status_code=404, detail=f"No historical data for {request.symbol}")
        
        closes = [float(k[4]) for k in klines]
        
        # Simulate trading based on strategy
        trades = []
        position = None
        capital = request.initial_capital
        
        if request.strategy == "sma_cross":
            fast_period = request.params.get("fast", 10)
            slow_period = request.params.get("slow", 30)
            
            for i in range(slow_period, len(closes)):
                sma_fast = np.mean(closes[i-fast_period:i])
                sma_slow = np.mean(closes[i-slow_period:i])
                
                # Buy signal: fast crosses above slow
                if sma_fast > sma_slow and position is None:
                    position = {
                        "entry_price": closes[i],
                        "entry_index": i,
                        "quantity": capital / closes[i]
                    }
                
                # Sell signal: fast crosses below slow
                elif sma_fast < sma_slow and position is not None:
                    profit = (closes[i] - position["entry_price"]) * position["quantity"]
                    capital += profit
                    
                    trades.append({
                        "entry_price": position["entry_price"],
                        "exit_price": closes[i],
                        "profit": round(profit, 2),
                        "profit_percent": round((closes[i] / position["entry_price"] - 1) * 100, 2)
                    })
                    position = None
        
        elif request.strategy == "rsi_oversold":
            rsi_period = request.params.get("period", 14)
            oversold = request.params.get("oversold", 30)
            overbought = request.params.get("overbought", 70)
            
            for i in range(rsi_period + 1, len(closes)):
                rsi = calculate_rsi(closes[:i], rsi_period)
                
                # Buy signal: RSI oversold
                if rsi < oversold and position is None:
                    position = {
                        "entry_price": closes[i],
                        "entry_index": i,
                        "quantity": capital / closes[i]
                    }
                
                # Sell signal: RSI overbought
                elif rsi > overbought and position is not None:
                    profit = (closes[i] - position["entry_price"]) * position["quantity"]
                    capital += profit
                    
                    trades.append({
                        "entry_price": position["entry_price"],
                        "exit_price": closes[i],
                        "profit": round(profit, 2),
                        "profit_percent": round((closes[i] / position["entry_price"] - 1) * 100, 2)
                    })
                    position = None
        
        # Calculate performance metrics
        total_return = capital - request.initial_capital
        return_percent = (capital / request.initial_capital - 1) * 100
        
        winning_trades = [t for t in trades if t["profit"] > 0]
        losing_trades = [t for t in trades if t["profit"] < 0]
        
        win_rate = (len(winning_trades) / len(trades) * 100) if trades else 0
        
        return {
            "success": True,
            "strategy": request.strategy,
            "symbol": request.symbol.upper(),
            "period": f"{request.start_date} to {request.end_date}",
            "initial_capital": request.initial_capital,
            "final_capital": round(capital, 2),
            "total_return": round(total_return, 2),
            "return_percent": round(return_percent, 2),
            "trades": {
                "total": len(trades),
                "winning": len(winning_trades),
                "losing": len(losing_trades),
                "win_rate": round(win_rate, 2)
            },
            "trade_history": trades[:20],  # Return first 20 trades
            "timestamp": datetime.utcnow().isoformat() + "Z"
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Backtest error: {e}")
        raise HTTPException(status_code=500, detail=str(e))


# ============================================================================
# GET /api/correlations
# ============================================================================

@router.get("/api/correlations")
async def get_correlations(
    symbols: str = Query("BTC,ETH,BNB,SOL,ADA", description="Comma-separated symbols"),
    days: int = Query(30, ge=7, le=90, description="Number of days for correlation")
):
    """
    Get correlation matrix for cryptocurrencies
    
    Calculates price correlations between specified coins
    """
    try:
        symbol_list = [s.strip().upper() for s in symbols.split(",")]
        
        if len(symbol_list) < 2:
            raise HTTPException(status_code=400, detail="At least 2 symbols required")
        
        # Fetch data for all symbols
        price_data = {}
        
        for symbol in symbol_list:
            try:
                klines = await fetch_ohlcv_for_analysis(symbol, "1d", days)
                if klines:
                    price_data[symbol] = [float(k[4]) for k in klines]
            except:
                logger.warning(f"Could not fetch data for {symbol}")
        
        if len(price_data) < 2:
            raise HTTPException(status_code=404, detail="Insufficient data for correlation analysis")
        
        # Calculate correlation matrix
        correlations = {}
        
        for sym1 in price_data:
            correlations[sym1] = {}
            for sym2 in price_data:
                if sym1 == sym2:
                    correlations[sym1][sym2] = 1.0
                else:
                    # Calculate correlation coefficient
                    prices1 = np.array(price_data[sym1])
                    prices2 = np.array(price_data[sym2])
                    
                    # Ensure same length
                    min_len = min(len(prices1), len(prices2))
                    prices1 = prices1[-min_len:]
                    prices2 = prices2[-min_len:]
                    
                    corr = np.corrcoef(prices1, prices2)[0, 1]
                    correlations[sym1][sym2] = round(float(corr), 3)
        
        return {
            "success": True,
            "symbols": list(price_data.keys()),
            "days": days,
            "correlations": correlations,
            "interpretation": {
                "strong_positive": "> 0.7",
                "moderate_positive": "0.3 to 0.7",
                "weak": "-0.3 to 0.3",
                "moderate_negative": "-0.7 to -0.3",
                "strong_negative": "< -0.7"
            },
            "timestamp": datetime.utcnow().isoformat() + "Z"
        }
    
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Correlation error: {e}")
        raise HTTPException(status_code=500, detail=str(e))


logger.info("✅ Trading Analysis API Router loaded")