Explorar o código

Ticket: 5131 ReEntry and Filters

Huzaifa-MQLDev hai 4 meses
pai
achega
a89616b6d3
Modificáronse 2 ficheiros con 433 adicións e 15 borrados
  1. BIN=BIN
      vol_hedge_strategy_mt5.ex5
  2. 433 15
      vol_hedge_strategy_mt5.mq5

BIN=BIN
vol_hedge_strategy_mt5.ex5


+ 433 - 15
vol_hedge_strategy_mt5.mq5

@@ -12,6 +12,48 @@ CTrade  trade;
12 12
 //+------------------------------------------------------------------+
13 13
 //| Expert initialization function                                   |
14 14
 //+------------------------------------------------------------------+
15
+enum NewsCloseOrder
16
+  {
17
+   CloseAllRunningOrder=0,
18
+   LetTheOrderRun=1,
19
+  };
20
+enum selectLine
21
+  {
22
+   LineOnNewsBar =0,
23
+   LineOnNewsStop=1
24
+  };
25
+
26
+#import "volHedgeNewsFilter.ex5"
27
+
28
+//+------------------------------------------------------------------+
29
+//|                                                                  |
30
+//+------------------------------------------------------------------+
31
+void initiateNews();
32
+int returnNewsStatus(bool High_Impact_News=true
33
+                     ,int High_Start_Time=60//Stop Trade before high News (min)
34
+                     ,int High_Stop_Time=15 //Stop Trade after high News (min)
35
+                     ,bool show_high_line=true//Show verticle Line when high news comes
36
+
37
+                     ,bool Medium_Impact_News=true
38
+                     ,int Medium_Start_Time=60//Stop Trade before medium News (min)
39
+                     ,int Medium_Stop_Time=15 //Stop Trade after medium News (min)
40
+                     ,bool show_medium_line=true//Show verticle Line when medium news comes
41
+
42
+                     ,bool Low_Impact_News=true
43
+                     ,int Low_Start_Time=60//Stop Trade before low News (min)
44
+                     ,int Low_Stop_Time=15 //Stop Trade after low News (min)
45
+                     ,bool show_low_line=true//Show verticle Line when low news comes
46
+
47
+                     ,string symbol=""
48
+                     ,string expert=""
49
+                     ,int GMT_Broker_Time=2
50
+                     ,selectLine sl=0
51
+                     ,string extraSymbol=""
52
+                    );
53
+void initiateNews();
54
+bool checkDate(string &lastNewsTitle, string &symbolNews, string &impactNews, string &news1, string &news2, string &news3, datetime &timeRemaining, datetime date,string expertname = "",string symbol = "",string extraSymbol="", int gmt=2,string description_1 = "",int candle = 0);
55
+void PrintStructure();
56
+#import
15 57
 
16 58
 struct new_trade_store
17 59
   {
@@ -42,6 +84,11 @@ enum lotcalculator
42 84
 
43 85
 sinput       string                  string_0                   = "<><><><><><> General SETTINGS <><><><><><>";   //__
44 86
 input        int                     magic_no                   = 333;             // Magic no
87
+input        bool                    bothHitsSl                 = false;           // Open after Both Hits StopLoss
88
+input        int                     maxTrades                  = 2;               // Max Concurrent Trades
89
+input        int                     maxSlippage                = 5;               // Max Slippage
90
+input        bool                    enableSpreadFilter         = false;           // Enable Spread Filter
91
+input        double                  maximumSpread              = 10;              // Maximum Spread
45 92
 
46 93
 input        string                  string_1                   = "<><><><><><> Lot Management<><><><><><>";   //__
47 94
 input        lotcalculator           lot_calculator             = fix;             // Lot Size Option
@@ -51,8 +98,25 @@ input        double                  dollars                    = 10;
51 98
 
52 99
 input        string                  string_2                   = "<><><><><><> Time Filter Setting <><><><><><> ";//_
53 100
 input        bool                    enableTimeSession          = false;           // Enable Time Session
54
-input        string                  start_time                 = "01:00";         // Start Session
55
-input        string                  end_time                   = "23:59";         // End Session
101
+input        string                  start_time_session         = "01:00";         // Start Session
102
+input        string                  end_time_session           = "23:59";         // End Session
103
+
104
+input        string                  news                       = "<><><><><><> News Settings <><><><><><>";         // News
105
+input        NewsCloseOrder          newsClose                  = CloseAllRunningOrder; // On News Action on Running Orders
106
+input        bool                    High_Impact_News           = true;                 //High Impact News
107
+input        int                     High_Start_Time            = 60;                   //Stop Trade before high News (min)
108
+input        int                     High_Stop_Time             = 15;                   //Stop Trade after high News (min)
109
+input        bool                    show_high_line             = true;                 //Show verticle Line when high news comes
110
+input        selectLine              Select_News_Line           = 0;                    //News Line
111
+input        bool                    mobileAlert                = true;                 //Mobile Alert
112
+input        bool                    Medium_Impact_News         = false;                // Medium Impact News
113
+input        int                     Medium_Start_Time          = 60;                   // Stop Trade before medium News (min)
114
+input        int                     Medium_Stop_Time           = 15;                   // Stop Trade after medium News (min)
115
+input        bool                    show_medium_line           = false;                // Show vertical Line when medium news comes
116
+input        bool                    Low_Impact_News            = false;                // Low Impact News
117
+input        int                     Low_Start_Time             = 60;                   // Stop Trade before low News (min)
118
+input        int                     Low_Stop_Time              = 15;                   // Stop Trade after low News (min)
119
+input        bool                    show_low_line              = false;                // Show vertical Line when low news comes
56 120
 
57 121
 // Global Variables
58 122
 static double tickCurrentBid = 0;
@@ -60,6 +124,9 @@ double tickPreviousBid = 0;
60 124
 static double tickCurrentAsk = 0;
61 125
 double tickPreviousAsk = 0;
62 126
 datetime startSessionTime, endSessionTime;
127
+int GMT_Broker_Time = +2; // GMT_Broker_Time Time of your Broker
128
+int                     gmt                        = 0;                   // GMT_Broker_Time Time of your Broker
129
+string Lname="newsLabel3";
63 130
 //+------------------------------------------------------------------+
64 131
 //|                                                                  |
65 132
 //+------------------------------------------------------------------+
@@ -67,9 +134,8 @@ int OnInit()
67 134
   {
68 135
 //---
69 136
    Print(" OnInIt. ");
70
-
71 137
    trade.SetExpertMagicNumber(magic_no);
72
-   trade.SetDeviationInPoints(10);
138
+   trade.SetDeviationInPoints(maxSlippage);
73 139
    trade.SetTypeFilling(ORDER_FILLING_IOC);
74 140
    trade.LogLevel(LOG_LEVEL_ALL);
75 141
    trade.SetAsyncMode(false);
@@ -124,8 +190,34 @@ int OnInit()
124 190
       Print(" InValid Handler. Error: ", GetLastError());
125 191
      }
126 192
 
127
-   timeFilter(true,start_time, end_time, startSessionTime, endSessionTime);
193
+   timeFilter(true,start_time_session, end_time_session, startSessionTime, endSessionTime);
128 194
    Print(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
195
+
196
+   int timeDifference = (int)TimeCurrent() - (int)TimeGMT();
197
+   Print("Time Difference is: ", timeDifference);
198
+
199
+   if(timeDifference > 0)
200
+     {
201
+      GMT_Broker_Time = (int)MathRound((double)timeDifference / 3600.0);
202
+     }
203
+   else
204
+      if(timeDifference < 0)
205
+        {
206
+         GMT_Broker_Time = (int)MathRound((double)timeDifference / 3600.0);
207
+        }
208
+      else // timeDifference == 0
209
+        {
210
+         GMT_Broker_Time = 0;
211
+        }
212
+
213
+   Print("Gmt Time: ", TimeGMT(), " Broker Gmt Time: ", GMT_Broker_Time);
214
+   gmt = GMT_Broker_Time;
215
+
216
+   if(!MQLInfoInteger(MQL_TESTER))
217
+     {
218
+      initiateNews(); // change here
219
+     }
220
+
129 221
 //---
130 222
    return(INIT_SUCCEEDED);
131 223
   }
@@ -141,6 +233,120 @@ void OnDeinit(const int reason)
141 233
 //| Expert tick function                                             |
142 234
 //+------------------------------------------------------------------+
143 235
 void OnTick()
236
+  {
237
+     static int status=-1,preStatus=-1;
238
+   if(!MQLInfoInteger(MQL_TESTER))
239
+     {
240
+      status=returnNewsStatus(High_Impact_News
241
+                              ,High_Start_Time
242
+                              ,High_Stop_Time
243
+
244
+                              ,show_high_line
245
+                              ,Medium_Impact_News
246
+                              ,Medium_Start_Time
247
+                              ,Medium_Stop_Time
248
+
249
+                              ,show_medium_line
250
+                              ,Low_Impact_News
251
+                              ,Low_Start_Time
252
+                              ,Low_Stop_Time
253
+
254
+                              ,show_low_line
255
+                              ,Symbol()
256
+                              ,MQLInfoString(MQL_PROGRAM_NAME)
257
+                              ,GMT_Broker_Time
258
+                              ,Select_News_Line
259
+                              ,""
260
+                             );
261
+     }
262
+   else
263
+      status=0;
264
+
265
+   if(status==0)
266
+     {
267
+      mainActivity();
268
+     }
269
+   if(status==1 || status==2 || status==3)
270
+     {
271
+      if(newsClose==0)
272
+        {
273
+         if(orderCount_1(POSITION_TYPE_BUY,magic_no)>0)
274
+            closeTrades(POSITION_TYPE_BUY,magic_no);
275
+         if(orderCount_1(POSITION_TYPE_SELL,magic_no)>0)
276
+            closeTrades(POSITION_TYPE_SELL,magic_no);
277
+        }
278
+      else
279
+         if(newsClose==1)
280
+           {
281
+            if((orderCount_1(POSITION_TYPE_BUY,magic_no)>0) || (orderCount_1(POSITION_TYPE_SELL,magic_no)>0))
282
+              {
283
+               mainActivity();
284
+              }
285
+           }
286
+
287
+     }
288
+   if(status!=preStatus)
289
+     {
290
+      if(status==0 && preStatus!=-1)
291
+        {
292
+         // if(ObjectFind(0,Lname))
293
+           {
294
+
295
+            // ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrRed);
296
+            ObjectSetString(0,Lname,OBJPROP_TEXT,"");
297
+           }
298
+         Alert("Trading is start");
299
+         if(mobileAlert)
300
+            SendNotification("Trading is start");
301
+
302
+         preStatus=status;
303
+        }
304
+      else
305
+         if(status==1)
306
+           {
307
+            ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrRed);
308
+            ObjectSetString(0,Lname,OBJPROP_TEXT,"High Impact News");
309
+            Alert("Trading Stop due to high impact news");
310
+
311
+            if(mobileAlert)
312
+               SendNotification("Trading Stop due to high impact news");
313
+
314
+            preStatus=status;
315
+           }
316
+         else
317
+            if(status==2)
318
+              {
319
+               ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrBlue);
320
+               ObjectSetString(0,Lname,OBJPROP_TEXT,"Medium Impact News");
321
+               Alert("Trading Stop due to medium impact news");
322
+
323
+               if(mobileAlert)
324
+                  SendNotification("Trading Stop due to medium impact news");
325
+
326
+               preStatus=status;
327
+              }
328
+            else
329
+               if(status==3)
330
+                 {
331
+                  ObjectSetInteger(0,Lname,OBJPROP_COLOR,clrGreen);
332
+                  ObjectSetString(0,Lname,OBJPROP_TEXT,"Low Impact News");
333
+                  Alert("Trading Stop due to low impact news");
334
+
335
+                  if(mobileAlert)
336
+                     SendNotification("Trading Stop due to low impact news");
337
+
338
+                  preStatus=status;
339
+                 }
340
+     }
341
+   else
342
+     {
343
+      mainActivity();
344
+     }
345
+  }
346
+//+------------------------------------------------------------------+
347
+//|                                                                  |
348
+//+------------------------------------------------------------------+
349
+void mainActivity()
144 350
   {
145 351
 //---
146 352
    double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
@@ -169,14 +375,94 @@ void OnTick()
169 375
      }
