-
Notifications
You must be signed in to change notification settings - Fork 12
/
artificialintelligence.mq5
287 lines (244 loc) · 12 KB
/
artificialintelligence.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
//+------------------------------------------------------------------+
//| ArtificialIntelligence.mq5 |
//| Copyright 2012, Integer |
//| https://login.mql5.com/ru/users/Integer |
//+------------------------------------------------------------------+
#property copyright "Integer"
#property link "https://login.mql5.com/ru/users/Integer"
#property description ""
#property version "1.00"
#property description "Expert rewritten from MQ4, the author is Reshetov (http://www.mql4.com/ru/users/Reshetov), link to original - http://codebase.mql4.com/ru/756"
#include <Trade/Trade.mqh>
#include <Trade/SymbolInfo.mqh>
#include <Trade/DealInfo.mqh>
#include <Trade/PositionInfo.mqh>
CTrade Trade;
CDealInfo Deal;
CSymbolInfo Sym;
CPositionInfo Pos;
/*
The author: Reshetov, http://www.mql4.com/ru/users/Reshetov
The original: http://codebase.mql4.com/ru/756
Brief description: Expert based on the AC indicator and linear perceptron.
How it works:
AC indicator is used, 4 values of the bars are taken with him: Shift, Shift+7, Shift+14, Shift+21. Each values of the indicator are multiplied by the weight which obtained as (100-x(n)), where x(n) - is optimized variable x1, x2, x2, x3. After multiplying the values are added and we get value at the output of the perceptron. When the perceptron output value is greater than 0 - open the buying , when the value is less than 0 - selling.
Position is opened with stoploss (stoploss required) and without takeprofit. When reaching the profit position which more at spread than stoplos values, and we have the opposite signal, then performed the coup of position by opening the positionthe with big volume If we don't have a opposite signal, then stoploss is permuted at the same level as it had been at the opening position (by calculations we have profit by the amount of spread).
If we can not opened a opposite position (because the stoploss value is near), then position will be closed, and on the next tick will be another attempt to open a position..
Stoploss is required because, closing of position or moving the stoploss is performed only when the position is in profit, we also need a way out in case of loss.
*/
//--- input parameters
input double Lots = 0.1; /*Lots*/ // Lot
input int StopLoss = 850; /*StopLoss*/ // Stoploss in points.
input int Shift = 1; /*Shift*/ // Bar on which indicators are checked: 0 - shaped bar, 1 - the first shaped bar
input int x1 = 135; /*x1*/ // Weights of perceptron from 0 to 200
input int x2 = 127;
input int x3 = 16;
input int x4 = 93;
int Handle=INVALID_HANDLE;
double a1[1],a2[1],a3[1],a4[1];
datetime ctm[1];
datetime LastTime;
double lot,slv,msl,mtp;
double perc;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit(){
// Loading indicators...
Handle=iAC(_Symbol,PERIOD_CURRENT);
if(Handle==INVALID_HANDLE){
Alert("Failed to loading the indicator, try again");
return(-1);
}
if(!Sym.Name(_Symbol)){
Alert("Failed to initialize CSymbolInfo, try again");
return(-1);
}
Print("Expert initialization was completed");
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason){
if(Handle!=INVALID_HANDLE)IndicatorRelease(Handle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick(){
if(CopyTime(_Symbol,PERIOD_CURRENT,0,1,ctm)==-1){
return;
}
if(Shift==0 || ctm[0]!=LastTime){
// Indicators
if(!Indicators()){
return;
}
// Signals
bool OpenBuy=SignalOpenBuy();
bool OpenSell=SignalOpenSell();
if(OpenBuy || OpenSell){
if(!Sym.RefreshRates())return;
if(Pos.Select(_Symbol)){
switch(Pos.PositionType()){
case POSITION_TYPE_BUY:
if(Sym.Bid()>(Pos.StopLoss()+(StopLoss*2+Sym.Spread())*Sym.Point())){
if(OpenSell){
slv=SolveSellSL(StopLoss);
if(CheckSellSL(slv)){
lot=NormalizeDouble(Pos.Volume()+Lots,2);
Trade.SetDeviationInPoints(Sym.Spread()*3);
if(!Trade.Sell(lot,_Symbol,0,slv,0,"")){
return;
}
}
else{
Print("Can not do the buy-sell rotation of position, running close");
Trade.PositionClose(_Symbol,Sym.Spread()*3);
return;
}
}
else{
slv=SolveBuySL(StopLoss);
if(CheckBuySL(slv)){
Trade.PositionModify(_Symbol,slv,0);
}
}
}
break;
case POSITION_TYPE_SELL:
if(Sym.Ask()<(Pos.StopLoss()-(StopLoss*2+Sym.Spread())*Sym.Point())){
if(OpenBuy){
slv=SolveBuySL(StopLoss);
if(CheckBuySL(slv)){
lot=NormalizeDouble(Pos.Volume()+Lots,2);
Trade.SetDeviationInPoints(Sym.Spread()*3);
if(!Trade.Buy(Lots,_Symbol,0,slv,0,"")){
return;
}
}
else{
Print("Can not do the buy-sell rotation of position, running close");
Trade.PositionClose(_Symbol,Sym.Spread()*3);
return;
}
}
else{
slv=SolveSellSL(StopLoss);
if(CheckSellSL(slv)){
Trade.PositionModify(_Symbol,slv,0);
}
}
}
break;
}
}
else{
if(OpenBuy && !OpenSell){
slv=SolveBuySL(StopLoss);
if(CheckBuySL(slv)){
Trade.SetDeviationInPoints(Sym.Spread()*3);
if(!Trade.Buy(Lots,_Symbol,0,slv,0,"")){
return;
}
}
else{
Print("Buy position does not open, stoploss or takeprofit is near");
}
}
// Sell
if(OpenSell && !OpenBuy){
slv=SolveSellSL(StopLoss);
if(CheckSellSL(slv)){
Trade.SetDeviationInPoints(Sym.Spread()*3);
if(!Trade.Sell(Lots,_Symbol,0,slv,0,"")){
return;
}
}
else{
Print("Sell position does not open, stoploss or takeprofit is near");
}
}
}
}
LastTime=ctm[0];
}
}
//+------------------------------------------------------------------+
//| The PERCEPTRON - a perceiving and recognizing function |
//+------------------------------------------------------------------+
double perceptron(){
double w1 = x1 - 100;
double w2 = x2 - 100;
double w3 = x3 - 100;
double w4 = x4 - 100;
return(w1*a1[0]+w2*a2[0]+w3*a3[0]+w4*a4[0]);
}
//+------------------------------------------------------------------+
//| Function of data copy for indicators and price |
//+------------------------------------------------------------------+
bool Indicators(){
if(
CopyBuffer(Handle,0,Shift,1,a1)==-1 ||
CopyBuffer(Handle,0,Shift+7,1,a2)==-1 ||
CopyBuffer(Handle,0,Shift+14,1,a3)==-1 ||
CopyBuffer(Handle,0,Shift+21,1,a4)==-1
){
return(false);
}
perc=perceptron();
return(true);
}
//+------------------------------------------------------------------+
//| Function for determining buy signals |
//+------------------------------------------------------------------+
bool SignalOpenBuy(){
return(perc>0);
}
//+------------------------------------------------------------------+
//| Function for determining sell signals |
//+------------------------------------------------------------------+
bool SignalOpenSell(){
return(perc<0);
}
//+------------------------------------------------------------------+
//| Function for calculation the buy stoploss |
//+------------------------------------------------------------------+
double SolveBuySL(int StopLossPoints){
if(StopLossPoints==0)return(0);
return(Sym.NormalizePrice(Sym.Ask()-Sym.Point()*StopLossPoints));
}
//+------------------------------------------------------------------+
//| Function for calculation the sell stoploss |
//+------------------------------------------------------------------+
double SolveSellSL(int StopLossPoints){
if(StopLossPoints==0)return(0);
return(Sym.NormalizePrice(Sym.Bid()+Sym.Point()*StopLossPoints));
}
//+------------------------------------------------------------------+
//| Function for calculation the minimum stoploss of buy |
//+------------------------------------------------------------------+
double BuyMSL(){
return(Sym.NormalizePrice(Sym.Bid()-Sym.Point()*Sym.StopsLevel()));
}
//+------------------------------------------------------------------+
//| Function for calculation the minimum stoploss of sell |
//+------------------------------------------------------------------+
double SellMSL(){
return(Sym.NormalizePrice(Sym.Ask()+Sym.Point()*Sym.StopsLevel()));
}
//+------------------------------------------------------------------+
//| Function for checking the buy stoploss |
//+------------------------------------------------------------------+
bool CheckBuySL(double StopLossPrice){
if(StopLossPrice==0)return(true);
return(StopLossPrice<BuyMSL());
}
//+------------------------------------------------------------------+
//| Function for checking the sell stoploss |
//+------------------------------------------------------------------+
bool CheckSellSL(double StopLossPrice){
if(StopLossPrice==0)return(true);
return(StopLossPrice>SellMSL());
}