Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apt-get update \

WORKDIR /app
COPY . .
RUN pip install --no-cache-dir -e ".[mcp]"
RUN pip install --no-cache-dir -e ".[mcp,telegram]"

# Persistent state volume (Railway mounts here)
RUN mkdir -p /data
Expand Down
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<img src="https://img.shields.io/badge/strategies-14-C9A84C" alt="Strategies" />
<img src="https://img.shields.io/badge/tests-483%20passing-brightgreen" alt="Tests" />
<img src="https://img.shields.io/badge/license-MIT-blue" alt="License" />
<img src="https://img.shields.io/badge/MCP-16%20tools-8A2BE2" alt="MCP" />
<img src="https://img.shields.io/badge/MCP-18%20tools-8A2BE2" alt="MCP" />
</p>

<p align="center">
Expand Down Expand Up @@ -75,6 +75,20 @@ hl run engine_mm -i ETH-PERP --tick 10 --mainnet
hl apex run --mainnet
```

### Funding Hedge

Propose a read-only BTCSWP funding-rate hedge from the CLI or any MCP client. This is the public, pure-math slice of the Nunchi funding hedge: same-side BTCSWP, 1/15 notional by default, no order execution.

```bash
hl hedge propose --asset BTC --side long --perp-notional 150000 --funding-apr 42
hl hedge propose --asset BTC --side long --perp-notional 150000 --funding-rate-8h 0.0003 --json
hl hedge backtest --csv funding.csv --asset BTC --side long --perp-notional 150000
```

Backtest CSVs need a `funding_rate_8h`, `perp_funding_rate_8h`, `funding_rate`, or `rate` column. Add `hedge_rate_8h`, `btcswp_rate_8h`, or `btcswp_funding_rate_8h` when you have realized BTCSWP rates; otherwise the backtest uses an idealized offset.

MCP tools: `funding_hedge_propose`, `funding_hedge_backtest`

---

## Strategies
Expand Down Expand Up @@ -119,7 +133,7 @@ Supporting strategies for portfolio management, block liquidity, and autonomous

| Strategy | Description | Key Parameters | When to Use |
|----------|-------------|----------------|-------------|
| `hedge_agent` | Reduces excess exposure per deterministic mandate. Fires when net notional exceeds threshold. | `notional_threshold` | Always-on risk overlay. Pairs with any MM or signal strategy. |
| `hedge_agent` | Inventory exposure reducer. Fires when net notional exceeds threshold. This is not the BTCSWP funding-rate hedge; use `hl hedge propose` / `hl hedge backtest` for that. | `notional_threshold` | Always-on risk overlay. Pairs with any MM or signal strategy. |
| `rfq_agent` | Block-size dark RFQ liquidity — quotes for large orders with wider spreads. | `min_size`, `spread_bps` | Institutional/block flow. Provides hidden liquidity for large counterparties. |
| `claude_agent` | Multi-model LLM trading agent. Sends market snapshot to an LLM (Gemini, Claude, or OpenAI), receives structured trade decisions. | `model`, `base_size` | Experimental/research. Autonomous decision-making using LLM reasoning. |

Expand Down Expand Up @@ -460,6 +474,8 @@ hl radar run [options] # Opportunity radar
hl pulse run [options] # Pulse momentum detector
hl guard run -i ETH-PERP [options] # Guard trailing stop
hl reflect run [--since DATE] # Performance review
hl hedge propose [options] # BTCSWP funding hedge proposal
hl hedge backtest --csv <path> # Local funding hedge cashflow backtest

# Infrastructure
hl builder approve [--mainnet] # Approve builder fee
Expand All @@ -481,7 +497,7 @@ hl mcp serve # stdio transport (default)
hl mcp serve --transport sse # SSE transport
```

**16 tools exposed:** `account`, `status`, `trade`, `run_strategy`, `strategies`, `radar_run`, `apex_status`, `apex_run`, `reflect_run`, `setup_check`, `builder_status`, `wallet_list`, `wallet_auto`, `agent_memory`, `trade_journal`, `judge_report`
**18 tools exposed:** `account`, `status`, `trade`, `run_strategy`, `strategies`, `funding_hedge_propose`, `funding_hedge_backtest`, `radar_run`, `apex_status`, `apex_run`, `reflect_run`, `setup_check`, `builder_status`, `wallet_list`, `wallet_auto`, `agent_memory`, `trade_journal`, `judge_report`

