MQL 4 isNewInterval isNewCandle isNthSecond

To add comments, please log in or register
Ercan Guler
37
Ercan Guler  

I was recently looking for some code like new candle but loads at first run then waits for new candle to run indicator faster... and accurate..

But during my tryouts come of wit this solution Time frame independent.

Let me share and what you think?

      //--------------------------------------------------------------------------------------------------------------||
      // isNewCandle()
      //--------------------------------------------------------------------------------------------------------------||  
       
      bool isNewCandle()
      {
      //--- memorize how many bars
         static bool Once=false;
         static int BarsOnChart=0;

         if((Bars != BarsOnChart ) || !Once) { 
         BarsOnChart=Bars; Once=true; return (true); } // 
         
      return(false);
      } // END isNewCandle       
      
      //--------------------------------------------------------------------------------------------------------------||
      // isNthSecond
      //--------------------------------------------------------------------------------------------------------------||  

      bool isNthSecond()
      {
         int SecsOnChart=Seconds();
         
         if(isSecond(SecsOnChart, 0, Frequency))
         return(true); 
            
      return(false);      
      } // END isNthSecond
       
      //--------------------------------------------------------------------------------------------------------------||
      // isSecond
      //--------------------------------------------------------------------------------------------------------------||  

      bool isSecond(int sec, int Min, int Max)
        {        
         static bool Once=false;
         
         if((sec <= Max && sec >=Min ) || !Once) { 
         Once=true; return (true); } //
         
         return(false);
       } // END isSecond  
William Roeder
20207
William Roeder  
  1. Indicators don't need any of that, they should only calculate the latest tick and always be up to date.
              How to do your lookbacks correctly.

  2. 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 and MetaTrader 4 - 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 and MetaTrader 4 - MQL4 programming forum

Ercan Guler
37
Ercan Guler  
I understand that but what Iam trying to figure out is  for ex.. 4 hrs chart some function run ever 1 minute.. for only one tick... it can wair for a tick if it takes more than a minute but not less.. thank you anyeays
William Roeder
20207
William Roeder  
Ercan Guler ex.. 4 hrs chart some function run ever 1 minute.. for only one tick... it can wair for a tick if it takes more than a minute but not less..

So code exactly that:

void OnTick(){
   static datetime perMinute = 0;
   datetime now = TimeCurrent();
   if(now > perMinute){ perMinute = now + 60; ...
Is that so hard?
Ercan Guler
37
Ercan Guler  
lol thank you.... sometimes ones do complicating stuff cant see simple thing front of us.. Thank you :)
Ercan Guler
37
Ercan Guler  
Is void tick working with custom indicator.. well I tried.. it didnt.. maybe I did something wrong but I used your code like this instead..  Thank you 
      //--------------------------------------------------------------------------------------------------------------||
      // isTickNthSec
      //--------------------------------------------------------------------------------------------------------------||  
 
      bool isTickNthSec(){
      
      static datetime perMinute = 0;
      datetime now = TimeCurrent();
      //--- Remember the initial value
      if(GetTickCount()!=0){ 
            if(now > perMinute){ perMinute = now + 30;
            return(true);
            }//---
          }//---
      return(false);
      }//---
William Roeder
20207
William Roeder  
What is the unit of time that GetTickCount returns? What does 30 represent?
Ercan Guler
37
Ercan Guler  

It doesnt matter wht get tick returns (it return some number bigger than zero not really matter what number it is as soon as its bigger than zero) what matters is it ticked so I dont have look for a tick in some intervall.. first get the tick then the interval so its get the data.. seems working..

Let me be more clear 30 is just a number for the TimeCurrent() (I treat time current as a number not a time- time is just another number anyway) as now and işt ws 152547478 bla bla.. Its integer as a number and it raise by like 152151154 etc and when you add etc 30 time cureent will interval till it counts plus 30 but tick got there first so it doesnt have to look for a tick again.. well it works... .. at least strategy tester


Finally.. 30 or 60 its doesnt even have to be time related.. I can write 120 and it will wait for 2 minutes to update again

William Roeder
20207
William Roeder  
Ercan Guler:

It doesnt matter wht get tick returns

I can write 120 and it will wait for 2 minutes to update again

It certainly does. And if you had bothered to read the documentation and answer my question you would know that 120 is not two minutes. It is not one minute. It is not 30 seconds.

Ercan Guler
37
Ercan Guler  
whroeder1:

It certainly does. And if you had bothered to read the documentation and answer my question you would know that 120 is not two minutes. It is not one minute. It is not 30 seconds.

It certainly does << it doesnt because as long as its not zero() it works if clause


ok.. sorry am trying to be clear as possible not rude.. but I tell you it really does work as I say.. its not about with time even it is. ITS working like I explained

It takes at least  2 minutes it to check again if I write 120 or if I write 300 5 minutes.. but the thin is it does as I say I tested now testing live market.. If it doesnt work as I say why Iwould say it.. I read the doc... by the way.. but what am telling you is not related to doc.. its about the result I wanted to get.. thank you

ITS WORKING as I say.. If I write 120  ITS WAITING 2 minutes minimum to check again.. TEST if you like... I tested with chronometer .. Thank you


Why I would say Its working if it doesn't.. with all my respect You should maybe better figure out why it does work as I say even the doc or your logic doesnt say so... 


I have just written on this indicator 5000 thousand lines of code and this was my first time to ask forum something interesting for me.. but yet it was simple... I really thank you...

nicholi shen
2392
nicholi shen  
Ercan Guler:

It certainly does << it doesnt because as long as its not zero() it works if clause


ok.. sorry am trying to be clear as possible not rude.. but I tell you it really does work as I say.. its not about with time even it is. ITS working like I explained

It takes at least  2 minutes it to check again if I write 120 or if I write 300 5 minutes.. but the thin is it does as I say I tested now testing live market.. If it doesnt work as I say why Iwould say it.. I read the doc... by the way.. but what am telling you is not related to doc.. its about the result I wanted to get.. thank you

ITS WORKING as I say.. If I write 120  ITS WAITING 2 minutes minimum to check again.. TEST if you like... I tested with chronometer .. Thank you


Why I would say Its working if it doesn't.. with all my respect You should maybe better figure out why it does work as I say even the doc or your logic doesnt say so... 


I have just written on this indicator 5000 thousand lines of code and this was my first time to ask forum something interesting for me.. but yet it was simple... I really thank you...

No need to over-complicate it... You can just check to see when a new bar forms on your desired timeframe... 

bool IsNewBarOnTimeframe(ENUM_TIMEFRAMES timeframe = PERIOD_CURRENT)
{
   static ENUM_TIMEFRAMES tf = PERIOD_CURRENT;
   static datetime last_bar = 0;
   if(timeframe != tf && timeframe != PERIOD_CURRENT)
   {
      tf = timeframe;
      last_bar = 0;
   }
   datetime curr_bar = (datetime)SeriesInfoInteger(_Symbol,tf,SERIES_LASTBAR_DATE);
   if(last_bar == 0)
   {
      last_bar = curr_bar;
      return false;
   }
   if(last_bar != curr_bar)
   {
      last_bar = curr_bar;
      return true;
   }
   return false;
}
12
To add comments, please log in or register