|
| 1 | +"""Orakle skill — read the Oreka desk's book. |
| 2 | +
|
| 3 | +Drops into `ainara/orakle/skills/trading/`. Read-only: it reports positions and |
| 4 | +history and cannot place, cancel or modify an order. See the README beside this |
| 5 | +file for why the add-on draws that line where it does. |
| 6 | +""" |
| 7 | + |
| 8 | +import logging |
| 9 | +from typing import Annotated, Any, Dict, Literal, Optional |
| 10 | + |
| 11 | +from ainara.framework.skill import Skill |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class TradingOrekaDesk(Skill): |
| 17 | + """Live and historical view of the Oreka delta-neutral desk.""" |
| 18 | + |
| 19 | + matcher_info = ( |
| 20 | + "Use this skill to report on the Oreka delta-neutral funding-carry desk:" |
| 21 | + " open hedged positions across Hyperliquid and dYdX, hedge health," |
| 22 | + " liquidation distance, funding earned, closed round trips, and whether" |
| 23 | + " the strategy realized the edge it predicted. Read-only; it never places" |
| 24 | + " an order. Keywords: my book, my positions, delta neutral, carry desk," |
| 25 | + " hedge health, funding earned, realized pnl, how is the desk doing." |
| 26 | + ) |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + super().__init__() |
| 30 | + self.name = "oreka_desk" |
| 31 | + self.logger = logging.getLogger(__name__) |
| 32 | + |
| 33 | + def run( |
| 34 | + self, |
| 35 | + action: Annotated[ |
| 36 | + Literal["status", "review", "analytics"], |
| 37 | + "'status' = positions open right now, with hedge health, liquidation" |
| 38 | + " distance and the funding each leg is paying or receiving." |
| 39 | + " 'review' = closed round trips reconstructed from venue history." |
| 40 | + " 'analytics' = each recorded trade's PREDICTED edge against what it" |
| 41 | + " actually realized, which is the one that answers whether the model" |
| 42 | + " is right rather than whether the plumbing works.", |
| 43 | + ] = "status", |
| 44 | + coin: Annotated[ |
| 45 | + str, |
| 46 | + "Which asset(s). Default 'ALL' — the whole book at once; use it" |
| 47 | + " whenever the user does not name one specific asset. Pass a single" |
| 48 | + " symbol (BTC, ETH, SOL, ...) ONLY when they explicitly ask about that" |
| 49 | + " one. Do NOT default to BTC.", |
| 50 | + ] = "ALL", |
| 51 | + lookback_days: Annotated[ |
| 52 | + Optional[float], |
| 53 | + "For 'review': how far back to reconstruct closed trades. Leave unset" |
| 54 | + " unless the user names a period - the default is derived from the" |
| 55 | + " strategy's own expected hold (at least 90 days), and a shorter" |
| 56 | + " window cannot see a completed trade at all.", |
| 57 | + ] = None, |
| 58 | + ) -> Dict[str, Any]: |
| 59 | + """Report the desk's book. Places no orders and signs nothing.""" |
| 60 | + try: |
| 61 | + from oreka.portfolio import TradingPortfolio |
| 62 | + except ImportError as e: |
| 63 | + return _not_installed(e) |
| 64 | + |
| 65 | + try: |
| 66 | + return TradingPortfolio().run( |
| 67 | + action=action, coin=coin, lookback_days=lookback_days) |
| 68 | + except Exception as e: |
| 69 | + # A read that failed is reported as a failure. It must never come |
| 70 | + # back looking like an empty book, which reads as "you hold nothing". |
| 71 | + self.logger.warning("oreka_desk %s failed: %s", action, e) |
| 72 | + return {"error": f"could not read the desk: {type(e).__name__}: {e}", |
| 73 | + "action": action, "coin": coin} |
| 74 | + |
| 75 | + |
| 76 | +def _not_installed(exc): |
| 77 | + return { |
| 78 | + "error": "Oreka is not installed in this environment, so the desk cannot" |
| 79 | + " be read. Install it into the environment Orakle runs in" |
| 80 | + f" (pip install -e <oreka>). Import failed with: {exc}", |
| 81 | + "installed": False, |
| 82 | + } |
0 commit comments