How can I execute a calculus only once per bar

 

Hi,

I would like to know how can I execute a calculus only once per bar.

I was trying to compare current time to last bar beginning time, but it did not work and to me it makes no sense.

I already read others posts here, but I could not find any solution that worked.

Thanks

#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red

//----Q Contador de Periodos MA Cross Parameters ----//
                                                     //
extern int Per_Fast_Trend=10;                        // 
extern int Per_Slow_Trend=21;                        //
                                                     //      
//---------------------------------------------------//

//---- Buffers --------------//
                             //
double Fast_Trend_Line[];    //
double Slow_Trend_Line[];    //
double Per_Up[];                //
double Per_Down[];              //
                             //
//---------------------------//

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

      IndicatorBuffers(4);
      
      SetIndexStyle(0, DRAW_HISTOGRAM);
      SetIndexBuffer(0, Per_Up);
      
      SetIndexStyle(1, DRAW_HISTOGRAM);
      SetIndexBuffer(1, Per_Down);
      
      SetIndexStyle(2, DRAW_NONE);
      SetIndexBuffer(2, Fast_Trend_Line);
      
      SetIndexStyle(3, DRAW_NONE);
      SetIndexBuffer(3, Slow_Trend_Line);
      
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int limit;
   bool Cross_Up;
   bool Cross_Down;
   static int count_up=0;
   static int count_down=0;
   
//---- Data ckecking ----//

   if (counted_bars==0)
      {
      limit=Bars-1;
      }
   if (counted_bars>0)
      {
      limit=Bars-counted_bars;
      } 
      

   for(int i=limit;i>=0;i--) // Count down for one pass
   {

   
     Fast_Trend_Line[i] =iMA(NULL,
                           0,
                           Per_Fast_Trend,
                           0,
                           MODE_EMA,
                           MODE_CLOSE,
                           i); 
                           
      
     Slow_Trend_Line[i] =iMA(NULL,
                           0,
                           Per_Slow_Trend,
                           0,
                           MODE_EMA,
                           MODE_CLOSE,
                           i);  
                           
//---- Flag to show an up cross over event ----//

   if(Fast_Trend_Line[i+1]< Slow_Trend_Line[i+1] && Fast_Trend_Line[i]> Slow_Trend_Line[i])
   {
     Cross_Up=1;
   }
   else
   {
     Cross_Up=0;
   }
   
//---- Flag to show an down cross over event ----//
   if(Fast_Trend_Line[i+1]> Slow_Trend_Line[i+1] && Fast_Trend_Line[i]< Slow_Trend_Line[i])
   {
     Cross_Down=1;
   }
   else
   {
     Cross_Down=0;
   }
   
//---- Calculus to count the number of bars after a cross over ----//
//UP and DOWN CrossOver COUNT//

datetime   NewBar_Time  = Time[i];
datetime   CurrentBar   = TimeCurrent();
Print("NewBar_Time = ", NewBar_Time);
Print("CurrentBar = ", CurrentBar);

  if((Fast_Trend_Line[i]> Slow_Trend_Line[i]) && (CurrentBar==NewBar_Time))
   {
      count_up++;
      count_down=0;
   }
   else
   {
      count_up=0;
      count_down++;
   }

   Per_Up[i]=count_up;
   
   Per_Down[i]=count_down;   
   
  }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

rodrigosm:

I would like to know how can I execute a calculus only once per bar.

You don't for an indicator
//if((Fast_Trend_Line[i]> Slow_Trend_Line[i]) && (CurrentBar==NewBar_Time))
if(Fast_Trend_Line[i]> Slow_Trend_Line[i])
   {
//      count_up++;
//      count_down=0;
      Per_Up[i]=Per_Up[i+1]+1;
      Per_Down[i]=0;
   }
   else
   {
//      count_up=0;
//      count_down++;
      Per_Down[i]=Per_Down[i+1]+1;
      Per_Up[i]=0;
   }
//   Per_Up[i]=count_up;
//   Per_Down[i]=count_down;  
 
WHRoeder:
You don't for an indicator

Thank you WHRoeder, I will test it tomorrow. It makes sense.
 
WHRoeder:
You don't for an indicator

Thank you, worked.... the other way is usefful only for experts?
Reason: