How to detect a new bar - page 3

To add comments, please log in or register
[Deleted]  
Stephen Njuki:
I use this...

Thanks man

Fabio Fontes
53
Fabio Fontes  
Stephen Njuki:
I use this...

I had many problems using time as a variable and I was looking for a way to use variables bars


thank you, work!


Thiago Duarte
89645
Thiago Duarte  
Mladen Rakic:

Why not simply using something like this :

   static datetime prevTime=0;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               // ...
          }
It should work in all cases (even when there is an error with CopyTime() it avoids a trap)

thank you bro!

Leonardo Joao Dos Santos Ferreira
119
Leonardo Joao Dos Santos Ferreira  
Mladen Rakic:

Why not simply using something like this :

   static datetime prevTime=0;
          datetime lastTime[1];
          if (CopyTime(_Symbol,_Period,0,1,lastTime)==1 && prevTime!=lastTime[0])
          {
               prevTime=lastTime[0];
              
               // ...
          }
It should work in all cases (even when there is an error with CopyTime() it avoids a trap)

Perfect.

I was looking for the same thing.


God bless you

BrianCus
7
BrianCus  

Thanks Leonard, really helpful.

Noticed a few issues when working with iHigh, iLow and iClose directy after so added a quick Sleep(10000) after calling.

void OnTick()
   {
    if(isNewBar())
       {
       Sleep(10000);
       ...
       }
   }

bool isNewBar()
   {
   
    static datetime prevTime = 0;
    datetime lastTime[1];
    if(CopyTime(Symbol(), Period(), 0, 1, lastTime) == 1 && prevTime != lastTime[0])
       {
        prevTime = lastTime[0];
        return(true);
       }
    return(false);
   }
123
To add comments, please log in or register