File size: 16,359 Bytes
b833c19
 
 
 
 
 
 
 
 
 
7b54172
b833c19
 
7b54172
b833c19
 
 
7b54172
 
 
b833c19
7b54172
b833c19
 
7b54172
06c2aae
 
b833c19
06c2aae
 
b833c19
06c2aae
b833c19
 
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b833c19
 
 
 
 
 
 
 
 
 
 
 
06c2aae
 
 
7b54172
 
 
 
b833c19
7b54172
 
 
b833c19
7b54172
b833c19
 
 
 
 
 
 
 
 
 
 
7b54172
b833c19
06c2aae
 
 
7b54172
b833c19
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
06c2aae
 
 
 
 
 
 
b833c19
 
 
 
 
 
 
 
 
 
 
 
06c2aae
 
 
 
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
 
 
 
 
 
 
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
06c2aae
 
b833c19
 
06c2aae
b833c19
 
 
 
 
 
 
 
06c2aae
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
b833c19
7b54172
 
 
 
b833c19
 
 
06c2aae
7b54172
b833c19
 
 
 
 
 
 
 
7b54172
b833c19
7b54172
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06c2aae
b833c19
7b54172
b833c19
 
 
 
 
 
 
 
 
7b54172
b833c19
 
 
 
 
 
 
7b54172
 
b833c19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import time
import json
import hmac
import hashlib
import threading
from typing import Any, Dict, List, Optional, Literal

import requests
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, ValidationError

# -----------------------------------------------------------------------------
# Config
# -----------------------------------------------------------------------------

GATE_API_KEY = os.getenv("GATE_API_KEY")
GATE_API_SECRET = os.getenv("GATE_API_SECRET")
GATE_API_BASE = os.getenv("GATE_API_BASE", "https://api.gate.io/api/v4")

LOG_FILE = os.getenv("TRADE_LOG_FILE", "trading_log.jsonl")
BAL_FILE = os.getenv("BALANCE_SNAP_FILE", "balance_snapshots.jsonl")

LLM_ENDPOINT = os.getenv("LLM_ENDPOINT")
LLM_API_KEY = os.getenv("LLM_API_KEY")

# DRY_RUN = true when explicitly set OR when exchange keys are missing
DRY_RUN = os.getenv("DRY_RUN", "0") == "1" or not (GATE_API_KEY and GATE_API_SECRET)

app = FastAPI(title="gate4-alpha-api", version="0.3.0", docs_url="/docs", redoc_url="/redoc")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # tighten for prod
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

_log_lock = threading.Lock()
_bal_lock = threading.Lock()

# -----------------------------------------------------------------------------
# Models
# -----------------------------------------------------------------------------

class TradeLog(BaseModel):
    timestamp: int = Field(default_factory=lambda: int(time.time()))
    action: Literal["long", "short", "flat", "close"]
    contract: str
    size: float
    entry_price: float
    exit_price: Optional[float] = None
    pnl_realized: float = 0.0
    pnl_estimate: float = 0.0
    reason: Optional[str] = None
    meta: Dict[str, Any] = Field(default_factory=dict)


class BalanceSnapshot(BaseModel):
    timestamp: int
    balance: float


class KPIResponse(BaseModel):
    total_pnl: float
    realized_pnl: float
    trade_count: int
    win_rate: float
    max_drawdown_pct: float
    avg_pnl_per_trade: float
    equity_curve: List[BalanceSnapshot]


class AlphaRequest(BaseModel):
    contract: str
    context: Optional[str] = None
    kpis_override: Optional[Dict[str, float]] = None


class AlphaDecision(BaseModel):
    action: Literal["long", "short", "flat"]
    confidence: float = Field(ge=0.0, le=1.0)
    size_factor: float = Field(ge=0.0, le=1.0)
    spread_bps: float
    kpis: Dict[str, float]
    comment: str
    raw_model_output: Optional[Any] = None


# -----------------------------------------------------------------------------
# File-backed state
# -----------------------------------------------------------------------------

def _safe_read_lines(path: str) -> List[str]:
    if not os.path.exists(path):
        return []
    with open(path, "r") as f:
        return [line for line in f if line.strip()]


def load_trades() -> List[TradeLog]:
    lines = _safe_read_lines(LOG_FILE)
    out: List[TradeLog] = []
    for line in lines:
        try:
            raw = json.loads(line)
            out.append(TradeLog(**raw))
        except (json.JSONDecodeError, ValidationError):
            continue
    return out


def load_balances() -> List[BalanceSnapshot]:
    lines = _safe_read_lines(BAL_FILE)
    out: List[BalanceSnapshot] = []
    for line in lines:
        try:
            raw = json.loads(line)
            out.append(BalanceSnapshot(**raw))
        except (json.JSONDecodeError, ValidationError):
            continue
    return out


def append_trade(trade: TradeLog) -> None:
    with _log_lock, open(LOG_FILE, "a") as f:
        f.write(trade.model_json() + "\n")


def append_balance_snapshot(balance: float) -> BalanceSnapshot:
    snap = BalanceSnapshot(timestamp=int(time.time()), balance=balance)
    with _bal_lock, open(BAL_FILE, "a") as f:
        f.write(snap.model_json() + "\n")
    return snap


# -----------------------------------------------------------------------------
# Gate.io helpers (dry-run aware)
# -----------------------------------------------------------------------------

def sign_request(method: str, path: str, query_string: str, body: str, timestamp: str) -> str:
    message = f"{method}\n{path}\n{query_string}\n{body}\n{timestamp}"
    return hmac.new(
        GATE_API_SECRET.encode(),
        message.encode(),
        hashlib.sha512,
    ).hexdigest()


def gate_private_get(path: str, query: str = "") -> Any:
    if DRY_RUN:
        raise HTTPException(status_code=503, detail="Exchange private API disabled in dry-run mode")

    method = "GET"
    timestamp = str(int(time.time()))
    body = ""

    sign = sign_request(method, path, query, body, timestamp)
    headers = {
        "KEY": GATE_API_KEY,
        "Timestamp": timestamp,
        "SIGN": sign,
    }
    url = f"{GATE_API_BASE}{path}"
    if query:
        url = f"{url}?{query}"

    try:
        res = requests.get(url, headers=headers, timeout=10)
        res.raise_for_status()
    except requests.RequestException as e:
        raise HTTPException(status_code=502, detail=f"Gate.io request failed: {e}")
    return res.json()


def gate_public_get(path: str, query: str = "") -> Any:
    if DRY_RUN:
        raise HTTPException(status_code=503, detail="Exchange public API disabled in dry-run mode")

    url = f"{GATE_API_BASE}{path}"
    if query:
        url = f"{url}?{query}"

    try:
        res = requests.get(url, timeout=10)
        res.raise_for_status()
    except requests.RequestException as e:
        raise HTTPException(status_code=502, detail=f"Gate.io public request failed: {e}")
    return res.json()


def get_futures_account_total_balance() -> float:
    if DRY_RUN:
        # In dry-run: use last balance if exists, else deterministic constant
        balances = load_balances()
        if balances:
            return balances[-1].balance
        return float(os.getenv("DRY_RUN_BALANCE", "10000.0"))

    path = "/futures/usdt/accounts"
    accounts = gate_private_get(path)
    total = 0.0
    for acc in accounts:
        try:
            total += float(acc.get("available", 0.0))
        except (TypeError, ValueError):
            continue
    return total


def get_contract_spread_bps(contract: str) -> float:
    if DRY_RUN:
        # Deterministic spread for offline mode; override via env if needed
        return float(os.getenv("DRY_RUN_SPREAD_BPS", "5.0"))

    path = "/futures/usdt/tickers"
    query = f"contract={contract}"
    tickers = gate_public_get(path, query=query)

    if not tickers:
        raise HTTPException(status_code=404, detail=f"No ticker data for {contract}")

    t = tickers[0]
    try:
        bid = float(t.get("bid", 0.0))
        ask = float(t.get("ask", 0.0))
    except (TypeError, ValueError):
        raise HTTPException(status_code=502, detail="Malformed ticker from Gate.io")

    if bid <= 0 or ask <= 0 or ask <= bid:
        return 0.0

    mid = 0.5 * (bid + ask)
    spread_bps = (ask - bid) / mid * 1e4
    return spread_bps


# -----------------------------------------------------------------------------
# KPI logic
# -----------------------------------------------------------------------------

def compute_kpis(trades: List[TradeLog], balances: List[BalanceSnapshot]) -> KPIResponse:
    realized_pnls = [t.pnl_realized for t in trades]
    est_pnls = [t.pnl_estimate for t in trades]

    realized_pnl = float(sum(realized_pnls))
    total_pnl = float(realized_pnl + sum(est_pnls))

    trade_count = len(trades)
    wins = sum(1 for t in trades if t.pnl_realized > 0)
    win_rate = float(wins / trade_count) if trade_count > 0 else 0.0
    avg_pnl_per_trade = float(realized_pnl / trade_count) if trade_count > 0 else 0.0

    equity_curve = balances
    max_drawdown_pct = 0.0
    if equity_curve:
        peak = equity_curve[0].balance
        for point in equity_curve:
            if point.balance > peak:
                peak = point.balance
            if peak > 0:
                dd = (point.balance - peak) / peak * 100.0
                if dd < max_drawdown_pct:
                    max_drawdown_pct = dd

    return KPIResponse(
        total_pnl=total_pnl,
        realized_pnl=realized_pnl,
        trade_count=trade_count,
        win_rate=win_rate,
        max_drawdown_pct=max_drawdown_pct,
        avg_pnl_per_trade=avg_pnl_per_trade,
        equity_curve=equity_curve,
    )


def kpis_to_feature_dict(kpis: KPIResponse) -> Dict[str, float]:
    return {
        "total_pnl": kpis.total_pnl,
        "realized_pnl": kpis.realized_pnl,
        "trade_count": float(kpis.trade_count),
        "win_rate": kpis.win_rate,
        "max_drawdown_pct": kpis.max_drawdown_pct,
        "avg_pnl_per_trade": kpis.avg_pnl_per_trade,
    }


# -----------------------------------------------------------------------------
# LLM integration (dry-run aware)
# -----------------------------------------------------------------------------

def _build_alpha_prompt(req: AlphaRequest, spread_bps: float, kpis: Dict[str, float]) -> str:
    kpi_json = json.dumps(kpis, sort_keys=True)
    ctx = req.context or ""
    return (
        "You are a deterministic trading policy engine.\n"
        "Given KPIs and spread metrics, choose ONE action: long, short, or flat.\n"
        "Respond ONLY with a compact JSON object:\n"
        '{ "action": "...", "confidence": 0.0-1.0, "size_factor": 0.0-1.0, "comment": "..." }\n\n'
        f"Contract: {req.contract}\n"
        f"Spread_bps: {spread_bps:.4f}\n"
        f"KPIs: {kpi_json}\n"
        f"Context: {ctx}\n"
        "Constraints:\n"
        "- If max_drawdown_pct < -25 or win_rate < 0.4 ⇒ prefer flat.\n"
        "- If trade_count < 10 ⇒ confidence <= 0.4.\n"
        "- size_factor must be <= 0.3 if spread_bps > 10.\n"
    )


def call_llm_for_alpha(prompt: str) -> Dict[str, Any]:
    if not LLM_ENDPOINT:
        # Dry-run LLM: force flat, no external call
        return {
            "action": "flat",
            "confidence": 0.0,
            "size_factor": 0.0,
            "comment": "LLM endpoint not configured; dry-run flat policy.",
        }

    payload = {
        "inputs": prompt,
        "parameters": {
            "max_new_tokens": 256,
            "temperature": 0.1,
            "top_p": 0.9,
            "return_full_text": False,
        },
    }
    headers = {"Content-Type": "application/json"}
    if LLM_API_KEY:
        headers["Authorization"] = f"Bearer {LLM_API_KEY}"

    try:
        res = requests.post(LLM_ENDPOINT, headers=headers, json=payload, timeout=20)
        res.raise_for_status()
    except requests.RequestException as e:
        raise HTTPException(status_code=502, detail=f"LLM request failed: {e}")

    try:
        data = res.json()
    except json.JSONDecodeError:
        raise HTTPException(status_code=502, detail="LLM returned non-JSON payload")

    if isinstance(data, list) and data and isinstance(data[0], dict) and "generated_text" in data[0]:
        text = data[0]["generated_text"]
    elif isinstance(data, dict) and "generated_text" in data:
        text = data["generated_text"]
    else:
        text = str(data)

    text = text.strip()
    start = text.find("{")
    end = text.rfind("}")
    if start == -1 or end == -1 or end <= start:
        raise HTTPException(status_code=502, detail="LLM output missing JSON object")

    snippet = text[start : end + 1]
    try:
        parsed = json.loads(snippet)
    except json.JSONDecodeError as e:
        raise HTTPException(status_code=502, detail=f"LLM JSON parse failed: {e}")

    return parsed


