0

I am learning MQL4 language and am using this Code to plot a Simple moving Average, the Code works fine, but when I load it up on my MT4 it takes a lot of time, am I missing something ?


int start()                         // Special function start()
{
   int i,                           // Bar index
       n,                           // Formal parameter
       Counted_bars;                // Number of counted bars
                                    // Sum of Low values for period
// --------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
   {  
      Buf_0[i]=(iMA(Symbol(),PERIOD_M5,200,i,MODE_EMA,PRICE_HIGH,0);
      i--;                          // Calculating index of the next bar
      }
// --------------------------------------------------------------------
   return;                          // Exit the special funct. start()
   }
// --------------------------------------------------------------------
user3666197
  • 1
  • 6
  • 50
  • 92
karim
  • 3
  • 3

1 Answers1

0

Q : am I missing something?

No, this is a standard feature to process all the Bars back, towards the earliest parts of the history.

If your intentions require a minimum setup-time, it is possible to "shorten" the re-painted part of the history to just, say, last week, not going all the way back all the Bars-number of Bars a few years back, as all that data have been stored in the OHLCV-history database.

That "shortened"-part of the history will this way become as long as your needs can handle and not a single bar "longer".

Hooray, The Problem was solved.


BONUS PART :

Given your code instructs to work with EMA, not SMA, there is one more vector of attack onto the shortest possible time.

For EMA, any next Bar value will become a product of alfa * High[next] added to a previously known as EMA[next+1]

Where a constant alfa = 2. / ( N_period + 1 ) is known and constant across the whole run of the history processed.

This approach helped me gain about ~20-30 [us] FASTER processing for a 20-cell Price-vector, when using this algorithmic shortcut on an array of float32-values compared to cell-by-cell processing. Be sure to benchmark the code for your use-case and may polish further tricks with using different call-signatures of iHigh() instead of accessing an array of High[]-s for any potential speedups, if in utmost need to shave-off any further [us] possible.

user3666197
  • 1
  • 6
  • 50
  • 92