fixed hedge_status.json

This commit is contained in:
2025-12-14 22:11:36 +01:00
parent e31079cdbb
commit b85fcb8246
3 changed files with 113 additions and 32 deletions

View File

@ -31,16 +31,16 @@ setup_logging("normal", "SCALPER_HEDGER")
# --- CONFIGURATION ---
COIN_SYMBOL = "ETH"
CHECK_INTERVAL = 1 # Faster check for scalper
LEVERAGE = 5
LEVERAGE = 5 # 3x Leverage
STATUS_FILE = "hedge_status.json"
# --- STRATEGY ZONES (Percent of Range Width) ---
# Bottom Hedge Zone: 0% to 15% -> Active Hedging
ZONE_BOTTOM_HEDGE_LIMIT = 0.1
ZONE_BOTTOM_HEDGE_LIMIT = 0.5
# Close Zone: 15% to 20% -> Close All Hedges (Flatten)
ZONE_CLOSE_START = 0.18
ZONE_CLOSE_END = 0.20
ZONE_CLOSE_START = 0.51
ZONE_CLOSE_END = 0.52
# Middle Zone: 20% to 85% -> Idle (No new orders, keep existing)
# Implied by gaps between other zones.
@ -50,7 +50,7 @@ ZONE_TOP_HEDGE_START = 0.8
# --- ORDER SETTINGS ---
PRICE_BUFFER_PCT = 0.0005 # 0.05% price move triggers order update
MIN_THRESHOLD_ETH = 0.005 # Minimum trade size in ETH
MIN_THRESHOLD_ETH = 0.01 # Minimum trade size in ETH
MIN_ORDER_VALUE_USD = 10.0 # Minimum order value for API safety
def get_active_automatic_position():
@ -67,25 +67,56 @@ def get_active_automatic_position():
return None
def update_position_zones_in_json(token_id, zones_data):
"""Updates the active position in JSON with calculated zone prices."""
"""Updates the active position in JSON with calculated zone prices and formats the entry."""
if not os.path.exists(STATUS_FILE): return
try:
with open(STATUS_FILE, 'r') as f:
data = json.load(f)
updated = False
for entry in data:
for i, entry in enumerate(data):
if entry.get('type') == 'AUTOMATIC' and entry.get('status') == 'OPEN' and entry.get('token_id') == token_id:
# Update keys
# Merge Zones
for k, v in zones_data.items():
entry[k] = v
# Format & Reorder
open_ts = entry.get('timestamp_open', int(time.time()))
opened_str = time.strftime('%H:%M %d/%m/%y', time.localtime(open_ts))
# Reconstruct Dict in Order
new_entry = {
"type": entry.get('type'),
"token_id": entry.get('token_id'),
"opened": opened_str,
"status": entry.get('status'),
"entry_price": round(entry.get('entry_price', 0), 2),
"target_value": round(entry.get('target_value', 0), 2),
# Amounts might be string or float or int. Ensure float.
"amount0_initial": round(float(entry.get('amount0_initial', 0)), 4),
"amount1_initial": round(float(entry.get('amount1_initial', 0)), 2),
"range_upper": round(entry.get('range_upper', 0), 2),
"zone_top_start_price": entry.get('zone_top_start_price'),
"zone_close_top_price": entry.get('zone_close_top_price'),
"zone_close_bottom_price": entry.get('zone_close_bottom_price'),
"zone_bottom_limit_price": entry.get('zone_bottom_limit_price'),
"range_lower": round(entry.get('range_lower', 0), 2),
"static_long": entry.get('static_long', 0.0),
"timestamp_open": open_ts,
"timestamp_close": entry.get('timestamp_close')
}
data[i] = new_entry
updated = True
break
if updated:
with open(STATUS_FILE, 'w') as f:
json.dump(data, f, indent=2)
logging.info(f"Updated JSON with Zone Prices for Position {token_id}")
logging.info(f"Updated JSON with Formatted Zone Prices for Position {token_id}")
except Exception as e:
logging.error(f"Error updating JSON zones: {e}")
@ -124,7 +155,10 @@ class HyperliquidStrategy:
# Method 1: Use Amount0 (WETH)
if entry_amount0 > 0:
amount0_eth = entry_amount0 / 10**18
# If amount is huge (Wei), scale it. If small (ETH), use as is.
if entry_amount0 > 1000: amount0_eth = entry_amount0 / 10**18
else: amount0_eth = entry_amount0
denom0 = (1/sqrt_P) - (1/sqrt_Pb)
if denom0 > 0.00000001:
self.L = amount0_eth / denom0
@ -132,7 +166,9 @@ class HyperliquidStrategy:
# Method 2: Use Amount1 (USDC)
if self.L == 0.0 and entry_amount1 > 0:
amount1_usdc = entry_amount1 / 10**6
if entry_amount1 > 100000: amount1_usdc = entry_amount1 / 10**6
else: amount1_usdc = entry_amount1
denom1 = sqrt_P - sqrt_Pa
if denom1 > 0.00000001:
self.L = amount1_usdc / denom1
@ -458,26 +494,26 @@ class ScalperHedger:
# Calculate Prices for Zones
zone_bottom_limit_price = clp_low_range + (range_width * ZONE_BOTTOM_HEDGE_LIMIT)
zone_close_start_price = clp_low_range + (range_width * ZONE_CLOSE_START)
zone_close_end_price = clp_low_range + (range_width * ZONE_CLOSE_END)
zone_close_bottom_price = clp_low_range + (range_width * ZONE_CLOSE_START)
zone_close_top_price = clp_low_range + (range_width * ZONE_CLOSE_END)
zone_top_start_price = clp_low_range + (range_width * ZONE_TOP_HEDGE_START)
# Update JSON with zone prices if missing
if 'zone_bottom_limit_price' not in active_pos:
update_position_zones_in_json(active_pos['token_id'], {
'zone_bottom_limit_price': zone_bottom_limit_price,
'zone_close_start_price': zone_close_start_price,
'zone_close_end_price': zone_close_end_price,
'zone_top_start_price': zone_top_start_price
'zone_top_start_price': round(zone_top_start_price, 2),
'zone_close_top_price': round(zone_close_top_price, 2),
'zone_close_bottom_price': round(zone_close_bottom_price, 2),
'zone_bottom_limit_price': round(zone_bottom_limit_price, 2)
})
# Check Zones
in_close_zone = (price >= zone_close_start_price and price <= zone_close_end_price)
in_close_zone = (price >= zone_close_bottom_price and price <= zone_close_top_price)
in_hedge_zone = (price <= zone_bottom_limit_price) or (price >= zone_top_start_price)
# --- Execute Logic ---
if in_close_zone:
logging.info(f"ZONE: CLOSE ({price:.2f} in {zone_close_start_price:.2f}-{zone_close_end_price:.2f}). Closing all hedge positions.")
logging.info(f"ZONE: CLOSE ({price:.2f} in {zone_close_bottom_price:.2f}-{zone_close_top_price:.2f}). Closing all hedge positions.")
self.close_all_positions()
time.sleep(CHECK_INTERVAL)
continue