def build_alpha_decision(
    req: AlphaRequest,
    spread_bps: float,
    kpi_features: Dict[str, float],
    raw_model_out: Dict[str, Any],
) -> AlphaDecision:
    action = str(raw_model_out.get("action", "")).lower().strip()
    if action not in ("long", "short", "flat"):
        action = "flat"

    confidence = float(raw_model_out.get("confidence", 0.0))
    size_factor = float(raw_model_out.get("size_factor", 0.0))
    comment = str(raw_model_out.get("comment", "")).strip()[:240]

    if confidence < 0.0:
        confidence = 0.0
    if confidence > 1.0:
        confidence = 1.0
    if size_factor < 0.0:
        size_factor = 0.0
    if size_factor > 1.0:
        size_factor = 1.0

    if kpi_features.get("max_drawdown_pct", 0.0) < -30.0:
        action = "flat"
        confidence = min(confidence, 0.3)
        size_factor = 0.0
        comment = (comment + " [forced_flat_due_to_drawdown]").strip()

    if kpi_features.get("trade_count", 0.0) < 10:
        confidence = min(confidence, 0.4)

    if spread_bps > 10.0 and size_factor > 0.3:
        size_factor = 0.3

    return AlphaDecision(
        action=action,
        confidence=confidence,
        size_factor=size_factor,
        spread_bps=spread_bps,
        kpis=kpi_features,
        comment=comment,
        raw_model_output=raw_model_out if LLM_ENDPOINT else None,
    )


# -----------------------------------------------------------------------------
# Routes
# -----------------------------------------------------------------------------

@app.get("/", response_class=HTMLResponse)
def home() -> str:
    mode = "DRY-RUN" if DRY_RUN else "LIVE"
    return f"""
    <html>
      <body>
        <h2>gate4-alpha-api ({mode})</h2>
        <p>Endpoints:</p>
        <ul>
          <li>GET /balance</li>
          <li>GET /performance</li>
          <li>GET /kpis</li>
          <li>POST /log_trade</li>
          <li>POST /alpha/entry</li>
          <li>GET /openapi.yaml</li>
          <li>GET /docs</li>
        </ul>
      </body>
    </html>
    """


@app.get("/openapi.yaml")
def get_openapi():
    if not os.path.exists("openapi.yaml"):
        raise HTTPException(status_code=404, detail="openapi.yaml not found")
    return FileResponse("openapi.yaml", media_type="text/yaml")


@app.get("/balance")
def get_balance():
    total = get_futures_account_total_balance()
    snap = append_balance_snapshot(total)
    return {
        "timestamp": snap.timestamp,
        "balance": round(snap.balance, 6),
        "dry_run": DRY_RUN,
    }


@app.get("/performance")
def get_performance():
    trades = load_trades()
    balances = load_balances()
    if not trades and not balances:
        return {"summary": "No trades or balances logged yet.", "dry_run": DRY_RUN}

    kpis = compute_kpis(trades, balances)
    summary = (
        f"Total PnL: {kpis.total_pnl:.2f}, "
        f"Realized: {kpis.realized_pnl:.2f}, "
        f"Trades: {kpis.trade_count}, "
        f"Win rate: {kpis.win_rate:.2%}, "
        f"Max DD: {kpis.max_drawdown_pct:.2f}%"
    )

    tail = []
    for t in trades[-5:]:
        tail.append(
            {
                "ts": t.timestamp,
                "action": t.action,
                "contract": t.contract,
                "pnl_realized": t.pnl_realized,
                "pnl_estimate": t.pnl_estimate,
                "reason": t.reason,
            }
        )

    return {
        "summary": summary,
        "last_trades": tail,
        "kpis": kpis.model_dump(),
        "dry_run": DRY_RUN,
    }


@app.get("/kpis", response_model=KPIResponse)
def get_kpis():
    trades = load_trades()
    balances = load_balances()
    return compute_kpis(trades, balances)


@app.post("/log_trade", response_model=TradeLog)
async def log_trade(request: Request):
    payload = await request.json()
    try:
        trade = TradeLog(**payload)
    except ValidationError as e:
        raise HTTPException(status_code=422, detail=e.errors())
    append_trade(trade)
    return trade


@app.post("/alpha/entry", response_model=AlphaDecision)
async def alpha_entry(req: AlphaRequest):
    trades = load_trades()
    balances = load_balances()
    base_kpis = compute_kpis(trades, balances)
    base_features = kpis_to_feature_dict(base_kpis)

    if req.kpis_override:
        features = {**base_features, **req.kpis_override}
    else:
        features = base_features

    spread_bps = get_contract_spread_bps(req.contract)
    prompt = _build_alpha_prompt(req, spread_bps, features)
    raw_model_out = call_llm_for_alpha(prompt)
    decision = build_alpha_decision(req, spread_bps, features, raw_model_out)
    return decision