-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFXEnforcer_V1open.mq4
225 lines (213 loc) · 7.94 KB
/
FXEnforcer_V1open.mq4
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
/*
Generated by EX4-TO-MQ4 decompiler V4.0.224.1 []
Website: http://purebeam.biz
E-mail : [email protected]
*/
#property copyright "Copyright © 2009, Trading Automatics Ltd"
#property link "http://www.tradingautomatics.com"
extern int EMA1 = 70;
extern int EMA2 = 21;
extern bool EMA1aboveEMA2 = TRUE;
extern int AOToEnter = 7;
extern int AOToClose = 6;
extern int BETrigger = 150;
extern bool UseMoneyManagement = FALSE;
extern double Risk = 2.0;
extern double Lots = 0.1;
extern int SL = 70;
extern int TP = 80;
extern int MagicNumber = 68709243;
extern int Slippage = 3;
extern bool Broker5Digits = TRUE;
int gi_140 = 0;
bool gi_144 = FALSE;
int init() {
if (Broker5Digits) {
SL = 10 * SL;
TP = 10 * TP;
Slippage = 10 * Slippage;
BETrigger = 10 * BETrigger;
}
return (0);
}
int deinit() {
return (0);
}
int start() {
double l_ima_0;
double l_ima_8;
double l_iao_16;
double l_iao_24;
bool li_32 = TRUE;
bool li_36 = TRUE;
bool li_40 = TRUE;
bool li_44 = TRUE;
int l_ticket_52 = 0;
int li_56 = 0;
int li_60 = 0;
int li_64 = 0;
int l_ticket_68 = 0;
int l_cmd_72 = -1;
double l_price_76 = 0;
double ld_unused_84 = 0;
if (TimeCurrent() > StrToTime("2020.1.1")) {
Comment("expired");
return (0);
}
li_60 = GetLongsNumber();
li_64 = GetShortsNumber();
li_56 = li_60 + li_64;
for (int l_pos_92 = 0; l_pos_92 < OrdersTotal(); l_pos_92++) {
OrderSelect(l_pos_92, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
l_ticket_68 = OrderTicket();
l_cmd_72 = OrderType();
break;
}
}
if (gi_140 != Time[0]) {
if (li_56 > 0) {
for (int li_48 = 1; li_48 <= AOToClose; li_48++) {
l_iao_16 = iAO(Symbol(), 0, li_48);
l_iao_24 = iAO(Symbol(), 0, li_48 + 1);
if (l_iao_16 > l_iao_24) li_44 = FALSE;
if (l_iao_16 < l_iao_24) li_40 = FALSE;
}
if (li_60 > 0 && li_44) {
CloseAllLongs();
li_56 = 0;
li_60 = 0;
}
if (li_64 > 0 && li_40) {
CloseAllShorts();
li_56 = 0;
li_64 = 0;
}
}
for (li_48 = 1; li_48 <= AOToEnter; li_48++) {
l_iao_16 = iAO(Symbol(), 0, li_48);
l_iao_24 = iAO(Symbol(), 0, li_48 + 1);
if (l_iao_16 > l_iao_24) li_36 = FALSE;
if (l_iao_16 < l_iao_24) li_32 = FALSE;
}
l_ima_0 = iMA(Symbol(), 0, EMA1, 0, MODE_EMA, PRICE_CLOSE, 1);
l_ima_8 = iMA(Symbol(), 0, EMA2, 0, MODE_EMA, PRICE_CLOSE, 1);
if (li_32 && Close[1] > l_ima_0 && l_ima_0 > l_ima_8 || !EMA1aboveEMA2) {
if (li_64 > 0) CloseAllShorts();
if (li_60 == 0) {
if (SL > 0) l_price_76 = Ask - SL * Point;
l_ticket_52 = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, Slippage, 0, 0, "", MagicNumber, 0, Blue);
if (l_ticket_52 > 0) OrderModify(l_ticket_52, OrderOpenPrice(), l_price_76, 0, 0, CLR_NONE);
gi_144 = TRUE;
}
}
if (li_36 && Close[1] < l_ima_0 && l_ima_0 < l_ima_8 || !EMA1aboveEMA2) {
if (li_60 > 0) CloseAllLongs();
if (li_64 == 0) {
if (SL > 0) l_price_76 = Bid + SL * Point;
l_ticket_52 = OrderSend(Symbol(), OP_SELL, GetLots(), Bid, Slippage, 0, 0, "", MagicNumber, 0, Red);
if (l_ticket_52 > 0) OrderModify(l_ticket_52, OrderOpenPrice(), l_price_76, 0, 0, CLR_NONE);
}
}
if (li_56 == 0 && gi_144 && iLongReEntry() && li_32) {
if (SL > 0) l_price_76 = Ask - SL * Point;
l_ticket_52 = OrderSend(Symbol(), OP_BUY, GetLots(), Ask, Slippage, 0, 0, "", MagicNumber, 0, Aqua);
if (l_ticket_52 > 0) OrderModify(l_ticket_52, OrderOpenPrice(), l_price_76, 0, 0, CLR_NONE);
gi_144 = FALSE;
}
gi_140 = Time[0];
}
if (li_56 > 0) {
if (BETrigger > 0) HandleBreakEven();
if (OrderLots() > 3.0 * GetLots() / 4.0) {
if (l_cmd_72 == OP_BUY)
if (Bid >= OrderOpenPrice() + TP * Point) OrderClose(l_ticket_68, OrderLots() / 2.0, Bid, Slippage, Blue);
if (l_cmd_72 == OP_SELL)
if (Ask <= OrderOpenPrice() - TP * Point) OrderClose(l_ticket_68, OrderLots() / 2.0, Ask, Slippage, Red);
}
}
return (0);
}
int iLongReEntry() {
int l_highest_8;
int l_shift_0 = 0;
int l_shift_4 = 0;
for (int l_pos_12 = OrdersHistoryTotal() - 1; l_pos_12 >= 0; l_pos_12--) {
OrderSelect(l_pos_12, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
l_shift_0 = iBarShift(Symbol(), 0, OrderOpenTime());
l_shift_4 = iBarShift(Symbol(), 0, OrderCloseTime());
l_highest_8 = iHighest(Symbol(), 0, MODE_HIGH, l_shift_0 - l_shift_4, l_shift_4);
if (Bid > High[l_highest_8]) return (1);
return (0);
}
}
return (0);
}
int GetLongsNumber() {
int l_count_0 = 0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) l_count_0++;
}
return (l_count_0);
}
int GetShortsNumber() {
int l_count_0 = 0;
for (int l_pos_4 = 0; l_pos_4 < OrdersTotal(); l_pos_4++) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) l_count_0++;
}
return (l_count_0);
}
void CloseAllLongs() {
int l_ord_total_0 = OrdersTotal();
for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_BUY) {
RefreshRates();
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Blue);
}
}
}
void CloseAllShorts() {
int l_ord_total_0 = OrdersTotal();
for (int l_pos_4 = l_ord_total_0 - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == OP_SELL) {
RefreshRates();
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Red);
}
}
}
void HandleBreakEven() {
for (int l_pos_0 = 0; l_pos_0 < OrdersTotal(); l_pos_0++) {
OrderSelect(l_pos_0, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) {
if (OrderType() == OP_BUY) {
if (Bid >= OrderOpenPrice() + BETrigger * Point && OrderStopLoss() < OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, CLR_NONE);
} else {
if (OrderType() == OP_SELL)
if (Ask <= OrderOpenPrice() - BETrigger * Point && OrderStopLoss() > OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, CLR_NONE);
}
}
}
}
double GetLots() {
double ld_ret_0;
double ld_8;
double l_lotstep_32;
double l_marginrequired_40;
if (UseMoneyManagement) {
l_lotstep_32 = MarketInfo(Symbol(), MODE_LOTSTEP);
l_marginrequired_40 = MarketInfo(Symbol(), MODE_MARGINREQUIRED);
ld_ret_0 = AccountBalance() * Risk / 100.0 / l_marginrequired_40;
ld_8 = MathMod(ld_ret_0, l_lotstep_32);
if (ld_8 != 0.0) ld_ret_0 -= ld_8;
} else ld_ret_0 = Lots;
double l_minlot_16 = MarketInfo(Symbol(), MODE_MINLOT);
double l_maxlot_24 = MarketInfo(Symbol(), MODE_MAXLOT);
if (ld_ret_0 < l_minlot_16) ld_ret_0 = l_minlot_16;
if (ld_ret_0 > l_maxlot_24) ld_ret_0 = l_maxlot_24;
return (ld_ret_0);
}