hedge and auto hedger in separate folders
This commit is contained in:
@ -76,7 +76,7 @@ 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 = 451
|
||||
MONITOR_INTERVAL_SECONDS = 120
|
||||
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
|
||||
@ -84,8 +84,8 @@ OPEN_POSITION_ENABLED = True # If True, will open a new position if no auto posi
|
||||
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 = 2000.0 # Target total investment value in Token1 terms (e.g. 350 USDC)
|
||||
RANGE_WIDTH_PCT = 0.01 # +/- 2% range for new positions
|
||||
TARGET_INVESTMENT_VALUE_TOKEN1 = 200 # Target total investment value in Token1 terms (e.g. 350 USDC)
|
||||
RANGE_WIDTH_PCT = 0.003 # +/- 2% range for new positions
|
||||
|
||||
# JSON File for tracking position state
|
||||
STATUS_FILE = "hedge_status.json"
|
||||
@ -153,7 +153,7 @@ def update_hedge_status_file(action, position_data):
|
||||
"opened": opened_str,
|
||||
"status": "OPEN",
|
||||
"entry_price": round(position_data['entry_price'], 2),
|
||||
"target_value": round(position_data.get('target_value', 0.0), 2),
|
||||
"target_value": round(position_data['target_value'], 2), # Use actual calculated value
|
||||
"amount0_initial": fmt_amt0,
|
||||
"amount1_initial": fmt_amt1,
|
||||
|
||||
@ -175,7 +175,7 @@ def update_hedge_status_file(action, position_data):
|
||||
current_data.append(new_entry)
|
||||
print(f"Recorded new AUTOMATIC position {position_data['token_id']} in {STATUS_FILE}")
|
||||
|
||||
elif action == "CLOSE":
|
||||
elif action == "CLOSING":
|
||||
found = False
|
||||
for entry in current_data:
|
||||
if (
|
||||
@ -183,9 +183,31 @@ def update_hedge_status_file(action, position_data):
|
||||
entry.get('status') == "OPEN" and
|
||||
entry.get('token_id') == position_data['token_id']
|
||||
):
|
||||
entry['status'] = "CLOSING"
|
||||
found = True
|
||||
print(f"Marked position {entry['token_id']} as CLOSING in {STATUS_FILE}")
|
||||
break
|
||||
if not found:
|
||||
print(f"WARNING: Could not find open AUTOMATIC position {position_data['token_id']} to mark closing.")
|
||||
|
||||
elif action == "CLOSE":
|
||||
found = False
|
||||
for entry in current_data:
|
||||
if (
|
||||
entry.get('type') == "AUTOMATIC" and
|
||||
(entry.get('status') == "OPEN" or entry.get('status') == "CLOSING") and
|
||||
entry.get('token_id') == position_data['token_id']
|
||||
):
|
||||
|
||||
entry['status'] = "CLOSED"
|
||||
entry['timestamp_close'] = int(time.time())
|
||||
|
||||
# Add Closing Stats if provided
|
||||
if 'fees_collected_usd' in position_data:
|
||||
entry['fees_collected_usd'] = round(position_data['fees_collected_usd'], 2)
|
||||
if 'closed_position_value_usd' in position_data:
|
||||
entry['closed_position_value_usd'] = round(position_data['closed_position_value_usd'], 2)
|
||||
|
||||
found = True
|
||||
print(f"Marked position {entry['token_id']} as CLOSED in {STATUS_FILE}")
|
||||
break
|
||||
@ -669,11 +691,36 @@ def main():
|
||||
print(f"⚠️ Automatic Position {token_id} is OUT OF RANGE! Initiating Close...")
|
||||
liq = pos_details['liquidity']
|
||||
if liq > 0:
|
||||
if decrease_liquidity(w3, npm_contract, account, token_id, liq):
|
||||
time.sleep(5)
|
||||
collect_fees(w3, npm_contract, account, token_id)
|
||||
update_hedge_status_file("CLOSE", {'token_id': token_id})
|
||||
print("Position Closed & Status Updated.")
|
||||
# Mark as CLOSING immediately to notify Hedger
|
||||
update_hedge_status_file("CLOSING", {'token_id': token_id})
|
||||
|
||||
# Capture Balances Before Close
|
||||
b0_start, b1_start = get_token_balances(w3, account.address, pos_details['token0_address'], pos_details['token1_address'])
|
||||
|
||||
# Execute Close
|
||||
decrease_success = decrease_liquidity(w3, npm_contract, account, token_id, liq)
|
||||
time.sleep(2)
|
||||
collect_fees(w3, npm_contract, account, token_id)
|
||||
|
||||
if decrease_success:
|
||||
# Capture Balances After Close
|
||||
b0_end, b1_end = get_token_balances(w3, account.address, pos_details['token0_address'], pos_details['token1_address'])
|
||||
|
||||
# Calculate Deltas (Principal + Fees)
|
||||
delta0 = from_wei(b0_end - b0_start, pos_details['token0_decimals'])
|
||||
delta1 = from_wei(b1_end - b1_start, pos_details['token1_decimals'])
|
||||
|
||||
# Calculate Values
|
||||
total_exit_usd = (delta0 * current_price) + delta1
|
||||
# We calculated total_fees_usd earlier in the loop
|
||||
|
||||
update_data = {
|
||||
'token_id': token_id,
|
||||
'fees_collected_usd': total_fees_usd,
|
||||
'closed_position_value_usd': total_exit_usd
|
||||
}
|
||||
update_hedge_status_file("CLOSE", update_data)
|
||||
print(f"Position Closed. Value: ${total_exit_usd:.2f}, Fees: ${total_fees_usd:.2f}")
|
||||
|
||||
# --- REBALANCE ON CLOSE (If Price Dropped) ---
|
||||
if REBALANCE_ON_CLOSE_BELOW_RANGE and status_str == "OUT OF RANGE (BELOW)":
|
||||
@ -715,7 +762,7 @@ def main():
|
||||
|
||||
else:
|
||||
print("Liquidity 0. Marking closed.")
|
||||
update_hedge_status_file("CLOSE", {'token_id': token_id})
|
||||
update_hedge_status_file("CLOSE", {'token_id': token_id, 'fees_collected_usd': 0.0, 'closed_position_value_usd': 0.0})
|
||||
|
||||
# 2. Opening Logic (If no active automatic position)
|
||||
if not active_automatic_position and OPEN_POSITION_ENABLED:
|
||||
|
||||
Reference in New Issue
Block a user