forked from KonzACDC/MQL5_MT5_EA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MTC Neural network plus MACD.mq5
343 lines (327 loc) · 23.7 KB
/
MTC Neural network plus MACD.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
//+------------------------------------------------------------------+
//| MTC Neural network plus MACD(barabashkakvn's edition).mq5 |
//| Copyright © 2008, Henadiy E. Batohov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Henadiy E. Batohov"
#property version "1.001"
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
CPositionInfo m_position; // trade position object
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CAccountInfo m_account; // account info wrapper
//--- input parameters for Neuro part
input int x11 = 100;
input int x12 = 100;
input int x13 = 100;
input int x14 = 100;
input double tp1 = 100;
input double sl1 = 50;
input int p1=10;
input int x21 = 100;
input int x22 = 100;
input int x23 = 100;
input int x24 = 100;
input double tp2 = 100;
input double sl2 = 50;
input int p2=10;
input int x31 = 100;
input int x32 = 100;
input int x33 = 100;
input int x34 = 100;
input int p3=10;
//--- input parameters
input int pass=3;
input double m_lots = 0.1;
input ulong m_magic=555;
static datetime prevtime=0;
static double take_profit=100;
static double stop_loss=50;
//---
int digits_adjust=0; // tuning for 3 or 5 digits
int handle_iMACD; // variable for storing the handle of the iMACD indicator
//+------------------------------------------------------------------+
//| 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();
//--- tuning for 3 or 5 digits
digits_adjust=1;
if(m_symbol.Digits()==3 || m_symbol.Digits()==5)
digits_adjust=10;
m_trade.SetExpertMagicNumber(m_magic); // sets magic number
//--- create handle of the indicator iMACD;
handle_iMACD=iMACD(Symbol(),Period(),12,26,9,PRICE_CLOSE);
//--- if the handle is not created
if(handle_iMACD==INVALID_HANDLE)
{
//--- tell about the failure and output the error code
PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped early
return(INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(iTime(m_symbol.Name(),Period(),0)==prevtime)
return;
prevtime=iTime(m_symbol.Name(),Period(),0);
if(!IsTradeAllowed())
{
again();
return;
}
for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
if(m_position.SelectByIndex(i))
if(m_position.Symbol()==Symbol() && m_position.Magic()==m_magic)
return;
int MACD = getMACD();
int perceptron = Supervisor();
if(MACD>0 && perceptron>0)
{
if(!RefreshRates())
return;
if(!m_trade.Buy(m_lots,Symbol(),m_symbol.Ask(),
m_symbol.Ask()-stop_loss*Point()*digits_adjust,
m_symbol.Ask()+take_profit*Point()*digits_adjust,MQLInfoString(MQL_PROGRAM_NAME)))
{
again();
}
}
if(MACD<0 && perceptron<0)
{
if(!RefreshRates())
return;
if(!m_trade.Sell(m_lots,Symbol(),m_symbol.Bid(),
m_symbol.Bid()+stop_loss*Point()*digits_adjust,
m_symbol.Bid()-take_profit*Point()*digits_adjust,MQLInfoString(MQL_PROGRAM_NAME)))
{
again();
}
}
return;
}
//+------------------------------------------------------------------+
//| calculate perciptrons value |
//+------------------------------------------------------------------+
int Supervisor()
{
if(pass>=3)
{
if(perceptron3()>0)
{
if(perceptron2()>0)
{
stop_loss=sl2;
take_profit=tp2;
return(1);
}
}
else
{
if(perceptron1()<0)
{
stop_loss=sl1;
take_profit=tp1;
return(-1);
}
}
return(0);
}
if(pass==2)
{
if(perceptron2()>0)
{
stop_loss=sl2;
take_profit=tp2;
return(1);
}
else
{
return(0);
}
}
if(pass==1)
{
if(perceptron1()<0)
{
stop_loss=sl1;
take_profit=tp1;
return(-1);
}
else
{
return(0);
}
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double perceptron1()
{
double w1 = x11 - 100;
double w2 = x12 - 100;
double w3 = x12 - 100;
double w4 = x12 - 100;
double a1 = iClose(m_symbol.Name(),Period(),0) - iOpen(m_symbol.Name(),Period(),p1);
double a2 = iOpen(m_symbol.Name(),Period(),p1) - iOpen(m_symbol.Name(),Period(),p1 * 2);
double a3 = iOpen(m_symbol.Name(),Period(),p1 * 2) - iOpen(m_symbol.Name(),Period(),p1 * 3);
double a4 = iOpen(m_symbol.Name(),Period(),p1 * 3) - iOpen(m_symbol.Name(),Period(),p1 * 4);
return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double perceptron2()
{
double w1 = x21 - 100;
double w2 = x22 - 100;
double w3 = x23 - 100;
double w4 = x24 - 100;
double a1 = iClose(m_symbol.Name(),Period(),0) - iOpen(m_symbol.Name(),Period(),p2);
double a2 = iOpen(m_symbol.Name(),Period(),p2) - iOpen(m_symbol.Name(),Period(),p2 * 2);
double a3 = iOpen(m_symbol.Name(),Period(),p2 * 2) - iOpen(m_symbol.Name(),Period(),p2 * 3);
double a4 = iOpen(m_symbol.Name(),Period(),p2 * 3) - iOpen(m_symbol.Name(),Period(),p2 * 4);
return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double perceptron3()
{
double w1 = x31 - 100;
double w2 = x32 - 100;
double w3 = x33 - 100;
double w4 = x34 - 100;
double a1 = iClose(m_symbol.Name(),Period(),0) - iOpen(m_symbol.Name(),Period(),p3);
double a2 = iOpen(m_symbol.Name(),Period(),p3) - iOpen(m_symbol.Name(),Period(),p3 * 2);
double a3 = iOpen(m_symbol.Name(),Period(),p3 * 2) - iOpen(m_symbol.Name(),Period(),p3 * 3);
double a4 = iOpen(m_symbol.Name(),Period(),p3 * 3) - iOpen(m_symbol.Name(),Period(),p3 * 4);
return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
}
//+------------------------------------------------------------------+
//| Calculate MACD value |
//+------------------------------------------------------------------+
int getMACD()
{
double MacdCurrent,MacdPrevious,SignalCurrent,SignalPrevious;
MacdCurrent=iMACDGet(MAIN_LINE,0);
MacdPrevious=iMACDGet(MAIN_LINE,2);
SignalCurrent=iMACDGet(SIGNAL_LINE,0);
SignalPrevious=iMACDGet(SIGNAL_LINE,2);
if(MacdCurrent<0 && MacdCurrent>=SignalCurrent && MacdPrevious<=SignalPrevious)
return(1);
if(MacdCurrent>0 && MacdCurrent<=SignalCurrent && MacdPrevious>=SignalPrevious)
return(-1);
return(0);
}
//+------------------------------------------------------------------+
//| pause and try to do expert again |
//+------------------------------------------------------------------+
void again()
{
prevtime=0;
Sleep(30000);
}
//+------------------------------------------------------------------+
//| Get value of buffers for the iMACD |
//| the buffer numbers are the following: |
//| 0 - MAIN_LINE, 1 - SIGNAL_LINE |
//+------------------------------------------------------------------+
double iMACDGet(const int buffer,const int index)
{
double MACD[1];
//--- reset error code
ResetLastError();
//--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index
if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0)
{
//--- if the copying fails, tell the error code
PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculated
return(0.0);
}
return(MACD[0]);
}
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+
//| 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);
}
//+------------------------------------------------------------------+