"execute on bar open" problem - MT4

To add comments, please log in or register
saunders1000
8
saunders1000  

Hi, I have 100 EA's on M1, M5 and H1 charts. all created by myself. they are running on 100 charts on one terminal. They do not close or open positions when they should. They do what they need to do when I restart the terminal. I use the code given below: 

// Start function

int start()

{

// Execute on bar open

if(CheckOncePerBar == true)

{

int BarShift = 1;

if(CurrentTimeStamp != Time[0]) 

{

CurrentTimeStamp = Time[0];

bool NewBar = true;

}

else NewBar = false;

}

else 

{

NewBar = true;

BarShift = 0;

}

if(NewBar == true )

{ the code

                      }


Any thoughts  and suggestions?

William Roeder
20233
William Roeder  
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. 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

Lorentzos Roussos
19011
Lorentzos Roussos  

Try adding this : 

//ON TICK START 
datetime barstamp=0;
void OnTick()
  {
  //CHECK BLOCK
  bool perform_ea_checks=false;
  if(CheckOncePerBar==false) perform_ea_checks=true;
  if(CheckOncePerBar==true) 
    {
    if(Time[0]!=barstamp)
      {
      perform_ea_checks=true;
      barstamp=Time[0];
      }
    }
   if(perform_ea_checks==true)
   {
   //YOUR CODE IN HERE (the code)
   }
  //CHECK BLOCK ENDS HERe 
  }
//ON TICK ENDS HERE 
To add comments, please log in or register