forked from KonzACDC/MQL5_MT5_EA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElli.mq5
424 lines (409 loc) · 33.7 KB
/
Elli.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
//+------------------------------------------------------------------+
//| Elli(barabashkakvn's edition).mq5 |
//| Yuriy Tokman |
//| [email protected] |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman"
#property link "[email protected]"
#property version "1.001"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Expert\Money\MoneyFixedRisk.mqh>
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CMoneyFixedRisk m_money;
//---
input ushort TakeProfit = 60;
input ushort StopLoss = 30;
input double Risk = 5;
input ulong m_magic=5101979;
input int tenkan = 19;
input int kijun = 60;
input int senkou_b = 120;
input int po = 20;
input ENUM_TIMEFRAMES adx_timeframe=PERIOD_M1;
input int adx_period = 10; //6
input int convert_high = 13; //7-20
input int convert_low = 6; //1-7
//---
ulong m_slippage=30; // Проскальзывание цены
//---
bool UseSound = true; // Использовать звуковой сигнал
string NameFileSound = "expert.wav"; // Наименование звукового файла
//---
int handle_iADX; // variable for storing the handle of the iADX indicator
int handle_iIchimoku; // variable for storing the handle of the iIchimoku indicator
ENUM_ACCOUNT_MARGIN_MODE m_margin_mode;
double m_adjusted_point; // point value adjusted for 3 or 5 points
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
SetMarginMode();
if(!IsHedging())
{
Print("Hedging only!");
return(INIT_FAILED);
}
//---
m_symbol.Name(Symbol()); // sets symbol name
if(!RefreshRates())
{
Print("Error RefreshRates. Bid=",DoubleToString(m_symbol.Bid(),Digits()),
", Ask=",DoubleToString(m_symbol.Ask(),Digits()));
return(INIT_FAILED);
}
m_symbol.Refresh();
//---
m_trade.SetExpertMagicNumber(m_magic);
//---
m_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;
//---
if(!m_money.Init(GetPointer(m_symbol),Period(),m_symbol.Point()*digits_adjust))
return(INIT_FAILED);
m_money.Percent(Risk); // risk
//--- create handle of the indicator iADX
handle_iADX=iADX(m_symbol.Name(),adx_timeframe,adx_period);
//--- if the handle is not created
if(handle_iADX==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the iADX indicator for the symbol %s/%s, error code %d",
m_symbol.Name(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
return(INIT_FAILED);
}
//--- create handle of the indicator iIchimoku
handle_iIchimoku=iIchimoku(m_symbol.Name(),PERIOD_H1,tenkan,kijun,senkou_b);
//--- if the handle is not created
if(handle_iIchimoku==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the iIchimoku 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)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double sl=0.0,tp=0.0;
int total=0;
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open 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)
total++;
if(total<1)
{
int signal=Signal();
if(signal!=0)
{
if(!IsTradeAllowed())
{
Sleep(5000);
if(!IsTradeAllowed())
return;
}
}
if(signal==1)
{
if(!RefreshRates())
return;
if(StopLoss>0)
sl=m_symbol.Ask()-StopLoss*m_adjusted_point;
if(TakeProfit>0)
tp=m_symbol.Ask()+TakeProfit*m_adjusted_point;
OpenBuy(sl,tp);
}
else if(signal==-1)
{
if(!RefreshRates())
return;
if(StopLoss>0)
sl=m_symbol.Bid()+StopLoss*m_adjusted_point;
if(TakeProfit>0)
tp=m_symbol.Bid()-TakeProfit*m_adjusted_point;
OpenSell(sl,tp);
}
}
}
//+------------------------------------------------------------------+
//| Signal |
//+------------------------------------------------------------------+
int Signal()
{
double adx_plus_0 = iADXGet(PLUSDI_LINE,0);
double adx_plus_1 = iADXGet(PLUSDI_LINE,1);
//double adx_minus_0 = iADX(NULL,adx_timeframe,adx_period,0);
//double adx_minus_1 = iADX(NULL,adx_timeframe,adx_period,1);
double ts=iIchimokuGet(KIJUNSEN_LINE,0);
double ks = iIchimokuGet(SENKOUSPANA_LINE,0);
double sa = iIchimokuGet(SENKOUSPANB_LINE,0);
double sb = iIchimokuGet(CHIKOUSPAN_LINE,0);
double close=iClose(0,m_symbol.Name(),PERIOD_H1);
double cf=MathAbs(ts-ks)/m_symbol.Point();
int ytg_Signal=0;
if(ts>ks && ks>sa && sa>sb && close>ks && adx_plus_1<convert_low && adx_plus_0>convert_high && cf>po)
{
ytg_Signal=1;
RefreshRates();
}
else if(ts<ks && ks<sa && sa<sb && close<ks && adx_plus_1<convert_low && adx_plus_0>convert_high && cf>po)
{
//if(ts<ks && ks<sa && sa<sb && close<ks && adx_minus_1<convert_low && adx_minus_0>convert_high && cf>po)
ytg_Signal=-1;
RefreshRates();
}
return (ytg_Signal);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void SetMarginMode(void)
{
m_margin_mode=(ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsHedging(void)
{
return(m_margin_mode==ACCOUNT_MARGIN_MODE_RETAIL_HEDGING);
}
//+------------------------------------------------------------------+
//| Refreshes the symbol quotes data |
//+------------------------------------------------------------------+
bool RefreshRates()
{
//--- refresh rates
if(!m_symbol.RefreshRates())
return(false);
//--- protection against the return value of "zero"
if(m_symbol.Ask()==0 || m_symbol.Bid()==0)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Get value of buffers for the iADX |
//| the buffer numbers are the following: |
//| 0 - MAIN_LINE, 1 - PLUSDI_LINE, 2 - MINUSDI_LINE |
//+------------------------------------------------------------------+
double iADXGet(const int buffer,const int index)
{
double ADX[];
ArraySetAsSeries(ADX,true);
//--- reset error code
ResetLastError();
//--- fill a part of the iADXBuffer array with values from the indicator buffer that has 0 index
if(CopyBuffer(handle_iADX,buffer,0,index+1,ADX)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(0.0);
}
return(ADX[index]);
}
//+------------------------------------------------------------------+
//| Get value of buffers for the iIchimoku |
//| the buffer numbers are the following: |
//| 0 - TENKANSEN_LINE, 1 - KIJUNSEN_LINE, 2 - SENKOUSPANA_LINE, |
//| 3 - SENKOUSPANB_LINE, 4 - CHIKOUSPAN_LINE |
//+------------------------------------------------------------------+
double iIchimokuGet(const int buffer,const int index)
{
double Ichimoku[1];
//--- reset error code
ResetLastError();
//--- fill a part of the iIchimoku array with values from the indicator buffer that has 0 index
if(CopyBuffer(handle_iIchimoku,buffer,index,1,Ichimoku)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the iIchimoku indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(0.0);
}
return(Ichimoku[0]);
}
//+------------------------------------------------------------------+
//| Get Close for specified bar index |
//+------------------------------------------------------------------+
double iClose(const int index,string symbol=NULL,ENUM_TIMEFRAMES timeframe=PERIOD_CURRENT)
{
if(symbol==NULL)
symbol=Symbol();
if(timeframe==0)
timeframe=Period();
double Close[1];
double close=0;
int copied=CopyClose(symbol,timeframe,index,1,Close);
if(copied>0) close=Close[0];
return(close);
}
//+------------------------------------------------------------------+
//| Open Buy position |
//+------------------------------------------------------------------+
void OpenBuy(double sl,double tp)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
double check_open_long_lot=m_money.CheckOpenLong(m_symbol.Ask(),sl);
//Print("sl=",DoubleToString(sl,m_symbol.Digits()),
// ", CheckOpenLong: ",DoubleToString(check_open_long_lot,2),
// ", Balance: ", DoubleToString(m_account.Balance(),2),
// ", Equity: ", DoubleToString(m_account.Equity(),2),
// ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
if(check_open_long_lot==0.0)
return;
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_long_lot,m_symbol.Ask(),ORDER_TYPE_BUY);
if(chek_volime_lot!=0.0)
if(chek_volime_lot>=check_open_long_lot)
{
if(m_trade.Buy(check_open_long_lot,NULL,m_symbol.Ask(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
else
{
Print("Buy -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
if(UseSound)
PlaySound(NameFileSound);
}
}
else
{
Print("Buy -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
//---
}
//+------------------------------------------------------------------+
//| Open Sell position |
//+------------------------------------------------------------------+
void OpenSell(double sl,double tp)
{
sl=m_symbol.NormalizePrice(sl);
tp=m_symbol.NormalizePrice(tp);
double check_open_short_lot=m_money.CheckOpenShort(m_symbol.Bid(),sl);
//Print("sl=",DoubleToString(sl,m_symbol.Digits()),
// ", CheckOpenLong: ",DoubleToString(check_open_short_lot,2),
// ", Balance: ", DoubleToString(m_account.Balance(),2),
// ", Equity: ", DoubleToString(m_account.Equity(),2),
// ", FreeMargin: ", DoubleToString(m_account.FreeMargin(),2));
if(check_open_short_lot==0.0)
return;
//--- check volume before OrderSend to avoid "not enough money" error (CTrade)
double chek_volime_lot=m_trade.CheckVolume(m_symbol.Name(),check_open_short_lot,m_symbol.Bid(),ORDER_TYPE_SELL);
if(chek_volime_lot!=0.0)
if(chek_volime_lot>=check_open_short_lot)
{
if(m_trade.Sell(check_open_short_lot,NULL,m_symbol.Bid(),sl,tp))
{
if(m_trade.ResultDeal()==0)
{
Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
else
{
Print("Sell -> true. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
if(UseSound)
PlaySound(NameFileSound);
}
}
else
{
Print("Sell -> false. Result Retcode: ",m_trade.ResultRetcode(),
", description of result: ",m_trade.ResultRetcodeDescription());
}
}
//---
}
//+------------------------------------------------------------------+
//| Gets the information about permission to trade |
//+------------------------------------------------------------------+
bool IsTradeAllowed()
{
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
{
Alert("Check if automated trading is allowed in the terminal settings!");
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
{
Alert("Check if automated trading is allowed in the terminal settings!");
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
else
{
if(!MQLInfoInteger(MQL_TRADE_ALLOWED))
{
Alert("Automated trading is forbidden in the program settings for ",__FILE__);
return(false);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(!AccountInfoInteger(ACCOUNT_TRADE_EXPERT))
{
Alert("Automated trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
" at the trade server side");
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
if(!AccountInfoInteger(ACCOUNT_TRADE_ALLOWED))
{
Comment("Trading is forbidden for the account ",AccountInfoInteger(ACCOUNT_LOGIN),
".\n Perhaps an investor password has been used to connect to the trading account.",
"\n Check the terminal journal for the following entry:",
"\n\'",AccountInfoInteger(ACCOUNT_LOGIN),"\': trading has been disabled - investor mode.");
return(false);
}
//---
return(true);
}
//+------------------------------------------------------------------+