-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZoneRecovery.mq4
281 lines (245 loc) · 9.55 KB
/
ZoneRecovery.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//+------------------------------------------------------------------+
//| ZoneRecovery.mq4 |
//| Inu Bayu Aji |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
/**
# Error
- close all order juga tidak berfungsi karena ada spread
# Event untuk backtest
* 6 januari 2021 posisi awal sell (10)
* 8 januari 2021 posisi awal buy (6)
* 12 januari 2021 posisi awal sell (6)
# Pengembangan kedepan
- cara menghitung compund atau inisial lot saat modal sudah bertamba
- midle price berdasarkan inisial order, jika order kedelapan agar nilai tengah tidak lebih dari spread
*/
#property copyright "Inu Bayu Aji"
#property link "https://github.com/inubayuaji/expert-advisor"
#property version "2.11"
#property strict
//--- input paramater
input int recoveryZoneGap = 200; // Recovery Gap in Point
input int recoveryZoneExit = 400; // Recovery Exit in Point
input double initialLotSize = 0.01; // Initial Lot Size
//--- global variable
bool inTrade = false;
int nextOrderType;
int zoneRecoveryCount = 0;
double buyLotTotal = 0;
double sellLotTotal = 0;
double spread;
double entryPrice;
double upperRecoveryZone;
double lowerRecoveryZone;
double middleRecoveryZone;
double upperRecoveryZoneProfit;
double lowerRecoveryZoneProfit;
double riskRewardRatio;
double devider;
double multiplier;
//+------------------------------------------------------------------+
int OnInit() {
devider = MathPow(10, Digits);
riskRewardRatio = recoveryZoneExit / recoveryZoneGap;
multiplier = (riskRewardRatio + 1) / riskRewardRatio;
DrawOBjects();
if(initialLotSize > MarketInfo(Symbol(), MODE_MAXLOT) || initialLotSize < MarketInfo(Symbol(), MODE_MINLOT)) {
return INIT_PARAMETERS_INCORRECT;
}
return INIT_SUCCEEDED;
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
DeleteObjects();
}
//+------------------------------------------------------------------+
void OnTick() {
if(IsTesting()) {
TestingEventListener();
}
if(inTrade) {
// make next recovery zone
if(nextOrderType == OP_BUY && zoneRecoveryCount < 8) {
if(Bid >= upperRecoveryZone) {
CreateNextRecoveryOrder(nextOrderType);
}
}
if(nextOrderType == OP_SELL && zoneRecoveryCount < 8) {
if(Bid <= lowerRecoveryZone) {
CreateNextRecoveryOrder(nextOrderType);
}
}
// exit recovery zone profit
if(Bid <= lowerRecoveryZoneProfit) {
CloseAllOrders();
}
if(Bid >= upperRecoveryZoneProfit) {
CloseAllOrders();
}
// exit zone recovery max order
if(nextOrderType == OP_BUY && zoneRecoveryCount == 8) {
if(Bid <= middleRecoveryZone) {
CloseAllOrders();
}
}
if(nextOrderType == OP_SELL && zoneRecoveryCount == 8) {
if(Bid >= middleRecoveryZone) {
CloseAllOrders();
}
}
}
}
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long& lparam,
const double& dparam,
const string& sparam) {
if(id == CHARTEVENT_OBJECT_CLICK) {
if(sparam == "btn_buy") {
CreateFirstOrder(OP_BUY);
Sleep(150);
ObjectSetInteger(0, "btn_buy", OBJPROP_STATE, 0);
}
if(sparam == "btn_sell") {
CreateFirstOrder(OP_SELL);
Sleep(150);
ObjectSetInteger(0, "btn_sell", OBJPROP_STATE, 0);
}
}
}
//+------------------------------------------------------------------+
void TestingEventListener() {
if(ObjectGetInteger(0, "btn_buy", OBJPROP_STATE)) {
CreateFirstOrder(OP_BUY);
ObjectSetInteger(0, "btn_buy", OBJPROP_STATE, 0);
}
if(ObjectGetInteger(0, "btn_sell", OBJPROP_STATE)) {
CreateFirstOrder(OP_SELL);
ObjectSetInteger(0, "btn_sell", OBJPROP_STATE, 0);
}
}
//+------------------------------------------------------------------+
void DrawOBjects() {
ObjectCreate(0, "btn_buy", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0, "btn_buy", OBJPROP_CORNER, CORNER_LEFT_LOWER);
ObjectSetInteger(0, "btn_buy", OBJPROP_XDISTANCE, 10);
ObjectSetInteger(0, "btn_buy", OBJPROP_YDISTANCE, 25);
ObjectSetString(0, "btn_buy", OBJPROP_TEXT, "Buy");
ObjectCreate(0, "btn_sell", OBJ_BUTTON, 0, 0, 0);
ObjectSetInteger(0, "btn_sell", OBJPROP_CORNER, CORNER_LEFT_LOWER);
ObjectSetInteger(0, "btn_sell", OBJPROP_XDISTANCE, 70);
ObjectSetInteger(0, "btn_sell", OBJPROP_YDISTANCE, 25);
ObjectSetString(0, "btn_sell", OBJPROP_TEXT, "Sell");
}
//+------------------------------------------------------------------+
void DrawLine(double price, string name, int _color) {
if(ObjectCreate(0, name, OBJ_HLINE, 0, 0, price)) {
ObjectSetInteger(0, name, OBJPROP_COLOR, _color);
ObjectSetInteger(0 ,name, OBJPROP_STYLE, STYLE_DASHDOTDOT);
ObjectSetInteger(0 ,name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0 ,name, OBJPROP_SELECTED, false);
}
}
//+------------------------------------------------------------------+
void DrawZoneRecoveryLine(int orderType) {
if(orderType == OP_BUY) {
DrawLine(entryPrice, "upper_zone_recovery_entry", clrLimeGreen);
} else {
DrawLine(upperRecoveryZone + spread, "upper_zone_recovery_entry", clrLimeGreen);
}
DrawLine(lowerRecoveryZone, "lower_zone_recovery_entry", clrLimeGreen);
DrawLine(upperRecoveryZoneProfit, "upper_zone_recovery_exit", clrRed);
DrawLine(lowerRecoveryZoneProfit, "lower_zone_recovery_exit", clrRed);
}
//+------------------------------------------------------------------+
void RemoveZoneRecoveryLine() {
ObjectDelete(0, "upper_zone_recovery_entry");
ObjectDelete(0, "lower_zone_recovery_entry");
ObjectDelete(0, "upper_zone_recovery_exit");
ObjectDelete(0, "lower_zone_recovery_exit");
}
//+------------------------------------------------------------------+
void DeleteObjects() {
ObjectDelete(0, "btn_buy");
ObjectDelete(0, "btn_sell");
}
//+------------------------------------------------------------------+
void CreateFirstOrder(int orderType) {
if(!inTrade && orderType == OP_BUY) {
entryPrice = Ask;
spread = Ask - Bid;
if(OrderSend(Symbol(), OP_BUY, initialLotSize, entryPrice, 5, 0, 0) != -1) {
inTrade = true;
buyLotTotal += initialLotSize;
zoneRecoveryCount++;
upperRecoveryZone = entryPrice - spread;
lowerRecoveryZone = entryPrice - (recoveryZoneGap / devider);
middleRecoveryZone = (entryPrice + lowerRecoveryZone) / 2;
upperRecoveryZoneProfit = entryPrice + (recoveryZoneExit / devider) + spread;
lowerRecoveryZoneProfit = lowerRecoveryZone - (recoveryZoneExit / devider) - spread;
nextOrderType = OP_SELL;
}
}
if(!inTrade && orderType == OP_SELL) {
entryPrice = Bid;
spread = Ask - Bid;
if(OrderSend(Symbol(), OP_SELL, initialLotSize, entryPrice, 5, 0, 0) != -1) {
inTrade = true;
sellLotTotal += initialLotSize;
zoneRecoveryCount++;
upperRecoveryZone = entryPrice - spread + (recoveryZoneGap / devider);
lowerRecoveryZone = entryPrice;
middleRecoveryZone = (upperRecoveryZone + spread + lowerRecoveryZone) / 2;
upperRecoveryZoneProfit = upperRecoveryZone + spread + (recoveryZoneExit / devider) + spread;
lowerRecoveryZoneProfit = lowerRecoveryZone - (recoveryZoneExit / devider) - spread;
nextOrderType = OP_BUY;
}
}
DrawZoneRecoveryLine(orderType);
}
//+------------------------------------------------------------------+
void CreateNextRecoveryOrder(int orderType) {
double lotSize = 0;
if(orderType == OP_BUY) {
lotSize = NormalizeDouble(((multiplier * sellLotTotal) - buyLotTotal) * 1.1, 2); // masih salah disii
buyLotTotal += lotSize;
if(OrderSend(Symbol(), orderType, lotSize, Ask, 5, 0, 0) != -1) {
zoneRecoveryCount++;
nextOrderType = OP_SELL;
}
}
if(orderType == OP_SELL) {
lotSize = NormalizeDouble(((multiplier * buyLotTotal) - sellLotTotal) * 1.1, 2); // masih salah disii
sellLotTotal += lotSize;
if(OrderSend(Symbol(), orderType, lotSize, Bid, 5, 0, 0) != -1) {
zoneRecoveryCount++;
nextOrderType = OP_BUY;
}
}
}
//+------------------------------------------------------------------+
void CloseAllOrders() {
int isSuccess;
for(int i = OrdersTotal() - 1; i >= 0; i--) {
if(OrderSelect(i, SELECT_BY_POS)) {
if(OrderSymbol() == Symbol()) {
if(OrderType() == OP_BUY) {
isSuccess = OrderClose(OrderTicket(), OrderLots(), Bid, 5);
}
if(OrderType() == OP_SELL) {
isSuccess = OrderClose(OrderTicket(), OrderLots(), Ask, 5);
}
if(OrderType() == OP_BUYSTOP || OrderType() == OP_SELLSTOP) {
isSuccess = OrderDelete(OrderTicket());
}
}
}
}
RemoveZoneRecoveryLine();
zoneRecoveryCount = 0;
buyLotTotal = 0;
sellLotTotal = 0;
inTrade = false;
}
//+------------------------------------------------------------------+