can anybody help me with this??

 

I can go inside the if block even when the condition is met. The IsNewBar() function works properly but for some reason the condition in the if() block is never met. The var counter never increments.  Any idea??

datetime dt;
int candleNro;
int counter = 0;
int OnInit()
  {
       dt = Time[0];
       return(INIT_SUCCEEDED);
  }

void OnTick()
  {
         if(Isnewbar()){
         counter++;
         }
  }

bool Isnewbar(){
   if(Time[0] !=dt){
      dt = Time[0];
      candleNro++;
      return true;
   }

   return false;
}
Improperly formatted code edited by moderator.
 
 you have a code intended to count the number of new bars within a specific time interval. Your program determines whether a new bar has formed based on the time difference between the current and previous bars.

Then, you mistakenly used the condition `if(Isnewbar())` in the `OnTick()` function to increment the `counter` variable. Since the `Isnewbar()` function itself increments this variable, there is no need to increase it again in the `OnTick()` function. In fact, doing so causes the `counter` value to increase excessively.
 

Your topic has been moved to the section: Expert Advisors and Automated Trading

Please always use the CODE button (Alt-S) when inserting code.

Code button in editor

 
Mtrading1970: I can go inside the if block even when the condition is met. The IsNewBar() function works properly but for some reason the condition in the if() block is never met. The var counter never increments.  Any idea??

Please review the following code example ...

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:38

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.
 
Mtrading1970:
datetime dt; int candleNro; int counter = 0; int OnInit()   {        dt = Time[0];        return(INIT_SUCCEEDED);   } void OnTick()   {          if(Isnewbar()){          counter++;          }   } bool Isnewbar(){    if(Time[0] !=dt){       dt = Time[0];       candleNro++;       return true;    }    return false; }

The code you provided seems to have a logical error in the OnInit() function. The candleNro variable is not initialized, and it may lead to unpredictable behavior in your program. Ensure you initialize candleNro properly in the OnInit() function:

int OnInit()
{
    dt = Time[0];
    candleNro = 0; // Initialize candleNro
    return INIT_SUCCEEDED;
}

Additionally, the condition in the Isnewbar() function checks if Time[0] is not equal to dt , and if true, it increments candleNro . However, the candleNro variable is not used in the OnTick() function where you increment the counter . If you intend to use candleNro in your logic, make sure to include it appropriately.

Here's an updated version of your code:

datetime dt;
int candleNro;
int counter = 0;

int OnInit()
{
    dt = Time[0];
    candleNro = 0; // Initialize candleNro
    return INIT_SUCCEEDED;
}

void OnTick()
{
    if (Isnewbar())
    {
        counter++;
        // You can use candleNro in your logic here if needed.
    }
}

bool Isnewbar()
{
    if (Time[0] != dt)
    {
        dt = Time[0];
        candleNro++;
        return true;
    }

    return false;
}

Make sure you include the necessary logic involving candleNro in your OnTick() function or adjust the code based on your specific requirements.

 
Mtrading1970:

I can go inside the if block even when the condition is met. The IsNewBar() function works properly but for some reason the condition in the if() block is never met. The var counter never increments.  Any idea??

Improperly formatted code edited by moderator.

Give it a try, you'll like it.

input bool            G_TickToFramesSwitch   = true;   
input string          G_TickToFramesSymbol   = "EURUSD";
input ENUM_TIMEFRAMES G_TickToFramesFrames   = PERIOD_M5;
      datetime        G_OldFramesTime        = D'1970.01.01 00:00:00';

void OnTick()
{
   if (!TickToFrames(AutoSymbol(G_TickToFramesSymbol), G_TickToFramesFrames, G_OldFramesTime))
   {
      return;
   }
   CallYouStrategy();
}

bool TickToFrames(string P_Symbol, ENUM_TIMEFRAMES P_Frames, datetime &P_OldTime)
{
   bool L_TimeLoop = P_OldTime == iTime(P_Symbol, P_Frames, 0);
   if(!L_TimeLoop || !G_TickToFramesSwitch)
   {
      P_OldTime = iTime(P_Symbol, P_Frames, 0);
      return (true);
   }
   else
   {
      return (false);
   }
}
Reason: