-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_bot.py
95 lines (75 loc) · 2.35 KB
/
simple_bot.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 14 11:42:32 2021
@author: Felipe
"""
import MetaTrader5 as mt5
import time
if not mt5.initialize():
print("Fail to init")
mt5.shutdown()
symbol = "USDJPY"
symbol_info = mt5.symbol_info(symbol)
if symbol_info is None:
print(symbol, "Not found")
mt5.shutdown()
quit()
if not symbol_info.visible:
print(symbol, "not visible")
if not mt5.symbol_select(symbol,True):
print("symbol_select({}}) failed, exit",symbol)
mt5.shutdown()
quit()
def open_trade(action, symbol, lot, sl, tp, deviation):
if action == 'buy':
trade_type = mt5.ORDER_TYPE_BUY
price = mt5.symbol_info_tick(symbol).ask
elif action == 'sell':
trade_type = mt5.ORDER_TYPE_SELL
price = mt5.symbol_info_tick(symbol).bid
tp *= -1
sl *= -1
point = mt5.symbol_info(symbol).point
request = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": trade_type,
"price": price,
"sl": price - sl * point,
"tp": price + tp * point,
"deviation": deviation,
"magic": 234000,
"comment": "python request",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
result = mt5.order_send(request)
return result, request
def close_trade(request, result, deviation):
symbol = request['symbol']
if request['type'] == mt5.ORDER_TYPE_BUY:
trade_type = mt5.ORDER_TYPE_SELL
price = mt5.symbol_info_tick(symbol).bid
elif request['type'] == mt5.ORDER_TYPE_SELL:
trade_type= mt5.ORDER_TYPE_BUY
price = mt5.symbol_info_tick(symbol).ask
position_id = result.order
lot = request['volume']
close_request={
"action": mt5.TRADE_ACTION_DEAL,
"symbol": symbol,
"volume": lot,
"type": trade_type,
"position": position_id,
"price": price,
"deviation": deviation,
"magic": 234000,
"comment": "python request end",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_RETURN,
}
result = mt5.order_send(close_request)
result, request = open_trade('sell', 'USDJPY', 1.0, 100, 100, 20)
time.sleep(5)
close_trade(request, result, 10)