Trigger on only new bar

 
hi,


int start()
  {
   if(NewBar()==true)
     {
   double sma6 = iMA (Symbol(), 0,6, 0, MODE_SMA, PRICE_CLOSE, 0);
       Sleep(2000);  
                 //buy
                if (sma6<Close[0])
                 {   SaveInfoMovingAverageToTheFile("BUY");
                   //  Sleep(15000);  
                    // FileDelete(_Symbol+"_"+"_movingAverage_"+TimeFrameToString(Period())+".txt",0);
                  //  FileDelete("EURUSD_movingAverage_M1.txt",0);
                 }
                 
                 //sell
                 if (sma6>Close[0])
                 {   
                    SaveInfoMovingAverageToTheFile("SELL");
                  //  Sleep(15000);  
                   // FileDelete("EURUSD_movingAverage_M1.txt",0);
                 //   FileDelete(_Symbol+"_"+"_movingAverage_"+TimeFrameToString(Period())+".txt",0);
                 }      

     }

}

This code works but problem is because works imediatelly when add indicator on Chart.
So how can I use this code on the next new bar ?

If I have chart 1 hour I need to start with next hour and then always use with next new bar?



 
chris.dotan: This code works but problem is because works imediatelly when add indicator on Chart. So how can I use this code on the next new bar ? If I have chart 1 hour I need to start with next hour and then always use with next new bar?

Read the following. Pay careful attention to the comments in the code.

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:46

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.

 
Fernando Carreiro #:

Read the following. Pay careful attention to the comments in the co

thanks it works.

 
chris.dotan #:thanks it works.
You are welcome!
Reason: