-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorders.py
More file actions
80 lines (66 loc) · 2.54 KB
/
Copy pathorders.py
File metadata and controls
80 lines (66 loc) · 2.54 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
import MetaTrader5 as mt5
import datetime
CANDLES_BETWEEN_OPERATIONS = 5
STOPLOSS = 200
TAKEPROFIT = 16
RSI_TOP = 65
RSI_BOTTOM = 35
def open_position(market: str, lotage: float, type_op):
"""Function to open a position.
Args:
market (string)
lotage (float)
type_op: Type of the operation
"""
point = mt5.symbol_info(market).point
price = mt5.symbol_info_tick(market).ask
if type_op == mt5.ORDER_TYPE_BUY:
sl = price-STOPLOSS*point
tp = price+TAKEPROFIT*point
else:
sl = price+STOPLOSS*point
tp = price-TAKEPROFIT*point
deviation = 20
operation = {
"action": mt5.TRADE_ACTION_DEAL,
"symbol": market,
"volume": lotage,
"type": type_op,
"price": price,
"sl": sl,
"tp": tp,
"deviation": deviation,
"magic": 234000,
"comment": "RSI PYTHON CLIENT",
"type_time": mt5.ORDER_TIME_GTC,
"type_filling": mt5.ORDER_FILLING_FOK,
}
# Sending the buy
result = mt5.order_send(operation)
print("[Thread - orders] 1. order_send(): by {} {} lots at {} with deviation={} points".format(market,lotage,price,deviation))
if result.retcode != mt5.TRADE_RETCODE_DONE:
print("[Thread - orders] Failed operation: retcode={}".format(result.retcode))
return None
def thread_orders(stop_event, data, trading_data):
"""Function executed by a thread. It checks if the conditions to open orders
are okay.
Args:
stop_event (thread.Event): Event to stop the thread.
data (dict): Dictionary with candles and the indicator.
trading_data (dict): Dictionary with the lotage and the market.
"""
last_operation_time = 0
ep = datetime.datetime(1970,1,1,0,0,0)
while data["RSI"] is None:
pass
print("[INFO]\tTHREAD - ORDERS STARTED")
while not stop_event.is_set():
cur_time = int((datetime.datetime.utcnow()- ep).total_seconds())
if data["RSI"][0] < RSI_BOTTOM \
and cur_time > last_operation_time+trading_data["time_period"]*CANDLES_BETWEEN_OPERATIONS: # OPEN BUY
last_operation_time = cur_time
open_position(trading_data["market"], trading_data["lotage"], mt5.ORDER_TYPE_BUY)
elif data["RSI"][0] > RSI_TOP \
and cur_time > last_operation_time+trading_data["time_period"]*CANDLES_BETWEEN_OPERATIONS: # OPEN SELL
last_operation_time = cur_time
open_position(trading_data["market"], trading_data["lotage"], mt5.ORDER_TYPE_SELL)