-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_server.py
More file actions
1215 lines (1050 loc) · 44.7 KB
/
bot_server.py
File metadata and controls
1215 lines (1050 loc) · 44.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Whilber-AI Trading Bot Server — Standalone Live Execution System
Runs on port 8001, independent from the website server (port 8000).
Reads tracker signals, filters by qualified strategy whitelist, executes on MT5.
"""
import asyncio
import json
import logging
import os
import sys
import time
from contextlib import asynccontextmanager
from datetime import datetime, timedelta, timezone
from pathlib import Path
import sqlite3
import MetaTrader5 as mt5
import uvicorn
from fastapi import FastAPI
from fastapi.responses import HTMLResponse, JSONResponse
# ---------------------------------------------------------------------------
# Paths
# ---------------------------------------------------------------------------
BASE_DIR = Path(r"C:\Users\Administrator\Desktop\mvp")
DATA_DIR = BASE_DIR / "data"
DB_PATH = BASE_DIR / "db" / "whilber.db"
WHITELIST_PATH = DATA_DIR / "bot_whitelist.json"
PENDING_SIGNALS_PATH = DATA_DIR / "pending_signals.json"
ACTIVE_TRACKS_PATH = BASE_DIR / "track_records" / "active_tracks.json"
EXECUTOR_CONFIG_PATH = DATA_DIR / "analysis" / "executor_config.json"
STATE_PATH = DATA_DIR / "bot_state.json"
TRADES_PATH = DATA_DIR / "bot_trades.json"
LOG_PATH = DATA_DIR / "bot_server.log"
# ---------------------------------------------------------------------------
# MT5 Credentials
# ---------------------------------------------------------------------------
MT5_PATH = r"C:\Program Files\Moneta Markets MT5 Terminal\terminal64.exe"
MT5_LOGIN = 1035360
MT5_PASSWORD = "G0Z#IQ1w"
MT5_SERVERS = ["MonetaMarketsTrading-Demo", "MonetaMarkets-Demo"] # Try in order
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
MAGIC_NUMBER = 888999
BOT_COMMENT_PREFIX = "BOT"
SIGNAL_POLL_INTERVAL = 10 # seconds
POSITION_CHECK_INTERVAL = 30 # seconds
MAX_OPEN_POSITIONS = 25
MAX_DRAWDOWN_PCT = 5.0
COOLDOWN_HOURS = 4
BE_PROFIT_RATIO = 0.50 # move to BE at 50% of SL distance
MIN_RR = 1.5
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)-7s | %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[
logging.FileHandler(LOG_PATH, encoding="utf-8"),
logging.StreamHandler(sys.stdout),
],
)
log = logging.getLogger("bot")
# ---------------------------------------------------------------------------
# Global State
# ---------------------------------------------------------------------------
state = {
"started": None,
"cycles": 0,
"orders_sent": 0,
"orders_failed": 0,
"last_cycle": None,
"daily_loss_usd": 0.0,
"daily_loss_reset": None,
"cooldowns": {}, # "strategy_id": ISO timestamp
"active_trades": {}, # ticket_str: {strategy_id, symbol, direction, entry, sl, tp, opened_at, lot}
"mt5_connected": False,
"running": False,
"balance_start": 0.0,
}
whitelist = {} # strategy_id -> {symbol, lot, ...}
whitelist_ids = set()
executor_config = {} # from executor_config.json
closed_trades = [] # history of closed trades
# ===================================================================
# MT5 Connection (standalone — no singleton, avoids website conflicts)
# ===================================================================
def mt5_connect() -> bool:
"""Connect to MT5 terminal. Uses path-only init to preserve AutoTrading state."""
if mt5.terminal_info() is not None:
info = mt5.terminal_info()
if info.connected:
state["mt5_connected"] = True
return True
# IMPORTANT: Initialize with path only (not login/password/server).
# Using credentials causes MT5 to re-initialize and disables AutoTrading.
# The terminal must already be logged in to the correct account.
init_ok = mt5.initialize(path=MT5_PATH)
if init_ok:
acct = mt5.account_info()
if acct and acct.login == MT5_LOGIN:
log.info(f"MT5 connected (path-only) — Account {acct.login}")
elif acct:
log.warning(f"MT5 connected but wrong account: {acct.login} (expected {MT5_LOGIN})")
# Wrong account — need to re-init with credentials (will disable AutoTrading)
mt5.shutdown()
init_ok = False
# Fallback: full credentials (this WILL disable AutoTrading — user must re-enable)
if not init_ok:
log.warning("Path-only init failed, trying with credentials (AutoTrading will be disabled)...")
for server_name in MT5_SERVERS:
init_ok = mt5.initialize(path=MT5_PATH, login=MT5_LOGIN, password=MT5_PASSWORD, server=server_name)
if init_ok:
log.info(f"MT5 connected with server: {server_name}")
log.warning("NOTE: AutoTrading was likely disabled by re-init. Please enable it in MT5.")
break
mt5.shutdown()
time.sleep(1)
if not init_ok:
log.error(f"MT5 init failed: {mt5.last_error()}")
state["mt5_connected"] = False
return False
acct = mt5.account_info()
if acct is None:
log.error("MT5 account_info() returned None")
state["mt5_connected"] = False
return False
state["mt5_connected"] = True
state["balance_start"] = acct.balance
log.info(f"MT5 connected — Account {acct.login} | Balance ${acct.balance:.2f} | Equity ${acct.equity:.2f}")
ti = mt5.terminal_info()
if ti and not ti.trade_allowed:
log.warning("AutoTrading is DISABLED. Enable it in MT5 toolbar (Algo Trading button).")
else:
log.info("AutoTrading is ENABLED — ready to trade")
return True
def mt5_ensure() -> bool:
"""Ensure MT5 is connected, reconnect if needed."""
try:
info = mt5.terminal_info()
if info and info.connected:
state["mt5_connected"] = True
return True
except Exception:
pass
log.warning("MT5 disconnected — reconnecting...")
mt5.shutdown()
time.sleep(1)
return mt5_connect()
BROKER_OVERRIDES = {
"US30": "DJ30",
}
def get_broker_name(symbol: str) -> str:
"""Resolve internal symbol name to broker name via executor_config, with overrides."""
if symbol in BROKER_OVERRIDES:
return BROKER_OVERRIDES[symbol]
if symbol in executor_config.get("symbols", {}):
bn = executor_config["symbols"][symbol]["broker_name"]
# Verify symbol exists on MT5
if mt5.symbol_info(bn) is not None:
return bn
# Try without + suffix
if bn.endswith("+") and mt5.symbol_info(bn[:-1]) is not None:
return bn[:-1]
return symbol
def get_symbol_config(symbol: str) -> dict:
"""Get full symbol config from executor_config."""
return executor_config.get("symbols", {}).get(symbol, {})
# ===================================================================
# Whitelist & Config Loading
# ===================================================================
def load_whitelist():
"""Load qualified strategy whitelist."""
global whitelist, whitelist_ids
try:
data = json.loads(WHITELIST_PATH.read_text(encoding="utf-8"))
whitelist = {}
for s in data.get("strategies", []):
whitelist[s["strategy_id"]] = s
whitelist_ids = set(whitelist.keys())
log.info(f"Whitelist loaded: {len(whitelist_ids)} strategies")
except Exception as e:
log.error(f"Failed to load whitelist: {e}")
def load_executor_config():
"""Load executor config for symbol details."""
global executor_config
try:
executor_config = json.loads(EXECUTOR_CONFIG_PATH.read_text(encoding="utf-8"))
log.info(f"Executor config loaded: {len(executor_config.get('symbols', {}))} symbols")
except Exception as e:
log.error(f"Failed to load executor config: {e}")
# ===================================================================
# Signal Reading
# ===================================================================
def read_pending_signals() -> list:
"""Read signals from pending_signals.json (non-destructive read)."""
try:
if not PENDING_SIGNALS_PATH.exists():
return []
text = PENDING_SIGNALS_PATH.read_text(encoding="utf-8").strip()
if not text:
return []
data = json.loads(text)
return data if isinstance(data, list) else []
except Exception as e:
log.warning(f"Error reading pending signals: {e}")
return []
def read_active_tracks() -> list:
"""Read active tracks from tracker to find new open signals."""
try:
if not ACTIVE_TRACKS_PATH.exists():
return []
text = ACTIVE_TRACKS_PATH.read_text(encoding="utf-8").strip()
if not text:
return []
data = json.loads(text)
if isinstance(data, dict):
# Could be keyed by strategy_id
tracks = []
for key, val in data.items():
if isinstance(val, list):
tracks.extend(val)
elif isinstance(val, dict):
tracks.append(val)
return tracks
return data if isinstance(data, list) else []
except Exception as e:
log.warning(f"Error reading active tracks: {e}")
return []
def filter_signals(signals: list) -> list:
"""Filter signals to only whitelisted strategies, no duplicates, respect cooldowns."""
now = datetime.now(timezone.utc)
valid = []
# Current open strategy_ids and symbol-direction map
open_strats = {t["strategy_id"] for t in state["active_trades"].values()}
open_sym_dirs = {} # symbol -> set of directions already open
for t in state["active_trades"].values():
sym = t.get("symbol", "")
d = t.get("direction", "")
open_sym_dirs.setdefault(sym, set()).add(d)
for sig in signals:
sid = sig.get("strategy_id", "")
# Must match whitelist (could be partial match — ADX_03 in ADX_03_BTCUSD_H1)
matched_wl = None
if sid in whitelist_ids:
matched_wl = whitelist[sid]
else:
# Try matching by prefix: signal might have "ADX_03" and whitelist has "ADX_03_BTCUSD_H1"
sym = sig.get("symbol", "")
for wl_id, wl_data in whitelist.items():
if wl_id.startswith(sid) and wl_data["symbol"] == sym:
matched_wl = wl_data
sid = wl_id
break
if matched_wl is None:
continue
# Symbol must match
sig_symbol = sig.get("symbol", "")
if sig_symbol != matched_wl["symbol"]:
continue
# Direction must be present
direction = sig.get("signal") or sig.get("direction") or sig.get("signal_type")
if direction not in ("BUY", "SELL"):
continue
# No duplicate positions (same strategy)
if sid in open_strats:
continue
# No conflicting direction on same symbol (prevent hedging BUY+SELL)
opposite = "SELL" if direction == "BUY" else "BUY"
if sig_symbol in open_sym_dirs and opposite in open_sym_dirs[sig_symbol]:
continue
# Cooldown check
cd_key = sid
if cd_key in state["cooldowns"]:
cd_time = datetime.fromisoformat(state["cooldowns"][cd_key])
if now < cd_time:
continue
# Must have SL and TP
sl = sig.get("sl_price", 0)
tp = sig.get("tp_price") or sig.get("tp1_price", 0)
entry = sig.get("entry_price", 0)
if not all([sl, tp, entry]):
continue
# R:R check
sl_dist = abs(entry - sl)
tp_dist = abs(tp - entry)
if sl_dist == 0 or (tp_dist / sl_dist) < MIN_RR:
continue
valid.append({
"strategy_id": sid,
"symbol": matched_wl["symbol"],
"direction": direction,
"entry_price": entry,
"sl_price": sl,
"tp_price": tp,
"lot": matched_wl["lot"],
"confidence": sig.get("confidence", 0),
})
return valid
# ===================================================================
# Trade Execution
# ===================================================================
def open_trade(signal: dict) -> bool:
"""Execute a trade on MT5 based on a filtered signal."""
symbol = signal["symbol"]
broker_name = get_broker_name(symbol)
direction = signal["direction"]
lot = signal["lot"]
sl_price = signal["sl_price"]
tp_price = signal["tp_price"]
strategy_id = signal["strategy_id"]
sym_config = get_symbol_config(symbol)
digits = sym_config.get("digits", 2)
point = sym_config.get("point", 0.01)
stop_level = sym_config.get("stop_level", 20)
# Ensure symbol is available
sym_info = mt5.symbol_info(broker_name)
if sym_info is None:
log.error(f"Symbol {broker_name} not found on MT5")
return False
if not sym_info.visible:
mt5.symbol_select(broker_name, True)
# Get current price
tick = mt5.symbol_info_tick(broker_name)
if tick is None:
log.error(f"No tick data for {broker_name}")
return False
if direction == "BUY":
order_type = mt5.ORDER_TYPE_BUY
price = tick.ask
else:
order_type = mt5.ORDER_TYPE_SELL
price = tick.bid
# Validate SL/TP distance against stop level
min_dist = stop_level * point
if abs(price - sl_price) < min_dist:
log.warning(f"SL too close for {strategy_id}: {abs(price - sl_price):.5f} < {min_dist:.5f}")
return False
if abs(price - tp_price) < min_dist:
log.warning(f"TP too close for {strategy_id}: {abs(price - tp_price):.5f} < {min_dist:.5f}")
return False
# Safety checks
positions = mt5.positions_get()
if positions and len(positions) >= MAX_OPEN_POSITIONS:
log.warning(f"Max positions ({MAX_OPEN_POSITIONS}) reached — skipping {strategy_id}")
return False
acct = mt5.account_info()
if acct:
dd = (acct.balance - acct.equity) / acct.balance * 100 if acct.balance > 0 else 0
if dd > MAX_DRAWDOWN_PCT:
log.warning(f"Drawdown {dd:.1f}% exceeds {MAX_DRAWDOWN_PCT}% — skipping")
return False
comment = f"{BOT_COMMENT_PREFIX}|{strategy_id}"[:31]
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": broker_name,
"volume": lot,
"type": order_type,
"price": price,
"sl": round(sl_price, digits),
"tp": round(tp_price, digits),
"deviation": 20,
"magic": MAGIC_NUMBER,
"comment": comment,
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC,
}
log.info(f"SENDING ORDER: {direction} {lot} {broker_name} @ {price:.{digits}f} | SL={sl_price:.{digits}f} TP={tp_price:.{digits}f} | {strategy_id}")
result = mt5.order_send(request)
if result is None:
log.error(f"order_send returned None — {mt5.last_error()}")
state["orders_failed"] += 1
return False
if result.retcode != mt5.TRADE_RETCODE_DONE:
log.error(f"Order FAILED: retcode={result.retcode} comment={result.comment} | {strategy_id}")
state["orders_failed"] += 1
return False
ticket = result.order
log.info(f"ORDER FILLED: ticket={ticket} | {direction} {lot} {broker_name} | {strategy_id}")
state["orders_sent"] += 1
# Track the trade
state["active_trades"][str(ticket)] = {
"strategy_id": strategy_id,
"symbol": symbol,
"broker_name": broker_name,
"direction": direction,
"entry_price": price,
"sl_price": sl_price,
"tp_price": tp_price,
"lot": lot,
"ticket": ticket,
"opened_at": datetime.now(timezone.utc).isoformat(),
"be_moved": False,
}
# Set cooldown
state["cooldowns"][strategy_id] = (datetime.now(timezone.utc) + timedelta(hours=COOLDOWN_HOURS)).isoformat()
# Persist immediately (prevent orphan if crash after this)
save_state()
db_record_trade_open(state["active_trades"][str(ticket)])
return True
# ===================================================================
# Position Management
# ===================================================================
def manage_positions():
"""Sync positions with MT5, detect closed trades, manage breakeven."""
mt5_positions = mt5.positions_get()
mt5_tickets = set()
if mt5_positions:
mt5_tickets = {str(p.ticket) for p in mt5_positions}
# Recover orphaned positions (in MT5 with our magic, but not in state)
if mt5_positions:
for pos in mt5_positions:
ticket_str = str(pos.ticket)
if pos.magic == MAGIC_NUMBER and ticket_str not in state["active_trades"]:
strategy_id = "RECOVERED"
if pos.comment and "|" in pos.comment:
strategy_id = pos.comment.split("|", 1)[1]
direction = "BUY" if pos.type == 0 else "SELL"
state["active_trades"][ticket_str] = {
"strategy_id": strategy_id,
"symbol": pos.symbol.rstrip("+"),
"broker_name": pos.symbol,
"direction": direction,
"entry_price": pos.price_open,
"sl_price": pos.sl,
"tp_price": pos.tp,
"lot": pos.volume,
"ticket": pos.ticket,
"opened_at": datetime.fromtimestamp(pos.time, tz=timezone.utc).isoformat(),
"be_moved": False,
}
log.info(f"RECOVERED orphaned position: ticket={pos.ticket} {direction} {pos.volume} {pos.symbol}")
save_state()
if not state["active_trades"]:
return
# Detect closed trades
closed_tickets = []
for ticket_str, trade in list(state["active_trades"].items()):
if ticket_str not in mt5_tickets:
closed_tickets.append(ticket_str)
for ticket_str in closed_tickets:
trade = state["active_trades"].pop(ticket_str)
record_closed_trade(trade)
if closed_tickets:
save_state()
# Breakeven management for remaining positions
if not mt5_positions:
return
for pos in mt5_positions:
ticket_str = str(pos.ticket)
if ticket_str not in state["active_trades"]:
continue
trade = state["active_trades"][ticket_str]
if trade.get("be_moved"):
continue
entry = trade["entry_price"]
sl = trade["sl_price"]
sl_dist = abs(entry - sl)
sym_config = get_symbol_config(trade["symbol"])
digits = sym_config.get("digits", 2)
point = sym_config.get("point", 0.01)
if trade["direction"] == "BUY":
current_profit_dist = pos.price_current - entry
be_target = sl_dist * BE_PROFIT_RATIO
if current_profit_dist >= be_target:
new_sl = entry + (2 * point) # tiny offset above entry
if new_sl > trade["sl_price"]:
move_to_breakeven(pos, trade, new_sl, digits)
else:
current_profit_dist = entry - pos.price_current
be_target = sl_dist * BE_PROFIT_RATIO
if current_profit_dist >= be_target:
new_sl = entry - (2 * point) # tiny offset below entry
if new_sl < trade["sl_price"]:
move_to_breakeven(pos, trade, new_sl, digits)
def move_to_breakeven(pos, trade: dict, new_sl: float, digits: int):
"""Modify position SL to breakeven."""
broker_name = trade["broker_name"]
request = {
"action": mt5.TRADE_ACTION_SLTP,
"symbol": broker_name,
"position": pos.ticket,
"sl": round(new_sl, digits),
"tp": round(trade["tp_price"], digits),
}
result = mt5.order_send(request)
if result and result.retcode == mt5.TRADE_RETCODE_DONE:
trade["be_moved"] = True
trade["sl_price"] = new_sl
ticket_str = str(pos.ticket)
state["active_trades"][ticket_str] = trade
log.info(f"BE MOVED: {trade['strategy_id']} ticket={pos.ticket} new_sl={new_sl:.{digits}f}")
else:
err = result.comment if result else mt5.last_error()
log.warning(f"BE move failed for {trade['strategy_id']}: {err}")
def record_closed_trade(trade: dict):
"""Record a closed trade to history."""
symbol = trade["symbol"]
sym_config = get_symbol_config(symbol)
pip_size = sym_config.get("pip_size", 1.0)
# Try to get close info from deal history
ticket = trade.get("ticket", 0)
close_price = 0.0
close_time = datetime.now(timezone.utc).isoformat()
try:
now = datetime.now(timezone.utc)
from_time = now - timedelta(days=7)
deals = mt5.history_deals_get(from_time, now, group=f"*{trade['broker_name']}*")
if deals:
for deal in reversed(deals):
if deal.position_id == ticket and deal.entry == 1: # entry=1 means exit deal
close_price = deal.price
close_time = datetime.fromtimestamp(deal.time, tz=timezone.utc).isoformat()
break
except Exception:
pass
if close_price == 0:
close_price = trade.get("entry_price", 0)
# Calculate PnL
if trade["direction"] == "BUY":
pnl_pips = (close_price - trade["entry_price"]) / pip_size
else:
pnl_pips = (trade["entry_price"] - close_price) / pip_size
record = {
"ticket": ticket,
"strategy_id": trade["strategy_id"],
"symbol": symbol,
"direction": trade["direction"],
"lot": trade["lot"],
"entry_price": trade["entry_price"],
"close_price": close_price,
"sl_price": trade.get("sl_price", 0),
"tp_price": trade.get("tp_price", 0),
"pnl_pips": round(pnl_pips, 1),
"opened_at": trade.get("opened_at", ""),
"closed_at": close_time,
"be_moved": trade.get("be_moved", False),
}
closed_trades.append(record)
save_trades()
db_record_trade_close(record)
outcome = "WIN" if pnl_pips > 0 else "LOSS"
log.info(f"TRADE CLOSED: {outcome} {trade['strategy_id']} | {pnl_pips:+.1f} pips | {trade['direction']} {symbol}")
# Update daily loss
if pnl_pips < 0:
state["daily_loss_usd"] += abs(pnl_pips)
def emergency_close_all(reason: str = "manual") -> int:
"""Close all open positions managed by this bot."""
closed = 0
positions = mt5.positions_get()
if not positions:
return 0
for pos in positions:
if pos.magic != MAGIC_NUMBER:
continue
close_type = mt5.ORDER_TYPE_SELL if pos.type == 0 else mt5.ORDER_TYPE_BUY
tick = mt5.symbol_info_tick(pos.symbol)
if not tick:
continue
price = tick.bid if pos.type == 0 else tick.ask
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": pos.symbol,
"volume": pos.volume,
"type": close_type,
"position": pos.ticket,
"price": price,
"deviation": 20,
"magic": MAGIC_NUMBER,
"comment": f"BOT_CLOSE|{reason}"[:31],
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC,
}
result = mt5.order_send(request)
if result and result.retcode == mt5.TRADE_RETCODE_DONE:
closed += 1
log.info(f"EMERGENCY CLOSE: ticket={pos.ticket} {pos.symbol} | reason={reason}")
else:
err = result.comment if result else mt5.last_error()
log.error(f"Failed to close ticket={pos.ticket}: {err}")
# Clear active trades
state["active_trades"].clear()
save_state()
return closed
# ===================================================================
# State Persistence
# ===================================================================
def save_state():
"""Save bot state to JSON file."""
try:
out = {k: v for k, v in state.items() if k != "mt5_connected" and k != "running"}
STATE_PATH.write_text(json.dumps(out, indent=2, default=str), encoding="utf-8")
except Exception as e:
log.warning(f"Failed to save state: {e}")
def load_state():
"""Load bot state from JSON file."""
try:
if STATE_PATH.exists():
data = json.loads(STATE_PATH.read_text(encoding="utf-8"))
for key in ("cycles", "orders_sent", "orders_failed", "daily_loss_usd",
"cooldowns", "active_trades", "balance_start"):
if key in data:
state[key] = data[key]
log.info(f"State restored: {data.get('cycles', 0)} cycles, {len(state['active_trades'])} active trades")
except Exception as e:
log.warning(f"Failed to load state: {e}")
def save_trades():
"""Save closed trade history."""
try:
TRADES_PATH.write_text(json.dumps(closed_trades, indent=2, default=str), encoding="utf-8")
except Exception as e:
log.warning(f"Failed to save trades: {e}")
def load_trades():
"""Load closed trade history."""
global closed_trades
try:
if TRADES_PATH.exists():
closed_trades = json.loads(TRADES_PATH.read_text(encoding="utf-8"))
log.info(f"Trade history loaded: {len(closed_trades)} closed trades")
except Exception as e:
log.warning(f"Failed to load trades: {e}")
# ===================================================================
# SQLite Trade Recording (shared with website server)
# ===================================================================
def init_trade_executions_table():
"""Create trade_executions table in shared SQLite database."""
try:
conn = sqlite3.connect(str(DB_PATH))
conn.execute("""
CREATE TABLE IF NOT EXISTS trade_executions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source TEXT NOT NULL DEFAULT 'bot_server',
ticket INTEGER,
strategy_id TEXT,
symbol TEXT,
direction TEXT,
entry_price REAL,
close_price REAL,
sl_price REAL,
tp_price REAL,
lot_size REAL,
pnl_pips REAL,
pnl_usd REAL,
magic INTEGER,
opened_at TEXT,
closed_at TEXT,
be_moved INTEGER DEFAULT 0,
status TEXT DEFAULT 'open',
created_at TEXT DEFAULT (datetime('now'))
)
""")
conn.execute("CREATE INDEX IF NOT EXISTS idx_te_ticket ON trade_executions(ticket)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_te_status ON trade_executions(status)")
conn.execute("CREATE INDEX IF NOT EXISTS idx_te_strategy ON trade_executions(strategy_id)")
conn.commit()
conn.close()
log.info("trade_executions table ready in shared SQLite DB")
except Exception as e:
log.warning(f"Failed to init trade_executions table: {e}")
def db_record_trade_open(trade: dict):
"""Write an opened trade to SQLite."""
try:
conn = sqlite3.connect(str(DB_PATH))
conn.execute("""
INSERT OR REPLACE INTO trade_executions
(source, ticket, strategy_id, symbol, direction, entry_price,
sl_price, tp_price, lot_size, magic, opened_at, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'open')
""", (
"bot_server",
trade.get("ticket", 0),
trade.get("strategy_id", ""),
trade.get("symbol", ""),
trade.get("direction", ""),
trade.get("entry_price", 0),
trade.get("sl_price", 0),
trade.get("tp_price", 0),
trade.get("lot", 0),
MAGIC_NUMBER,
trade.get("opened_at", ""),
))
conn.commit()
conn.close()
except Exception as e:
log.warning(f"Failed to record trade open to DB: {e}")
def db_record_trade_close(record: dict):
"""Update a closed trade in SQLite."""
try:
conn = sqlite3.connect(str(DB_PATH))
conn.execute("""
UPDATE trade_executions
SET close_price=?, pnl_pips=?, closed_at=?, be_moved=?, status='closed'
WHERE ticket=? AND source='bot_server'
""", (
record.get("close_price", 0),
record.get("pnl_pips", 0),
record.get("closed_at", ""),
1 if record.get("be_moved") else 0,
record.get("ticket", 0),
))
# Also compute PnL USD from deal history if available
conn.commit()
conn.close()
except Exception as e:
log.warning(f"Failed to record trade close to DB: {e}")
def db_backfill_active_trades():
"""Backfill existing active trades to SQLite (for trades opened before this feature)."""
try:
conn = sqlite3.connect(str(DB_PATH))
for ticket_str, trade in state.get("active_trades", {}).items():
existing = conn.execute(
"SELECT id FROM trade_executions WHERE ticket=? AND source='bot_server'",
(trade.get("ticket", 0),)
).fetchone()
if not existing:
conn.execute("""
INSERT INTO trade_executions
(source, ticket, strategy_id, symbol, direction, entry_price,
sl_price, tp_price, lot_size, magic, opened_at, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'open')
""", (
"bot_server",
trade.get("ticket", 0),
trade.get("strategy_id", ""),
trade.get("symbol", ""),
trade.get("direction", ""),
trade.get("entry_price", 0),
trade.get("sl_price", 0),
trade.get("tp_price", 0),
trade.get("lot", 0),
MAGIC_NUMBER,
trade.get("opened_at", ""),
))
# Also backfill closed trades
for record in closed_trades:
existing = conn.execute(
"SELECT id FROM trade_executions WHERE ticket=? AND source='bot_server'",
(record.get("ticket", 0),)
).fetchone()
if not existing:
conn.execute("""
INSERT INTO trade_executions
(source, ticket, strategy_id, symbol, direction, entry_price,
close_price, sl_price, tp_price, lot_size, pnl_pips, magic,
opened_at, closed_at, be_moved, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'closed')
""", (
"bot_server",
record.get("ticket", 0),
record.get("strategy_id", ""),
record.get("symbol", ""),
record.get("direction", ""),
record.get("entry_price", 0),
record.get("close_price", 0),
record.get("sl_price", 0),
record.get("tp_price", 0),
record.get("lot", 0),
record.get("pnl_pips", 0),
MAGIC_NUMBER,
record.get("opened_at", ""),
record.get("closed_at", ""),
1 if record.get("be_moved") else 0,
))
conn.commit()
conn.close()
log.info(f"DB backfill complete: {len(state.get('active_trades', {}))} active + {len(closed_trades)} closed")
except Exception as e:
log.warning(f"DB backfill failed: {e}")
# ===================================================================
# Orphaned Position Recovery
# ===================================================================
def recover_orphaned_positions():
"""Discover MT5 positions with our magic number that aren't in active_trades."""
try:
mt5_positions = mt5.positions_get()
if not mt5_positions:
return
recovered = 0
for pos in mt5_positions:
ticket_str = str(pos.ticket)
if pos.magic == MAGIC_NUMBER and ticket_str not in state["active_trades"]:
# Extract strategy_id from comment (format: "BOT|strategy_id")
strategy_id = "RECOVERED"
if pos.comment and "|" in pos.comment:
strategy_id = pos.comment.split("|", 1)[1]
direction = "BUY" if pos.type == 0 else "SELL"
state["active_trades"][ticket_str] = {
"strategy_id": strategy_id,
"symbol": pos.symbol.rstrip("+"),
"broker_name": pos.symbol,
"direction": direction,
"entry_price": pos.price_open,
"sl_price": pos.sl,
"tp_price": pos.tp,
"lot": pos.volume,
"ticket": pos.ticket,
"opened_at": datetime.fromtimestamp(pos.time, tz=timezone.utc).isoformat(),
"be_moved": False,
}
recovered += 1
log.info(f"RECOVERED orphaned position: ticket={pos.ticket} {direction} {pos.volume} {pos.symbol} | {strategy_id}")
if recovered > 0:
save_state()
log.info(f"Recovered {recovered} orphaned positions from MT5")
except Exception as e:
log.warning(f"Orphan recovery failed: {e}")
# ===================================================================
# Background Tasks
# ===================================================================
async def signal_scanner_loop():
"""Main loop: scan signals every 10 seconds."""
log.info("Signal scanner started")
while state["running"]:
try:
if not mt5_ensure():
await asyncio.sleep(SIGNAL_POLL_INTERVAL)
continue
# Daily loss reset
today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
if state.get("daily_loss_reset") != today:
state["daily_loss_reset"] = today
state["daily_loss_usd"] = 0.0
# Drawdown check
acct = mt5.account_info()
if acct and acct.balance > 0:
dd = (acct.balance - acct.equity) / acct.balance * 100
if dd > MAX_DRAWDOWN_PCT:
log.warning(f"DRAWDOWN {dd:.1f}% > {MAX_DRAWDOWN_PCT}% — emergency close!")
emergency_close_all("max_drawdown")
await asyncio.sleep(SIGNAL_POLL_INTERVAL)
continue
# Read signals from both sources
pending = read_pending_signals()
tracks = read_active_tracks()
# Extract signals from active tracks (open status + BUY/SELL)
track_signals = []
for t in tracks:
if t.get("status") in ("open", "active") and t.get("direction") in ("BUY", "SELL"):
track_signals.append({
"strategy_id": t.get("strategy_id", ""),
"symbol": t.get("symbol", ""),
"signal": t.get("direction"),
"entry_price": t.get("entry_price", 0),
"sl_price": t.get("sl_price", 0),
"tp_price": t.get("tp1_price") or t.get("tp_price", 0),
"confidence": t.get("confidence", 50),
})
all_signals = pending + track_signals
if all_signals:
valid = filter_signals(all_signals)
for sig in valid:
log.info(f"SIGNAL: {sig['direction']} {sig['symbol']} | {sig['strategy_id']} | conf={sig['confidence']}")
open_trade(sig)
state["cycles"] += 1
state["last_cycle"] = datetime.now(timezone.utc).isoformat()
if state["cycles"] % 60 == 0:
pos_count = len(state["active_trades"])
balance = acct.balance if acct else 0
log.info(f"STATUS: cycle={state['cycles']} | positions={pos_count} | balance=${balance:.2f} | orders={state['orders_sent']}")
save_state()
except Exception as e:
log.error(f"Signal scanner error: {e}")
await asyncio.sleep(SIGNAL_POLL_INTERVAL)
async def position_manager_loop():
"""Position management loop: check every 30 seconds."""
log.info("Position manager started")
while state["running"]:
try: