How to detect NEW Bar? - page 2

 
Musngi:
How to detect NEW Bar? 

I want to reset my indicator variables if there's a new bar. 
I'm thinking to use the volumes data to detect it but I don't know if that's right formula or best code to do it??


bool NewBar()
  {
   static datetime lastbar;
   datetime curbar = Time[0];
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return (true);
     }
   else
     {
      return(false);
     }
  }
  
  
  int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
  // Your codes
  if (NewBar())  init(); // Reset Indicators
  
  }
 

Hello Good time I wrote a multi-time program that I want to give an alarm in 15 minutes, for example, so that the next candle of the program does not run. Please help me

نماد «مورد تأیید انجمن»
 
amin dehqan #:

Hello Good time I wrote a multi-time program that I want to give an alarm in 15 minutes, for example, so that the next candle of the program does not run. Please help me

EDIT: if you're concerned about the opening of a new bar, just keep the value of iTime(NULL, PERIOD_M15, 0) in a global/static variable. From there, just call iTime(NULL, PERIOD_M15, 0) checking for a greater value than the value you have stored. If it's greater, store the new value in the global/static variable and execute the rest of your code.

Just going to leave this here since I already wrote it up

----------------

Sets a 15min timer from when the program loads

//--- Global
datetime alarmTime;

//--- OnInit
const datetime time = TimeCurrent();
alarmTime           = time + (PERIOD_M15 * 60);

//--- OnCalculate / OnTick
const datetime now = TimeCurrent();
const bool isAlarm = now >= alarmTime;

if (isAlarm) {
  alarmTime = now + (PERIOD_M15 * 60);

  //--- do something
}
For every 15min based off start of the hour (ex: 10:00, 10:15, 10:30, 10:45, etc.)
const datetime time = TimeCurrent() - (TimeCurrent() % 60); //--- remove seconds from TimeCurrent
const bool is15min  = time % (PERIOD_M15 * 60) == 0;        //--- if remainder of time / 15min == 0, it's s 15min interval

if (is15min) {
  // do something
}
 
Alexander Martinez #: Sets a 15min timer from when the program loads
const datetime time = TimeCurrent();

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), 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.
Reason: