-
Notifications
You must be signed in to change notification settings - Fork 0
/
trader.py
72 lines (63 loc) · 3.5 KB
/
trader.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
from df_parser import Parser
from metatrader import mt5
from config import SYMBOL_HEAD, SYMBOL_TAIL, DEVIATION
class Currency:
def __init__(self, pair, strategy, params, logger):
self.name = SYMBOL_HEAD + pair + SYMBOL_TAIL
self.params = params
self.df = Parser(pair=pair, strategy=strategy, start=0, end=1, trading=True, rsi_vwap_period=self.params[4]).m1
self.point = mt5.symbol_info(self.name).point
self.deviation = DEVIATION
self.logger = logger
self.open_positions = mt5.positions_get(symbol=self.name)
self.profits = []
self.losses = []
self.bought_idx = []
self.sold_idx = []
self.closed_buy_idx = []
self.closed_sell_idx = []
self.pips = 0
self.profit_trades = 0
self.loss_trades = 0
self.profit_longs = 0
self.loss_longs = 0
self.profit_shorts = 0
self.loss_shorts = 0
self.total_trades = 0
self.index = self.df.index
def m15_rsi_vwap(self, account, index, lot_size, equity):
# if len(self.open_positions) > 0:
# for i in range(len(self.open_positions)):
# if self.open_positions[i].type == 0:
# if self.rsi_vwap[index - 1] > self.params[6]:
# position_id = self.open_positions[i].ticket
# price = mt5.symbol_info_tick(self.name).bid
# lot_size = self.open_positions[i].volume
# account.close_order(
# direction="long", strategy="rsi_vwap", symbol=self.name, position_id=position_id, price=price,
# lot_size=lot_size, deviation=self.deviation, currency_logger=self.logger
# )
# elif self.open_positions[i].type == 1:
# if self.rsi_vwap[index - 1] < self.params[7]:
# position_id = self.open_positions[i].ticket
# price = mt5.symbol_info_tick(self.name).ask
# lot_size = self.open_positions[i].volume
# account.close_order(
# direction="short", strategy="rsi_vwap", symbol=self.name, position_id=position_id, price=price,
# lot_size=lot_size, deviation=self.deviation, currency_logger=self.logger
# )
if round(self.df.rsi_vwap[index-2], 2) <= self.params[2] < round(self.df.rsi_vwap[index-1], 2):
price = mt5.symbol_info_tick(self.name).ask
margin = self.check_margin(mt5.ORDER_TYPE_BUY, price, lot_size)
if margin <= equity:
account.place_order(direction="long", symbol=self.name, price=price, deviation=self.deviation,
currency_logger=self.logger, tp=self.params[0], sl=self.params[1], point=self.point)
if round(self.df.rsi_vwap[index-2], 2) >= self.params[3] > round(self.df.rsi_vwap[index-1], 2):
price = mt5.symbol_info_tick(self.name).bid
margin = self.check_margin(mt5.ORDER_TYPE_SELL, price, lot_size)
if margin <= equity:
account.place_order(direction="short", symbol=self.name, price=price, deviation=self.deviation,
currency_logger=self.logger, tp=self.params[0], sl=self.params[1], point=self.point)
def check_margin(self, action, price, lot_size):
margin = mt5.order_calc_margin(action, self.name, lot_size, price)
return margin