forked from hung96ad/MQL4_Experts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gann Line.mq4
707 lines (676 loc) · 60.5 KB
/
Gann Line.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
//+------------------------------------------------------------------+
//| GANNLINE |
//| http://algorithmic-trading-ea-mt4.000webhostapp.com/ |
//| AHARON TZADIK |
//+------------------------------------------------------------------+
#property copyright "AHARON TZADIK"
#property link "http://algorithmic-trading-ea-mt4.000webhostapp.com/"
#property version "1.00"
#property strict
//--------------------------------------------------------------------
extern int LEN_GANNLINE=50; // GANNLINE length (bars)
extern color Col_GANNLINE=Orange; // GANNLINE color
//--------------------------------------------------------
extern bool USEMOVETOBREAKEVEN=true;//Enable "no loss"
extern double WHENTOMOVETOBE=10; //When to move break even
extern double PIPSTOMOVESL=5; //How much pips to move sl
extern double Lots=0.01; //Lots size
input double MaximumRisk =0.02;
input double DecreaseFactor=0.001;
extern double TrailingStop=40; //TrailingStop
extern double Stop_Loss=400; //Stop Loss
extern int MagicNumber=1234; //MagicNumber
input double TakeProfit=400; //TakeProfit
extern int FastMA=1; //FastMA
extern int SlowMA=20; //SlowMA
extern double Mom_Sell=0.3; //Momentum_Sell
extern double Mom_Buy=0.3; //Momentum_Buy
int err;
int total=0;
double
Lot,Dmax,Dmin,// Amount of lots in a selected order
Lts, // Amount of lots in an opened order
Min_Lot, // Minimal amount of lots
Step, // Step of lot size change
Free, // Current free margin
One_Lot, // Price of one lot
Price, // Price of a selected order,
pips,
MA_1,MA_2,MA_3,MACD_SIGNAL;
int Type,freeze_level,Spread;
//--- price levels for orders and positions
double priceopen,stoploss,takeprofit;
datetime timeTag;
//--- ticket of the current order
int orderticket;
double Linenow=0;
double Lineprev=0;
//--------------------------------------------------------------- 3 --
int
Period_MA_2, Period_MA_3, // Calculation periods of MA for other timefr.
Period_MA_02, Period_MA_03, // Calculation periods of supp. MAs
K2,K3,T;
//---
datetime dt=0;
datetime dt1=0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//Create();
// drow();
//--------------------------------------------------------------- 5 --
double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
if(ticksize==0.00001 || ticksize==0.001)
pips=ticksize*10;
else pips=ticksize;
return(INIT_SUCCEEDED);
//--- distance from the activation price, within which it is not allowed to modify orders and positions
freeze_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL);
if(freeze_level!=0)
{
PrintFormat("SYMBOL_TRADE_FREEZE_LEVEL=%d: order or position modification is not allowed,"+
" if there are %d points to the activation price",freeze_level,freeze_level);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick(void)
{
stop();
Create();
drow();
if(USEMOVETOBREAKEVEN) MOVETOBREAKEVEN();
Trail1();
if(timeTag<Time[0])
{
int ticket=0;
// Check for New Bar (Compatible with both MQL4 and MQL5)
static datetime dtBarCurrent=WRONG_VALUE;
datetime dtBarPrevious=dtBarCurrent;
dtBarCurrent=(datetime) SeriesInfoInteger(_Symbol,_Period,SERIES_LASTBAR_DATE);
bool NewBarFlag=(dtBarCurrent!=dtBarPrevious);
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
//---
if(Bars<100)
{
Print("bars less than 100");
return;
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return;
}
//--- to simplify the coding and speed up access data are put into internal variables
double MA_250=iMA(NULL,0,FastMA,0,MODE_LWMA,PRICE_TYPICAL,1); // МА_2
double MA_500=iMA(NULL,0,SlowMA,0,MODE_LWMA,PRICE_TYPICAL,1); // МА_2
//----------------------------------------------------------------------------
double MomLevel=MathAbs(100-iMomentum(NULL,0,14,PRICE_CLOSE,1));
double MomLevel1=MathAbs(100 - iMomentum(NULL,0,14,PRICE_CLOSE,2));
double MomLevel2=MathAbs(100 - iMomentum(NULL,0,14,PRICE_CLOSE,3));
//--------------------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
double middleBB=iBands(Symbol(),0,20, 2,0,0,MODE_MAIN,1);//middle
double lowerBB=iBands(Symbol(),0,20, 2,0,0,MODE_LOWER,1);//lower
double upperBB=iBands(Symbol(),0,20, 2,0,0,MODE_UPPER,1);//upper
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double MacdMAIN=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
double MacdSIGNAL=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
double MacdMAIN1=iMACD(NULL,0,12,26,1,PRICE_CLOSE,MODE_MAIN,1);
double MacdMAIN2=iMACD(NULL,0,6,13,1,PRICE_CLOSE,MODE_MAIN,1);
//+------------------------------------------------------------------+
//----------------------------------------------------------------------------
double MA_1_t=iMA(NULL,0,FastMA,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_1
double MA_2_t=iMA(NULL,0,SlowMA,0,MODE_LWMA,PRICE_TYPICAL,0); // МА_2
//-------------------------------------------------------------------------------------------------
Linenow=ObjectGet("GANNLINE",OBJPROP_TIME2);//2nd time coord.
Lineprev=ObjectGet("GANNLINE",OBJPROP_TIME1);//1st time coord.
dt=(datetime)Linenow;//convert double to datetime 2nd time coord.
dt1=(datetime)Lineprev;//convert double to datetime 1st time coord.
//--------------------------------------------------------------------------------------------------
if(getOpenOrders()==0)
{
//--- no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ",AccountFreeMargin());
return;
}
// if(NewBarFlag)
// {
//--- check for long position (BUY) possibility
//+------------------------------------------------------------------+
//| BUY BUY BUY |
//+------------------------------------------------------------------+
if(MA_1_t>MA_2_t)
if(MomLevel<Mom_Buy || MomLevel1<Mom_Buy || MomLevel2<Mom_Buy)
{
if(IsTesting()==true)
{
ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+TakeProfit*pips),"renko",MagicNumber,0,PaleGreen);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
return;
}
if(IsTesting()==false)//only in live trading we use the GANNLINE
{
if(High[iBarShift(NULL,0,dt)]>High[iBarShift(NULL,0,dt1)])
ticket=OrderSend(Symbol(),OP_BUY,LotsOptimized(),ND(Ask),3,NDTP(Bid-Stop_Loss*pips),NDTP(Bid+TakeProfit*pips),"renko",MagicNumber,0,PaleGreen);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
return;
}
}
//--- check for short position (SELL) possibility
//+------------------------------------------------------------------+
//| SELL SELL SELL |
//+------------------------------------------------------------------+
if(MA_1_t<MA_2_t)
if(MomLevel<Mom_Sell || MomLevel1<Mom_Sell || MomLevel2<Mom_Sell)
{
if(IsTesting()==true)
{
ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-TakeProfit*pips),"Short 1",MagicNumber,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
}
if(IsTesting()==false)//only in live trading we use the GANNLINE
{
if(High[iBarShift(NULL,0,dt)]<High[iBarShift(NULL,0,dt1)])
ticket=OrderSend(Symbol(),OP_SELL,LotsOptimized(),ND(Bid),3,NDTP(Ask+Stop_Loss*pips),NDTP(Ask-TakeProfit*pips),"Short 1",MagicNumber,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
timeTag=TimeCurrent();
}
return;
}
//--- exit from the "no opened orders" block
return;
}
}
}
//+------------------------------------------------------------------+
//| stop |
//+------------------------------------------------------------------+
//-----------------------------------------------------------------------------+
void stop()
{
int cnt;
//+------------------------------------------------------------------+
double middleBB=iBands(Symbol(),0,20, 2,0,0,MODE_MAIN,1);//middle
double lowerBB=iBands(Symbol(),0,20, 2,0,0,MODE_LOWER,1);//lower
double upperBB=iBands(Symbol(),0,20, 2,0,0,MODE_UPPER,1);//upper
//+------------------------------------------------------------------+
double MacdMAIN=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
double MacdSIGNAL=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
//+------------------------------------------------------------------+
for(cnt=0;cnt<total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
//--- long position is opened
if(OrderType()==OP_BUY)
{
//--- should it be closed?
if(Close[1]==lowerBB)
exitbuys();
//--- check for trailing stop
}
else // go to short position
{
//--- should it be closed?
if(Close[1]==lowerBB)
exitsells();
//--- check for trailing stop
}
}
}
}
//+------------------------------------------------------------------+
//| Trailing stop loss |
//+------------------------------------------------------------------+
// --------------- ----------------------------------------------------------- ------------------------
void Trail1()
{
total=OrdersTotal();
//--- it is important to enter the market correctly, but it is more important to exit it correctly...
for(int cnt=0;cnt<total;cnt++)
{
if(!OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES))
continue;
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
//--- long position is opened
if(OrderType()==OP_BUY)
{
//--- check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>pips*TrailingStop)
{
if(OrderStopLoss()<Bid-pips*TrailingStop)
{
RefreshRates();
stoploss=Bid-(pips*TrailingStop);
takeprofit=OrderTakeProfit()+pips*TrailingStop;
double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);
if(stoploss<StopLevel*pips) stoploss=StopLevel*pips;
string symbol=OrderSymbol();
double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
if(MathAbs(OrderStopLoss()-stoploss)>point)
if((pips*TrailingStop)>(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL)*pips)
//--- modify order and exit
if(CheckStopLoss_Takeprofit(OP_BUY,stoploss,takeprofit))
if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Green))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
else // go to short position
{
//--- check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(pips*TrailingStop))
{
if((OrderStopLoss()>(Ask+pips*TrailingStop)) || (OrderStopLoss()==0))
{
RefreshRates();
stoploss=Ask+(pips*TrailingStop);
takeprofit=OrderTakeProfit()-pips*TrailingStop;
double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)+MarketInfo(Symbol(),MODE_SPREAD);
if(stoploss<StopLevel*pips) stoploss=StopLevel*pips;
if(takeprofit<StopLevel*pips) takeprofit=StopLevel*pips;
string symbol=OrderSymbol();
double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
if(MathAbs(OrderStopLoss()-stoploss)>point)
if((pips*TrailingStop)>(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL)*pips)
//--- modify order and exit
if(CheckStopLoss_Takeprofit(OP_SELL,stoploss,takeprofit))
if(OrderModifyCheck(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit))
if(!OrderModify(OrderTicket(),OrderOpenPrice(),stoploss,takeprofit,0,Red))
Print("OrderModify error ",GetLastError());
return;
}
}
}
}
}
}
}
//+------------------------------------------------------------------+
//+---------------------------------------------------------------------------+
//| MOVE TO BREAK EVEN |
//+---------------------------------------------------------------------------+
void MOVETOBREAKEVEN()
{
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=MagicNumber)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_BUY)
if(Bid-OrderOpenPrice()>WHENTOMOVETOBE*pips)
if(OrderOpenPrice()>OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(PIPSTOMOVESL*pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
for(int s=OrdersTotal()-1;s>=0;s--)
{
if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=MagicNumber)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELL)
if(OrderOpenPrice()-Ask>WHENTOMOVETOBE*pips)
if(OrderOpenPrice()<OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(PIPSTOMOVESL*pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
}
//--------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=OrdersHistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//--- select lot size
if(MaximumRisk>0)
{
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
}
//--- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() /*|| OrderType()>OP_SELL*/)
continue;
//---
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//--- minimal allowed volume for trade operations
double minlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);
if(lot<minlot)
{ lot=minlot; }
// Print("Volume is less than the minimal allowed ,we use",minlot);}
// lot=minlot;
//--- maximal allowed volume of trade operations
double maxlot=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);
if(lot>maxlot)
{ lot=maxlot; }
// Print("Volume is greater than the maximal allowed,we use",maxlot);}
// lot=maxlot;
//--- get minimal step of volume changing
double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);
int ratio=(int)MathRound(lot/volume_step);
if(MathAbs(ratio*volume_step-lot)>0.0000001)
{ lot=ratio*volume_step;}
return(lot);
/* else Print("StopOut level Not enough money for ",OP_SELL," ",lot," ",Symbol());
return(0);*/
}
//+------------------------------------------------------------------+
double NDTP(double val)
{
RefreshRates();
double SPREAD=MarketInfo(Symbol(),MODE_SPREAD);
double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL);
if(val<StopLevel*pips+SPREAD*pips) val=StopLevel*pips+SPREAD*pips;
// double STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);
//int Stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
//if (Stops_level*pips<val-Bid)
//val=Ask+Stops_level*pips;
return(NormalizeDouble(val, Digits));
// return(val);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
double ND(double val)
{
return(NormalizeDouble(val, Digits));
}
//+------------------------------------------------------------------+
int IfOrderDoesNotExistBuy()
{
bool exists=false;
for(int i=OrdersTotal(); i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS)==true && OrderSymbol()==Symbol())
{
exists = true; return(exists);
}else{
Print("OrderSelect() error - ",(GetLastError()));
}
}
if(exists==false)
{
//BuyOrderType();
// return(exists);
}
return(0);
}
////////////////////////////////////////////////////////////////////////////////////
int getOpenOrders()
{
int Orders=0;
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
{
continue;
}
if(OrderSymbol()!=Symbol() || OrderMagicNumber()!=MagicNumber)
{
continue;
}
Orders++;
}
return(Orders);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Checking the new values of levels before order modification |
//+------------------------------------------------------------------+
bool OrderModifyCheck(int ticket,double price,double sl,double tp)
{
//--- select order by ticket
if(OrderSelect(ticket,SELECT_BY_TICKET))
{
//--- point size and name of the symbol, for which a pending order was placed
string symbol=OrderSymbol();
double point=SymbolInfoDouble(symbol,SYMBOL_POINT);
//--- check if there are changes in the Open price
bool PriceOpenChanged=true;
int type=OrderType();
if(!(type==OP_BUY || type==OP_SELL))
{
PriceOpenChanged=(MathAbs(OrderOpenPrice()-price)>point);
}
//--- check if there are changes in the StopLoss level
bool StopLossChanged=(MathAbs(OrderStopLoss()-sl)>point);
//--- check if there are changes in the Takeprofit level
bool TakeProfitChanged=(MathAbs(OrderTakeProfit()-tp)>point);
//--- if there are any changes in levels
if(PriceOpenChanged || StopLossChanged || TakeProfitChanged)
return(true); // order can be modified
//--- there are no changes in the Open, StopLoss and Takeprofit levels
else
//--- notify about the error
PrintFormat("Order #%d already has levels of Open=%.5f SL=%.5f TP=%.5f",
ticket,OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
}
//--- came to the end, no changes for the order
return(false); // no point in modifying
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type,double SL,double TP)
{
//--- get the SYMBOL_TRADE_STOPS_LEVEL level
int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
if(stops_level!=0)
{
PrintFormat("SYMBOL_TRADE_STOPS_LEVEL=%d: StopLoss and TakeProfit must"+
" not be nearer than %d points from the closing price",stops_level,stops_level);
}
//---
bool SL_check=false,TP_check=false;
//--- check only two order types
switch(type)
{
//--- Buy operation
case ORDER_TYPE_BUY:
{
//--- check the StopLoss
SL_check=(Bid-SL>stops_level*_Point);
if(!SL_check)
PrintFormat("For order %s StopLoss=%.5f must be less than %.5f"+
" (Bid=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",
EnumToString(type),SL,Bid-stops_level*_Point,Bid,stops_level);
//--- check the TakeProfit
TP_check=(TP-Bid>stops_level*_Point);
if(!TP_check)
PrintFormat("For order %s TakeProfit=%.5f must be greater than %.5f"+
" (Bid=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",
EnumToString(type),TP,Bid+stops_level*_Point,Bid,stops_level);
//--- return the result of checking
return(SL_check&&TP_check);
}
//--- Sell operation
case ORDER_TYPE_SELL:
{
//--- check the StopLoss
SL_check=(SL-Ask>stops_level*_Point);
if(!SL_check)
PrintFormat("For order %s StopLoss=%.5f must be greater than %.5f "+
" (Ask=%.5f + SYMBOL_TRADE_STOPS_LEVEL=%d points)",
EnumToString(type),SL,Ask+stops_level*_Point,Ask,stops_level);
//--- check the TakeProfit
TP_check=(Ask-TP>stops_level*_Point);
if(!TP_check)
PrintFormat("For order %s TakeProfit=%.5f must be less than %.5f "+
" (Ask=%.5f - SYMBOL_TRADE_STOPS_LEVEL=%d points)",
EnumToString(type),TP,Ask-stops_level*_Point,Ask,stops_level);
//--- return the result of checking
return(TP_check&&SL_check);
}
break;
}
//--- a slightly different function is required for pending orders
return false;
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| exitbuys() |
//+------------------------------------------------------------------+
void exitbuys()
{
double result;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);
if(result!=true)//if it did not close
{
err=GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
}
}
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| exitsells() |
//+------------------------------------------------------------------+
void exitsells()
{
double result;
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
result=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE);
if(result!=true)//if it did not close
{
err=GetLastError(); Print("LastError = ",err);//get the reason why it didn't close
}
}
}
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int drow()
{
datetime T2=0; // Second time coordinate
double t2=0;
int Error; // Error code
//--------------------------------------------------------------- 3 --
t2=ObjectGet("GANNLINE",OBJPROP_TIME2);// Requesting t2 coord.
//T2=t2;
Error=GetLastError(); // Getting an error code
if(Error==4202) // If no object :(
{
Create();
T2=Time[1]; // Current value of t2 coordinate
}
//--------------------------------------------------------------- 4 --
if(T2!=Time[1]) // If object is not in its place
{
ObjectMove("GANNLINE",0,Time[LEN_GANNLINE-1],Open[LEN_GANNLINE-1]); //New t1 coord.
ObjectMove("GANNLINE",1,Time[1],Open[1]); //New t2 coord.
WindowRedraw(); // Redrawing the image
//Create();
}
return (0);
}
//--------------------------------------------------------------- 6 --
int Create() // User-defined function..
{ // ..of object creation
datetime T1=Time[LEN_GANNLINE-1]; // Defining 1st time coord.
datetime T2=Time[1]; // Defining 2nd time coord.
ObjectCreate("GANNLINE",OBJ_GANNLINE,0,T1,Open[LEN_GANNLINE-1],T2,Open[1]);// Creation
//--- change Gann angle
//ObjectSet("GANNLINE",OBJPROP_ANGLE,angle);
//--- set line color
ObjectSet("GANNLINE",OBJPROP_COLOR,Col_GANNLINE); // Color
//--- set line width
ObjectSet("GANNLINE",OBJPROP_WIDTH,1);
//--- enable (true) or disable (false) the mode of moving the line by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
ObjectSet("GANNLINE",OBJPROP_SELECTABLE,true);
ObjectSet("GANNLINE",OBJPROP_SELECTED,true);
//--- set the priority for receiving the event of a mouse click in the chart
ObjectSet("GANNLINE",OBJPROP_ZORDER,0);
//--- enable (true) or disable (false) the mode of continuation of the line's display to the right
ObjectSet("GANNLINE",OBJPROP_RAY,true); // Ray
ObjectSet("GANNLINE",OBJPROP_STYLE,STYLE_DASH);// Style
//--- display in the foreground (false) or background (true)
ObjectSet("GANNLINE",OBJPROP_BACK,true);
WindowRedraw(); // Image redrawing
return(0);
}
//--------------------------------------------------------------- 7 --