-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.py
36 lines (33 loc) · 1.21 KB
/
order.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
def open_trade(action, symbol, lot, sl_points, tp_points, deviation):
'''https://www.mql5.com/en/docs/integration/python_metatrader5/mt5ordersend_py
'''
# prepare the buy request structure
symbol_info = mt5.symbol_info(symbol)
if action == 'buy':
trade_type = mt5.ORDER_TYPE_BUY
price = mt5.symbol_info_tick(symbol).ask
slval = round(price - sl_points * 0.0001,5)
tpval = round(price + tp_points * 0.0001,5)
elif action =='sell':
trade_type = mt5.ORDER_TYPE_SELL
price = mt5.symbol_info_tick(symbol).bid
slval = round(price + sl_points * 0.0001,5)
tpval = round(price - tp_points * 0.0001,5)
point = mt5.symbol_info(symbol).point
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": trade_type,
"price": price,
"sl": slval,
"tp": tpval,
"deviation": deviation,
"magic": ordernum,
"comment": "sent by python",
"type_time": mt5.ORDER_TIME_GTC, # good till cancelled
"type_filling": mt5.ORDER_FILLING_RETURN,
}
# send a trading request
result = mt5.order_send(request)
return result, request