Update app.py
Browse files
app.py
CHANGED
|
@@ -43,7 +43,58 @@ symbol_volumes = {
|
|
| 43 |
"EURUSD": 0.01,
|
| 44 |
"_DEFAULT_FOREX": 0.01, # catch-all for any forex pair not explicitly listed
|
| 45 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
logger.info(f"Per-symbol volumes initialized: {symbol_volumes}")
|
|
|
|
| 47 |
|
| 48 |
# Global caches to eliminate latency of symbol resolution and limits querying
|
| 49 |
SYMBOL_ID_CACHE = {} # symbol_name_upper -> symbol_id
|
|
@@ -782,21 +833,31 @@ async def dashboard(request: Request):
|
|
| 782 |
if sym not in symbol_volumes:
|
| 783 |
continue
|
| 784 |
vol = symbol_volumes[sym]
|
|
|
|
|
|
|
|
|
|
| 785 |
display_name = "Default Forex" if sym == "_DEFAULT_FOREX" else sym
|
| 786 |
row_bg = "#0f172a" if display_order.index(sym) % 2 == 0 else "#111827"
|
| 787 |
volume_rows_html += f'''
|
| 788 |
<tr style="border-bottom: 1px solid #1e293b; background: {row_bg};">
|
| 789 |
-
<
|
| 790 |
-
|
| 791 |
-
<
|
| 792 |
-
|
| 793 |
<input type="text" name="volume" value="{vol:.2f}"
|
| 794 |
style="background: #1e293b; border: 1px solid #334155; border-radius: 6px; padding: 6px 10px; color: #e2e8f0; font-size: 14px; width: 80px;">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 795 |
<button type="submit" class="btn" style="padding: 6px 14px; font-size: 12px;">Update</button>
|
| 796 |
-
</
|
| 797 |
-
</
|
| 798 |
-
|
| 799 |
-
</tr>'''
|
| 800 |
|
| 801 |
# 2. Construct OAuth status card HTML
|
| 802 |
auth_card_html = ""
|
|
@@ -834,6 +895,8 @@ async def dashboard(request: Request):
|
|
| 834 |
<tr style="border-bottom: 1px solid #334155;">
|
| 835 |
<th style="text-align: left; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Symbol</th>
|
| 836 |
<th style="text-align: left; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Lot Size</th>
|
|
|
|
|
|
|
| 837 |
<th style="padding: 8px 12px;"></th>
|
| 838 |
</tr>
|
| 839 |
</thead>
|
|
@@ -1144,16 +1207,31 @@ async def callback(request: Request, code: str = None):
|
|
| 1144 |
error_detail = f" Error: {res_data.get('errorCode', '')} - {res_data.get('description', '')}"
|
| 1145 |
return HTMLResponse(f"Authorization failed. Please check your Client ID/Secret and logs.{error_detail}", status_code=400)
|
| 1146 |
|
| 1147 |
-
@app.post("/
|
| 1148 |
-
async def
|
| 1149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1150 |
try:
|
| 1151 |
vol_float = float(volume.strip())
|
| 1152 |
symbol_upper = symbol.strip().upper()
|
| 1153 |
symbol_volumes[symbol_upper] = vol_float
|
| 1154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1155 |
except Exception as e:
|
| 1156 |
-
logger.error(f"Invalid
|
| 1157 |
|
| 1158 |
space_host = os.environ.get("SPACE_HOST")
|
| 1159 |
if space_host:
|
|
@@ -1179,6 +1257,14 @@ async def webhook(request: Request):
|
|
| 1179 |
if action not in ["BUY", "SELL", "STOP-TRADE", "CLOSE", "ANCHOR"]:
|
| 1180 |
raise HTTPException(status_code=400, detail="Invalid action. Must be BUY, SELL, STOP-TRADE, CLOSE, or ANCHOR")
|
| 1181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1182 |
# Load and refresh tokens
|
| 1183 |
tokens = get_valid_tokens()
|
| 1184 |
if not tokens or not tokens.get("account_id") or not tokens.get("access_token"):
|
|
|
|
| 43 |
"EURUSD": 0.01,
|
| 44 |
"_DEFAULT_FOREX": 0.01, # catch-all for any forex pair not explicitly listed
|
| 45 |
}
|
| 46 |
+
|
| 47 |
+
# Per-symbol trade execution status toggles
|
| 48 |
+
symbol_status = {
|
| 49 |
+
"NAS100": {"buy": True, "sell": True},
|
| 50 |
+
"NAS100m": {"buy": True, "sell": True},
|
| 51 |
+
"USTEC": {"buy": True, "sell": True},
|
| 52 |
+
"US500": {"buy": True, "sell": True},
|
| 53 |
+
"US500m": {"buy": True, "sell": True},
|
| 54 |
+
"US30": {"buy": True, "sell": True},
|
| 55 |
+
"US30m": {"buy": True, "sell": True},
|
| 56 |
+
"BTCUSD": {"buy": True, "sell": True},
|
| 57 |
+
"EURUSD": {"buy": True, "sell": True},
|
| 58 |
+
"_DEFAULT_FOREX": {"buy": True, "sell": True},
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
CONFIG_FILE = "bot_config.json"
|
| 62 |
+
|
| 63 |
+
def load_config():
|
| 64 |
+
global symbol_volumes, symbol_status
|
| 65 |
+
if os.path.exists(CONFIG_FILE):
|
| 66 |
+
try:
|
| 67 |
+
with open(CONFIG_FILE, "r") as f_conf:
|
| 68 |
+
config = json.load(f_conf)
|
| 69 |
+
if "volumes" in config:
|
| 70 |
+
symbol_volumes.update(config["volumes"])
|
| 71 |
+
if "status" in config:
|
| 72 |
+
# Merge keys carefully
|
| 73 |
+
for k, v in config["status"].items():
|
| 74 |
+
if k in symbol_status:
|
| 75 |
+
symbol_status[k].update(v)
|
| 76 |
+
else:
|
| 77 |
+
symbol_status[k] = v
|
| 78 |
+
logger.info("Configuration loaded from bot_config.json")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Error loading bot_config.json: {e}")
|
| 81 |
+
|
| 82 |
+
def save_config():
|
| 83 |
+
try:
|
| 84 |
+
config = {
|
| 85 |
+
"volumes": symbol_volumes,
|
| 86 |
+
"status": symbol_status
|
| 87 |
+
}
|
| 88 |
+
with open(CONFIG_FILE, "w") as f_conf:
|
| 89 |
+
json.dump(config, f_conf, indent=4)
|
| 90 |
+
logger.info("Configuration saved to bot_config.json")
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"Error saving bot_config.json: {e}")
|
| 93 |
+
|
| 94 |
+
# Initial load of configuration
|
| 95 |
+
load_config()
|
| 96 |
logger.info(f"Per-symbol volumes initialized: {symbol_volumes}")
|
| 97 |
+
logger.info(f"Per-symbol statuses initialized: {symbol_status}")
|
| 98 |
|
| 99 |
# Global caches to eliminate latency of symbol resolution and limits querying
|
| 100 |
SYMBOL_ID_CACHE = {} # symbol_name_upper -> symbol_id
|
|
|
|
| 833 |
if sym not in symbol_volumes:
|
| 834 |
continue
|
| 835 |
vol = symbol_volumes[sym]
|
| 836 |
+
status = symbol_status.get(sym, {"buy": True, "sell": True})
|
| 837 |
+
buy_checked = 'checked' if status.get("buy", True) else ''
|
| 838 |
+
sell_checked = 'checked' if status.get("sell", True) else ''
|
| 839 |
display_name = "Default Forex" if sym == "_DEFAULT_FOREX" else sym
|
| 840 |
row_bg = "#0f172a" if display_order.index(sym) % 2 == 0 else "#111827"
|
| 841 |
volume_rows_html += f'''
|
| 842 |
<tr style="border-bottom: 1px solid #1e293b; background: {row_bg};">
|
| 843 |
+
<form action="/update-symbol-config" method="post">
|
| 844 |
+
<input type="hidden" name="symbol" value="{sym}">
|
| 845 |
+
<td style="padding: 8px 12px; color: #e2e8f0; font-size: 14px; font-weight: 600;">{display_name}</td>
|
| 846 |
+
<td style="padding: 8px 12px;">
|
| 847 |
<input type="text" name="volume" value="{vol:.2f}"
|
| 848 |
style="background: #1e293b; border: 1px solid #334155; border-radius: 6px; padding: 6px 10px; color: #e2e8f0; font-size: 14px; width: 80px;">
|
| 849 |
+
</td>
|
| 850 |
+
<td style="padding: 8px 12px; text-align: center;">
|
| 851 |
+
<input type="checkbox" name="allow_buy" value="true" {buy_checked} style="width: 18px; height: 18px; cursor: pointer; accent-color: #00E676;">
|
| 852 |
+
</td>
|
| 853 |
+
<td style="padding: 8px 12px; text-align: center;">
|
| 854 |
+
<input type="checkbox" name="allow_sell" value="true" {sell_checked} style="width: 18px; height: 18px; cursor: pointer; accent-color: #FF5252;">
|
| 855 |
+
</td>
|
| 856 |
+
<td style="padding: 8px 12px; text-align: right;">
|
| 857 |
<button type="submit" class="btn" style="padding: 6px 14px; font-size: 12px;">Update</button>
|
| 858 |
+
</td>
|
| 859 |
+
</form>
|
| 860 |
+
</tr>''''
|
|
|
|
| 861 |
|
| 862 |
# 2. Construct OAuth status card HTML
|
| 863 |
auth_card_html = ""
|
|
|
|
| 895 |
<tr style="border-bottom: 1px solid #334155;">
|
| 896 |
<th style="text-align: left; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Symbol</th>
|
| 897 |
<th style="text-align: left; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Lot Size</th>
|
| 898 |
+
<th style="text-align: center; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Allow BUY</th>
|
| 899 |
+
<th style="text-align: center; padding: 8px 12px; color: #94a3b8; font-size: 13px;">Allow SELL</th>
|
| 900 |
<th style="padding: 8px 12px;"></th>
|
| 901 |
</tr>
|
| 902 |
</thead>
|
|
|
|
| 1207 |
error_detail = f" Error: {res_data.get('errorCode', '')} - {res_data.get('description', '')}"
|
| 1208 |
return HTMLResponse(f"Authorization failed. Please check your Client ID/Secret and logs.{error_detail}", status_code=400)
|
| 1209 |
|
| 1210 |
+
@app.post("/update-symbol-config")
|
| 1211 |
+
async def update_symbol_config(
|
| 1212 |
+
request: Request,
|
| 1213 |
+
symbol: str = Form(...),
|
| 1214 |
+
volume: str = Form(...),
|
| 1215 |
+
allow_buy: str = Form(None),
|
| 1216 |
+
allow_sell: str = Form(None)
|
| 1217 |
+
):
|
| 1218 |
+
global symbol_volumes, symbol_status
|
| 1219 |
try:
|
| 1220 |
vol_float = float(volume.strip())
|
| 1221 |
symbol_upper = symbol.strip().upper()
|
| 1222 |
symbol_volumes[symbol_upper] = vol_float
|
| 1223 |
+
|
| 1224 |
+
# HTML form check boxes only submit if they are checked (value = 'true')
|
| 1225 |
+
buy_bool = allow_buy == "true"
|
| 1226 |
+
sell_bool = allow_sell == "true"
|
| 1227 |
+
symbol_status[symbol_upper] = {"buy": buy_bool, "sell": sell_bool}
|
| 1228 |
+
|
| 1229 |
+
# Save config to file
|
| 1230 |
+
save_config()
|
| 1231 |
+
|
| 1232 |
+
logger.info(f"Config updated for '{symbol_upper}': vol={vol_float} lots, buy={buy_bool}, sell={sell_bool}")
|
| 1233 |
except Exception as e:
|
| 1234 |
+
logger.error(f"Invalid config update for symbol '{symbol}': vol={volume}. Error: {e}")
|
| 1235 |
|
| 1236 |
space_host = os.environ.get("SPACE_HOST")
|
| 1237 |
if space_host:
|
|
|
|
| 1257 |
if action not in ["BUY", "SELL", "STOP-TRADE", "CLOSE", "ANCHOR"]:
|
| 1258 |
raise HTTPException(status_code=400, detail="Invalid action. Must be BUY, SELL, STOP-TRADE, CLOSE, or ANCHOR")
|
| 1259 |
|
| 1260 |
+
# Check per-symbol status settings
|
| 1261 |
+
if action in ["BUY", "SELL"]:
|
| 1262 |
+
status = symbol_status.get(ticker, symbol_status.get("_DEFAULT_FOREX", {"buy": True, "sell": True}))
|
| 1263 |
+
is_allowed = status.get(action.lower(), True)
|
| 1264 |
+
if not is_allowed:
|
| 1265 |
+
logger.info(f"Webhook {action} received for {ticker} but IGNORED because {action} is disabled on the dashboard.")
|
| 1266 |
+
return {"status": "ignored", "message": f"{action} is disabled for {ticker} on the dashboard"}
|
| 1267 |
+
|
| 1268 |
# Load and refresh tokens
|
| 1269 |
tokens = get_valid_tokens()
|
| 1270 |
if not tokens or not tokens.get("account_id") or not tokens.get("access_token"):
|