-
Notifications
You must be signed in to change notification settings - Fork 2
/
Area51_Lib.mqh
374 lines (346 loc) · 13.5 KB
/
Area51_Lib.mqh
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//+------------------------------------------------------------------+
//| Area51_Lib.mqh |
//| Copyright 2018, VBApps |
//| https://vbapps.co |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, VBApps"
#property link "https://vbapps.co"
#property strict
#define SLIPPAGE 5
#define NO_ERROR 1
#define AT_LEAST_ONE_FAILED 2
//+------------------------------------------------------------------+
bool CheckMoneyForTrade(string symb,double lots,int type)
{
double free_margin=AccountFreeMarginCheck(symb,type,lots);
//-- wenn es Geldmittel nicht ausreichend sind
if(free_margin<0)
{
string oper=(type==OP_BUY)? "Buy":"Sell";
Print("Not enough money for ",oper," ",lots," ",symb," Error code=",GetLastError());
return(false);
}
//-- die Überprüfung ist erfolgreich gelaufen
return(true);
}
//+------------------------------------------------------------------+
bool CompareDoubles(double number1,double number2)
{
if(NormalizeDouble(number1-number2,5)==0) return(true);
else return(false);
}
//+------------------------------------------------------------------+
bool CheckStopLoss_Takeprofit(string symbolName,ENUM_ORDER_TYPE type,double SLT,double TPT)
{
return CheckStopLoss_Takeprofit_End(0, 0.0, symbolName,type,SLT,TPT);
}
bool CheckStopLoss_Takeprofit(int orderTicket,double orderOpenPrice,string symbolName,ENUM_ORDER_TYPE type,double SLT,double TPT)
{
return CheckStopLoss_Takeprofit_End(orderTicket,orderOpenPrice,symbolName,type,SLT,TPT);
}
bool CheckStopLoss_Takeprofit_End(int orderTicket,double orderOpenPrice,string symbolName,ENUM_ORDER_TYPE type,double SLT,double TPT)
{
int stops_level=(int)SymbolInfoInteger(symbolName,SYMBOL_TRADE_STOPS_LEVEL);
if(stops_level!=0)
{
PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must be"+
" less %d points from close price",stops_level,stops_level);
}
bool SLT_check=false,TPT_check=false;
switch(type)
{
case ORDER_TYPE_BUY:
{
SLT_check=(MarketInfo(symbolName,MODE_BID)-SLT>stops_level*MarketInfo(symbolName,MODE_POINT));
if(!SLT_check)
PrintFormat("For order nr. %i and OpenPrice=%.5f %s StopLoss=%.5f must be less than %.5f"+
" (Bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",
orderTicket,orderOpenPrice,EnumToString(type),SLT,MarketInfo(symbolName,MODE_BID)-stops_level*MarketInfo(symbolName,MODE_POINT),MarketInfo(symbolName,MODE_BID),stops_level);
TPT_check=(TPT-MarketInfo(symbolName,MODE_BID)>stops_level*MarketInfo(symbolName,MODE_POINT));
if(!TPT_check)
PrintFormat("For order nr. %i and OpenPrice=%.5f %s TakeProfit=%.5f must be greater than %.5f"+
" (Bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",
orderTicket,orderOpenPrice,EnumToString(type),TPT,MarketInfo(symbolName,MODE_BID)+stops_level*MarketInfo(symbolName,MODE_POINT),MarketInfo(symbolName,MODE_BID),stops_level);
return(SLT_check&&TPT_check);
}
case ORDER_TYPE_SELL:
{
SLT_check=(SLT-MarketInfo(symbolName,MODE_ASK)>stops_level*MarketInfo(symbolName,MODE_POINT));
if(!SLT_check)
PrintFormat("For order nr. %i and OpenPrice=%.5f %s StopLoss=%.5f must be greater than %.5f "+
" (Ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",
orderTicket,orderOpenPrice,EnumToString(type),SLT,MarketInfo(symbolName,MODE_ASK)+stops_level*MarketInfo(symbolName,MODE_POINT),MarketInfo(symbolName,MODE_ASK),stops_level);
TPT_check=(MarketInfo(symbolName,MODE_ASK)-TPT>stops_level*MarketInfo(symbolName,MODE_POINT));
if(!TPT_check)
PrintFormat("For order nr. %i and OpenPrice=%.5f %s TakeProfit=%.5f must be less than %.5f "+
" (Ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",
orderTicket,orderOpenPrice,EnumToString(type),TPT,MarketInfo(symbolName,MODE_ASK)-stops_level*MarketInfo(symbolName,MODE_POINT),MarketInfo(symbolName,MODE_ASK),stops_level);
return(TPT_check&&SLT_check);
}
break;
}
return false;
}
//+------------------------------------------------------------------+
//| Check the correctness of the order volume |
//+------------------------------------------------------------------+
bool CheckVolumeValue(string symbolName,double volume)
{
//--- minimal allowed volume for trade operations
string description="";
double min_volume=SymbolInfoDouble(symbolName,SYMBOL_VOLUME_MIN);
if(volume<min_volume)
{
PrintFormat("Volume is less than the minimal allowed SYMBOL_VOLUME_MIN=%.2f",min_volume);
return(false);
}
//--- maximal allowed volume of trade operations
double max_volume=SymbolInfoDouble(symbolName,SYMBOL_VOLUME_MAX);
if(volume>max_volume)
{
PrintFormat("Volume is greater than the maximal allowed SYMBOL_VOLUME_MAX=%.2f",max_volume);
return(false);
}
//--- get minimal step of volume changing
double volume_step=SymbolInfoDouble(symbolName,SYMBOL_VOLUME_STEP);
int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
{
Print(StringFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
volume_step,ratio*volume_step));
return(false);
}
return(true);
}
//+-----------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar;
datetime curbar=Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return(true);
}
else
{
return(false);
}
}
//+------------------------------------------------------------------+
string getTimeframeFromMinutes(int ai_0)
{
string timeFrame;
switch(ai_0)
{
case 1:
timeFrame="M1";
break;
case 5:
timeFrame="M5";
break;
case 15:
timeFrame="M15";
break;
case 30:
timeFrame="M30";
break;
case 60:
timeFrame="H1";
break;
case 240:
timeFrame="H4";
break;
case 1440:
timeFrame="D1";
break;
case 10080:
timeFrame="W1";
break;
case 43200:
timeFrame="MN";
}
return (timeFrame);
}
//+------------------------------------------------------------------+
int getTimeframeFromString(string timeFrameFromString)
{
int timeFrame=0;
if(timeFrameFromString=="M1"){timeFrame=1;}
if(timeFrameFromString=="M5"){timeFrame=5;}
if(timeFrameFromString=="M15"){timeFrame=15;}
if(timeFrameFromString=="M30"){timeFrame=30;}
if(timeFrameFromString=="H1"){timeFrame=60;}
if(timeFrameFromString=="H4"){timeFrame=240;}
if(timeFrameFromString=="D1"){timeFrame=1440;}
if(timeFrameFromString=="W1"){timeFrame=10080;}
if(timeFrameFromString=="MN"){timeFrame=43200;}
return (timeFrame);
}
//+------------------------------------------------------------------+
//| Überprüft - ob noch eine Order gesetzt werden kann |
//+------------------------------------------------------------------+
bool IsNewOrderAllowed()
{
//--- Bekommen die Anzahl der erlaubten Pending Orders am Konto
int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);
//--- wenn es keine Beschränkungen gibt - geben true zurück, man kann auch Order absenden
if(max_allowed_orders==0) return(true);
//--- wenn es bis zu dieser Stelle angekommen ist, bedeutet dies, dass eine Beschränkung gibt, wie viel Order schon gelten
int orders=OrdersTotal();
//--- geben wir das Ergebnis des Vergleiches zurück
return(orders<max_allowed_orders);
}
//+------------------------------------------------------------------+
int CloseAll()
{
bool rv=NO_ERROR;
int numOfOrders=OrdersTotal();
int FirstOrderType=0;
for(int index=0; index<OrdersTotal(); index++)
{
bool oS=OrderSelect(index,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
FirstOrderType=OrderType();
break;
}
}
for(int index=numOfOrders-1; index>=0; index--)
{
bool oS=OrderSelect(index,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
switch(OrderType())
{
case OP_BUY:
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),SLIPPAGE,Blue))
rv=AT_LEAST_ONE_FAILED;
break;
case OP_SELL:
if(!OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),SLIPPAGE,Red))
rv=AT_LEAST_ONE_FAILED;
break;
case OP_BUYLIMIT:
case OP_SELLLIMIT:
case OP_BUYSTOP:
case OP_SELLSTOP:
if(!OrderDelete(OrderTicket()))
rv=AT_LEAST_ONE_FAILED;
break;
}
}
return(rv);
}
//+------------------------------------------------------------------+
int getTicketCurrentType(int TicketNr)
{
int result=-1;
if(OrderSelect(TicketNr,SELECT_BY_TICKET,MODE_TRADES))
{
result=OrderType();
}
return result;
}
//+------------------------------------------------------------------+
bool CurrentCandleHasNoOpenedTrades(string symbolName)
{
bool positionCanBeOpened=false;
int currentAlreadyOpenedPositions=0;
if(OrdersTotal()>0)
{
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
{
if((OrderType()==OP_SELL || OrderType()==OP_BUY) && OrderSymbol()==symbolName && (OrderMagicNumber()==MagicNumber))
{
if(Period()==PERIOD_M1 || Period()==PERIOD_M5 || Period()==PERIOD_M15 || Period()==PERIOD_M30)
{
if(TimeMinute(Time[0])==TimeMinute(OrderOpenTime()))
{
positionCanBeOpened=false;
}else{
positionCanBeOpened=true;
}
}
if(Period()==PERIOD_H1 || Period()==PERIOD_H4)
{
if(TimeHour(Time[0])==TimeHour(OrderOpenTime()))
{
positionCanBeOpened=false;
}else{
positionCanBeOpened=true;
}
}
if(Period()==PERIOD_D1 || Period()==PERIOD_W1)
{
if(TimeDay(Time[0])==TimeDay(OrderOpenTime()))
{
positionCanBeOpened=false;
}else{
positionCanBeOpened=true;
}
}
if(Period()==PERIOD_MN1)
{
if(TimeMinute(Time[0])==TimeMinute(OrderOpenTime()))
{
positionCanBeOpened=false;
}else{
positionCanBeOpened=true;
}
}
}
}
}
} else {
positionCanBeOpened=true;
}
return positionCanBeOpened;
}
//+------------------------------------------------------------------+
//Function to return the index (position) of viValue within a given array
int fniGetElementIndex(int &vaiArray[],int viValue)
{
int viElementCount = ArrayRange(vaiArray, 0); //Get the total number of elements within that array.
int viFoundAt = -1; //The element index where viValue is found within the array.
//Loop through every element in vaiArray
for(int viElement=0; viElement<viElementCount; viElement++)
{
if(vaiArray[viElement]==viValue)
{
//If the element value is the same as viValue, then FOUND, break the for loop.
viFoundAt=viElement;
break;
}
}
return( viFoundAt );
}
//+------------------------------------------------------------------+
double getTempLoss() {
double TempLoss=0;
for(int j=0;j<OrdersTotal();j++)
{
if(OrderSelect(j,SELECT_BY_POS,MODE_TRADES))
{
if(TradeOnAllSymbols && OrderMagicNumber()==MagicNumber)
{
TempLoss=TempLoss+OrderProfit();
}
else if(!TradeOnAllSymbols && OrderSymbol()==Symbol() && (OrderMagicNumber()==MagicNumber))
{
TempLoss=TempLoss+OrderProfit();
}
}
}
return TempLoss;
}
//+------------------------------------------------------------------+
bool tradingAllowed() {
bool TradingAllowed=false;
if((StartHour>-1 && EndHour<24) && (((Hour()>StartHour) || (Hour()==StartHour)) && (Hour()<EndHour || Hour()==EndHour)))
{
TradingAllowed=true;
}
return TradingAllowed;
}