-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoolinger Band MrTan.mq5
426 lines (375 loc) · 16.7 KB
/
Boolinger Band MrTan.mq5
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//+------------------------------------------------------------------+
//| Boolinger Band MrTan.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>
CPositionInfo m_position; // trade position object
CTrade trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CAccountInfo m_account; // account info wrapper
CMoneyFixedMargin *m_money;
//--- input parameters
input double Lot_size =0.1 ;
input double distance =50 ; // Distance from highestigh or loweslow to start trade
input double TP=4000; // Take profit
input double SL=2000; // Stop loss
input ushort InpTrailingStop = 60; // Trailing Stop (in pips)
input ushort InpTrailingStep = 5; // Trailing Step (in pips)
input int InpMaxPositions = 5; // Maximum positions
input ulong m_magic=47978073; // magic number
int input EXPERT_MAGIC = 1234567;
input ENUM_TIMEFRAMES Trading_timframe=PERIOD_H1;
// Input Indicator declaration
input int period_Band =20; // bollingerBand period
input int Shift_band =1; // Shift bar of band
input double Deviation_band =1; // Deviation band
input int period_MA_volume =26 ; // Period of moving average volume tick
input int Shift_volume =1; // Shift bar of Volume tick
// Global Variable
//SL-TP management
double m_adjusted_point; // point value adjusted for 3 or 5 points
ulong m_slippage=10; // slippage
double ExtDistance=0.0;
double ExtStopLoss=0.0;
double ExtTakeProfit=0.0;
double ExtTrailingStop=0.0;
double ExtTrailingStep=0.0;
double ExtSpreadLimit=0.0;
// Indicator Declaration
int handel_Bollingerband;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
if(!m_symbol.Name(Symbol())) // sets symbol name
return(INIT_FAILED);
RefreshRates();
//---
trade.SetExpertMagicNumber(m_magic);
trade.SetMarginMode();
trade.SetTypeFillingBySymbol(m_symbol.Name());
trade.SetDeviationInPoints(m_slippage);
//--- tuning for 3 or 5 digits
int digits_adjust=1;
if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
digits_adjust=10;
m_adjusted_point=m_symbol.Point()*digits_adjust;
ExtStopLoss = SL * m_adjusted_point;
ExtTakeProfit = TP * m_adjusted_point;
ExtTrailingStop= InpTrailingStop * m_adjusted_point;
ExtTrailingStep= InpTrailingStep * m_adjusted_point;
ExtDistance = distance*m_adjusted_point; double profit=0;
//--- create handle of the indicator Bollinger band
handel_Bollingerband=iBands(Symbol(),Trading_timframe,period_Band,Shift_band,Deviation_band,PRICE_CLOSE);
//--- if the handle is not created
if(handel_Bollingerband==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the Bollinger band indicator for the symbol %s/%s, error code %d",
m_symbol.Name(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
return(INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
if(m_money!=NULL)
delete m_money;
ChartRedraw();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Declaration the Candle
double high[]; ArraySetAsSeries(high,true);CopyHigh(Symbol(),Trading_timframe,0,1000,high);
double low[]; ArraySetAsSeries(low,true);CopyLow(Symbol(),Trading_timframe,0,1000,low);
double open[]; ArraySetAsSeries(open,true);CopyOpen(Symbol(),Trading_timframe,0,1000,open);
double close[]; ArraySetAsSeries(close,true);CopyClose(Symbol(),Trading_timframe,0,1000,close);
// Declaration Array for Bollinger band
double Uperband[]; ArraySetAsSeries(Uperband,true);CopyBuffer(handel_Bollingerband,UPPER_BAND,0,1000,Uperband);// Array for Uperband
double Lowerband[]; ArraySetAsSeries(Lowerband,true);CopyBuffer(handel_Bollingerband,LOWER_BAND,0,1000,Lowerband);// Array for lowerband
double Midleband[]; ArraySetAsSeries(Midleband,true);CopyBuffer(handel_Bollingerband,BASE_LINE,0,1000,Midleband);// Array for midel band
// Declaration the volume tick
double MA_volume[]; ArraySetAsSeries(MA_volume,true); Get_MA_volume(0,0,MA_volume,1000);
double volume[];ArraySetAsSeries(volume,true); Get_ivolume(0,0,volume,1000);
// declaration count positions
int count_buy=0; int count_sell=0;double profit=0;
CalculatePositions(count_buy,count_sell,profit);
// Display discription volume check
string discription=" ";
// Declaration parameter befor send to the broker
double Ask= SymbolInfoDouble(Symbol(),SYMBOL_ASK);
double Bid= SymbolInfoDouble(Symbol(),SYMBOL_BID);
double Stop_level=(int)SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL);
// condition check volume
// Execution main Trade
// Only trade at new bar
if(BarOpen(Symbol(),Trading_timframe))
{
CalculatePositions(count_buy,count_sell,profit);
// Trailing stop
Trailing();
{
// Looking for to go long if there is no long position
if(count_buy==0 )
{
if( low[2]<Midleband[2] && close[1]>Midleband[1] && close[1]>open[1] && volume[1]>MA_volume[1] )
{
double entryprice= Ask;
double sl=entryprice-ExtStopLoss;
double tp =entryprice + ExtTakeProfit;
//if(CheckVolumeValue(Symbol(),Lot_size,discription,entryprice,ORDER_TYPE_BUY))
//trade.Buy(Lot_size,Symbol(),entryprice,sl,tp);
// you enter code buy or sell
}
}
else if(count_sell==0 )
{
if( high[2]>Midleband[2]&&close[1]<Midleband[1] && close[1]<open[1] && volume[1]>MA_volume[1] )
{
double entryprice= Bid;
double sl=entryprice+ExtStopLoss;
double tp =entryprice - ExtTakeProfit;
//if(CheckVolumeValue(Symbol(),Lot_size,discription,entryprice,ORDER_TYPE_SELL))
//trade.Sell(Lot_size,Symbol(),entryprice,sl,tp);
// you enter code buy or sell
}
}
}
}
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates(void)
{
//--- refresh rates
if(!m_symbol.RefreshRates())
{
Print("RefreshRates error");
return(false);
}
//--- protection against the return value of "zero"
if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Calculate positions Buy and Sell |
//+------------------------------------------------------------------+
void CalculatePositions(int &count_buys,int &count_sells,double &profit)
{
count_buys=0;
count_sells=0;
profit=0.0;
for(int i=PositionsTotal()-1;i>=0;i--)
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() //&& m_position.Magic()==m_magic
)
{
profit+=m_position.Commission()+m_position.Swap()+m_position.Profit();
if(m_position.PositionType()==POSITION_TYPE_BUY)
count_buys++;
if(m_position.PositionType()==POSITION_TYPE_SELL)
count_sells++;
}
}
//+------------------------------------------------------------------+
//| close all positions |
//+------------------------------------------------------------------+
void ClosePositions(const ENUM_POSITION_TYPE pos_type)
{
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions
if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties
if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
if(m_position.PositionType()==pos_type) // gets the position type
trade.PositionClose(m_position.Ticket()); // close a position by the specified symbol
}
//+------------------------------------------------------------------+
//| get highest value for range |
//+------------------------------------------------------------------+
double Highest(const double&array[],int range,int fromIndex)
{
double res=0;
//---
res=array[fromIndex];
for(int i=fromIndex;i<fromIndex+range;i++)
{
if(res<array[i]) res=array[i];
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| get lowest value for range |
//+------------------------------------------------------------------+
double Lowest(const double&array[],int range,int fromIndex)
{
double res=0;
//---
res=array[fromIndex];
for(int i=fromIndex;i<fromIndex+range;i++)
{
if(res>array[i]) res=array[i];
}
//---
return(res);
}
//+------------------------------------------------------------------+
// Get value of buffers for the volume
double Get_ivolume( int buffer, int index, double &value[],int count)
{
int handel_Volume=iVolumes(Symbol(),Trading_timframe,VOLUME_TICK);
//--- reset error code
ResetLastError();
//--- fill a part of the iIchimoku array with values from the indicator buffer that has 0 index
if(CopyBuffer(handel_Volume,buffer,index,count,value)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the volume indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
//return(0.0);
}
return(value[0]);
}
//+------------------------------------------------------------------+
// Get value of buffers for the Bollinger band
double Get_MA_volume( int buffer, int index, double &value[],int count)
{
//--- reset error code
ResetLastError();
//--- create handle of the indicator volume
int handel_Volume=iVolumes(Symbol(),Trading_timframe,VOLUME_TICK);
int hande_MA_volume= iMA(Symbol(),Trading_timframe,period_MA_volume,Shift_volume,MODE_EMA,handel_Volume);
//--- fill a part of the iIchimoku array with values from the indicator buffer that has 0 index
if(CopyBuffer(hande_MA_volume,buffer,index,count,value)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the Moving Arverage indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
//return(0.0);
}
return(value[0]);
}
//+------------------------------------------------------------------+
//| Get current server time function |
//+------------------------------------------------------------------+
datetime m_prev_bar;
bool BarOpen(string symbol,ENUM_TIMEFRAMES timeframe)
{
datetime bar_time = iTime(symbol, timeframe, 0);
if (bar_time == m_prev_bar)
{
return false;
}
m_prev_bar = bar_time;
return true;
}
//+------------------------------------------------------------------+
//| Trailing |
//+------------------------------------------------------------------+
void Trailing()
{
if(InpTrailingStop==0)
return;
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
if(m_position.SelectByIndex(i))
if(m_position.Symbol()==m_symbol.Name() //&& m_position.Magic()==m_magic
)
{
if(m_position.PositionType()==POSITION_TYPE_BUY)
{
if(m_position.PriceCurrent()-m_position.PriceOpen()>ExtTrailingStop+ExtTrailingStep)
if(m_position.StopLoss()<m_position.PriceCurrent()-(ExtTrailingStop+ExtTrailingStep))
{
if(!trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()-ExtTrailingStop),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket(),
" Position -> false. Result Retcode: ",trade.ResultRetcode(),
", description of result: ",trade.ResultRetcodeDescription());
RefreshRates();
m_position.SelectByIndex(i);
continue;
}
}
else
{
if(m_position.PriceOpen()-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep)
if((m_position.StopLoss()>(m_position.PriceCurrent()+(ExtTrailingStop+ExtTrailingStep))) ||
(m_position.StopLoss()==0))
{
if(!trade.PositionModify(m_position.Ticket(),
m_symbol.NormalizePrice(m_position.PriceCurrent()+ExtTrailingStop),
m_position.TakeProfit()))
Print("Modify ",m_position.Ticket(),
" Position -> false. Result Retcode: ",trade.ResultRetcode(),
", description of result: ",trade.ResultRetcodeDescription());
RefreshRates();
m_position.SelectByIndex(i);
}
}
}
}
//+------------------------------------------------------------------+
//| Check the correctness of the order volume |
//+------------------------------------------------------------------+
bool CheckVolumeValue(string symbol,double volume,string &description,double price ,ENUM_ORDER_TYPE type)
{
//--- minimal allowed volume for trade operations
double margin = 0;
double min_volume=SymbolInfoDouble(symbol,SYMBOL_VOLUME_MIN);
if(volume<min_volume)
{
description=StringFormat("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(symbol,SYMBOL_VOLUME_MAX);
if(volume>max_volume)
{
description=StringFormat("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(symbol,SYMBOL_VOLUME_STEP);
int ratio=(int)MathRound(volume/volume_step);
if(MathAbs(ratio*volume_step-volume)>0.0000001)
{
description=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);
}
if(OrderCalcMargin(type,Symbol(),volume,price,margin))
{
double free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
if(free_margin<0 )
{
return false;
}
}
description="Correct volume value";
return(true);
}