Zigzag returns me 0

 

I am trying to get the first lower point of the ZigZag indicator which it shouldn't be the current candle may somebody help me please and tell me what is wrong with my code I think I'm doing it right and I didn't get the point

Thank's in advance

int zigzag,xs;
double zigzagDown[];


int OnInit()
  {

   zigzag=iCustom(_Symbol,PERIOD_D1,"Examples\\ZigZag",12,5,3);
   CopyBuffer(zigzag,0,0,Bars(_Symbol,PERIOD_D1),zigzagDown);
   ArraySetAsSeries(zigzagDown,true);

   //I Set xs as 1 in order to skip the current Candle in order if the trend is bearish it wont count it
   for(xs=1; xs<Bars(_Symbol,PERIOD_D1); xs++)
     {
      if(zigzagDown[xs]!=EMPTY_VALUE && zigzagDown[xs] < SymbolInfoDouble(_Symbol,SYMBOL_LAST))
         break;
     }
   //This piece of code prints me 0.0
   Print(zigzagDown[xs]);

   return(INIT_SUCCEEDED);
  }
 
  1. Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  2. You get handles in OnInit. In OnTick (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

 
ashkankhalaj :

I am trying to get the first lower point of the ZigZag indicator which it shouldn't be the current candle may somebody help me please and tell me what is wrong with my code I think I'm doing it right and I didn't get the point

Thank's in advance

Do you have a question about MQL5 or MQL4?

 
William Roeder:
  1. Don't try to use any price or server related functions in OnInit (or on load), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  2. You get handles in OnInit. In OnTick (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 2020.03.08
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
              How to call indicators in MQL5 - MQL5 Articles 12 March 2010

actually this is an assisting expert which I run it only once daily while the market is closed. it's job is to loop through all the securities in a stock market and searchs if a certain condition met.

is there any possibke way for?

Thanks 

 
Vladimir Karputov:

Do you have a question about MQL5 or MQL4?

I'll be thankful if you kindly help me with my issue. 

 
Vladimir Karputov:

Do you have a question about MQL5 or MQL4?

ashkankhalaj:

I'll be thankful if you kindly help me with my issue. 

I think that Vladimir is asking whether your question concerns MQL5 or MQL4.

 
Keith Watford:

I think that Vladimir is asking whether your question concerns MQL5 or MQL4.

yep it's an mql5 question I thought it's clear with the handle part.

 
ashkankhalaj :

yep it's an mql5 question I thought it's clear with the handle part.

I often see a lot of wild code. So you always have to ask.

And you immediately have a GREAT MISTAKE: you are creating a NEW handle AT EVERY TICK !!! Remember, in MQL5, the indicator handle MUST BE CREATED ONCE and you need to do it in OnInit !!!

Please fix this bug and post the changed code again.

 
Vladimir Karputov:

I often see a lot of wild code. So you always have to ask.

And you immediately have a GREAT MISTAKE: you are creating a NEW handle AT EVERY TICK !!! Remember, in MQL5, the indicator handle MUST BE CREATED ONCE and you need to do it in OnInit !!!

Please fix this bug and post the changed code again.

My Entire code is in OnInit, I want all the functions run only once when the market is closed, So there's no OnTick function.

"actually this is an assisting expert which I run it only once daily while the market is closed. it's job is to loop through all the securities in a stock market and searchs if a certain condition met.

is there any possibke way for?"

Thanks 

 
ashkankhalaj :

My Entire code is in OnInit, I want all the functions run only once when the market is closed, So there's no OnTick function.

"actually this is an assisting expert which I run it only once daily while the market is closed. it's job is to loop through all the securities in a stock market and searchs if a certain condition met.

is there any possibke way for?"

Thanks 

Mistake number two: OnInit prepares data. And you need to receive data in OnTick.

Correct your mistake.

 
Vladimir Karputov:

Mistake number two: OnInit prepares data. And you need to receive data in OnTick.

Correct your mistake

Still it doesn't run when the market is closed. There is no Price Ticking

Reason: