-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
206 lines (169 loc) · 8.8 KB
/
main.py
File metadata and controls
206 lines (169 loc) · 8.8 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
"""
main.py
───────
Entry point for the EMA Crossover Trading Bot.
Wires all modules together and runs the main trading loop:
config.py ← all settings
logger.py ← logging setup
mt5_connector.py ← MT5 session + data
ema_crossover.py ← EMA signal generation
risk_manager.py ← lot sizing, SL/TP, drawdown guard
trade_manager.py ← order execution + position management
performance_metrics.py ← Sharpe, Sortino, Calmar etc.
backtest.py ← vectorised backtester + optimiser
notifier.py ← Telegram bot + notifications
Run:
python main.py
"""
import time
import threading
import schedule
import config
from logger import log
from mt5_connector import MT5Connector
from ema_crossover import StrategyEngine
from risk_manager import RiskManager
from trade_manager import TradeManager
from backtest import EMABacktester
from notifier import TelegramNotifier
# ═══════════════════════════════════════════════════════════════════
# SHARED BOT STATE
# Passed by reference into TelegramNotifier so Telegram commands
# can start/stop the bot and update counters.
# ═══════════════════════════════════════════════════════════════════
bot_state = {
"running": False, # Set True via /start in Telegram
"trades_today": 0,
"total_pnl": 0.0,
"peak_balance": 0.0,
}
# ═══════════════════════════════════════════════════════════════════
# BOT TICK — called every CHECK_INTERVAL_SEC by the scheduler
# ═══════════════════════════════════════════════════════════════════
def bot_tick(connector: MT5Connector,
engine: StrategyEngine,
risk_manager: RiskManager,
trade_manager: TradeManager,
notifier: TelegramNotifier):
"""
One iteration of the main trading loop:
1. Check drawdown circuit breaker
2. Update trailing stop on open position (if any)
3. Get current EMA signal
4. If opposite signal → close existing position
5. If signal → open new position
"""
if not bot_state["running"]:
return
# ── Reconnect if session dropped ──────────────────────────────
if not connector.ensure_connected():
log.error("bot_tick: MT5 reconnect failed — skipping tick.")
return
acct = connector.get_account_info()
if not acct:
log.warning("bot_tick: could not fetch account info.")
return
equity = acct["equity"]
balance = acct["balance"]
# ── Track peak balance ────────────────────────────────────────
if equity > bot_state["peak_balance"]:
bot_state["peak_balance"] = equity
# ── Drawdown circuit breaker ──────────────────────────────────
if risk_manager.check_drawdown(bot_state["peak_balance"], equity):
bot_state["running"] = False
notifier.notify_drawdown_halt(bot_state["peak_balance"], equity)
return
# ── Trailing stop update ──────────────────────────────────────
if config.TRAILING_SL:
pos = trade_manager.get_open_position()
if pos:
risk_manager.update_trailing_stop(pos)
# ── Signal ────────────────────────────────────────────────────
signal = engine.get_signal()
log.info(f"Tick | Signal: {signal} | Equity: ${equity:.2f} | Balance: ${balance:.2f}")
if signal == "HOLD":
return
pos = trade_manager.get_open_position()
# ── Opposite signal → close current position ──────────────────
if pos is not None:
pos_is_buy = pos.type == 0
sig_is_buy = signal == "BUY"
if pos_is_buy != sig_is_buy:
result = trade_manager.close_trade(pos, comment=f"EMABot-{signal}")
if result["success"]:
bot_state["total_pnl"] += pos.profit
notifier.notify_trade_closed(pos, reason=f"Opposite signal ({signal})")
else:
log.error(f"Failed to close position: {result}")
return # don't open a new trade if close failed
else:
log.info(f"Same direction ({signal}) — holding existing position.")
return
# ── Open new position ─────────────────────────────────────────
result = trade_manager.open_trade(signal)
if result["success"]:
bot_state["trades_today"] += 1
notifier.notify_trade_opened(result)
else:
log.error(f"Failed to open trade: {result}")
# ═══════════════════════════════════════════════════════════════════
# SCHEDULER THREAD — runs bot_tick in the background
# ═══════════════════════════════════════════════════════════════════
def run_scheduler(connector, engine, risk_manager, trade_manager, notifier):
"""Runs in a daemon thread — fires bot_tick every N seconds."""
interval = config.CHECK_INTERVAL_SEC
schedule.every(interval).seconds.do(
bot_tick, connector, engine, risk_manager, trade_manager, notifier
)
log.info(f"Scheduler running — scanning every {interval}s")
while True:
schedule.run_pending()
time.sleep(1)
# ═══════════════════════════════════════════════════════════════════
# MAIN
# ═══════════════════════════════════════════════════════════════════
def main():
log.info("=" * 60)
log.info(" EMA CROSSOVER BOT — Starting up")
log.info("=" * 60)
# ── 1. Connect to MT5 ─────────────────────────────────────────
connector = MT5Connector()
if not connector.connect():
log.error("Cannot connect to MT5. Check config.py credentials.")
return
acct = connector.get_account_info()
bot_state["peak_balance"] = acct.get("balance", 0)
log.info(
f"Account: {acct.get('login')} | "
f"{acct.get('balance', 0):.2f} {acct.get('currency', '')} | "
f"Server: {acct.get('server', '')}"
)
# ── 2. Build modules ──────────────────────────────────────────
engine = StrategyEngine(connector)
risk_manager = RiskManager(connector)
trade_manager = TradeManager(connector, risk_manager)
backtester = EMABacktester(engine)
# ── 3. Build Telegram notifier ────────────────────────────────
notifier = TelegramNotifier(
connector, engine, backtester, trade_manager, bot_state
)
notifier.build()
# ── 4. Start scheduler in background thread ───────────────────
sched_thread = threading.Thread(
target=run_scheduler,
args=(connector, engine, risk_manager, trade_manager, notifier),
daemon=True,
)
sched_thread.start()
log.info("Background scheduler thread started.")
# ── 5. Start Telegram polling (blocking) ─────────────────────
log.info("Starting Telegram polling — open your bot and type /start")
try:
notifier.start_polling()
except KeyboardInterrupt:
log.info("Interrupted by user.")
finally:
connector.disconnect()
log.info("Bot shut down cleanly.")
if __name__ == "__main__":
main()