Accessing variables

 

Quick question: What takes less milliseconds to process?

macd_signal=iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)

OnTick()
{
 Print(macd_signal);
 Print(macd_signal+1);
 Print(macd_signal+2);
 Print(macd_signal+3);
 Print(macd_signal+4);
 Print(macd_signal+5);
}

or

OnTick()
{
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1));
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)+1);
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)+2);
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)+3);
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)+4);
 Print(iMACD(ChartSymbol(),PERIOD_D1,8,21,5,PRICE_TYPICAL,MODE_SIGNAL,1)+5);
}

I'm currently assuming the first one processes quicker, but since a variable needs to accessed for every Print() line in both cases (either the macd_signal or the iMACD(bla, bla) variable) it may make no difference at all...

can anyone put this to bed for me once and for all? ;-) 

 
Route206: Quick question: What takes less milliseconds to process? or I'm currently assuming the first one processes quicker, but since a variable needs to accessed for every Print() line in both cases (either the macd_signal or the iMACD(bla, bla) variable) it may make no difference at all... can anyone put this to bed for me once and for all? ;-) 

Obviously, the first one is the quickest as it is working with an local variable. Doing multiple calls to the iCustom function (which in turn needs needs to access the indicator code) on every line, is much more costly.

But if you want it even faster, only update the MACD variable with iCustom call, once per bar and not on every tick.

 

Hmm ok. That is how i view things too - it's just that i had heard (or read) somewhere that is didn't make a difference. Note this is not iCustom, but iMACD, which is native to MQL4 terminal. Would that make a difference?

as for doing it once per bar: how would you do that? i have OnTick() and OnTimer(), not OnBarOpen() ... which make me thirsty now that i think of that ;-)

 
Route206 #: Hmm ok. That is how i view things too - it's just that i had heard (or read) somewhere that is didn't make a difference. Note this is not iCustom, but iMACD, which is native to MQL4 terminal. Would that make a difference? as for doing it once per bar: how would you do that? i have OnTick() and OnTimer(), not OnBarOpen() ... which make me thirsty now that i think of that ;-)

iCustom or iMACD — they are still function calls with underlying functionality that needs to be processed on every call. A local variable will always be faster.

Below is an example of detecting a new bar. Sample untested, uncompiled code to serve as an example of possible implementation in an Expert Advisor.

// Tick Event Handler
void OnTick(void)
{
   // Detect new bar condition
      static datetime dtBarCurrent  = WRONG_VALUE;
             datetime dtBarPrevious = dtBarCurrent;
                      dtBarCurrent  = (datetime) SeriesInfoInteger( _Symbol, _Period, SERIES_LASTBAR_DATE );
             bool     bBarNew       = ( dtBarCurrent != dtBarPrevious );

   // Execute only on a new bar condition
      if( bBarNew )
      {
         // Do something here
      };
};
 
Fernando Carreiro #:

iCustom or iMACD — they are still function calls with underlying functionality that needs to be processed on every call. A local variable will always be faster.

Below is an example of detecting a new bar. Sample untested, uncompiled code to serve as an example of possible implementation in an Expert Advisor.

Ah i see, thanks for that Fernando - this code does make it chart (ie timeframe) dependent, not ideal for me. I would probably go for something like:

OnTick()
{
 static int hr_prev;
        int hr=TimeHour(TimeLocal());
                        
 if(hr>hr_prev)
   {
    //do something
   }
 else
   {
    hr_prev=hr;
   }
}

this would allow for doing something every day, hour, minute etc.

 
Route206 #: Ah i see, thanks for that Fernando - this code does make it chart (ie timeframe) dependent, not ideal for me. I would probably go for something like:

this would allow for doing something every day, hour, minute etc.

Your code has a few logic bugs in my opinion! Have a look at the following and see if you can understand why that is.

OnTick()
{
   static int hr_prev = WRONG_VALUE;
   int hr = TimeHour( TimeCurrent() );
                        
   if( hr != hr_prev )
   {
      hr_prev = hr;    
      //do something
   };
};
 
Fernando Carreiro #:

Your code has a few logic bugs in my opinion! Have a look at the following and see if you can understand why that is.

Yeah the ">" is an issue for when the day moves into the next (24 --> 1)

and while i'm not familiar with 

WRONG_VALUE

other than that i know it equals '-1', i do like your

 if(hr!=hr_prev)

as that saves having to do an 'else' statement.

Unless you want to prevent the code to run when market is closed, i'm not sure why TimeCurrent() needs to replace TimeLocal(). 

 
Route206 #:

While i'm not familiar with 

other than that i know it equals '-1', i do like your

as that saves having to do an 'else' statement.

Unless you want to prevent the code to run when market is closed, i'm not sure why TimeCurrent() needs to replace TimeLocal(). 

Then again, both work i think ?

  1. You should always initialise the variable to some value that you know will never be of a valid content. Since hours are between 0 and 23, then -1 is an obvious choice and using "Wrong Value" is self documented. If you don't initialise the variable it could potentially have "valid" content that could cause logic problems.
  2. " != " instead of ">" because when the hour goes beyond 23 and back to 0, then your condition will no longer be valid and no longer triggered.
  3. The OnTick event handler, is only called when a new tick arrives. It will never be called if the market is closed. However, local time is on a different zone with different daylight savings time changes at different dates or the user could simply make corrections to the local time and then your code would fail. Stick to the trade server time, as that is your data reference, not your local time.
 

Thanks  - fully agree on 1. and 2.

As for LocalTime()... i use it a lot w/o incident or hickup. i find it easier to execute trades at market open when using my 'own' time. I guess with Server time, combined with GMT and Daylight Saving corrections the same can be achieved..

Reason: