Make EA open a trade only when a new bar starts

To add comments, please log in or register
Pedro Pacheco
342
Pedro Pacheco  

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 

Jose Miguel Soriano
5043
Jose Miguel Soriano  
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 

//------------------------------------- NUEVA VELA -------------------------------------
bool nuevaVela(string simb= NULL, ENUM_TIMEFRAMES marcoTmp= PERIOD_CURRENT)
{
        static datetime nuevoTiempo=0;
        datetime horaVela= iTime(simb, marcoTmp, 0);
        bool resp= false;
        if(nuevoTiempo!=horaVela)
        {
                nuevoTiempo= horaVela;
                resp= true;
        }
        return(resp);
}
datetime iTime(string simb, ENUM_TIMEFRAMES marcoTmp, int nVela= 0)
  {
   datetime aValor[1];
   int nCopiado=CopyTime(simb, marcoTmp, nVela, 1, aValor);
   if(nCopiado!=1) return(0);
   return(aValor[0]);       //si el valor devuelto es 0, falló la función
  }

 The above code reports the start of a new bar.

Konstantinos Antonakis
13
Konstantinos Antonakis  
add in this if (Volume == 0) {   }
Alain Verleyen
39034
Alain Verleyen  
kostas50:
add in this if (Volume == 0) {   }
Not reliable. Your EA can miss ticks and don't get a Volume=0 on new bar.
JDeel
139
JDeel  
int counter=0;

void OnTick()
{
   if (NewBar()) {
      counter++;
      Comment("Bar "+counter);
   }
}

bool NewBar()
{
   static datetime time0; // memory
   datetime Time[];
   
   ArraySetAsSeries(Time,true);
   CopyTime(Symbol(),0,0,1,Time);
   
   if (time0<Time[0])
   {
      time0=Time[0];
      return true;
   }
   return false;
}
This even works without ArraySetAsSeries(), I don't really know if it's needed.
Jose Miguel Soriano
5043
Jose Miguel Soriano  
JDeel:
This even works without ArraySetAsSeries(), I don't really know if it's needed.
No is necessary because time[] has dimension one.
ztmalick
1391
ztmalick  
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 



I use this: 



int start()
  {
//----
if(IsNewCandle())
{

// add your code

   return(0);
  }
  return(0);
  }

//+------------------------------------------------------------------+
//insuring its a new candle function
//+------------------------------------------------------------------+
bool IsNewCandle()
{
   static int BarsOnChart=0;
        if (Bars == BarsOnChart)
        return (false);
        BarsOnChart = Bars;
        return(true);
}
Alain Verleyen
39034
Alain Verleyen  
ztmalick:

Please don't reply inside quote.

Your code is for mql4 only. Doesn't work with mql5.

Number of bars isn't a reliable way to detect a new bar. (What happens when max bars in chart is reached ?)

ztmalick
1391
ztmalick  
angevoyageur:

Please don't reply inside quote.

Your code is for mql4 only. Doesn't work with mql5.

Number of bars isn't a reliable way to detect a new bar. (What happens when max bars in chart is reached ?)

apologies I made a mistake and your right, I realised I was on the wrong mql site as well sorry!
Pedro Pacheco
342
Pedro Pacheco  

Thank you very much for everyone who replied with solutions to my problem.

 I tried all the code you people gave me.

Some of them don't work, it gives error after error, and the others don't make the EA open trades only when a new bar opens, the EA continues to make trades as soon as the indicators are right.

Pedro Pacheco
342
Pedro Pacheco  

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? 

123
To add comments, please log in or register