hedge and auto hedger in separate folders

This commit is contained in:
2025-12-16 14:19:24 +01:00
parent 109ef7cd24
commit e1b3c5814b
8 changed files with 749 additions and 59 deletions

53
clp_hedger_auto/AGENTS.md Normal file
View File

@ -0,0 +1,53 @@
# AGENTS.md - CLP Hedger Project Guide
## Development Commands
### Installation
```bash
pip install -r requirements.txt
```
### Running the Application
```bash
# Main hedger bot
python clp_hedger.py
# Development with debug logging
python -c "from logging_utils import setup_logging; setup_logging('debug', 'CLP_HEDGER'); import clp_hedger"
```
### Testing
No formal test framework. Manual testing:
```bash
# Check configuration
python -c "import clp_hedger; print(clp_hedger.get_manual_position_config())"
```
## Code Style Guidelines
### Imports
- Order: standard library → third-party → local modules
- Add project root to sys.path for local imports
- Use absolute imports from project root
### Environment & Logging
- Use `.env` files with python-dotenv
- Use `setup_logging("normal"/"debug", "MODULE_NAME")` convention
- Include emojis: 🚀, ✅, ⚡, 🔄
### Architecture
- PascalCase classes (HyperliquidStrategy, CLPHedger)
- Private methods start with underscore (_init_strategy)
- Module-level constants: UPPER_SNAKE_CASE
- Functions/variables: snake_case
### Error Handling
- Wrap API calls in try/except blocks
- Log errors with context
- Return None/0.0 for non-critical failures
- Use sys.exit(1) for critical failures
### Mathematical Operations
- Use math.sqrt() for square roots
- Implement proper rounding for API requirements
- Handle floating-point precision appropriately

View File

@ -0,0 +1,469 @@
import os
import time
import logging
import sys
import math
import json
from dotenv import load_dotenv
# --- FIX: Add project root to sys.path to import local modules ---
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(current_dir)
sys.path.append(project_root)
# Now we can import from root
from logging_utils import setup_logging
from eth_account import Account
from hyperliquid.exchange import Exchange
from hyperliquid.info import Info
from hyperliquid.utils import constants
# Load environment variables from .env in current directory
dotenv_path = os.path.join(current_dir, '.env')
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)
else:
# Fallback to default search
load_dotenv()
# Setup Logging using project convention
setup_logging("normal", "CLP_HEDGER")
# --- CONFIGURATION DEFAULTS (Can be overridden by JSON) ---
REBALANCE_THRESHOLD = 0.15 # ETH
CHECK_INTERVAL = 30 # Seconds
LEVERAGE = 5
STATUS_FILE = "hedge_status.json"
# Gap Recovery Configuration
PRICE_BUFFER_PCT = 0.002 # 0.5% buffer to prevent churn
TIME_BUFFER_SECONDS = 120 # 2 minutes wait between mode switches
def get_manual_position_config():
"""Reads hedge_status.json and returns the first OPEN MANUAL position dict, or None."""
if not os.path.exists(STATUS_FILE):
return None
try:
with open(STATUS_FILE, 'r') as f:
data = json.load(f)
for entry in data:
if entry.get('type') == 'MANUAL' and entry.get('status') == 'OPEN':
return entry
except Exception as e:
logging.error(f"ERROR reading status file: {e}")
return None
class HyperliquidStrategy:
def __init__(self, entry_weth, entry_price, low_range, high_range, start_price, static_long=0.4):
# Your Pool Configuration
self.entry_weth = entry_weth
self.entry_price = entry_price
self.low_range = low_range
self.high_range = high_range
self.static_long = static_long
# Gap Recovery State
self.start_price = start_price
# GAP = max(0, ENTRY - START). If Start > Entry (we are winning), Gap is 0.
self.gap = max(0.0, entry_price - start_price)
self.recovery_target = entry_price + (2 * self.gap)
self.current_mode = "NORMAL" # "NORMAL" (100% Hedge) or "RECOVERY" (0% Hedge)
self.last_switch_time = 0
logging.info(f"Strategy Init. Start Px: {start_price:.2f} | Gap: {self.gap:.2f} | Recovery Tgt: {self.recovery_target:.2f}")
# Calculate Constant Liquidity (L) once
# Formula: L = x / (1/sqrt(P) - 1/sqrt(Pb))
try:
sqrt_P = math.sqrt(entry_price)
sqrt_Pb = math.sqrt(high_range)
self.L = entry_weth / ((1/sqrt_P) - (1/sqrt_Pb))
logging.info(f"Liquidity (L): {self.L:.4f}")
except Exception as e:
logging.error(f"Error calculating liquidity: {e}")
sys.exit(1)
def get_pool_delta(self, current_price):
"""Calculates how much ETH the pool currently holds (The Risk)"""
# If price is above range, you hold 0 ETH (100% USDC)
if current_price >= self.high_range:
return 0.0
# If price is below range, you hold Max ETH
if current_price <= self.low_range:
sqrt_Pa = math.sqrt(self.low_range)
sqrt_Pb = math.sqrt(self.high_range)
return self.L * ((1/sqrt_Pa) - (1/sqrt_Pb))
# If in range, calculate active ETH
sqrt_P = math.sqrt(current_price)
sqrt_Pb = math.sqrt(self.high_range)
return self.L * ((1/sqrt_P) - (1/sqrt_Pb))
def calculate_rebalance(self, current_price, current_short_position_size):
"""
Determines if we need to trade and the exact order size.
"""
# 1. Base Target (Full Hedge)
pool_delta = self.get_pool_delta(current_price)
raw_target_short = pool_delta + self.static_long
# 2. Determine Mode (Normal vs Recovery)
# Buffers
entry_upper = self.entry_price * (1 + PRICE_BUFFER_PCT)
entry_lower = self.entry_price * (1 - PRICE_BUFFER_PCT)
desired_mode = self.current_mode # Default to staying same
if self.current_mode == "NORMAL":
# Switch to RECOVERY if:
# Price > Entry + Buffer AND Price < Recovery Target
if current_price > entry_upper and current_price < self.recovery_target:
desired_mode = "RECOVERY"
elif self.current_mode == "RECOVERY":
# Switch back to NORMAL if:
# Price < Entry - Buffer (Fell back down) OR Price > Recovery Target (Finished)
if current_price < entry_lower or current_price >= self.recovery_target:
desired_mode = "NORMAL"
# 3. Apply Time Buffer
now = time.time()
if desired_mode != self.current_mode:
if (now - self.last_switch_time) >= TIME_BUFFER_SECONDS:
logging.info(f"🔄 MODE SWITCH: {self.current_mode} -> {desired_mode} (Px: {current_price:.2f})")
self.current_mode = desired_mode
self.last_switch_time = now
else:
logging.info(f"⏳ Mode Switch Delayed (Time Buffer). Pending: {desired_mode}")
# 4. Set Final Target based on Mode
if self.current_mode == "RECOVERY":
target_short_size = 0.0
logging.info(f"🩹 RECOVERY MODE ACTIVE (0% Hedge). Target: {self.recovery_target:.2f}")
else:
target_short_size = raw_target_short
# 5. Calculate Difference
diff = target_short_size - abs(current_short_position_size)
return {
"current_price": current_price,
"pool_delta": pool_delta,
"target_short": target_short_size,
"raw_target": raw_target_short,
"current_short": abs(current_short_position_size),
"diff": diff, # Positive = SELL more (Add Short), Negative = BUY (Reduce Short)
"action": "SELL" if diff > 0 else "BUY",
"mode": self.current_mode
}
def round_to_sz_decimals(amount, sz_decimals=4):
"""
Hyperliquid requires specific rounding 'szDecimals'.
For ETH, this is usually 4 (e.g., 1.2345).
"""
factor = 10 ** sz_decimals
# Use floor to avoid rounding up into money you don't have,
# but strictly simply rounding is often sufficient for small adjustments.
# Using round() standard here.
return round(abs(amount), sz_decimals)
def round_to_sig_figs(x, sig_figs=5):
"""
Rounds a number to a specified number of significant figures.
Hyperliquid prices generally require 5 significant figures.
"""
if x == 0:
return 0.0
return round(x, sig_figs - int(math.floor(math.log10(abs(x)))) - 1)
class CLPHedger:
def __init__(self):
self.private_key = os.environ.get("SWING_AGENT_PK")
self.vault_address = os.environ.get("MAIN_WALLET_ADDRESS")
if not self.private_key:
logging.error("No private key found (HEDGER_PRIVATE_KEY or AGENT_PRIVATE_KEY) in .env")
sys.exit(1)
if not self.vault_address:
logging.warning("MAIN_WALLET_ADDRESS not found in .env. Assuming Agent is the Vault (not strictly recommended for CLPs).")
self.account = Account.from_key(self.private_key)
# API Connection
self.info = Info(constants.MAINNET_API_URL, skip_ws=True)
# Note: If this agent is trading on behalf of a Vault (Main Account),
# the exchange object needs the vault's address as `account_address`.
self.exchange = Exchange(self.account, constants.MAINNET_API_URL, account_address=self.vault_address)
# Load Manual Config from JSON
self.manual_config = get_manual_position_config()
self.coin_symbol = "ETH" # Default, but will try to read from JSON
self.sz_decimals = 4
self.strategy = None
if self.manual_config:
self.coin_symbol = self.manual_config.get('coin_symbol', 'ETH')
if self.manual_config.get('hedge_enabled', False):
self._init_strategy()
else:
logging.warning("MANUAL position found but 'hedge_enabled' is FALSE. Hedger will remain idle.")
else:
logging.warning("No MANUAL position found in hedge_status.json. Hedger will remain idle.")
# Set Leverage on Initialization (if coin symbol known)
try:
logging.info(f"Setting leverage to {LEVERAGE}x (Cross) for {self.coin_symbol}...")
self.exchange.update_leverage(LEVERAGE, self.coin_symbol, is_cross=True)
except Exception as e:
logging.error(f"Failed to update leverage: {e}")
# Fetch meta once to get szDecimals
self.sz_decimals = self._get_sz_decimals(self.coin_symbol)
logging.info(f"CLP Hedger initialized. Agent: {self.account.address}. Coin: {self.coin_symbol} (Decimals: {self.sz_decimals})")
def _init_strategy(self):
try:
entry_p = self.manual_config['entry_price']
lower = self.manual_config['range_lower']
upper = self.manual_config['range_upper']
static_long = self.manual_config.get('static_long', 0.0)
# Require entry_amount0 (or entry_weth)
entry_weth = self.manual_config.get('entry_amount0', 0.45) # Default to 0.45 if missing for now
start_price = self.get_market_price(self.coin_symbol)
if start_price is None:
logging.warning("Waiting for initial price to start strategy...")
# Logic will retry in run loop
return
self.strategy = HyperliquidStrategy(
entry_weth=entry_weth,
entry_price=entry_p,
low_range=lower,
high_range=upper,
start_price=start_price,
static_long=static_long
)
logging.info(f"Strategy Initialized for {self.coin_symbol}.")
except Exception as e:
logging.error(f"Failed to init strategy: {e}")
self.strategy = None
def _get_sz_decimals(self, coin):
try:
meta = self.info.meta()
for asset in meta["universe"]:
if asset["name"] == coin:
return asset["szDecimals"]
logging.warning(f"Could not find szDecimals for {coin}, defaulting to 4.")
return 4
except Exception as e:
logging.error(f"Failed to fetch meta: {e}")
return 4
def get_funding_rate(self, coin):
try:
meta, asset_ctxs = self.info.meta_and_asset_ctxs()
for i, asset in enumerate(meta["universe"]):
if asset["name"] == coin:
# Funding rate is in the asset context at same index
return float(asset_ctxs[i]["funding"])
return 0.0
except Exception as e:
logging.error(f"Error fetching funding rate: {e}")
return 0.0
def get_market_price(self, coin):
try:
# Get all mids is efficient
mids = self.info.all_mids()
if coin in mids:
return float(mids[coin])
else:
logging.error(f"Price for {coin} not found in all_mids.")
return None
except Exception as e:
logging.error(f"Error fetching price: {e}")
return None
def get_current_position(self, coin):
try:
# We need the User State of the Vault (or the account we are trading for)
user_state = self.info.user_state(self.vault_address or self.account.address)
for pos in user_state["assetPositions"]:
if pos["position"]["coin"] == coin:
# szi is the size. Positive = Long, Negative = Short.
return float(pos["position"]["szi"])
return 0.0 # No position
except Exception as e:
logging.error(f"Error fetching position: {e}")
return 0.0
def execute_trade(self, coin, is_buy, size, price):
logging.info(f"🚀 EXECUTING: {coin} {'BUY' if is_buy else 'SELL'} {size} @ ~{price}")
# Check for reduceOnly logic
# If we are BUYING to reduce a SHORT, it is reduceOnly.
# If we are SELLING to increase a SHORT, it is NOT reduceOnly.
# Since we are essentially managing a Short hedge:
# Action BUY = Reducing Hedge -> reduceOnly=True
# Action SELL = Increasing Hedge -> reduceOnly=False
reduce_only = is_buy
try:
# Market order (limit with aggressive TIF or just widely crossing limit)
# Hyperliquid SDK 'order' method parameters: coin, is_buy, sz, limit_px, order_type, reduce_only
# We use a limit price slightly better than market to ensure fill or just use market price logic
# Using a simplistic "Market" approach by setting limit far away
slippage = 0.05 # 5% slippage tolerance
raw_limit_px = price * (1.05 if is_buy else 0.95)
limit_px = round_to_sig_figs(raw_limit_px, 5)
order_result = self.exchange.order(
coin,
is_buy,
size,
limit_px,
{"limit": {"tif": "Ioc"}},
reduce_only=reduce_only
)
status = order_result["status"]
if status == "ok":
response_data = order_result["response"]["data"]
if "statuses" in response_data and "error" in response_data["statuses"][0]:
logging.error(f"Order API Error: {response_data['statuses'][0]['error']}")
else:
logging.info(f"✅ Trade Success: {response_data}")
else:
logging.error(f"Order Failed: {order_result}")
except Exception as e:
logging.error(f"Exception during trade execution: {e}")
def close_all_positions(self):
logging.info("Attempting to close all open positions...")
try:
# 1. Get latest price
price = self.get_market_price(self.coin_symbol)
if price is None:
logging.error("Could not fetch price to close positions. Aborting close.")
return
# 2. Get current position
current_pos = self.get_current_position(self.coin_symbol)
if current_pos == 0:
logging.info("No open positions to close.")
return
# 3. Determine Side and Size
# If Short (-), we need to Buy (+).
# If Long (+), we need to Sell (-).
is_buy = current_pos < 0
abs_size = abs(current_pos)
# Ensure size is rounded correctly for the API
final_size = round_to_sz_decimals(abs_size, self.sz_decimals)
if final_size == 0:
logging.info("Position size effectively 0 after rounding.")
return
logging.info(f"Closing Position: {current_pos} {self.coin_symbol} -> Action: {'BUY' if is_buy else 'SELL'} {final_size}")
# 4. Execute
self.execute_trade(self.coin_symbol, is_buy, final_size, price)
except Exception as e:
logging.error(f"Error during close_all_positions: {e}")
def run(self):
logging.info(f"Starting Hedge Monitor Loop. Interval: {CHECK_INTERVAL}s")
while True:
try:
# Reload Config periodically
self.manual_config = get_manual_position_config()
# Check Global Enable Switch
if not self.manual_config or not self.manual_config.get('hedge_enabled', False):
# If previously active, close?
# Yes, safety first.
if self.strategy is not None:
logging.info("Hedge Disabled. Closing any remaining positions.")
self.close_all_positions()
self.strategy = None
else:
# Just idle check to keep connection alive or log occasionally
# logging.info("Idle. Hedge Disabled.")
pass
time.sleep(CHECK_INTERVAL)
continue
# If enabled but strategy not init, Init it.
if self.strategy is None:
self._init_strategy()
if self.strategy is None: # Init failed
time.sleep(CHECK_INTERVAL)
continue
# 1. Get Data
price = self.get_market_price(self.coin_symbol)
if price is None:
time.sleep(5)
continue
funding_rate = self.get_funding_rate(self.coin_symbol)
current_pos_size = self.get_current_position(self.coin_symbol)
# 2. Calculate Logic
# Pass raw size (e.g. -1.5). The strategy handles the logic.
calc = self.strategy.calculate_rebalance(price, current_pos_size)
diff_abs = abs(calc['diff'])
trade_size = round_to_sz_decimals(diff_abs, self.sz_decimals)
# Logging Status
status_msg = (
f"Price: {price:.2f} | Fund: {funding_rate:.6f} | "
f"Mode: {calc['mode']} | "
f"Pool Delta: {calc['pool_delta']:.3f} | "
f"Tgt Short: {calc['target_short']:.3f} | "
f"Act Short: {calc['current_short']:.3f} | "
f"Diff: {calc['diff']:.3f}"
)
if calc.get('is_recovering'):
status_msg += f" | 🩹 REC MODE ({calc['raw_target']:.3f} -> {calc['target_short']:.3f})"
logging.info(status_msg)
# 3. Check Threshold
if diff_abs >= REBALANCE_THRESHOLD:
if trade_size > 0:
logging.info(f"⚡ THRESHOLD TRIGGERED ({diff_abs:.3f} >= {REBALANCE_THRESHOLD})")
is_buy = (calc['action'] == "BUY")
self.execute_trade(self.coin_symbol, is_buy, trade_size, price)
else:
logging.info("Trade size rounds to 0. Skipping.")
time.sleep(CHECK_INTERVAL)
except KeyboardInterrupt:
logging.info("Stopping Hedger...")
self.close_all_positions()
break
except Exception as e:
logging.error(f"Loop Error: {e}", exc_info=True)
time.sleep(10)
if __name__ == "__main__":
hedger = CLPHedger()
hedger.run()

View File

@ -0,0 +1,18 @@
[
{
"type": "MANUAL",
"token_id": 5147464,
"status": "OPEN",
"hedge_enabled": true,
"coin_symbol": "ETH",
"entry_price": 3332.66,
"range_lower": 2844.11,
"range_upper": 3477.24,
"target_value": 6938.95,
"amount0_initial": 0.45,
"amount1_initial": 5439.23,
"static_long": 0.0,
"timestamp_open": 1765575924,
"timestamp_close": null
}
]

View File

@ -0,0 +1,85 @@
# CLP Hedger - Working Configuration Summary
## Current Setup Status
**ACTIVE**: Hedger is running and successfully trading on Hyperliquid
## Position Configuration (`hedge_status.json`)
```json
{
"type": "MANUAL",
"token_id": 5147464,
"status": "OPEN",
"hedge_enabled": true,
"coin_symbol": "ETH",
"entry_price": 3332.66,
"range_lower": 2844.11,
"range_upper": 3477.24,
"target_value": 6938.95,
"amount0_initial": 0.45,
"amount1_initial": 5439.23,
"static_long": 0.0,
"timestamp_open": 1765575924,
"timestamp_close": null
}
```
## Trading Parameters
- **Coin**: ETH
- **Leverage**: 5x (Cross)
- **Entry Price**: $3,332.66
- **Price Range**: $2,844.11 - $3,477.24
- **Position Size**: 0.45 ETH
- **Static Long**: 0% (fully hedged)
- **Target Value**: $6,938.95
## Hedger Configuration (`clp_hedger.py`)
- **Check Interval**: 30 seconds
- **Rebalance Threshold**: 0.15 ETH
- **Price Buffer**: 0.2% (prevents churn)
- **Time Buffer**: 120 seconds (between mode switches)
- **Status File**: `hedge_status.json`
## Strategy Parameters
- **Entry WETH**: 0.45 ETH
- **Low Range**: $2,844.11
- **High Range**: $3,477.24
- **Start Price**: $3,332.66
- **Static Long Ratio**: 0.0 (0% static long exposure)
## Gap Recovery Settings
- **Current Mode**: NORMAL (100% hedge)
- **Gap Recovery**: Enabled
- **Recovery Target**: Entry price + (2 × Gap)
- **Price Buffer**: 0.2%
- **Mode Switch Delay**: 120 seconds
## Environment
- **Wallet**: 0xcb262ceaae5d8a99b713f87a43dd18e6be892739
- **Network**: Hyperliquid Mainnet
- **Logging Level**: Normal
- **Virtual Environment**: Active
## Last Status
- ✅ API Connection: Working
- ✅ Price Feed: Active
- ✅ Position Tracking: Enabled
- ✅ Hedge Logic: Operational
- ✅ Order Execution: Successful
## Key Files
- `clp_hedger.py`: Main hedger bot
- `hedge_status.json`: Position configuration
- `.env`: API credentials (not shown for security)
## Monitoring
The hedger runs a continuous loop every 30 seconds, checking:
1. Current market price
2. Position size deviation
3. Gap recovery conditions
4. Funding rate opportunities
5. Automatic rebalancing needs
## Operations
- **Normal Mode**: Maintains 100% hedge against ETH exposure
- **Recovery Mode**: Reduces hedge to 0% when gap recovery conditions are met
- **Auto-Rebalancing**: Triggers when position deviates by >0.15 ETH