170 376
 
171 377
 // Print(" Time is: ", TimeCurrent());
172
-   timeFilter(false,start_time, end_time, startSessionTime, endSessionTime);
173
-   Comment(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
378
+   timeFilter(false,start_time_session, end_time_session, startSessionTime, endSessionTime);
379
+// Comment(" Session Start  = ", startSessionTime,  " Asian  Session End = ", endSessionTime);
174 380
    if((!enableTimeSession) || (enableTimeSession && TimeCurrent() >= startSessionTime && TimeCurrent() <= endSessionTime))
175 381
      {
176 382
       tradePlacingCheck();
383
+      if(bothHitsSl)
384
+        {
385
+         removeBuySellOnSlHit();
386
+        }
387
+     }
388
+  }
389
+//+------------------------------------------------------------------+
390
+//+------------------------------------------------------------------+
391
+//|                                                                  |
392
+//+------------------------------------------------------------------+
393
+void closeTrades(int type,int magicno)
394
+  {
395
+   Print("Total order: ",OrdersTotal());
396
+   for(int i= PositionsTotal()-1; i>=0; i--)
397
+     {
398
+      //  Print(" Selection: ",OrderSelect(i,SELECT_BY_POS)," i: ",i,"  OrdersTotal(): ", OrdersTotal());
399
+      ulong ticket = PositionGetTicket(i);
400
+      if(PositionSelectByTicket(ticket))
401
+        {
402
+         if(PositionGetInteger(POSITION_TYPE) == type)
403
+           {
404
+            if(PositionGetInteger(POSITION_MAGIC) == magicno && PositionGetString(POSITION_SYMBOL) == Symbol())
405
+              {
406
+               //trade.PositionClose(PositionGetInteger(POSITION_TICKET))
407
+               if(!trade.PositionClose(PositionGetInteger(POSITION_TICKET)))
408
+                 {Print("Problem in closing order order ",PositionGetInteger(POSITION_TICKET)); }
409
+               else
410
+                 {
411
+                  Print("Order Closed by closeTrades() new filter",PositionGetInteger(POSITION_TICKET));
412
+                 }
413
+              }
414
+           }
415
+        }
416
+     }
417
+  }
418
+//+------------------------------------------------------------------+
419
+//|                                                                  |
420
+//+------------------------------------------------------------------+
421
+int orderCount_1(int type,int magic)
422
+  {
423
+   int count1=0;
424
+   for(int i= PositionsTotal()-1; i>=0; i--)
425
+     {
426
+      //  Print(" Selection: ",OrderSelect(i,SELECT_BY_POS)," i: ",i,"  OrdersTotal(): ", OrdersTotal());
427
+      ulong ticket = PositionGetTicket(i);
428
+      if(PositionSelectByTicket(ticket))
429
+        {
430
+         //   if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
431
+         if(PositionGetInteger(POSITION_MAGIC) == magic && PositionGetString(POSITION_SYMBOL) == Symbol())
432
+           {
433
+            //trade.PositionClose(PositionGetInteger(POSITION_TICKET))
434
+            if(PositionGetInteger(POSITION_TYPE) == type)
435
+              {
436
+               count1++;
437
+              }
438
+           }
439
+        }
177 440
      }
441
+   return count1;
178 442
   }
179 443
 //+------------------------------------------------------------------+
444
+//|                                                                  |
445
+//+------------------------------------------------------------------+
446
+int orderCount(int type)
447
+  {
448
+   int count = 0;
449
+   for(int i= PositionsTotal()-1; i>=0; i--)
450
+     {
451
+      ulong ticket = PositionGetTicket(i);
452
+
453
+      if(PositionSelectByTicket(ticket))
454
+        {
455
+         if(PositionGetInteger(POSITION_MAGIC) == magic_no && PositionGetString(POSITION_SYMBOL) == Symbol())
456
+           {
457
+            if(PositionGetInteger(POSITION_TYPE) == type)
458
+              {
459
+               count++;
460
+              }
461
+           }
462
+        }
463
+     }
464
+   return count;
465
+  }
180 466
 //+------------------------------------------------------------------+
181 467
 //|                                                                  |
182 468
 //+------------------------------------------------------------------+
@@ -221,15 +507,24 @@ void tradePlacingCheck()
221 507
         {
222 508
          if(newTradeStore[i].price > 0)
223 509
            {
224
-            double levelPriceIs = newTradeStore[i].price;
225
-            if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
226
-               (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
510
+            if(TimeCurrent() > newTradeStore[i].start_time && TimeCurrent() < newTradeStore[i].end_time)
227 511
               {
228
-               ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
229
-               ulong sellTicket = 0; // placeSellTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
230
-
231
-               newTradeStore[i].buy_ticket = buyTicket;
232
-               newTradeStore[i].sell_ticket = sellTicket;
512
+               double levelPriceIs = newTradeStore[i].price;
513
+               if((tickPreviousBid > levelPriceIs && tickCurrentBid < levelPriceIs) ||
514
+                  (tickPreviousBid < levelPriceIs && tickCurrentBid > levelPriceIs))
515
+                 {
516
+                  if(countLiveTrades() < maxTrades)
517
+                    {
518
+                     if((enableSpreadFilter && spreadFilter()) || !enableSpreadFilter)
519
+                       {
520
+                        ulong buyTicket = placeBuyTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
521
+                        ulong sellTicket = placeSellTrade(newTradeStore[i].stop_loss, newTradeStore[i].take_profit);
522
+
523
+                        newTradeStore[i].buy_ticket = buyTicket;
524
+                        newTradeStore[i].sell_ticket = sellTicket;
525
+                       }
526
+                    }
527
+                 }
233 528
               }
234 529
            }
235 530
         }
@@ -450,3 +745,126 @@ void timeFilter(bool onInit,string startTime,string endTime,datetime & sessionSt
450 745
 //+------------------------------------------------------------------+
451 746
 //|                                                                  |
452 747
 //+------------------------------------------------------------------+
748
+void removeBuySellOnSlHit()
749
+  {
750
+   for(int i = 0 ; i < MaxOrders ; i++)
751
+     {
752
+      bool isBuyPresent=false, isSellPresent=false;
753
+      if(newTradeStore[i].buy_ticket != -1 && newTradeStore[i].sell_ticket != -1)
754
+        {
755
+         for(int j = PositionsTotal()-1; j>=0; j--)
756
+           {
757
+            ulong ticket = PositionGetTicket(j);
758
+            if(PositionSelectByTicket(ticket))
759
+              {
760
+               if(ticket == newTradeStore[i].buy_ticket)
761
+                 {
762
+                  isBuyPresent=true;
763
+                 }
764
+              }
765
+           }
766
+
767
+         for(int j = PositionsTotal()-1; j>=0; j--)
768
+           {
769
+            ulong ticket = PositionGetTicket(j);
770
+            if(PositionSelectByTicket(ticket))
771
+              {
772
+               if(ticket == newTradeStore[i].sell_ticket)
773
+                 {
774
+                  isSellPresent = true;
775
+                 }
776
+              }
777
+           }
778
+
779
+         if(!isBuyPresent && !isSellPresent)
780
+           {
781
+            if(tradeHitSL(newTradeStore[i].buy_ticket) && tradeHitSL(newTradeStore[i].sell_ticket))
782
+              {
783
+               Print("Buy/Sell Ticket is closed on StopLoss so removed from struct. Buy Ticket: ", newTradeStore[i].buy_ticket, " Sell Ticket: ", newTradeStore[i].sell_ticket);
784
+               newTradeStore[i].buy_ticket  = (ulong)-1;
785
+               newTradeStore[i].sell_ticket = (ulong)-1;
786
+               //newTradeStore[i].symbol      = "";
787
+               //newTradeStore[i].price       = 0.0;
788
+               //newTradeStore[i].stop_loss   = 0.0;
789
+               //newTradeStore[i].take_profit = 0.0;
790
+               //newTradeStore[i].start_time  = 0;
791
+               //newTradeStore[i].end_time    = 0;
792
+              }
793
+           }
794
+        }
795
+     }
796
+  }
797
+//+------------------------------------------------------------------+
798
+//|                                                                  |
799
+//+------------------------------------------------------------------+
800
+bool tradeHitSL(ulong positionID)
801
+  {
802
+
803
+   if(HistorySelectByPosition(positionID))
804
+     {
805
+      for(int i = 0; i < HistoryDealsTotal(); i++)
806
+        {
807
+         ulong dealTicket = HistoryDealGetTicket(i);
808
+         ulong dealPositionID = HistoryDealGetInteger(dealTicket, DEAL_POSITION_ID);
809
+
810
+         if(HistoryDealGetInteger(dealTicket, DEAL_ENTRY) == DEAL_ENTRY_OUT && dealPositionID == positionID)
811
+           {
812
+            int dealType = (int)HistoryDealGetInteger(dealTicket, DEAL_TYPE);
813
+            string comment = HistoryDealGetString(dealTicket, DEAL_COMMENT);
814
+
815
+            if(StringFind(comment, "sl", 0) >= 0)
816
+              {
817
+               //               if(dealType == DEAL_TYPE_BUY)
818
+               //                 {
819
+               //
820
+               //                 }
821
+               return true;
822
+              }
823
+           }
824
+        }
825
+     }
826
+   return false;
827
+  }
828
+//+------------------------------------------------------------------+
829
+//|                                                                  |
830
+//+------------------------------------------------------------------+
831
+int countLiveTrades()
832
+  {
833
+   int count = 0;
834
+
835
+   for(int i = 0; i < PositionsTotal(); i++)
836
+     {
837
+      ulong ticket = PositionGetTicket(i);
838
+      if(PositionSelectByTicket(ticket))
839
+        {
840
+         if(PositionGetInteger(POSITION_MAGIC) == magic_no)
841
+           {
842
+            if(PositionGetString(POSITION_SYMBOL) == Symbol())
843
+              {
844
+               if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY || PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
845
+                 {
846
+                  count++;
847
+                 }
848
+              }
849
+           }
850
+        }
851
+     }
852
+   return count;
853
+  }
854
+//+------------------------------------------------------------------+
855
+//|                                                                  |
856
+//+------------------------------------------------------------------+
857
+bool spreadFilter()
858
+  {
859
+   long spreadIs = SymbolInfoInteger(Symbol(), SYMBOL_SPREAD);
860
+   if(spreadIs > maximumSpread)
861
+     {
862
+      return false;
863
+     }
864
+   return true;
865
+  }
866
+//+------------------------------------------------------------------+
867
+//|                                                                  |
868
+//+------------------------------------------------------------------+
869
+
870
+//+------------------------------------------------------------------+