-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathStat_Euclidean_Metric.mq4
303 lines (266 loc) · 12.5 KB
/
Stat_Euclidean_Metric.mq4
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
//+------------------------------------------------------------------+
//| Stat_Euclidean_Metric.mq4 |
//| StatBars TO |
//| http://ridecrufter.narod.ru |
//+------------------------------------------------------------------+
#property copyright "StatBars TO"
#property link "http://ridecrufter.narod.ru"
#define v_dim_x 6 //Êîëè÷åñòâî âåêòîðîâ ó÷àâñòâóþùèõ â ðàñïîçíîâàíèè...
#define Num_neighbour 10 //Êîëè÷åñòâî áëèæàéøèõ ñîñåäåé ïî êîòîðûì è ïðèíèìàåòñÿ ðåøåíèå î ïðåíàäëåæíîñòè âåêòîðà ê 0 èëè 1
extern bool Base=false;
extern double buy_threshold=0.6;
extern double sell_threshold=0.6;
extern bool inverse_position_open_?=true;
extern double invers_buy_threshold=0.3;
extern double invers_sell_threshold=0.3;
extern int fast=12;
extern int slow=34;
extern int tp=40;
extern int sl=30;
extern bool close_orders=false;
double base_buy[][v_dim_x];
double base_sell[][v_dim_x];
int numbers_of_vectors_buy=0;
int numbers_of_vectors_sell=0;
int init()
{
if(!Base)
{
int Hadle_1=FileOpen("Buy_Position.dat",FILE_BIN|FILE_READ);
ArrayResize(base_buy,FileSize(Hadle_1)/(v_dim_x*8));
int count=0;
while(!FileIsEnding(Hadle_1))
{
base_buy[count][0]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
base_buy[count][1]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
base_buy[count][2]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
base_buy[count][3]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
base_buy[count][4]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
base_buy[count][5]=FileReadDouble(Hadle_1,DOUBLE_VALUE);
Print(base_sell[count][5]);
count++;
}
numbers_of_vectors_buy=count;
int Hadle_2=FileOpen("Sell_Position.dat",FILE_BIN|FILE_READ);
ArrayResize(base_sell,FileSize(Hadle_2)/(v_dim_x*8));
count=0;
while(!FileIsEnding(Hadle_2))
{
base_sell[count][0]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
base_sell[count][1]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
base_sell[count][2]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
base_sell[count][3]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
base_sell[count][4]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
base_sell[count][5]=FileReadDouble(Hadle_2,DOUBLE_VALUE);
Print(base_sell[count][5]);
count++;
}
numbers_of_vectors_sell=count;
}
FileClose(Hadle_1);
FileClose(Hadle_2);
return(0);
}
int deinit()
{
if(Base)
{
int Hadle_1=FileOpen("Buy_Position.dat",FILE_BIN|FILE_WRITE);
int count;
double ordinate_1,ordinate_2,ordinate_3,ordinate_4,ordinate_5;
for(int i=OrdersHistoryTotal()-1;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderType()==0)
{
if(OrderProfit()>=0)//Çäåñü çàïèñûâàåòñÿ áàçà âåêòîðîâ äëÿ ïîëîæèòåëüíî çàêðûâøèõñÿ îðäåðîâ
{
count=iBarShift(Symbol(),Period(),OrderOpenTime());
count++;//Ìû áóäåì ðàññìàòðèâàòü ïðåäûäóùèé áàð(ïåðåä îòêðûòèåì îðäåðà) ÷òîáû ïðîãíîçèðîâàòü óáûòî÷íîñòü/ïðèáûëüíîñòü ïîçèöèè
ordinate_1=iMA(Symbol(),Period(),89,0,0,5,count)/iMA(Symbol(),Period(),144,0,0,5,count);
ordinate_2=iMA(Symbol(),Period(),144,0,0,5,count)/iMA(Symbol(),Period(),233,0,0,5,count);
ordinate_3=iMA(Symbol(),Period(),21,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_4=iMA(Symbol(),Period(),55,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_5=iMA(Symbol(),Period(),2,0,0,5,count)/iMA(Symbol(),Period(),55,0,0,5,count);
FileWriteDouble(Hadle_1,ordinate_1,DOUBLE_VALUE);//Çàïèñûâàåì âåêòîð â áàçó...
FileWriteDouble(Hadle_1,ordinate_2,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_3,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_4,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_5,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,1,DOUBLE_VALUE);
}
if(OrderProfit()<0)//Çäåñü çàïèñûâàåòñÿ áàçà âåêòîðîâ äëÿ çàêðûâøèõñÿ îðäåðîâ ñ îòðèöàòåëüíûì ïðîôèòîì(ïðîùå ãîâîðÿ ñ ëîñîì)
{
count=iBarShift(Symbol(),Period(),OrderOpenTime());
count++;//Ìû áóäåì ðàññìàòðèâàòü ïðåäûäóùèé áàð(ïåðåä îòêðûòèåì îðäåðà) ÷òîáû ïðîãíîçèðîâàòü óáûòî÷íîñòü/ïðèáûëüíîñòü ïîçèöèè
ordinate_1=iMA(Symbol(),Period(),89,0,0,5,count)/iMA(Symbol(),Period(),144,0,0,5,count);
ordinate_2=iMA(Symbol(),Period(),144,0,0,5,count)/iMA(Symbol(),Period(),233,0,0,5,count);
ordinate_3=iMA(Symbol(),Period(),21,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_4=iMA(Symbol(),Period(),55,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_5=iMA(Symbol(),Period(),2,0,0,5,count)/iMA(Symbol(),Period(),55,0,0,5,count);
FileWriteDouble(Hadle_1,ordinate_1,DOUBLE_VALUE);//Çàïèñûâàåì âåêòîð â áàçó...
FileWriteDouble(Hadle_1,ordinate_2,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_3,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_4,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,ordinate_5,DOUBLE_VALUE);
FileWriteDouble(Hadle_1,0,DOUBLE_VALUE);//Îáðàòèòü âíèìàíèå ÷òî çäåñü âåêòîð ïîìå÷åí êàê 0, à âûøå êàê 1
}
}
}
int Hadle_2=FileOpen("Sell_Position.dat",FILE_BIN|FILE_WRITE);
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
if(OrderType()==1)//Òåïåðü çàïèñûâàåì áàçó äëÿ êîðîòêèõ ïîçèöèé
{
if(OrderProfit()>=0)//Çäåñü çàïèñûâàåòñÿ áàçà âåêòîðîâ äëÿ ïîëîæèòåëüíî çàêðûâøèõñÿ îðäåðîâ
{
count=iBarShift(Symbol(),Period(),OrderOpenTime());
count++;//Ìû áóäåì ðàññìàòðèâàòü ïðåäûäóùèé áàð(ïåðåä îòêðûòèåì îðäåðà) ÷òîáû ïðîãíîçèðîâàòü óáûòî÷íîñòü/ïðèáûëüíîñòü ïîçèöèè
ordinate_1=iMA(Symbol(),Period(),89,0,0,5,count)/iMA(Symbol(),Period(),144,0,0,5,count);
ordinate_2=iMA(Symbol(),Period(),144,0,0,5,count)/iMA(Symbol(),Period(),233,0,0,5,count);
ordinate_3=iMA(Symbol(),Period(),21,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_4=iMA(Symbol(),Period(),55,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_5=iMA(Symbol(),Period(),2,0,0,5,count)/iMA(Symbol(),Period(),55,0,0,5,count);
FileWriteDouble(Hadle_2,ordinate_1,DOUBLE_VALUE);//Çàïèñûâàåì âåêòîð â áàçó...
FileWriteDouble(Hadle_2,ordinate_2,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_3,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_4,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_5,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,1,DOUBLE_VALUE);
}
if(OrderProfit()<0)//Çäåñü çàïèñûâàåòñÿ áàçà âåêòîðîâ äëÿ çàêðûâøèõñÿ îðäåðîâ ñ îòðèöàòåëüíûì ïðîôèòîì(ïðîùå ãîâîðÿ ñ ëîñîì)
{
count=iBarShift(Symbol(),Period(),OrderOpenTime());
count++;//Ìû áóäåì ðàññìàòðèâàòü ïðåäûäóùèé áàð(ïåðåä îòêðûòèåì îðäåðà) ÷òîáû ïðîãíîçèðîâàòü óáûòî÷íîñòü/ïðèáûëüíîñòü ïîçèöèè
ordinate_1=iMA(Symbol(),Period(),89,0,0,5,count)/iMA(Symbol(),Period(),144,0,0,5,count);
ordinate_2=iMA(Symbol(),Period(),144,0,0,5,count)/iMA(Symbol(),Period(),233,0,0,5,count);
ordinate_3=iMA(Symbol(),Period(),21,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_4=iMA(Symbol(),Period(),55,0,0,5,count)/iMA(Symbol(),Period(),89,0,0,5,count);
ordinate_5=iMA(Symbol(),Period(),2,0,0,5,count)/iMA(Symbol(),Period(),55,0,0,5,count);
FileWriteDouble(Hadle_2,ordinate_1,DOUBLE_VALUE);//Çàïèñûâàåì âåêòîð â áàçó...
FileWriteDouble(Hadle_2,ordinate_2,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_3,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_4,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,ordinate_5,DOUBLE_VALUE);
FileWriteDouble(Hadle_2,0,DOUBLE_VALUE);//Îáðàòèòü âíèìàíèå ÷òî çäåñü âåêòîð ïîìå÷åí êàê 0, à âûøå êàê 1
}
}
}
}
return(0);
}
int start()
{
if(!isNewBar())return(0);
double MACD_1=iMACD(Symbol(),Period(),fast,slow,9,PRICE_TYPICAL,0,1);
double MACD_2=iMACD(Symbol(),Period(),fast,slow,9,PRICE_TYPICAL,0,2);
double MACD_3=iMACD(Symbol(),Period(),fast,slow,9,PRICE_TYPICAL,0,3);
double Prob_win;
double veñtor[5];
if(Base)
{
if(MACD_3<=MACD_2 && MACD_2>MACD_1)
{
if(close_orders)Close_Orders_by_type(OP_BUY);
OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Ask+sl*Point,Bid-tp*Point);
}
if(MACD_3>=MACD_2 && MACD_2<MACD_1)
{
if(close_orders)Close_Orders_by_type(OP_SELL);
OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-sl*Point,Ask+tp*Point);
}
}
if(!Base)
{
if(MACD_3<=MACD_2 && MACD_2>MACD_1)
{
if(close_orders)Close_All_Orders();
veñtor[0]=iMA(Symbol(),Period(),89,0,0,5,1)/iMA(Symbol(),Period(),144,0,0,5,1);
veñtor[1]=iMA(Symbol(),Period(),144,0,0,5,1)/iMA(Symbol(),Period(),233,0,0,5,1);
veñtor[2]=iMA(Symbol(),Period(),21,0,0,5,1)/iMA(Symbol(),Period(),89,0,0,5,1);
veñtor[3]=iMA(Symbol(),Period(),55,0,0,5,1)/iMA(Symbol(),Period(),89,0,0,5,1);
veñtor[4]=iMA(Symbol(),Period(),2,0,0,5,1)/iMA(Symbol(),Period(),55,0,0,5,1);
Prob_win=Euclidean_Metric(base_sell,veñtor,numbers_of_vectors_sell);
if(Prob_win>=sell_threshold)OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Ask+sl*Point,Bid-tp*Point);
if(inverse_position_open_? && Prob_win<=invers_sell_threshold)OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-sl*Point,Ask+tp*Point);
}
if(MACD_3>=MACD_2 && MACD_2<MACD_1)
{
if(close_orders)Close_All_Orders();
veñtor[0]=iMA(Symbol(),Period(),89,0,0,5,1)/iMA(Symbol(),Period(),144,0,0,5,1);
veñtor[1]=iMA(Symbol(),Period(),144,0,0,5,1)/iMA(Symbol(),Period(),233,0,0,5,1);
veñtor[2]=iMA(Symbol(),Period(),21,0,0,5,1)/iMA(Symbol(),Period(),89,0,0,5,1);
veñtor[3]=iMA(Symbol(),Period(),55,0,0,5,1)/iMA(Symbol(),Period(),89,0,0,5,1);
veñtor[4]=iMA(Symbol(),Period(),2,0,0,5,1)/iMA(Symbol(),Period(),55,0,0,5,1);
Prob_win=Euclidean_Metric(base_buy,veñtor,numbers_of_vectors_buy);
if(Prob_win>=buy_threshold)OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Bid-sl*Point,Ask+tp*Point);
if(inverse_position_open_? && Prob_win<=invers_buy_threshold)OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Ask+sl*Point,Bid-tp*Point);
}
}
return(0);
}
void Close_Orders_by_type(int type)
{
for(int num=OrdersTotal()-1;num>=0;num--)
{
OrderSelect(num,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==type)
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Gold);
}
}
void Close_All_Orders()
{
for(int num=OrdersTotal()-1;num>=0;num--)
{
OrderSelect(num,SELECT_BY_POS,MODE_TRADES);
if(OrderProfit()>0)
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Gold);
}
}
double Euclidean_Metric(double&X_Data_Base[][v_dim_x], double Vector[v_dim_x],int num_v)
{
int i=0,i1=0,i3,i2,i4;
double Metric[1];
double t,sum;
ArrayResize(Metric,num_v);
ArrayInitialize(Metric,0.0);
for(i=0;i<num_v;i++)
{
for(i1=0;i1<v_dim_x-1;i1++)Metric[i]+=MathPow(X_Data_Base[i][i1]-Vector[i1],2);
Metric[i]=MathSqrt(Metric[i]);
}
//sort by ASCEND Metric
for(i3=0;i3<num_v-1;i3++)
for(i2=i3+1;i2<num_v;i2++)
if(Metric[i3]>Metric[i2])
{
t=Metric[i3];
Metric[i3]=Metric[i2];
Metric[i2]=t;
for(i4=0;i4<v_dim_x;i4++)
{
t=X_Data_Base[i3][i4];
X_Data_Base[i3][i4]=X_Data_Base[i2][i4];
X_Data_Base[i2][i4]=t;
}
}
sum=0;
for(i=0;i<Num_neighbour;i++)
{
sum+=X_Data_Base[i][5];//ñóììèðóåì 0 è 1 áëèæàéøèõ ñîñåäåé ÷òîáû âûâåñòè èòîãîâóþ âåðîÿòíîñòü âûèãðûøà â ýòîé ñäåëêå
//Print(X_Data_Base[i][5]);
}
return(sum/Num_neighbour);
}
bool isNewBar()
{
static datetime BarTime;
bool res=false;
if (BarTime!=Time[0])
{
BarTime=Time[0];
res=true;
}
return(res);
}