This repository has been archived by the owner on Mar 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10-3-2019.mq5
655 lines (632 loc) · 19.4 KB
/
10-3-2019.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//+------------------------------------------------------------------+
//| |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "Soroush.trb"
#property link ""
#property version "1.01"
#include<Trade\Trade.mqh>
#include <LibCisNewBar.mqh>
#include <Generic\ArrayList.mqh>
CisNewBar current_chart;
CisNewBar current_chart2;
//+---------------------
enum signal {buy,sell,none,closeBuy,closeSell};
//--- EA inputs
input string EAinputs="EA inputs"; // EA inputs
input double order_volume=0.01; // Lot size
input int POSITIONS=1;
//--- Trading timespan
input string Tradingtimespan="Trading timespan"; // Trading timespan
input char time_h_start=1; // Trading start time
input char time_h_stop=23; // Trading stop time
input bool mon=true; // Work on Monday
input bool tue=true; // Work on Tuesday
input bool wen=true; // Work on Wednesday
input bool thu=true; // Work on Thursday
input bool fri=true; // Work on Friday
input string InpFileName="optimize.csv"; // optimize file name
double cvolume=0;
//--- Variable
MqlDateTime time_now_str;
datetime time_now_var;
CTrade trade;
CTrade trade2;
signal OpenSignal;
bool work_day=true;
double InitBalance;
double OPZ[51];
int TradePerDay=0;
MqlDateTime lastOptimize;
CArrayList<ulong>oList;
int CIN=14;
int SL=100,TP=100;
//+---------------------------------------------+
int OnInit()
{
if(false==ReadFileToArrayCSV(InpFileName,OPZ))
{
for(int i=0;i<50;i++)
{
OPZ[i]++;
}
}
TimeToStruct(TimeCurrent(),lastOptimize);
Optimize();
CINSelect();
cvolume=order_volume;
trade.SetExpertMagicNumber(939393);
trade2.SetExpertMagicNumber(46);
InitBalance=AccountInfoDouble(ACCOUNT_BALANCE);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
bool ReadFileToArrayCSV(string FileName,double &Lines[])
{
ResetLastError();
int h=FileOpen(FileName,FILE_READ|FILE_CSV,";");
if(h==INVALID_HANDLE)
{
int ErrNum=GetLastError();
printf("Error opening file %s # %i",FileName,ErrNum);
return(false);
}
while(!FileIsEnding(h))
{
int key=(int)FileReadString(h);
while(!FileIsLineEnding(h))
{
double value=(double)FileReadString(h);
Lines[key]=value;
}
}
FileClose(h);
return(true);
}
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
int file_handle=FileOpen(InpFileName,FILE_WRITE|FILE_CSV,";");
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for writing",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
for(int i=0;i<ArraySize(OPZ);i++) FileWrite(file_handle,i,OPZ[i]);
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
}
//+------------------------------------------------------------------+
int minDiff(MqlDateTime &a,MqlDateTime &b)
{
int yrDiff = MathAbs(a.year-b.year);
int mnDiff = MathAbs(a.mon-b.mon);
int dyDiff = MathAbs(a.day-b.day);
int hrDiff = MathAbs(a.hour-b.hour);
return yrDiff*8760;
}
//+------------------------------------------------------------------+
void Optimize()
{
MqlDateTime currentOptimize;
TimeToStruct(TimeCurrent(),currentOptimize);
if(TradePerDay==0) return;
if(currentOptimize.hour!=0) return;
/*if(currentOptimize.day_of_year>lastOptimize.day_of_year+1) return;*/
HistorySelect(0,TimeCurrent());
if(HistoryDealsTotal()<50) return;
double sdprofit=0;
for(int i=HistoryDealsTotal()-1; i>=0; i--)
{
ulong dticket=HistoryDealGetTicket(i);
if(oList.Contains(dticket)) continue;
else oList.Add(dticket);
long dmagic=HistoryDealGetInteger(dticket,DEAL_MAGIC);
double dprofit=HistoryDealGetDouble(dticket,DEAL_PROFIT);
ENUM_DEAL_ENTRY dentry=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(dticket,DEAL_ENTRY);
datetime dtime=(datetime)HistoryDealGetInteger(dticket,DEAL_TIME);
ENUM_DEAL_TYPE dtype=(ENUM_DEAL_TYPE)HistoryDealGetInteger(dticket,DEAL_TYPE);
if(dmagic==939393)
{
sdprofit+=dprofit;
}
}
double ProfitPerTrade=sdprofit/TradePerDay;
double ProfitPerHour=sdprofit/24;
TradePerDay=0;
OPZ[CIN]+=(ProfitPerTrade+ProfitPerHour)/2;
CINSelect();
TimeToStruct(TimeCurrent(),lastOptimize);
}
//+------------------------------------------------------------------+
void CINSelect()
{
double rc=Rand(0,50);
double cr=0;
for(int i=0;i<50;i++)
{
if(rc<OPZ[i]+cr)
{
CIN=i+1;
break;
}
cr+=OPZ[i];
if(i==49)
{
rc=Rand(0,50);
i=1;
}
}
}
//+------------------------------------------------------------------+
void CloseAllBuyPositions()
{
for(int i=PositionsTotal()-1;i>=0;i--)
{
ulong ticket=PositionGetTicket(i);
if(ticket>0)
{
trade.PositionClose(i);
}
}
}
//+------------------------------------------------------------------+
signal DayTrade()
{
static double MACDMN;
static double MACDMX;
int BBoi=iBands(_Symbol,PERIOD_D1,6,0,2.0,PRICE_CLOSE);//86
int RSIi=iRSI(_Symbol,PERIOD_D1,3,PRICE_CLOSE);//21
int OBVi=iOBV(_Symbol,PERIOD_D1,VOLUME_TICK);
int MACD=iMACD(_Symbol,PERIOD_D1,9,6,3,PRICE_CLOSE);
int Stoc=iStochastic(_Symbol,PERIOD_D1,5,3,3,MODE_EMA,STO_LOWHIGH);
double BBoiV[];
double RSIiV[];
double OBViV[];
double MACDV[];
double StocV[];
double StocS[];
ArraySetAsSeries(BBoiV,true);
CopyBuffer(BBoi,0,0,3,BBoiV);
ArraySetAsSeries(RSIiV,true);
CopyBuffer(RSIi,0,0,3,RSIiV);
ArraySetAsSeries(OBViV,true);
CopyBuffer(OBVi,0,0,3,OBViV);
ArraySetAsSeries(MACDV,true);
CopyBuffer(MACD,0,0,3,MACDV);
ArraySetAsSeries(StocV,true);
CopyBuffer(Stoc,0,0,3,StocV);
ArraySetAsSeries(StocS,true);
CopyBuffer(Stoc,1,0,3,StocS);
double H=iHigh(_Symbol,PERIOD_D1,0),
L=iLow(_Symbol,PERIOD_D1,0),
C=iClose(_Symbol,PERIOD_D1,0),
O=iOpen(_Symbol,PERIOD_D1,0);
MACDMX=MathMax(MACDMX,MACDV[0]);
MACDMN=MathMin(MACDMN,MACDV[0]);
bool BBvO = BBoiV[0]>O;
bool BBvC = BBoiV[0]<C;
bool BBvL = BBoiV[0]>L;
bool BBvH = BBoiV[0]<H;
bool FGBB = BBvO && BBvL;
bool SGBB = BBvC && BBvH;
if(FGBB && SGBB)
{
bool RSIvSL=RSIiV[0]>RSIiV[1];
if(RSIiV[1]>75 && RSIvSL)
{
bool OBVvOBV=OBViV[0]>OBViV[1];
if(OBViV[1]>OBViV[2] && OBVvOBV)
{
bool MACDvMACD=MACDV[0]<MACDV[1];
if(MACDvMACD && MACDV[1]<MACDMX)
{
bool FGSTOC = StocV[0] > 70;
bool SGSTOC = (StocV[1] > 50) && (StocV[0] > 50);
bool AGSTOC = FGSTOC && SGSTOC;
if(StocV[0]>StocV[1] && AGSTOC)
{
return buy;
}
}
}
}
}
BBvO = BBoiV[0]<O;
BBvC = BBoiV[0]>C;
BBvL = BBoiV[0]<L;
BBvH = BBoiV[0]>H;
FGBB = BBvO && BBvL;
SGBB = BBvC && BBvH;
if(FGBB && SGBB)
{
bool RSIvSL=RSIiV[0]<RSIiV[1];
if(RSIiV[1]<25 && RSIvSL)
{
bool OBVvOBV=OBViV[0]<OBViV[1];
if(OBViV[1]<OBViV[2] && OBVvOBV)
{
bool MACDvMACD=MACDV[0]<MACDV[1];
if(MACDvMACD && MACDV[1]>MACDMN)
{
bool FGSTOC = StocV[0] > 30;
bool SGSTOC = (StocV[1] < 50) && (StocV[0] < 50);
bool AGSTOC = FGSTOC && SGSTOC;
if(StocV[0]>StocV[1] && AGSTOC)
{
return buy;
}
}
}
}
}
return none;
}
//+------------------------------------------------------------------+
void OnTick()
{
bool NC=false;
bool NCH1=false;
int period_seconds=PeriodSeconds(_Period);
int period_secondsH1=PeriodSeconds(PERIOD_D1);
datetime new_time=TimeCurrent()/period_seconds*period_seconds;
datetime new_time2=TimeCurrent()/period_secondsH1*period_secondsH1;
if(current_chart.isNewBar(new_time)) NC=true;
if(current_chart2.isNewBar(new_time2)) NCH1=true;
double Balance= AccountInfoDouble(ACCOUNT_BALANCE);
double Equity = AccountInfoDouble(ACCOUNT_EQUITY);
int pos=PositionsTotal();
Optimize();
double price_ask=SymbolInfoDouble(_Symbol,SYMBOL_ASK);
double price_bid=SymbolInfoDouble(_Symbol,SYMBOL_BID);
signal CurrentSignal=none;
signal BigCurrentSignal=none;
signal HA=none;
if(NCH1)
{
BigCurrentSignal=DayTrade();
}
if(NC)
{
CurrentSignal=ManualDem(CIN);
signal HA1=HeikenAshi(PERIOD_M1);
signal HA2=HeikenAshi(PERIOD_M10);
signal HA3=HeikenAshi(PERIOD_M20);
signal HA4=HeikenAshi(PERIOD_M10);
signal HA5=HeikenAshi(PERIOD_M1);
int b=1,s=1;
if(HA1==buy) b++;
else if(HA1==sell) s++;
if(HA2==buy) b++;
else if(HA2==sell) s++;
if(HA3==buy) b++;
else if(HA3==sell) s++;
if(HA4==buy) b++;
else if(HA4==sell) s++;
if(HA5==buy) b++;
else if(HA5==sell) s++;
if(b>s) HA=buy;
else if(b<s) HA=sell;
}
//---
time_now_var=TimeCurrent(time_now_str);
bool work=false;
switch(time_now_str.day_of_week)
{
case 1: if(mon==false){work_day=false;}
else {work_day=true;}
break;
case 2: if(tue==false){work_day=false;}
else {work_day=true;}
break;
case 3: if(wen==false){work_day=false;}
else {work_day=true;}
break;
case 4: if(thu==false){work_day=false;}
else {work_day=true;}
break;
case 5: if(fri==false){work_day=false;}
else {work_day=true;}
break;
}
Comment("\nDem: ",CIN,"\nVolume: ",cvolume,"\n Signal big: ",EnumToString(BigCurrentSignal),"\n Signal small: ",EnumToString(CurrentSignal));
if(time_h_start>time_h_stop)
{
if(time_now_str.hour>=time_h_start || time_now_str.hour<=time_h_stop)
{
work=true;
}
}
else
{
if(time_now_str.hour>=time_h_start && time_now_str.hour<=time_h_stop)
{
work=true;
}
}
if(NC && pos>0)
{
TrailingStop(price_ask,price_bid);
}
if(NCH1 && pos>0)
{
TrailingStopH1(price_ask,price_bid);
}
if(CurrentSignal!=none && pos<POSITIONS)
{
if(Equity>=InitBalance*1.5) CloseAllBuyPositions();
if(Balance>InitBalance) cvolume+=order_volume;
else cvolume=order_volume;
}
if(work==true && work_day==true)
{
if(CurrentSignal==buy && HA==buy)
{
if(pos<POSITIONS)
{
if(trade.Buy(cvolume,_Symbol,price_ask,price_ask-SL*_Point,price_ask+TP*_Point,""))
{
OpenSignal=buy;
TradePerDay++;
}
}
}
else if(CurrentSignal==sell && HA==sell)
{
if(pos<POSITIONS)
{
if(trade.Sell(cvolume,_Symbol,price_bid,price_bid+SL*_Point,price_bid-TP*_Point,""))
{
OpenSignal=sell;
TradePerDay++;
}
}
}
if(BigCurrentSignal==buy)
{
if(pos<POSITIONS+1)
{
if(trade2.Buy(cvolume,_Symbol,price_ask,price_ask-(SL*500)*_Point,price_ask+(TP*500)*_Point,"AR"))
{
OpenSignal=buy;
}
}
}
else if(BigCurrentSignal==sell)
{
if(pos<POSITIONS+1)
{
if(trade2.Sell(cvolume,_Symbol,price_bid,price_bid+(SL*500)*_Point,price_bid-(TP*500)*_Point,"AR"))
{
OpenSignal=sell;
}
}
}
}
if(InitBalance>=Equity)
{
SL = MathMax(SL/2,50);
TP = MathMax(TP/2,50);
}
else
{
SL = (int)MathMin(SL*1.2,1000);
TP = (int)MathMin(TP*1.2,1000);
}
}
//+------------------------------------------------------------------+
signal HeikenAshi(ENUM_TIMEFRAMES _period=PERIOD_H1)
{
MqlRates RateArray[];
ArrayResize(RateArray,3);
if(!CopyRates(_Symbol,_period,0,3,RateArray)) return none;
double HAC=(RateArray[0].open+RateArray[0].high+RateArray[0].low+RateArray[0].close)/4;
double HAO=(RateArray[1].open+RateArray[1].close)/2;
if(HAO<HAC) return buy;
else return sell;
return none;
}
//+------------------------------------------------------------------+
signal ManualDem(int len)
{
MqlRates RateArray[];
double DeMax[],DeMin[];
ArrayResize(RateArray,len);
ArrayResize(DeMax,len);
ArrayResize(DeMin,len);
if(!CopyRates(_Symbol,_Period,0,len,RateArray)) return none;
static double LMA;
double MA=SMARate(RateArray);
for(int i=1;i<ArraySize(RateArray);i++)
{
DeMax[i-1]=RateArray[i].high-RateArray[i-1].high;
DeMax[i-1]=(DeMax[i-1]>0?DeMax[i-1]:0);
DeMin[i-1]=RateArray[i-1].low-RateArray[i].low;
DeMin[i-1]=(DeMin[i-1]>0?DeMin[i-1]:0);
}
double MADeMax=SMA(DeMax);
double MADeMin=SMA(DeMin);
double root=MADeMax+MADeMin;
root=NormalizeDouble(root,5);
if(root!=0.0)
{
double DeM=MADeMax/root;
bool UT=false;
double std=Stdev(MA,LMA);
if(MA>LMA)
{
UT=true;
LMA=MA;
}
if(DeM>0.7 && !UT)
{
return sell;
}
else if(DeM<0.3 && UT)
{
return buy;
}
}
return none;
}
double MathRandRange(double x,double y) { return(x+MathMod(MathRand(),MathAbs(x-y))); }
//+------------------------------------------------------------------+
double Stdev(double a,double b)
{
return MathSqrt(MathPow(a-b,2))/2;
}
//+------------------------------------------------------------------+
double SMA(double &CArray[])
{
return ArraySum(CArray)/ArraySize(CArray);
}
//+------------------------------------------------------------------+
double ArraySum(double &rates[])
{
double SM=0;
for(int i=0;i<ArraySize(rates);i++)
{
SM+=rates[i];
}
return SM;
}
//+------------------------------------------------------------------+
double SMARate(MqlRates &CArray[])
{
return ArraySumRate(CArray)/ArraySize(CArray);
}
//+------------------------------------------------------------------+
double ArraySumRate(MqlRates &rates[])
{
double SM=0;
for(int i=0;i<ArraySize(rates);i++)
{
SM+=(rates[i].low+rates[i].high+rates[i].close)/3;
}
return SM;
}
//+------------------------------------------------------------------+
void TrailingStopH1(double price_ask,double price_bid)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if(symbol==_Symbol)
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
double SLC=PositionGetDouble(POSITION_SL);
double TPC=PositionGetDouble(POSITION_TP);
string CMC=PositionGetString(POSITION_COMMENT);
long MGC=PositionGetInteger(POSITION_MAGIC);
if(MGC==939393) continue;
if(CMC==NULL && CMC=="") continue;
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
double NSL=NormalizeDouble(price_ask-1000*_Point,_Digits);
double NSL2=NormalizeDouble(price_ask-100*_Point,_Digits);
if(NSL>SLC)
{
if(trade.PositionModify(PositionTicket,NSL,TPC+200*_Point))
{
Print("error");
}
}
else if(NSL2<SLC)
{
if(trade.PositionModify(PositionTicket,NSL2,TPC-100*_Point))
{
Print("error");
}
}
}
else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
double NSL=NormalizeDouble(price_ask+500*_Point,_Digits);
double NSL2=NormalizeDouble(price_ask+50*_Point,_Digits);
if(NSL<SLC)
{
if(!trade.PositionModify(PositionTicket,NSL,TPC-200*_Point))
{
Print("error");
}
}
else if(NSL2<SLC)
{
if(!trade.PositionModify(PositionTicket,NSL2,TPC+100*_Point))
{
Print("error");
}
}
}
}
}
}
//+------------------------------------------------------------------+
void TrailingStop(double price_ask,double price_bid)
{
for(int i=PositionsTotal()-1; i>=0; i--)
{
string symbol=PositionGetSymbol(i);
if(symbol==_Symbol)
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
double SLC=PositionGetDouble(POSITION_SL);
double TPC=PositionGetDouble(POSITION_TP);
string CMC=PositionGetString(POSITION_COMMENT);
long MGC=PositionGetInteger(POSITION_MAGIC);
double PPC=PositionGetDouble(POSITION_PROFIT);
signal HASHI=HeikenAshi();
if(MGC==46) continue;
if(CMC!=NULL && CMC!="") continue;
int lSL=50,lSL2=1;
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
double NSL=NormalizeDouble(price_ask+(PPC>0?50:20)*_Point,_Digits);
double NSL2=NormalizeDouble(price_ask-(PPC>0?50:20)*_Point,_Digits);
if(HASHI==buy)
{
if(trade.PositionModify(PositionTicket,NSL,TPC+(PPC>0?10:5)*_Point))
{
Print("error");
}
}
else
{
if(trade.PositionModify(PositionTicket,NSL2,TPC-(PPC>0?10:2)*_Point))
{
Print("error");
}
}
}
else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
double NSL=NormalizeDouble(price_bid-(PPC>0?50:20)*_Point,_Digits);
double NSL2=NormalizeDouble(price_bid+(PPC>0?50:20)*_Point,_Digits);
if(HASHI==sell)
{
if(!trade.PositionModify(PositionTicket,NSL,TPC-(PPC>0?10:5)*_Point))
{
Print("error");
}
}
else
{
if(!trade.PositionModify(PositionTicket,NSL2,TPC+(PPC>0?10:2)*_Point))
{
Print("error");
}
}
}
}
}
}
//+------------------------------------------------------------------+
double Rand(const double min,const double max)
{
double f=(MathRand()/32768.0);
return min + (int)(f * (max - min));
}
//+------------------------------------------------------------------+