Fast tools (strategies, builder, wallet, setup, memory, journal, judge) call Python directly — zero subprocess overhead.

Expand Down Expand Up @@ -572,7 +588,7 @@ hl run engine_mm -i BTCSWP-USDYP --tick 10
```
cli/ CLI commands and trading engine
commands/ Subcommand modules (run, apex, radar, pulse, guard, reflect, house, ...)
mcp_server.py MCP server (16 tools via FastMCP)
mcp_server.py MCP server (18 tools via FastMCP)
hl_adapter.py Direct HL API adapter (live + mock)
builder_fee.py Builder fee config (HL native BuilderInfo)
keystore.py Encrypted keystore (geth-compatible)
Expand Down
86 changes: 86 additions & 0 deletions cli/commands/hedge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
"""hl hedge — funding-rate hedge proposal tools."""
from __future__ import annotations

import json
from pathlib import Path
from typing import Optional

import typer

hedge_app = typer.Typer(no_args_is_help=True)


@hedge_app.command("propose", help="Propose a BTCSWP funding-rate hedge")
def hedge_propose(
asset: str = typer.Option("BTC", "--asset", help="Underlying perp exposure. BTC is deployed today."),
side: str = typer.Option("long", "--side", help="Perp exposure side: long or short."),
perp_notional: float = typer.Option(..., "--perp-notional", help="Absolute perp notional in USD."),
funding_apr: Optional[float] = typer.Option(
None,
"--funding-apr",
help="Annualized funding APR. Accepts 0.42 or 42 for 42%.",
),
funding_rate_8h: Optional[float] = typer.Option(
None,
"--funding-rate-8h",
help="8h funding rate as a decimal, e.g. 0.0003. Used only if --funding-apr is omitted.",
),
vol_multiplier: float = typer.Option(15.0, "--vol-multiplier", help="BTCSWP hedge multiplier."),
json_output: bool = typer.Option(False, "--json", help="Output machine-readable JSON."),
) -> None:
"""Return a read-only BTCSWP sizing proposal for a BTC funding exposure."""
from modules.funding_hedge import format_proposal, propose_funding_hedge

try:
proposal = propose_funding_hedge(
asset=asset,
perp_side=side,
perp_notional_usd=perp_notional,
funding_apr=funding_apr,
funding_rate_8h=funding_rate_8h,
vol_multiplier=vol_multiplier,
)
except ValueError as exc:
raise typer.BadParameter(str(exc)) from exc

if json_output:
typer.echo(json.dumps(proposal.to_dict(), indent=2))
else:
typer.echo(format_proposal(proposal))


@hedge_app.command("backtest", help="Backtest BTCSWP funding hedge cashflows from CSV")
def hedge_backtest(
csv_path: Path = typer.Option(
...,
"--csv",
exists=True,
file_okay=True,
dir_okay=False,
readable=True,
help="CSV with funding_rate_8h/funding_rate column and optional hedge_rate_8h.",
),
asset: str = typer.Option("BTC", "--asset", help="Underlying perp exposure. BTC is deployed today."),
side: str = typer.Option("long", "--side", help="Perp exposure side: long or short."),
perp_notional: float = typer.Option(..., "--perp-notional", help="Absolute perp notional in USD."),
vol_multiplier: float = typer.Option(15.0, "--vol-multiplier", help="BTCSWP hedge multiplier."),
json_output: bool = typer.Option(False, "--json", help="Output machine-readable JSON."),
) -> None:
"""Backtest funding cashflows for a same-side BTCSWP hedge."""
from modules.funding_hedge import backtest_funding_hedge_csv, format_backtest

try:
backtest = backtest_funding_hedge_csv(
csv_path=csv_path,
asset=asset,
perp_side=side,
perp_notional_usd=perp_notional,
vol_multiplier=vol_multiplier,
)
except ValueError as exc:
raise typer.BadParameter(str(exc)) from exc

if json_output:
typer.echo(json.dumps(backtest.to_dict(), indent=2))
else:
typer.echo(format_backtest(backtest))
Loading