Delay timer for new bar event

 

Hi, 


I am learning to code in MQl4 and i am trying to piece something together. At this point i am writing small pieces of code and testing them out by just printing or alerting messages.

I have no previous coding experience so everything is still new to me, i did follow a online course on programming. 

@ this moment i am kind of stuck and i am searching for a way to delay a new bar event by a few seconds in order to have other variabels to have completed, now that happen simultaneously and this causes trouble. 


I am looking to indentify a cci hook by comparing the cci value of 1 period back with the value of 2 periods and 3 periods back in combination with a new bar event. The new bar event only happens 1 bar later. If i could delay the new bar event by a few seconds then my signal can happen much faster.

I cant find a delay timer function in the mql4 language, should i write one myself using a for loop? 


This is the code that i wrote to indentify a bearish cci hook, later i will combine it with a cci level it has to be above and a new bar event.

void OnTick()
{  

double firstCci = iCCI (NULL,0,14,PRICE_TYPICAL,2);
double secondCci = iCCI (NULL,0,14,PRICE_TYPICAL,1);
double thirdCci = iCCI (NULL,0,14,PRICE_TYPICAL,0);

if (firstCci < secondCci && secondCci > thirdCci && NewBar())
   { 
     Alert ( "bearish hook"+ Symbol());
   }

if (NewBar())

     {
     Print (thirdCci, "tirdhtcci",Symbol());
     }


}



This is the code that i found here for a new bar event 

bool NewBar()

  {

   static datetime lastbar;

   datetime curbar = Time[0];

   if(lastbar!=curbar)

     {

      lastbar=curbar;

      return (true);

     }

   else

     {

      return(false);

     }

  }

This is my attempt to get a delayed new bar event by 10 seconds, but obviously it didn't work :)

bool NewBarDelayed()



{  

  static datetime lastbartemp;

static datetime lastbar = lastbartemp +10;

   datetime curbartemp = Time [0];

   datetime curbar = curbartemp +10;

   

  

  

   if(lastbar!=curbar)

     {

      lastbar=curbar;

      return (true);

     }

   else

     {

      return(false);

     }

  }


Anyone can help me with this? 


Thanks in advance,

Sitting Duck

 
Please edit your post and use the code button (Alt+S) when pasting code.
EDIT your original post, please do not just post the code correctly in a new post.
 

Hi Keith, 


I changed it, sorry wasn't aware of this rule.


Best regards

Sitting Duck

 
sitting_duck: This is my attempt to get a delayed new bar event by 10 seconds, but obviously it didn't work :)

  if(lastbar!=curbar)

Code fails if there is no tick during that specific second. Try:

#define  MAX_DATETIME   D'3000.12.31 23:59:59'
bool NewBarDelayed(){
  static datetime delayed = MAX_DATETIME;

  if(NewBar()) delayed = Time[0]+10;
  bool isNow = TimeCurrent() >= delayed;
  delayed = MAX_DATETIME;   return isNow; 
}
 
I would suggest to use LONG_MAX as a replacement for MAX_DATETIME

datetime is a typedef of long.


 
Dominik Egert: I would suggest to use LONG_MAX as a replacement for MAX_DATETIME

datetime is a typedef of long.

It may be a uLong, but the maximum datetime is in the documentation and is much less than LONG_MAX.

 
Dominik Egert: I would suggest to use LONG_MAX as a replacement for MAX_DATETIME. datetime is a typedef of long.

No, that would give an invalid datetime!

Print("Date: ", (datetime) LONG_MAX); // Result in log "Date: wrong datetime"
 
Yes, this is true. The internal interpreter does not acknowledge that value to be valid.

Still, for the purpose used, it is valid as a comparison value.

Datetime is long, not ulong. You can have negative datetime values and you can assign long vars directly to datetime vars without complain. Ulong gets a warning.

Datetime is a signed 64 bit integer, therefore LONG_MAX should be sufficient to be used as the maximum value which can be represented by this data type.

At least as much as I understand the above code and it's use purpose.

Yes, it can be made a case, the data type datetime has some specialty attached to it and therefore it might be syntactically correct to use a custom definition of a max value. On the other hand, the underlying data type is a fundamental type and it can be made the case to stick to raw machine interpretation.

I would say, depends on how you approach MQL at a whole, probably.

To me datetime is a helper type used by the API and implemented into mql for various reasons. Still, mql is derived from a C/C++ compiler and therefore I like to stick to the fundamentals, if possible.

 

Interesting behaviour after investigating this a little more in depth.


void testfunc_dt()
{
    // Internal test
        datetime begin;
        datetime end;
        datetime next;
        
        TestDatetime(begin, end, next);

};


void TestDatetime(ulong& begin, ulong& end, ulong& next)
{ return; };

accepted without warnings.


As well as this:

void testfunc_ulong()
{
    // Internal test
        long begin;
        long end;
        long next;
        
        TestDatetime(begin, end, next);

};


void TestDatetime(ulong& begin, ulong& end, ulong& next)
{ return; };



And this:

void testfunc_dt()
{
    // Internal test
        datetime begin;
        datetime end;
        datetime next;
        
        TestDatetime(begin, end, next);

};


void TestDatetime(long& begin, long& end, long& next)
{ return; };


Also this is accepted:

void testfunc_dt()
{
    // Internal test
        
ulong begin;
        ulong end;
        ulong next;

};

void TestDatetime(long& begin, long& end, long& next)
{ return; }



So maybe i have derived a wrong conclusion on the datatype datetime. - I am not sure what this is supposed to imply, but it does not seem to be type-safe.

Therefore, as I was using long& as an input type parameter and I could pass in datetime without issues, I assumed, datetime is of type long, not of type ulong.


This gets contradicted by this part of code:

    // Internal test
        ulong begin;
        long end;

        datetime test1 = begin;
        datetime test2 = end;


While following code is no problem (for the compiler):

    // Internal test
        ulong begin = NULL;
        long  end   = NULL;

        datetime test1 = begin;
        datetime test2 = NULL;
        test1 = -1;      
	test1 = ULONG_MAX;  
        test2--;
	test2 = LONG_MAX;

	test1 = (ulong)0xFFFFFFFFFFFFFFFF;
	test2 = (long)0x7FFFFFFFFFFFFFFF;


To be honest I am confused now....

Reason: