forked from Joaopeuko/Mql5-Python-Integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrade.py
381 lines (329 loc) · 13.7 KB
/
trade.py
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
from datetime import datetime, timedelta
import MetaTrader5 as Mt5
class Trade:
"""
Represents a trading strategy for a financial instrument.
Args:
expert_name (str): The name of the expert advisor.
version (str): The version of the expert advisor.
symbol (str): The financial instrument symbol.
magic_number (int): The magic number for identifying trades.
lot (float): The number of lots to trade.
stop_loss (float): The stop loss level.
emergency_stop_loss (float): Emergency stop loss as a protection.
take_profit (float): The take profit level.
emergency_take_profit (float): Emergency take profit for gain.
start_time (str): The time when the expert advisor can start trading.
finishing_time (str): The time until new positions can be opened.
ending_time (str): The time when any remaining position will be closed.
fee (float): The average fee per trading.
"""
def __init__(
self,
expert_name: str,
version: str,
symbol: str,
magic_number: int,
lot: float,
stop_loss: float,
emergency_stop_loss: float,
take_profit: float,
emergency_take_profit: float,
start_time: str = "9:15",
finishing_time: str = "17:30",
ending_time: str = "17:50",
fee: float = 0.0,
) -> None:
"""
Initialize the Trade object.
Returns:
None
"""
self.expert_name: str = expert_name
self.version: str = version
self.symbol: str = symbol
self.magic_number: int = magic_number
self.lot: float = lot
self.stop_loss: float = stop_loss
self.emergency_stop_loss: float = emergency_stop_loss
self.take_profit: float = take_profit
self.emergency_take_profit: float = emergency_take_profit
self.start_time_hour, self.start_time_minutes = start_time.split(":")
self.finishing_time_hour, self.finishing_time_minutes = finishing_time.split(":")
self.ending_time_hour, self.ending_time_minutes = ending_time.split(":")
self.fee: float = fee
self.loss_deals: int = 0
self.profit_deals: int = 0
self.total_deals: int = 0
self.balance: float = 0.0
self.ticket: int = 0
print("\nInitializing the basics.")
self.initialize()
self.select_symbol()
self.prepare_symbol()
self.sl_tp_steps: float = Mt5.symbol_info(self.symbol).trade_tick_size / Mt5.symbol_info(self.symbol).point
print("Initialization successfully completed.")
print()
self.summary()
print("Running")
print()
def initialize(self) -> None:
"""
Initialize the MetaTrader 5 instance.
Returns:
None
"""
if not Mt5.initialize():
print("Initialization failed, check internet connection. You must have Meta Trader 5 installed.")
Mt5.shutdown()
else:
print(
f"You are running the {self.expert_name} expert advisor,"
f" version {self.version}, on symbol {self.symbol}."
)
def select_symbol(self) -> None:
"""
Select the trading symbol.
Returns:
None
"""
Mt5.symbol_select(self.symbol, True)
def prepare_symbol(self) -> None:
"""
Prepare the trading symbol for opening positions.
Returns:
None
"""
symbol_info = Mt5.symbol_info(self.symbol)
if symbol_info is None:
print(f"It was not possible to find {self.symbol}")
Mt5.shutdown()
print("Turned off")
quit()
if not symbol_info.visible:
print(f"The {self.symbol} is not visible, needed to be switched on.")
if not Mt5.symbol_select(self.symbol, True):
print(f"The expert advisor {self.expert_name} failed in select the symbol {self.symbol}, turning off.")
Mt5.shutdown()
print("Turned off")
quit()
def summary(self) -> None:
"""
Print a summary of the expert advisor parameters.
Returns:
None
"""
print(
f"Summary:\n"
f"ExpertAdvisor name: {self.expert_name}\n"
f"ExpertAdvisor version: {self.version}\n"
f"Running on symbol: {self.symbol}\n"
f"MagicNumber: {self.magic_number}\n"
f"Number of lot(s): {self.lot}\n"
f"StopLoss: {self.stop_loss}\n"
f"TakeProfit: {self.take_profit}\n"
f"Emergency StopLoss: {self.emergency_stop_loss}\n"
f"Emergency TakeProfit: {self.emergency_take_profit}\n"
f"Start trading time: {self.start_time_hour}:{self.start_time_minutes}\n"
f"Finishing trading time: {self.finishing_time_hour}:{self.finishing_time_minutes}\n"
f"Closing position after: {self.ending_time_hour}:{self.ending_time_minutes}\n"
f"Average fee per trading: {self.fee}\n"
f"StopLoss & TakeProfit Steps: {self.sl_tp_steps}\n"
)
def statistics(self) -> None:
"""
Print statistics of the expert advisor.
Returns:
None
"""
print(f"Total of deals: {self.total_deals}, {self.profit_deals} gain, {self.loss_deals} loss.")
print(
f"Balance: {self.balance}, fee: {self.total_deals * self.fee}, final balance:"
f" {self.balance - (self.total_deals * self.fee)}."
)
if self.total_deals != 0:
print(f"Accuracy: {round((self.profit_deals / self.total_deals) * 100, 2)}%.\n")
def open_buy_position(self, comment: str = "") -> None:
"""
Open a Buy position.
Args:
comment (str): A comment for the trade.
Returns:
None
"""
point = Mt5.symbol_info(self.symbol).point
price = Mt5.symbol_info_tick(self.symbol).ask
self.ticket = Mt5.positions_get()[0].ticket if len(Mt5.positions_get()) == 1 else 0
request = {
"action": Mt5.TRADE_ACTION_DEAL,
"symbol": self.symbol,
"volume": self.lot,
"type": Mt5.ORDER_TYPE_BUY,
"price": price,
"sl": price - self.emergency_stop_loss * point,
"tp": price + self.emergency_take_profit * point,
"deviation": 5,
"magic": self.magic_number,
"comment": str(comment),
"type_time": Mt5.ORDER_TIME_GTC,
"type_filling": Mt5.ORDER_FILLING_RETURN,
"position": (Mt5.positions_get()[0].ticket if len(Mt5.positions_get()) == 1 else 0),
}
result = Mt5.order_send(request)
self.request_result(price, result)
def open_sell_position(self, comment: str = "") -> None:
"""
Open a Sell position.
Args:
comment (str): A comment for the trade.
Returns:
None
"""
point = Mt5.symbol_info(self.symbol).point
price = Mt5.symbol_info_tick(self.symbol).bid
self.ticket = Mt5.positions_get()[0].ticket if len(Mt5.positions_get()) == 1 else 0
request = {
"action": Mt5.TRADE_ACTION_DEAL,
"symbol": self.symbol,
"volume": self.lot,
"type": Mt5.ORDER_TYPE_SELL,
"price": price,
"sl": price + self.emergency_stop_loss * point,
"tp": price - self.emergency_take_profit * point,
"deviation": 5,
"magic": self.magic_number,
"comment": str(comment),
"type_time": Mt5.ORDER_TIME_GTC,
"type_filling": Mt5.ORDER_FILLING_RETURN,
"position": (Mt5.positions_get()[0].ticket if len(Mt5.positions_get()) == 1 else 0),
}
result = Mt5.order_send(request)
self.request_result(price, result)
def request_result(self, price: float, result) -> None:
"""
Process the result of a trading request.
Args:
price (float): The price of the trade.
result (Mt5.TradeResult): The result of the trading request.
Returns:
None
"""
# Send a trading request
# Check the execution result
print(f"Order sent: {self.symbol}, {self.lot} lot(s), at {price}.")
if result.retcode != Mt5.TRADE_RETCODE_DONE:
print(f"Something went wrong while retrieving ret_code, error: {result.retcode}")
# Print the result
if result.retcode == Mt5.TRADE_RETCODE_DONE:
if len(Mt5.positions_get(symbol=self.symbol)) == 1:
order_type = "Buy" if Mt5.positions_get(symbol=self.symbol)[0].type == 0 else "Sell"
print(order_type, "Position Opened:", result.price)
else:
print(f"Position Closed: {result.price}")
def open_position(self, buy: bool, sell: bool, comment: str = "") -> None:
"""
Open a position based on buy and sell conditions.
Args:
buy (bool): True if a Buy position should be opened, False otherwise.
sell (bool): True if a Sell position should be opened, False otherwise.
comment (str): A comment for the trade.
Returns:
None
"""
if (len(Mt5.positions_get(symbol=self.symbol)) == 0) and self.trading_time():
if buy and not sell:
self.open_buy_position(comment)
self.total_deals += 1
if sell and not buy:
self.open_sell_position(comment)
self.total_deals += 1
self.stop_and_gain(comment)
if self.days_end():
print("It is the end of trading the day.")
print("Closing all positions.")
self.close_position(comment)
self.summary()
def close_position(self, comment: str = "") -> None:
"""
Close an open position.
Args:
comment (str): A comment for the trade.
Returns:
None
"""
# buy (0) and sell(1)
if len(Mt5.positions_get(symbol=self.symbol)) == 1:
if Mt5.positions_get(symbol=self.symbol)[0].type == 0: # if Buy
self.open_sell_position(comment)
elif Mt5.positions_get(symbol=self.symbol)[0].type == 1: # if Sell
self.open_buy_position(comment)
def stop_and_gain(self, comment: str = "") -> None:
"""
Check for stop loss and take profit conditions and close positions accordingly.
Args:
comment (str): A comment for the trade.
Returns:
None
"""
if len(Mt5.positions_get()) == 1:
points = (
Mt5.positions_get()[0].profit
* Mt5.symbol_info(self.symbol).trade_tick_size
/ Mt5.symbol_info(self.symbol).trade_tick_value
) / Mt5.positions_get()[0].volume
if points / Mt5.symbol_info(self.symbol).point >= self.take_profit:
self.profit_deals += 1
self.close_position(comment)
print(
f"Take profit reached. ("
f"{Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[-1].profit}"
f")\n"
)
if (
Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[-1].symbol
== self.symbol
):
self.balance += Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[
-1
].profit
self.statistics()
elif ((points / Mt5.symbol_info(self.symbol).point) * -1) >= self.stop_loss:
self.loss_deals += 1
self.close_position(comment)
print(
f"Stop loss reached. ("
f"{Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[-1].profit}"
f")\n"
)
if (
Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[-1].symbol
== self.symbol
):
self.balance += Mt5.history_deals_get((datetime.today() - timedelta(days=1)), datetime.now())[
-1
].profit
self.statistics()
def days_end(self) -> bool:
"""
Check if it is the end of trading for the day.
Returns:
bool: True if it is the end of trading for the day, False otherwise.
"""
if datetime.now().hour >= int(self.ending_time_hour) and datetime.now().minute >= int(self.ending_time_minutes):
return True
return False
def trading_time(self) -> bool:
"""
Check if it is within the allowed trading time.
Returns:
bool: True if it is within the allowed trading time, False otherwise.
"""
if int(self.start_time_hour) < datetime.now().hour < int(self.finishing_time_hour):
return True
elif datetime.now().hour == int(self.start_time_hour):
if datetime.now().minute >= int(self.start_time_minutes):
return True
elif datetime.now().hour == int(self.finishing_time_hour):
if datetime.now().minute < int(self.finishing_time_minutes):
return True
return False