Download MetaTrader 5
To add comments, please log in or register
Order a robot or an indicator for MetaTrader. The Freelance service will help you!
Michael
496
Michael 2014.04.04 00:13 
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not? The code I have doesn't work anymore.
Keith Watford
Moderator
7948
Keith Watford 2014.04.04 00:19  
nondisclosure:
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not? The code I have doesn't work anymore.

Show the code that you have. I can't think of any reason why it would not work if it worked before
Michael
496
Michael 2014.04.04 00:27  

Here it is. The other problem it has is it's based on time. I'm using Renko charts, so I need a way that doesn't rely on time. PS. This code doesn't work either. No errors, it just misses the candle. I snargled this from an mql4.com forum post years ago.

bool funcIsNewBar(int timeFrame)
   {
   bool res=false;
   
   // the array contains open time of the current (zero) bar
   // for 7 (seven) timeframes
   static datetime _sTime[7];  
   int i=6;
   //Note: i below will be 6 or timeframe will be day.
   switch (timeFrame) 
      {
      case 1  : i=0; break;
      case 5  : i=2; break;
      case 15 : i=3; break;
      case 30 : i=4; break;
      case 60 : i=5; break;
      case 240: break;
      case 1440:break;
      default:  timeFrame = 1440;
      }
//----
   if (_sTime[i]==0 || _sTime[i]!=iTime(Symbol(),timeFrame,0))
      {
      _sTime[i] = iTime(Symbol(),timeFrame,0);
      res=true;
      }
      
//----
   return(res);   
   }
whroeder1
13619
whroeder1 2014.04.04 13:21  
GumRai:

nondisclosure:
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not? The code I have doesn't work anymore.
Show the code that you have. I can't think of any reason why it would not work if it worked before
I agree, if it was good before, it should still be.
void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 
     : // Once per bar
   }
   : // every tick
}
Note the above does not handle chart changes
Michael
496
Michael 2014.05.14 00:27  
WHRoeder:
I agree, if it was good before, it should still be.
Note the above does not handle chart changes


Hey WHRoeder, been meaning to ask:

This may be the stroke talking but does this line of code :

bool isNewBar = timeCur != timePre;

mean the same same thing as these lines of code:

if (timeCur != timePre) isNewBar=true;
else isNewBar=false;
Michael
496
Michael 2014.05.14 01:03  
nondisclosure:


Hey WHRoeder, been meaning to ask:

This may be the stroke talking but does this line of code :

mean the same same thing as these lines of code:

New problem with the code. When the first tick comes in after the code is launched, it thinks there's a new candle even though there isn't.
qjol
3243
qjol 2014.05.14 01:18  
nondisclosure:
New problem with the code. When the first tick comes in after the code is launched, it thinks there's a new candle even though there isn't.


then make it happen exactly the way you want it


void OnTick(){
   static datetime timeCur = Time[0]; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ 
     : // Once per bar
   }
   : // every tick
}
whroeder1
13619
whroeder1 2014.05.14 15:27  
  1. nondisclosure:

    his may be the stroke talking but does this line of code :

    bool isNewBar = timeCur != timePre;

    mean the same same thing as these lines of code:

    if (timeCur != timePre) isNewBar=true;
    else isNewBar=false;
    if (timeCur != timePre) isNewBar=true;
    else                    isNewBar=false;
    if (condition) isNewBar=condition;
    else           isNewBar=condition;
    isNewBar=timeCur != timePre;
    isNewBar=condition;
    Think about it.
  2. qjol: then make it happen exactly the way you want it
       static datetime timeCur = Time[0]; datetime timePre = timeCur; timeCur=Time[0];
    This won't compile. Must be initialized with a constant.

  3. nondisclosure: New problem with the code. When the first tick comes in after the code is launched, it thinks there's a new candle even though there isn't.
    You have to code it properly.
    datetime time0; int init(){ time0 = Time[0];  // No mid bar allowed.
       :
    }
    int start(){
       bool isNewBar = time0 != Time[0]; time0 = Time[0];
  4. or
    datetime time0; int init(){ 
      time0 = Time[0];                           // No mid bar allowed unless it's
      if(TimeCurrent()-time0 < _Period) time0=0; // less than 1.7% of a new candle.
      :
    }
    int start(){
       bool isNewBar = time0 != Time[0]; time0 = Time[0]; 
Alain Verleyen
Moderator
28606
Alain Verleyen 2014.05.14 22:46  
WHRoeder:

  1. qjol: then make it happen exactly the way you want it
    static datetime timeCur = Time[0];
    This won't compile. Must be initialized with a constant.

Yes it compiles. Already written, already answered.
whroeder1
13619
whroeder1 2014.05.15 11:18  
angevoyageur: Yes it compiles. Already written, already answered.
  1. I missed the other post.
  2. It shouldn't compile, non-constant expression.
    Static Variables - MQL4 Documentation
    A static variable can be initialized by a constant or constant expression corresponding to its type, unlike a simple local variable, which can be initialized by any expression.
    Initialization of Variables - MQL4 Documentation
    Global and static variables can be initialized only by a constant of the corresponding type or a constant expression. Local variables can be initialized by any expression, not just a constant.

Alain Verleyen
Moderator
28606
Alain Verleyen 2014.05.15 13:03  
WHRoeder:
  1. I missed the other post.
  2. It shouldn't compile, non-constant expression.
    Static Variables - MQL4 Documentation
    A static variable can be initialized by a constant or constant expression corresponding to its type, unlike a simple local variable, which can be initialized by any expression.
    Initialization of Variables - MQL4 Documentation
    Global and static variables can be initialized only by a constant of the corresponding type or a constant expression. Local variables can be initialized by any expression, not just a constant.

I know

It seems Predefined Variables are special cases.

Values of predefined variables are set by the client terminal before a mql4-program is started. Predefined variables are constant and cannot be changed from a mql4-program.

/
To add comments, please log in or register