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

@ -76,15 +76,16 @@ RPC_URL = os.environ.get("MAINNET_RPC_URL")
PRIVATE_KEY = os.environ.get("MAIN_WALLET_PRIVATE_KEY") or os.environ.get("PRIVATE_KEY")
# Script behavior flags
MONITOR_INTERVAL_SECONDS = 30
MONITOR_INTERVAL_SECONDS = 451
COLLECT_FEES_ENABLED = False # If True, will attempt to collect fees once and exit if no open auto position
CLOSE_POSITION_ENABLED = True # If True, will attempt to close auto position when out of range
CLOSE_IF_OUT_OF_RANGE_ONLY = True # If True, closes only if out of range; if False, closes immediately
OPEN_POSITION_ENABLED = True # If True, will open a new position if no auto position exists
REBALANCE_ON_CLOSE_BELOW_RANGE = False # If True, will sell 50% of WETH to USDC when closing below range
# New Position Parameters
TARGET_INVESTMENT_VALUE_TOKEN1 = 350.0 # Target total investment value in Token1 terms (e.g. 350 USDC)
RANGE_WIDTH_PCT = 0.005 # +/- 2% range for new positions
TARGET_INVESTMENT_VALUE_TOKEN1 = 2000.0 # Target total investment value in Token1 terms (e.g. 350 USDC)
RANGE_WIDTH_PCT = 0.01 # +/- 2% range for new positions
# JSON File for tracking position state
STATUS_FILE = "hedge_status.json"
@ -131,20 +132,46 @@ def update_hedge_status_file(action, position_data):
current_data = []
if action == "OPEN":
# Format Timestamp
open_ts = int(time.time())
opened_str = time.strftime('%H:%M %d/%m/%y', time.localtime(open_ts))
# Scale Amounts
raw_amt0 = position_data.get('amount0_initial', 0)
raw_amt1 = position_data.get('amount1_initial', 0)
# Handle if they are already scaled (unlikely here, but safe)
if raw_amt0 > 1000: fmt_amt0 = round(raw_amt0 / 10**18, 4)
else: fmt_amt0 = round(raw_amt0, 4)
if raw_amt1 > 1000: fmt_amt1 = round(raw_amt1 / 10**6, 2)
else: fmt_amt1 = round(raw_amt1, 2)
new_entry = {
"type": "AUTOMATIC",
"token_id": position_data['token_id'],
"opened": opened_str,
"status": "OPEN",
"entry_price": position_data['entry_price'],
"range_lower": position_data['range_lower'],
"range_upper": position_data['range_upper'],
"target_value": position_data.get('target_value', 0.0), # Save Actual Value as Target for hedging accuracy
"amount0_initial": position_data.get('amount0_initial', 0),
"amount1_initial": position_data.get('amount1_initial', 0),
"entry_price": round(position_data['entry_price'], 2),
"target_value": round(position_data.get('target_value', 0.0), 2),
"amount0_initial": fmt_amt0,
"amount1_initial": fmt_amt1,
"range_upper": round(position_data['range_upper'], 2),
# Zones (if present in position_data, otherwise None/Skip)
"zone_top_start_price": round(position_data['zone_top_start_price'], 2) if 'zone_top_start_price' in position_data else None,
"zone_close_top_price": round(position_data['zone_close_end_price'], 2) if 'zone_close_end_price' in position_data else None,
"zone_close_bottom_price": round(position_data['zone_close_start_price'], 2) if 'zone_close_start_price' in position_data else None,
"zone_bottom_limit_price": round(position_data['zone_bottom_limit_price'], 2) if 'zone_bottom_limit_price' in position_data else None,
"range_lower": round(position_data['range_lower'], 2),
"static_long": 0.0,
"timestamp_open": int(time.time()),
"timestamp_open": open_ts,
"timestamp_close": None
}
# Remove None keys to keep it clean? Or keep structure?
# User wants specific structure.
current_data.append(new_entry)
print(f"Recorded new AUTOMATIC position {position_data['token_id']} in {STATUS_FILE}")
@ -644,7 +671,7 @@ def main():
print("Position Closed & Status Updated.")
# --- REBALANCE ON CLOSE (If Price Dropped) ---
if status_str == "OUT OF RANGE (BELOW)":
if REBALANCE_ON_CLOSE_BELOW_RANGE and status_str == "OUT OF RANGE (BELOW)":
print("📉 Position closed BELOW range (100% ETH). Selling 50% of WETH inventory to USDC...")
try:
# Get WETH Balance