| name | funding-arb-strategy |
| description | Domain knowledge for funding rate arbitrage execution. Use when implementing strategy logic, tranche calculations, capital allocation, or position management. Contains the business rules that govern how trades are opened, managed, and closed. |
Funding Rate Arbitrage Strategy Skill
Core Concept
Open opposite perpetual positions (long on one exchange, short on another) for the same asset to capture the net funding rate differential while remaining delta-neutral.
Position Sizing Formula
from decimal import Decimal
available_capital = min(hl_balance, bb_balance)
allocated_capital = available_capital * allocation_pct / Decimal("100")
position_size_usd = allocated_capital * leverage
position_size_coins = position_size_usd / current_price
Tranche Calculation
tranche_size = total_size / num_tranches
for i in range(num_tranches):
offset = mid_price * tranche_offset_bps * (i + 1) / Decimal("10000")
if is_buy:
tranche_price = mid_price - offset
else:
tranche_price = mid_price + offset
Execution Sequence
- Validate: balances, leverage, no conflicting positions
- Set leverage on both exchanges
- For each tranche:
a. Place limit order on Exchange A (leg 1)
b. Place limit order on Exchange B (leg 2) — simultaneously via asyncio.gather
c. Wait for fills (timeout: fill_timeout_sec)
d. Handle mismatches (one fills, other doesn't)
- After all tranches: place SL/TP on both exchanges
- Start position monitor
Fill Mismatch Handling
Both fill → proceed to next tranche
Neither fills → cancel both, re-price, retry (max 3 retries)
One fills → wait grace_period_sec
→ If other fills: proceed
→ If not: cancel unfilled
→ If partial execution acceptable: keep matched portion, report
→ If not: close filled leg via MARKET ORDER (speed > slippage), report error
SL/TP Rules
- SL/TP placed AFTER position is fully opened (all tranches)
- Use average entry price across tranches for SL/TP calculation
- SL/TP are exchange-native trigger orders (survive app restart)
- If one leg's SL/TP triggers, the position monitor closes the other leg
Closing Sequence
- Cancel existing SL/TP on both exchanges
- Execute close in tranches (same logic as open, but reduce_only)
- Verify both positions are fully closed
- Log final PnL including funding collected
Funding Rate Comparison
When comparing rates, normalize to the same time interval:
hl_hourly = hl_funding_rate
bb_hourly = bb_funding_rate / Decimal("8")
net_hourly = hl_hourly - bb_hourly