Using iCustom function of the ZigZag indicator to close open trades when a new peak or trough is form

 

Hello programmers, 

Please I need your assistance using the iCustom function of the ZigZag indicator for my EA . The EA is meant to close open orders once the Z

//+------------------------------------------------------------------+
//|                                         ZigZag_Closed Trades.mq4 |
//|                                                     Copyright ,  |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright , "
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
     {
      int PeakPointCount=0,TroughPointCount=0;
      string sym;
      int tf;
      double zz=0.0, PeakPointValue=0.0, TroughPointValue=0.0;
      sym=Symbol();
      tf=Period();

      for(int i=0; i<Bars; i++)
        {
         string stime;

         //To detect a zigzag peak piont
         PeakPointValue=0.0;
         PeakPointValue= iCustom(sym,tf,"zigzag",12,5,3,1,i);

         for(int e = OrdersTotal() - 1; e >= 0; e--)
           {
            if(OrderSelect(e, SELECT_BY_POS, MODE_TRADES))
              {

               if((PeakPointValue==iHigh(sym,tf,i)) && OrderSymbol() == Symbol() && OrderType()== OP_BUY)
                 {

                  bool Close_Buy_Trade = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3); // Bid price
                  if(Close_Buy_Trade)
                     Alert("ZigZag peak reached! Buy Trade closed on ",Symbol());   //
                  if(!Close_Buy_Trade)
                     Alert("ZigZag peak reached! Buy Trade not closed on ", Symbol());//&& SendAlert
                 }

               //To detect a zigzag trough point
               TroughPointValue=0.0;
               TroughPointValue= iCustom(sym,tf,"zigzag",12,5,3,0,i);

               if((TroughPointValue==iLow(sym,tf,i)) && OrderSymbol() == Symbol() && OrderType()== OP_SELL)
                 {

                  bool Close_Sell_Trade = OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 3); 
                  if(Close_Sell_Trade)
                     Alert("ZigZag trough reached! Sell Trade closed on ",Symbol());   //
                  if(!Close_Sell_Trade)
                     Alert("ZigZag trough reached! Sell Trades not closed on ", Symbol());//&& SendAlert
                 }
              }
            
           }
        }
     }
  }
//+------------------------------------------------------------------+

igzag indicator forms a new peak or trough. So basically, I want a situation where once the ZigZag leg breaks in a new direction (peak or trough) only then will the close trade action be executed. Below is the code I used but it closes trade once it is activated on the chart without waiting for a new peak or trough to form.

Please any help in this direction will be highly appreciated!

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Fernando Carreiro #:
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Ok sir! Thanks 
 
Gabriel Nsor: The EA is meant to close open orders once the Zigzag indicator forms a new peak or trough.

ZZ is a repainting indicator. You don't want to do that.

 
William Roeder #:

ZZ is a repainting indicator. You don't want to do that.

Okay sir! Many thanks for your response 
 
Gabriel Nsor:

Hello programmers, 

Please I need your assistance using the iCustom function of the ZigZag indicator for my EA . The EA is meant to close open orders once the Z

igzag indicator forms a new peak or trough. So basically, I want a situation where once the ZigZag leg breaks in a new direction (peak or trough) only then will the close trade action be executed. Below is the code I used but it closes trade once it is activated on the chart without waiting for a new peak or trough to form.

Please any help in this direction will be highly appreciated!

The problem with ZigZag is not only that it redraws and is overdue, but also that on the bar we do not know which event occurred first: High or Low.
This is especially critical for long bars, when there should be a local minimum and a local maximum on the same bar.


Therefore, to make a correct decision, it is necessary to take into account the tick history.

 
Nikolai Semko #:

The problem with ZigZag is not only that it redraws and is overdue, but also that on the bar we do not know which event occurred first: High or Low.
This is especially critical for long bars, when there should be a local minimum and a local maximum on the same bar.


Therefore, to make a correct decision, it is necessary to take into account the tick history.

Yes that's very correct! Thanks so much for your input