How can I get the last two fixed values of the ZigZag indicator.

 

In my EA below, I have programmatically drawn the default ZigZag indicator. I would like to access the last two fixed values of the ZigZag. How can I achieve this?

I saw this post (https://www.mql5.com/en/forum/131897)but I'm not certain how to apply this to arrive at my solution 


#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Trade\OrderInfo.mqh>
#include <Expert\Money\MoneyFixedMargin.mqh>

CPositionInfo  m_position;                   // trade position object
CTrade         m_trade;                      // trading object
CSymbolInfo    m_symbol;                     // symbol info object
CAccountInfo   m_account;                    // account info wrapper
COrderInfo     m_order;                      // pending orders object
CMoneyFixedMargin *m_money;

int            handle_iCustom;               // variable for storing the handle of the iCustom indicator

int OnInit()
  {
//---

//---

   //--- create handle of the indicator iCustom
   handle_iCustom=iCustom(m_symbol.Name(),Period(),"Examples\\ZigZag");
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iCustom indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      if(m_money!=NULL)
         delete m_money;
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   // --- 
   ChartIndicatorAdd ( 0 , 0 , handle_iCustom); 
// --- 


   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
How to find the last two ZigZag values?
How to find the last two ZigZag values?
  • 2011.02.19
  • www.mql5.com
Dear forum, I'm trying to get the previous two, fixed ZigZag values -- I've tried the method found here, but it doesn't seem to work...
 

I couldn't understand the forum link you posted! and I would just drag the indicator onto the chart, but...

If you call this function from somewhere, it gives a print of the zz values, bars 0-9. - related to the attached image.

So at bar0 and bar5 are zz changes and you just work out the buffer number to extract the direction, where "buffer" is just the return value of iCustom(..)

It looks like buffer0 (x =0) is price, buffer1 (x=1)is down, buffer2(x=2 is up?

          if(buffer0!=0&&buffer1!=0&&buffer2==0) // down  etc etc

That gives you the bar, and from that you can work out price,time etc etc


string calc(){
string txt;
int depth=12;
for(int y=0;y<10;y++){  // last 10 bars
 txt=StringConcatenate(txt,"\nBar ",y," : ");
 for(int x=0;x<10;x++)  // indicator buffers
  txt=StringConcatenate(txt,iCustom(_Symbol,_Period,"zigzag",depth,5,3,x,y)," ");
}
return txt;
}
/* 
Bar 0 : 1.04448 1.04448 0 0 0 0 0 0 0 0 
Bar 1 : 0 0 0 0 0 0 0 0 0 0 
Bar 2 : 0 0 0 0 0 0 0 0 0 0 
Bar 3 : 0 0 0 0 0 0 0 0 0 0 
Bar 4 : 0 0 0 0 0 0 0 0 0 0 
Bar 5 : 1.04085 0 1.04085 0 0 0 0 0 0 0 
Bar 6 : 0 0 0 0 0 0 0 0 0 0 
Bar 7 : 0 0 0 0 0 0 0 0 0 0 
Bar 8 : 0 0 0 0 0 0 0 0 0 0 
Bar 9 : 0 0 0 0 0 0 0 0 0 0 

*/
zz
 

You can use the following code:

//Before the main function
double ZigZagArray[],zigzag[100];
int ZigZagHandle;

void onTick(){
//Calling the function
CheckForZigZag();
//Print as many as you want. for example 2 value here
Print(zigzag[0],zigzag[1]);
}
void CheckForZigZag()
  {
   ZigZagHandle=iCustom(_Symbol,PERIOD_M15,"Examples\\ZigZag",12,5,3);
   ArraySetAsSeries(ZigZagArray,true);
   CopyBuffer(ZigZagHandle,0,0,100,ZigZagArray);
   int zigzag_counter=0;
   for(int i=0; i<100; i++)
     {
      if(ZigZagArray[i]!=0.0)
        {
         zigzag[zigzag_counter]=ZigZagArray[i];
         zigzag_counter++;
        }
     }
  }
Reason: