How to have only one trade per bar?

 
Hi guys,

What is the easiest way to enforce just one trade per bar on close of the bar? 

Best Regards, 

Ashish. 
 
Ashish Kulkarni:
Hi guys,

What is the easiest way to enforce just one trade per bar on close of the bar? 

Best Regards, 

Ashish. 

Maybe

datetime NewTime=0;
void start()
  {
   if(NewTime!=Time[0]) // Run only on new bars
     {
      NewTime=Time[0];
  // Enter code to run once per bar here.
     }
  }
 
Thanks John 
 
int barstemp=0;
void start()
{
    if(barstemp!=Bars)
    {
         barstemp = Bars;  
         //put your code
    }
}
 
 
Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
If the start_time and stop_time parameters are defined, the function returns the number of bars in the specified time interval, otherwise it returns the total number of bars. If data for the timeseries with specified parameters are not formed in the terminal by the time of the Bars() function call, or data of the timeseries are not synchronized...
 
John Davis #:

Maybe

It doesn't work dude it even doesn't recognize this function...

 
Ashish Kulkarni: What is the easiest way to enforce just one trade per bar on close of the bar?

You can't know when a candle closes. Only when a new tick arrives that starts a new bar is the old bar closed.

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          MT4: New candle - MQL4 programming forum #3 (2014)
          MT5: Accessing variables - MQL4 programming forum #3 (2022)

I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
          Running EA once at the start of each bar - MQL4 programming forum (2011)

Bars is unreliable.
 
Comments that do not relate to this topic, have been moved to "Off Topic Posts".