Make EA open a trade only when a new bar starts - page 2

 
xredburn:

I believe the right code is:

 if (iTime(NULL, 0, 0)

However as soon as I add this code, the compiler starts saying that there are { in the wrong place and always too many or too few. When I remove the code if (iTime(NULL, 0, 0) everything is fine again...

 Any suggestions? 

Show your code.
 
//Hi,
//add the following line to your declarations header

static datetime _lastBarTime = 0;           // DateTime of the last new bar which opened

//Next add this line to your init function:

_lastBarTime = iTime(Symbol(),_period(),0); 

//Now add the condition to check for a new bar which is:

if(NewBar()==true)   {
   ... do stuff here
}


//And dont forget to include the NewBar function which goes like this:

bool NewBar() {
 if (iTime(Symbol(),Period(),0) != _lastBarTime) {
  _lastBarTime = iTime(Symbol(), Period(),0);  
  
  return (true);
 } else
  return (false);
}
 
xredburn:

Hello,

 I made a simple EA but to speed up optimization I want to make the EA opena new trade only when the indicators are right and only when a new bar starts.

The EA has to wait until the bar closes and then opens a new trade only when the new bar opens.

Could someone tell me the code to use? I believe it will be something like if bar opens but I am sure and I can't find the info online.

 Thank you 

Have you tried EA generator software to build EA? May be useful for you.
 

Hi

This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.

Maybe you can use it.

 

// NEW BAR CHECK.
//------------------

// Rates structure for BAR data.
   MqlRates TikData[1];
   CopyRates(Symbol(), Period(), 1, 1, TikData); // Copy the data of the last COMPLETE bar to the rates structure;

   static double   dBar_Open;     
   static double   dBar_High;
   static double   dBar_Low;
   static double   dBar_Close;
   static long     lBar_Volume;
   static datetime nBar_Time;

// Boolean confirmation for a new BAR.
   bool bStart_NewBar = false;

// Check of de prijs is veranderd tov de vorige BAR.   
   if(TikData[0].open != dBar_Open || TikData[0].high != dBar_High || TikData[0].low != dBar_Low || TikData[0].close != dBar_Close || TikData[0].tick_volume 
      != lBar_Volume || TikData[0].time != nBar_Time || prev_calculated == 0)
         {
         bStart_NewBar = true; // New bar detected        

// reset data         
         dBar_Open   = TikData[0].open;      
         dBar_High   = TikData[0].high;
         dBar_Low    = TikData[0].low;
         dBar_Close  = TikData[0].close;                 
         lBar_Volume = TikData[0].tick_volume;
         nBar_Time   = TikData[0].time;
         }

   if(bStart_NewBar == true) // Check new BAR
         {
         
// your code.         
         
         }
 
Phil Meijer:

Awesome! It works for me like a charm!

Thanks for your input.

 
How can a newcomer learn to create an EA?
 
Olawale Daniel:
How can a newcomer learn to create an EA?

Hi @Olawale Daniel 

Please see: https://www.mql5.com/en/docs

And you can also try codebase for example code: https://www.mql5.com/en/code

MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
MQL5 Reference - How to use algorithmic/automated trading language for MetaTrader 5
  • www.mql5.com
MetaQuotes Language 5 (MQL5) is a high-level language designed for developing technical indicators, trading robots and utility applications, which automate financial trading. MQL5 has been developed by MetaQuotes Software Corp. for their trading platform. The language syntax is very close to C++ enabling programmers to develop applications in...
 
Snelle Moda:

Hi

This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.

Maybe you can use it.

 

prev_calculated undeclared identified, where and how to define it

 
Olawale Daniel: How can a newcomer learn to create an EA?
Learn to code it.
 
Snelle Moda: This simple script detects the start of a new BAR but it does only works if the price/volume has changed during the BAR period.

Simple? 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.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum

Reason: