-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexecutor.py
More file actions
312 lines (275 loc) · 11.5 KB
/
Copy pathexecutor.py
File metadata and controls
312 lines (275 loc) · 11.5 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
"""
Executor — Order management: send, partial close (50/30/20),
SL-to-breakeven, delta trailing, emergency exit.
"""
import logging
import time as _time
from dataclasses import dataclass, field
from datetime import datetime, timezone
from typing import List, Optional
import MetaTrader5 as mt5
from . import config
log = logging.getLogger(__name__)
@dataclass
class ManagedPosition:
"""State for a live position being managed."""
ticket: int
direction: str # "long" or "short"
entry_price: float
stop_loss: float
take_profits: List[float] # [TP1, TP2, TP3]
total_volume: float
remaining_volume: float
risk_amount: float
tp_stage: int = 0 # 0=none closed, 1=TP1 hit, 2=TP2 hit, 3=all closed
breakeven_moved: bool = False
opened_at: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
class Executor:
"""
Handles all execution logic:
- Market entry (with DRY_RUN support)
- Partial close at TP1/TP2/TP3 (50%/30%/20% scale-out)
- SL to breakeven after TP1
- Delta-based trailing after TP2
- Emergency full close
"""
def __init__(self, symbol_spec):
self.spec = symbol_spec # SymbolSpec from mt5_connector
self._positions: dict[int, ManagedPosition] = {}
# ── Entry ──────────────────────────────────────
def open_trade(
self,
direction: str,
volume: float,
stop_loss: float,
take_profits: List[float],
risk_amount: float,
) -> Optional[ManagedPosition]:
"""
Send a market order. If DRY_RUN is True, simulate only.
Returns ManagedPosition on success, None on failure.
"""
symbol = config.SYMBOL
if direction == "long":
order_type = mt5.ORDER_TYPE_BUY
price = mt5.symbol_info_tick(symbol).ask
else:
order_type = mt5.ORDER_TYPE_SELL
price = mt5.symbol_info_tick(symbol).bid
if config.DRY_RUN:
fake_ticket = int(_time.time() * 1000) % 10_000_000
pos = ManagedPosition(
ticket=fake_ticket,
direction=direction,
entry_price=price,
stop_loss=stop_loss,
take_profits=take_profits,
total_volume=volume,
remaining_volume=volume,
risk_amount=risk_amount,
)
self._positions[fake_ticket] = pos
log.info("[DRY_RUN] Opened %s %.2f lots @ %.5f SL=%.5f TPs=%s",
direction, volume, price, stop_loss, take_profits)
return pos
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": volume,
"type": order_type,
"price": price,
"sl": stop_loss,
"tp": take_profits[0] if take_profits else 0.0,
"deviation": config.DEVIATION,
"magic": config.MAGIC_NUMBER,
"comment": "OFScalper entry",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_IOC,
}
result = mt5.order_send(request)
if result is None or result.retcode != mt5.TRADE_RETCODE_DONE:
code = result.retcode if result else "None"
log.error("Order failed: retcode=%s comment=%s",
code, getattr(result, "comment", ""))
return None
pos = ManagedPosition(
ticket=result.order,
direction=direction,
entry_price=result.price,
stop_loss=stop_loss,
take_profits=take_profits,
total_volume=volume,
remaining_volume=volume,
risk_amount=risk_amount,
)
self._positions[result.order] = pos
log.info("Opened %s %.2f lots @ %.5f ticket=%d",
direction, volume, result.price, result.order)
return pos
# ── Manage (called every analysis cycle) ──────
def manage_positions(self, current_price: float, delta_reversing: bool = False):
"""
Check all managed positions for TP hits / trailing / emergency.
"""
closed_tickets = []
for ticket, pos in self._positions.items():
if pos.tp_stage >= 3:
closed_tickets.append(ticket)
continue
self._check_tp_stages(pos, current_price)
if delta_reversing and pos.tp_stage >= 2:
self._emergency_close(pos, reason="delta reversal")
closed_tickets.append(ticket)
for t in closed_tickets:
self._positions.pop(t, None)
def _check_tp_stages(self, pos: ManagedPosition, price: float):
"""Scale-out logic: TP1 → 50%, TP2 → 30%, TP3 → 20%."""
tps = pos.take_profits
if not tps:
return
hit_long = pos.direction == "long"
# TP1
if pos.tp_stage == 0 and len(tps) > 0:
if (hit_long and price >= tps[0]) or (not hit_long and price <= tps[0]):
close_vol = round(pos.total_volume * config.TP_SPLIT[0], 2)
close_vol = max(close_vol, self.spec.volume_min)
self._partial_close(pos, close_vol, "TP1")
pos.tp_stage = 1
self._move_sl_to_breakeven(pos)
# TP2
if pos.tp_stage == 1 and len(tps) > 1:
if (hit_long and price >= tps[1]) or (not hit_long and price <= tps[1]):
close_vol = round(pos.total_volume * config.TP_SPLIT[1], 2)
close_vol = max(close_vol, self.spec.volume_min)
self._partial_close(pos, close_vol, "TP2")
pos.tp_stage = 2
# TP3
if pos.tp_stage == 2 and len(tps) > 2:
if (hit_long and price >= tps[2]) or (not hit_long and price <= tps[2]):
self._close_remaining(pos, "TP3")
pos.tp_stage = 3
def _partial_close(self, pos: ManagedPosition, volume: float, label: str):
volume = min(volume, pos.remaining_volume)
if volume <= 0:
return
if config.DRY_RUN:
pos.remaining_volume = round(pos.remaining_volume - volume, 2)
log.info("[DRY_RUN] Partial close %s: %.2f lots remaining=%.2f",
label, volume, pos.remaining_volume)
return
symbol = config.SYMBOL
if pos.direction == "long":
order_type = mt5.ORDER_TYPE_SELL
price = mt5.symbol_info_tick(symbol).bid
else:
order_type = mt5.ORDER_TYPE_BUY
price = mt5.symbol_info_tick(symbol).ask
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": volume,
"type": order_type,
"price": price,
"position": pos.ticket,
"deviation": config.DEVIATION,
"magic": config.MAGIC_NUMBER,
"comment": f"OFScalper {label}",
"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:
pos.remaining_volume = round(pos.remaining_volume - volume, 2)
log.info("Partial close %s: %.2f lots @ %.5f remaining=%.2f",
label, volume, result.price, pos.remaining_volume)
else:
code = result.retcode if result else "None"
log.error("Partial close %s failed: %s", label, code)
def _move_sl_to_breakeven(self, pos: ManagedPosition):
"""After TP1, move SL to entry price."""
if pos.breakeven_moved:
return
pos.breakeven_moved = True
new_sl = pos.entry_price
if config.DRY_RUN:
pos.stop_loss = new_sl
log.info("[DRY_RUN] SL moved to breakeven %.5f", new_sl)
return
request = {
"action": mt5.TRADE_ACTION_SLTP,
"symbol": config.SYMBOL,
"position": pos.ticket,
"sl": new_sl,
"tp": pos.take_profits[1] if len(pos.take_profits) > 1 else 0.0,
"magic": config.MAGIC_NUMBER,
}
result = mt5.order_send(request)
if result and result.retcode == mt5.TRADE_RETCODE_DONE:
pos.stop_loss = new_sl
log.info("SL moved to breakeven %.5f for ticket %d", new_sl, pos.ticket)
else:
log.error("Failed to move SL to breakeven for ticket %d", pos.ticket)
def trail_stop(self, pos: ManagedPosition, new_sl: float):
"""Trail SL further (e.g., after TP2 using delta-based logic)."""
if pos.direction == "long" and new_sl <= pos.stop_loss:
return
if pos.direction == "short" and new_sl >= pos.stop_loss:
return
if config.DRY_RUN:
pos.stop_loss = new_sl
log.info("[DRY_RUN] Trailing SL to %.5f", new_sl)
return
request = {
"action": mt5.TRADE_ACTION_SLTP,
"symbol": config.SYMBOL,
"position": pos.ticket,
"sl": new_sl,
"tp": 0.0,
"magic": config.MAGIC_NUMBER,
}
result = mt5.order_send(request)
if result and result.retcode == mt5.TRADE_RETCODE_DONE:
pos.stop_loss = new_sl
log.info("Trailing SL to %.5f for ticket %d", new_sl, pos.ticket)
def _close_remaining(self, pos: ManagedPosition, label: str):
if pos.remaining_volume <= 0:
return
self._partial_close(pos, pos.remaining_volume, label)
def _emergency_close(self, pos: ManagedPosition, reason: str = "emergency"):
log.warning("Emergency close ticket %d — reason: %s", pos.ticket, reason)
self._close_remaining(pos, f"EMERGENCY({reason})")
pos.tp_stage = 3
def emergency_close_all(self, reason: str = "shutdown"):
"""Close everything immediately (e.g., on shutdown or daily loss hit)."""
for pos in list(self._positions.values()):
self._emergency_close(pos, reason)
self._positions.clear()
# ── Helpers ────────────────────────────────────
def has_open_positions(self) -> bool:
return len(self._positions) > 0
def get_positions(self) -> List[ManagedPosition]:
return list(self._positions.values())
def compute_tp_levels(self, entry: float, direction: str) -> List[float]:
"""
Compute TP1/TP2/TP3 from entry price using config tick distances.
"""
tick = self.spec.point
if direction == "long":
return [
round(entry + config.TP1_TICKS * tick, self.spec.digits),
round(entry + config.TP2_TICKS * tick, self.spec.digits),
round(entry + config.TP3_TICKS * tick, self.spec.digits),
]
else:
return [
round(entry - config.TP1_TICKS * tick, self.spec.digits),
round(entry - config.TP2_TICKS * tick, self.spec.digits),
round(entry - config.TP3_TICKS * tick, self.spec.digits),
]
def compute_sl(self, entry: float, direction: str) -> float:
"""Compute SL from entry using config STOP_TICKS."""
tick = self.spec.point
if direction == "long":
return round(entry - config.STOP_TICKS * tick, self.spec.digits)
else:
return round(entry + config.STOP_TICKS * tick, self.spec.digits)