having problem with fractal finder function

 
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input ENUM_TIMEFRAMES time=PERIOD_H1;
int OnInit()
  {
   for(int j=1; j<100; j++)
     {
      if(find_first_down_fract(j)< Bid)
        {
         double fract_down_value=find_first_down_fract(j);
         Print("fract_down_value= ",fract_down_value);
         break;
        }
     }
   for(int j=1; j<100; j++)
     {
      if(find_first_up_fract(j)> Ask)
        {
         double fract_up_value=find_first_up_fract(j);
         Print("fract_up_value= ", fract_up_value);
         break;
        }
     }
   return(INIT_SUCCEEDED);

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

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

  }
//+------------------------------------------------------------------+
double find_first_up_fract(int j)
  {
   double res=0;
   for(int i=3; i<100; i++)
     {
      if(iFractals(Symbol(),time,1,i)!=EMPTY_VALUE && iFractals(Symbol(),time,1,i) > Ask)
        {
         return(iFractals(Symbol(),time,1,i));
        }
     }
   return(res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double find_first_down_fract(int j)
  {
   double res=0;
   for(int i=3; i<100; i++)
     {
      if(iFractals(Symbol(),time,2,i)!=EMPTY_VALUE && iFractals(Symbol(),time,2,i) < Bid)
        {
         return(iFractals(Symbol(),time,2,i));
        }
     }
   return(res);
  }

Hi, I have problem with fractal finder function (code is attached), when I run the program, only output is for up fractal and down fractal for all cases is zero, I don't know where is the problem, I tried many solutions but it didn't work. please help me, thanks in advance.

P.S. which one is it better to use? NULL, _Symbol or Symbol() ? Although I read a post about them, but still don't know what is the differences between them?

Fractals - Bill Williams' Indicators - MetaTrader 5 Help
Fractals - Bill Williams' Indicators - MetaTrader 5 Help
  • www.metatrader5.com
All markets are characterized by the fact that on the most part the prices do not change too much, and only short periods of time (15–30 percent)...
 
No source code was attached!
 
Fernando Carreiro:
No source code was attached!

Sorry, it is attached.

 
337252:

Hi, I have problem with fractal finder function (code is attached), when I run the program, only output is for up fractal and down fractal for all cases is zero, I don't know where is the problem, I tried many solutions but it didn't work. please help me, thanks in advance.

P.S. which one is it better to use? NULL, _Symbol or Symbol() ? Although I read a post about them, but still don't know what is the differences between them?

I believe you have been told before, but if not, please pay attention to the fact that you should not do such things in the OnInit(). Usually no data is available during the initialization of the EA.

Forum on trading, automated trading systems and testing trading strategies

Getting Indicator Values in EA OnInit() functions to be processed.

William Roeder, 2020.09.30 16:39

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.
 

Do it in the OnTick() event handler and add code to detect when a new bar has been created and only search for the fractals when a new bar occurs. In this case search for a new bar based on your "time" variable.

Here is an example of detecting a new bar:

// Check for New Bar
static datetime dtBarCurrent   = WRONG_VALUE;
       datetime dtBarPrevious  = dtBarCurrent;
                dtBarCurrent   = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );

       bool     boolNewBarFlag = ( dtBarCurrent != dtBarPrevious );

if( boolNewBarFlag ) { Print( "... do something ..." ); }

Instead of "_Period", you can use your "time" variable.

 Also, try to stay away from using "NULL" and use "_Symbol" or "Symbol()" instead. I personally prefer "_Symbol"!
 
double find_first_up_fract(int j)
⋮
double find_first_down_fract(int j)
What is the purpose of passing an int, when you never use it in the function?
 
thank you so much. I did what you recommended, but still the problem  exists
//+------------------------------------------------------------------+
//|                                               fractal finder.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
input ENUM_TIMEFRAMES time=PERIOD_H1;
datetime old_time;
int OnInit()
  {
   return(INIT_SUCCEEDED);

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  if(is_new_candle(time))
     {
      for(int j=1; j<100; j++)
        {
         if(find_first_down_fract(j)< Bid)
           {
            double fract_down_value=find_first_down_fract(j);
            Print("fract_down_value= ",fract_down_value);
            break;
           }
        }
      for(int j=1; j<100; j++)
        {
         if(find_first_up_fract(j)> Ask)
           {
            double fract_up_value=find_first_up_fract(j);
            Print("fract_up_value= ", fract_up_value);
            break;
           }
        }
     }
  }
//+------------------------------------------------------------------+
double find_first_up_fract(int j)
  {
   double res=0;
   for(int i=3; i<100; i++)
     {
      if(iFractals(Symbol(),time,1,i)!=EMPTY_VALUE && iFractals(Symbol(),time,1,i) > Ask)
        {
         return(iFractals(Symbol(),time,1,i));
        }
     }
   return(res);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double find_first_down_fract(int j)
  {
   double res=0;
   for(int i=3; i<100; i++)
     {
      if(iFractals(Symbol(),time,2,i)!=EMPTY_VALUE && iFractals(Symbol(),time,2,i) < Bid)
        {
         return(iFractals(Symbol(),time,2,i));
        }
     }
   return(res);
  }
//+------------------------------------------------------------------+
// Check for New Bar
/* static datetime dtBarCurrent   = WRONG_VALUE;
 datetime dtBarPrevious  = dtBarCurrent;
 dtBarCurrent =(datetime)SeriesInfoInteger(_Symbol, _Period, SERIES_LASTBAR_DATE);
 bool     boolNewBarFlag=(dtBarCurrent != dtBarPrevious);
*/
//+------------------------------------------------------------------+
bool is_new_candle(ENUM_TIMEFRAMES timeframe)
  {
   bool res=false;
   datetime new_time=iTime(_Symbol,timeframe,0);
   if(new_time!=old_time)
     {
      res=true;
      old_time=new_time;
     }
   return(res);
  }
//+------------------------------------------------------------------+

, please see the attachment.
 
William Roeder:
What is the purpose of passing an int, when you never use it in the function?

Actually I'm looking for first up and down fractals (bigger than Ask and less than Bid to use them in my code as stop loss as below:

//-----------------------------------------

for(int j=1; j<100; j++)

           {

            if(find_first_down_fract(j)<SymbolInfoDouble(_Symbol,SYMBOL_BID))

              {

               double fract_value=find_first_down_fract(j);

               open_pos(ORDER_TYPE_BUY,fract_value-fix_sl*10*_Point,SymbolInfoDouble(_Symbol,SYMBOL_ASK)+fix_tp*10*_Point,100);

               break;

              }

           }

//----------------------------------

I will be appreciate if you guide me.        

Reason: