-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathG.P.Morgan-KS.mq4
638 lines (579 loc) · 17.5 KB
/
G.P.Morgan-KS.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
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
#property copyright "http://biggame24.tripod.com/"
extern string com0 = "Êîëëè÷åñòâî çíàêîâ â ëîòå ïîñëå çàïÿòîé";
extern int lot_st = 2;
extern string com1 = "Ðàçìåð ëîòà";
extern double Lots = 0.01;
extern double LotExponent = 1.667;
extern double TakeProfit = 10;
extern double Stoploss = 500;
extern double TrailStart = 10;
extern double TrailStop = 10;
extern double PipStep = 30;
extern int Insure1 = 0;
extern int Insure2 = 0;
extern int MaxTrades = 10;
extern bool UseEquityStop = false;
extern double TotalEquityRisk = 20; //loss as a percentage of equity
extern bool UseTrailingStop = false;
extern bool UseTimeOut = false;
extern double MaxTradeOpenHours = 48;
extern double slip = 3;
extern int MagicNumber = 88824;
double PriceTarget, StartEquity, BuyTarget, SellTarget;
double AveragePrice, SellLimit, BuyLimit;
double LastBuyPrice, LastSellPrice, ClosePrice, Spread;
int flag;
extern string EAName = "G.P.Morgan-KS";
Comment("BigGame24.tripod.com");
datetime timeprev=0, expiration;
int NumOfTrades=0;
double iLots;
int cnt=0, total;
double Stopper=0;
bool TradeNow = false, LongTrade=false, ShortTrade=false;
int ticket;
bool NewOrdersPlaced = false;// óñòàíîâëåííûå îðäåðà
int init()
{
Spread = MarketInfo(Symbol(), MODE_SPREAD)*Point;
return(0);
}
int deinit()
{
return(0);
}
int start()
{
if (UseTrailingStop) TrailingAlls(TrailStart, TrailStop, AveragePrice); //ïîòîì
if (UseTimeOut) // ïî óìîë÷àíèþ false
if(CurTime() >= expiration) //ïîòîì
{
CloseThisSymbolAll();
Print("Closed All due to TimeOut");
}
if(timeprev==Time[0])
return(0);
timeprev=Time[0];
double CurrentPairProfit = CalculateProfit(); //ïîäñ÷åò òåêóùåãî ïðîôèòà
if(UseEquityStop)//âíåøíèé ïàðàìåòð - òèïà "ñòîï ëîññ"
if(CurrentPairProfit<0 && MathAbs(CurrentPairProfit)>(TotalEquityRisk/100)*AccountEquityHigh())
{
CloseThisSymbolAll();
Print("Closed All due to Stop Out");
NewOrdersPlaced = false;
}
int total=OrdersTotal();
int count=0;
for(int pos=0;pos<total;pos++)
{
if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES)==false) continue;
count++;
}
total=CountTrades(); // êîëè÷åñòâî ñäåëîê ñ êîíêðåòíûì èäåíòèôèêàöèîííûì íîìåðîì (îòêðûòûõ)
if (total == 0) flag = 0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)//ïðîâåðêà - â êàêóþ ñòîðîíó îòêðûòà ñäåëêà
{
OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY)
{
if(Insure1!=0||Insure2!=0)
if(Insure1==count||Insure2==count)
{
LongTrade = true;
ShortTrade = false;
break;
}
else {
LongTrade = false;
ShortTrade = true;
break;
}
else {
LongTrade = true;
ShortTrade = false;
break;
}
}
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_SELL)
{
if(Insure1!=0||Insure2!=0)
if(Insure1==count||Insure2==count)
{
LongTrade = false;
ShortTrade = true;
break;
}
else {
LongTrade = true;
ShortTrade = false;
break;
}
else {
LongTrade = false;
ShortTrade = true;
break;
}
}
}
if(total>0 && total <= MaxTrades)//åñëè ãðàôèê óøåë áîëüøå/ìåíüøå PipStep òî îòêðûâàåì ñäåëêó
{
RefreshRates();
LastBuyPrice = FindLastBuyPrice();
LastSellPrice = FindLastSellPrice();
if(LongTrade && (LastBuyPrice - Ask) >= (PipStep*Point))
{
TradeNow = true;
}
if(ShortTrade && (Bid - LastSellPrice) >= (PipStep*Point))
{
TradeNow = true;
}
}
if (total < 1)
{
ShortTrade = false;
LongTrade = false;
TradeNow = true;
StartEquity = AccountEquity();//Âîçâðàùàåò ñóììó ñîáñòâåííûõ ñðåäñòâ äëÿ òåêóùåãî ñ÷åòà. Ðàñ÷åò equity çàâèñèò îò íàñòðîåê òîðãîâîãî ñåðâåðà.
}
if (TradeNow) //åñëè ãðàôèê äåðíóëñÿ áîëüøå PipStep îòêðûâàåì ñäåëêó â ïðîòèâîïîëîæíóþ ñòîðîíó
{
LastBuyPrice = FindLastBuyPrice();
LastSellPrice = FindLastSellPrice();
if(ShortTrade)
{
NumOfTrades = total;
iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
RefreshRates();
ticket = OpenPendingOrder(OP_SELL,iLots,Bid,slip,Ask,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
if(ticket<0){Print("Error: ",GetLastError()); return(0);}
LastSellPrice = FindLastSellPrice();
TradeNow = false;
NewOrdersPlaced = true;
}
else if (LongTrade)
{
NumOfTrades = total;
iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
ticket = OpenPendingOrder(OP_BUY,iLots,Ask,slip,Bid,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
if(ticket<0){Print("Error: ",GetLastError()); return(0);}
LastBuyPrice = FindLastBuyPrice();
TradeNow = false;
NewOrdersPlaced = true;
}
}
if (TradeNow && total<1) //åñëè ñäåëîê îòêðûòûõ íåò
{
double PrevCl = iClose(Symbol(),0,2);//Âîçâðàùàåò çíà÷åíèå öåíû çàêðûòèÿ
double CurrCl = iClose(Symbol(),0,1);
SellLimit = Bid;
BuyLimit = Ask;
if (!ShortTrade && !LongTrade)
{
NumOfTrades = total;
iLots = NormalizeDouble(Lots*MathPow(LotExponent,NumOfTrades),lot_st);
if(PrevCl > CurrCl)
{
ticket = OpenPendingOrder(OP_SELL,iLots,SellLimit,slip,SellLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,HotPink);
if(ticket<0){Print("Error: ",GetLastError()); return(0);}
LastBuyPrice = FindLastBuyPrice();
NewOrdersPlaced = true;
}
else
{
ticket = OpenPendingOrder(OP_BUY,iLots,BuyLimit,slip,BuyLimit,0,0,EAName+"-"+NumOfTrades,MagicNumber,0,Lime);
if(ticket<0){Print("Error: ",GetLastError()); return(0);}
LastSellPrice = FindLastSellPrice();
NewOrdersPlaced = true;
}
if(ticket>0) expiration = CurTime()+MaxTradeOpenHours*60*60;
TradeNow = false;
}
}
//----------------------- CALCULATE AVERAGE OPENING PRICE
total=CountTrades();
AveragePrice=0;
double Count = 0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
AveragePrice=AveragePrice+OrderOpenPrice()*OrderLots();
Count = Count + OrderLots();
}
}
if(total > 0)
AveragePrice=NormalizeDouble(AveragePrice/Count, Digits);
//----------------------- RECALCULATE STOPLOSS & PROFIT TARGET BASED ON AVERAGE OPENING PRICE
if(NewOrdersPlaced)
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY) // Calculate profit/stop target for long
{
PriceTarget=AveragePrice+(TakeProfit*Point);
BuyTarget = PriceTarget;
Stopper=AveragePrice-(Stoploss*Point);
// Stopper=0;
flag = 1;
}
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_SELL) // Calculate profit/stop target for short
{
PriceTarget=AveragePrice-(TakeProfit*Point);
SellTarget = PriceTarget;
Stopper=AveragePrice+(Stoploss*Point);
// Stopper=0;
flag = 1;
}
}
//----------------------- IF NEEDED CHANGE ALL OPEN ORDERS TO NEWLY CALCULATED PROFIT TARGET
if(NewOrdersPlaced)
if(flag==1)// check if average has really changed
{
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
// PriceTarget=total;
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
// OrderModify(OrderTicket(),0,Stopper,PriceTarget,0,Yellow);// set all positions to averaged levels
OrderModify(OrderTicket(),AveragePrice,OrderStopLoss(),PriceTarget,0,Yellow);// set all positions to averaged levels
NewOrdersPlaced = false;
}
}
}
int CountTrades()
{
int count=0;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_SELL || OrderType()==OP_BUY)
count++;
}//for
return(count);
}
void CloseThisSymbolAll()
{
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);
if(OrderSymbol()!=Symbol())
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber)
{
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots(),Bid,slip,Blue);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots(),Ask,slip,Red);
}
Sleep(1000);
}
}
int OpenPendingOrder(int pType,double pLots,double pLevel,int sp, double pr, int sl, int tp,string pComment,int pMagic,datetime pExpiration,color pColor)
{
int ticket=0;
int err=0;
int c = 0;
int NumberOfTries = 100;
switch (pType)
{
case OP_BUYLIMIT:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_BUYLIMIT,pLots,pLevel,sp,StopLong(pr,sl),TakeLong(pLevel,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(1000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_BUYSTOP:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_BUYSTOP,pLots,pLevel,sp,StopLong(pr,sl),TakeLong(pLevel,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_BUY:
for(c = 0 ; c < NumberOfTries ; c++)
{
RefreshRates();
ticket=OrderSend(Symbol(),OP_BUY,pLots,Ask,sp,StopLong(Bid,sl),TakeLong(Ask,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_SELLLIMIT:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELLLIMIT,pLots,pLevel,sp,StopShort(pr,sl),TakeShort(pLevel,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_SELLSTOP:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELLSTOP,pLots,pLevel,sp,StopShort(pr,sl),TakeShort(pLevel,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
case OP_SELL:
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELL,pLots,Bid,sp,StopShort(Ask,sl),TakeShort(Bid,tp),pComment,pMagic,pExpiration,pColor);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
break;
}
return(ticket);
}
double StopLong(double price,int stop)
{
if(stop==0)
return(0);
else
return(price-(stop*Point));
}
double StopShort(double price,int stop)
{
if(stop==0)
return(0);
else
return(price+(stop*Point));
}
double TakeLong(double price,int take)
{
if(take==0)
return(0);
else
return(price+(take*Point));
}
double TakeShort(double price,int take)
{
if(take==0)
return(0);
else
return(price-(take*Point));
}
double CalculateProfit()
{
double Profit=0;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
if(OrderType()==OP_BUY || OrderType()==OP_SELL)
{
Profit=Profit+OrderProfit();
}
}
return(Profit);
}
void TrailingAlls(int start,int stop, double AvgPrice)
{
int profit;
double stoptrade;
double stopcal;
if(stop==0)
return;
int trade;
for(trade=OrdersTotal()-1;trade>=0;trade--)
{
if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol()||OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)
{
profit=NormalizeDouble((Bid-AvgPrice)/Point,0);
if(profit<start)
continue;
stoptrade=OrderStopLoss();
stopcal=Bid-(stop*Point);
if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))
// OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
OrderModify(OrderTicket(),AvgPrice,stopcal,OrderTakeProfit(),0,Aqua);
}//Long
if(OrderType()==OP_SELL)
{
profit=NormalizeDouble((AvgPrice-Ask)/Point,0);
if(profit<start)
continue;
stoptrade=OrderStopLoss();
stopcal=Ask+(stop*Point);
if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))
// OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
OrderModify(OrderTicket(),AvgPrice,stopcal,OrderTakeProfit(),0,Red);
}//Shrt
}
Sleep(1000);
}//for
}
double AccountEquityHigh()
{
static double AccountEquityHighAmt,PrevEquity;
if(CountTrades()==0) AccountEquityHighAmt=AccountEquity();
if(AccountEquityHighAmt < PrevEquity) AccountEquityHighAmt=PrevEquity;
else AccountEquityHighAmt=AccountEquity();
PrevEquity = AccountEquity();
return(AccountEquityHighAmt);
}
double FindLastBuyPrice()//âîçâðàùàåò íîìåð ïîñëåäíåé ñäåëêè áàé
{
double oldorderopenprice = 0, orderprice;
int cnt, oldticketnumber = 0, ticketnumber;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_BUY)
{
ticketnumber = OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}
double FindLastSellPrice()
{
double oldorderopenprice = 0, orderprice;
int cnt, oldticketnumber = 0, ticketnumber;
for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)
continue;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber && OrderType()==OP_SELL)
{
ticketnumber = OrderTicket();
if(ticketnumber>oldticketnumber)
{
orderprice=OrderOpenPrice();
oldorderopenprice=orderprice;
oldticketnumber=ticketnumber;
}
}
}
return(orderprice);
}