Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import ssl
|
|
| 5 |
import struct
|
| 6 |
import logging
|
| 7 |
import time
|
| 8 |
-
import asyncio
|
| 9 |
from datetime import datetime
|
| 10 |
import requests
|
| 11 |
from fastapi import FastAPI, Request, Form, HTTPException
|
|
@@ -873,8 +872,8 @@ async def webhook(request: Request):
|
|
| 873 |
ticker = data.get("ticker", "").upper()
|
| 874 |
price = data.get("price")
|
| 875 |
|
| 876 |
-
if action not in ["BUY", "SELL", "STOP-TRADE", "CLOSE"
|
| 877 |
-
raise HTTPException(status_code=400, detail="Invalid action. Must be BUY, SELL, STOP-TRADE,
|
| 878 |
|
| 879 |
# Load and refresh tokens
|
| 880 |
tokens = get_valid_tokens()
|
|
@@ -885,7 +884,7 @@ async def webhook(request: Request):
|
|
| 885 |
access_token = tokens["access_token"]
|
| 886 |
|
| 887 |
log_time = datetime.now().strftime("%H:%M:%S")
|
| 888 |
-
color = "#60a5fa" if action == "BUY" else "#f87171" if action == "SELL" else "#fb923c" if action == "CLOSE" else "#
|
| 889 |
|
| 890 |
try:
|
| 891 |
# Establish SSL Socket connection to cTrader Open API
|
|
@@ -980,42 +979,6 @@ async def webhook(request: Request):
|
|
| 980 |
else:
|
| 981 |
execution_msg = f"No open positions on {ticker} to close."
|
| 982 |
|
| 983 |
-
elif action == "ANCHOR":
|
| 984 |
-
# ANCHOR = consolidation detected. Open opposite trade, wait 5 sec, then close ALL.
|
| 985 |
-
anchor_direction = data.get("direction", "").upper()
|
| 986 |
-
|
| 987 |
-
# Step 1: Open the opposite (hedging) trade
|
| 988 |
-
if anchor_direction == "BUY":
|
| 989 |
-
logger.info(f"ANCHOR: Opening hedging BUY on {ticker}...")
|
| 990 |
-
res = place_market_order(ssl_sock, account_id, symbol_id, ProtoOATradeSide.BUY, current_trade_volume)
|
| 991 |
-
logger.info(f"ANCHOR: Hedging BUY opened. Result: {res.get('status')} {res.get('message', '')}")
|
| 992 |
-
elif anchor_direction == "SELL":
|
| 993 |
-
logger.info(f"ANCHOR: Opening hedging SELL on {ticker}...")
|
| 994 |
-
res = place_market_order(ssl_sock, account_id, symbol_id, ProtoOATradeSide.SELL, current_trade_volume)
|
| 995 |
-
logger.info(f"ANCHOR: Hedging SELL opened. Result: {res.get('status')} {res.get('message', '')}")
|
| 996 |
-
else:
|
| 997 |
-
logger.warning(f"ANCHOR: No direction specified, skipping hedge trade.")
|
| 998 |
-
|
| 999 |
-
# Step 2: Wait 5 seconds for the hedge trade to fully settle
|
| 1000 |
-
ssl_sock.close()
|
| 1001 |
-
logger.info(f"ANCHOR: Waiting 5 seconds before closing all positions on {ticker}...")
|
| 1002 |
-
await asyncio.sleep(5)
|
| 1003 |
-
|
| 1004 |
-
# Step 3: Reconnect and close ALL positions on this symbol
|
| 1005 |
-
ssl_sock = get_authenticated_socket(account_id, access_token)
|
| 1006 |
-
symbol_id = resolve_symbol_id(ssl_sock, account_id, ticker)
|
| 1007 |
-
positions = get_open_positions(ssl_sock, account_id)
|
| 1008 |
-
symbol_positions = [p for p in positions if p.tradeData.symbolId == symbol_id]
|
| 1009 |
-
|
| 1010 |
-
closed_count = 0
|
| 1011 |
-
for p in symbol_positions:
|
| 1012 |
-
side = ProtoOATradeSide.SELL if p.tradeData.tradeSide == 1 else ProtoOATradeSide.BUY
|
| 1013 |
-
logger.info(f"ANCHOR: Closing {'BUY' if p.tradeData.tradeSide == 1 else 'SELL'} position {p.positionId}")
|
| 1014 |
-
place_market_order(ssl_sock, account_id, symbol_id, side, p.tradeData.volume, position_id=p.positionId)
|
| 1015 |
-
closed_count += 1
|
| 1016 |
-
|
| 1017 |
-
execution_msg = f"ANCHOR: Opened hedging {anchor_direction}, then closed {closed_count} position(s) on {ticker}. Consolidation neutralized."
|
| 1018 |
-
|
| 1019 |
ssl_sock.close()
|
| 1020 |
|
| 1021 |
# Log to in-memory dashboard logs
|
|
|
|
| 5 |
import struct
|
| 6 |
import logging
|
| 7 |
import time
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
import requests
|
| 10 |
from fastapi import FastAPI, Request, Form, HTTPException
|
|
|
|
| 872 |
ticker = data.get("ticker", "").upper()
|
| 873 |
price = data.get("price")
|
| 874 |
|
| 875 |
+
if action not in ["BUY", "SELL", "STOP-TRADE", "CLOSE"]:
|
| 876 |
+
raise HTTPException(status_code=400, detail="Invalid action. Must be BUY, SELL, STOP-TRADE, or CLOSE")
|
| 877 |
|
| 878 |
# Load and refresh tokens
|
| 879 |
tokens = get_valid_tokens()
|
|
|
|
| 884 |
access_token = tokens["access_token"]
|
| 885 |
|
| 886 |
log_time = datetime.now().strftime("%H:%M:%S")
|
| 887 |
+
color = "#60a5fa" if action == "BUY" else "#f87171" if action == "SELL" else "#fb923c" if action == "CLOSE" else "#94a3b8"
|
| 888 |
|
| 889 |
try:
|
| 890 |
# Establish SSL Socket connection to cTrader Open API
|
|
|
|
| 979 |
else:
|
| 980 |
execution_msg = f"No open positions on {ticker} to close."
|
| 981 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 982 |
ssl_sock.close()
|
| 983 |
|
| 984 |
# Log to in-memory dashboard logs
|