Discussion of article "LifeHack for traders: Fast food made of indicators" - page 12

 
fxsaber:

Standard indicators (they are the only ones discussed in the article) are cached in an elementary way! Because all input parameters are known.

It is difficult only to write a universal hash function. But it was not required in the article. It deals with the simplest case. And even for it there is no hash function.

MACD Sample 4 to 5 MQL4 style.mq5


Without caching

i = 0 Pass = 0 OnTester = 15.070 s.: Count = 9753093, 647186.0 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 build = 1755
i = 1 Pass = 1 OnTester = 14.969 s.: Count = 9753093, 651552.7 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 build = 1755


With stupid caching

i = 0 Pass = 0 OnTester = 11.073 s.: Count = 9753093, 880799.5 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 build = 1755
i = 1 Pass = 1 OnTester = 11.007 s.: Count = 9753093, 886080.9 unit/sec, Agent = C:\Program Files\Alpari Limited MT5\Tester\Agent-127.0.0.1-3000 build = 1755


25% faster if you don't make any wisecracks (head-on). Criticism of "inefficiency" of the presented approach is justified.

 
Can you show me the code?
 
Rashid Umarov:
Can you show the code?
template <typename T>
struct HANDLE
{
private:  
  int Handle;
  
  T Inputs;

public:  
  HANDLE() : Handle(INVALID_HANDLE)
  {
  }
  
  bool operator ==( const T &Inputs2 ) const
  {
    return(this.Inputs == Inputs2);
  }
  
  void operator =( const T &Inputs2 )
  {
    this.Inputs = Inputs2;
  }
  
  int GetHandle()
  {
    return((this.Handle != INVALID_HANDLE) ? this.Handle : (this.Handle = this.Inputs.GetHandle()));
  }
};

template <typename T>
int GetHandle( HANDLE<T> &Handles[], const T &Inputs )
{
  const int Size = ArraySize(Handles);
  
  for (int i = 0; i < Size; i++)
    if (Handles[i] == Inputs)
      return(Handles[i].GetHandle());

  ArrayResize(Handles, Size + 1);
  Handles[Size] = Inputs;
  
  return(Handles[Size].GetHandle());
}

struct MACD
{
  string             symbol;
  ENUM_TIMEFRAMES    period;
  int                fast_ema_period;
  int                slow_ema_period;
  int                signal_period;
  ENUM_APPLIED_PRICE applied_price;
  
  MACD( void )
  {
  }
  
  MACD( const string             &isymbol,
        const ENUM_TIMEFRAMES    &iperiod,
        const int                &ifast_ema_period,
        const int                &islow_ema_period,
        const int                &isignal_period,
        const ENUM_APPLIED_PRICE &iapplied_price ) : 
        symbol((isymbol == NULL) || (isymbol == "") ? _Symbol : isymbol),
        period(iperiod == PERIOD_CURRENT ? ::Period() : iperiod),
        fast_ema_period(ifast_ema_period),
        slow_ema_period(islow_ema_period),
        signal_period(isignal_period),
        applied_price(iapplied_price)     
  {
  }
  
  int GetHandle( void ) const
  {
    return(::iMACD(this.symbol, this.period, this.fast_ema_period, this.slow_ema_period, this.signal_period, this.applied_price));
  }
  
  bool operator ==( const MACD &Inputs ) const
  {
    return((this.symbol == Inputs.symbol) &&
           (this.period == Inputs.period) &&
           (this.fast_ema_period == Inputs.fast_ema_period) &&
           (this.slow_ema_period == Inputs.slow_ema_period) &&
           (this.signal_period == Inputs.signal_period) &&
           (this.applied_price == Inputs.applied_price));
  }  
};

struct MA
{
  string             symbol;
  ENUM_TIMEFRAMES    period;
  int                ma_period;
  int                ma_shift;
  ENUM_MA_METHOD     ma_method;
  ENUM_APPLIED_PRICE applied_price;
  
  MA( void )
  {
  }
  
  MA( const string             &isymbol,
      const ENUM_TIMEFRAMES    &iperiod,
      const int                &ima_period,
      const int                &ima_shift,
      const ENUM_MA_METHOD     &ima_method,
      const ENUM_APPLIED_PRICE &iapplied_price ) :
      symbol((isymbol == NULL) || (isymbol == "") ? _Symbol : isymbol),
      period(iperiod == PERIOD_CURRENT ? ::Period() : iperiod),
      ma_period(ima_period),
      ma_shift(ima_shift),
      ma_method(ima_method),
      applied_price(iapplied_price)
  {
  }
  
  int GetHandle( void ) const
  {
    return(::iMA(this.symbol, this.period, this.ma_period, this.ma_shift, this.ma_method, this.applied_price));
  }
  
  bool operator ==( const MA &Inputs ) const
  {
    return((this.symbol == Inputs.symbol) &&
           (this.period == Inputs.period) &&
           (this.ma_period == Inputs.ma_period) &&
           (this.ma_shift == Inputs.ma_shift) &&
           (this.ma_method == Inputs.ma_method) &&
           (this.applied_price == Inputs.applied_price));
  }  
};

// MT5 variant of iMACD.
int iMACD2( const string             symbol,
            const ENUM_TIMEFRAMES    period,
            const int                fast_ema_period,
            const int                slow_ema_period,
            const int                signal_period,
            const ENUM_APPLIED_PRICE applied_price )
{
  static HANDLE<MACD> Handles[];  
  const MACD Inputs(symbol, period, fast_ema_period, slow_ema_period, signal_period, applied_price);
  
  return(GetHandle(Handles, Inputs));
}            

// MT5 variant of iMA.
int iMA2( const string             symbol,
          const ENUM_TIMEFRAMES    period,
          const int                ma_period,
          const int                ma_shift,
          const ENUM_MA_METHOD     ma_method,
          const ENUM_APPLIED_PRICE applied_price )
{
  static HANDLE<MA> Handles[];  
  const MA Inputs(symbol, period, ma_period, ma_shift, ma_method, applied_price);
  
  return(GetHandle(Handles, Inputs));
}            


In IndicatorsMQL4.mqh add the above code and make the following changes

   int handle=iMACD2(symbol,timeframe,fast_ema_period,slow_ema_period,signal_period,applied_price);
//...
   int handle=iMA2(symbol,timeframe,ma_period,ma_shift,ma_method,applied_price);
 
Rashid Umarov:
I would like an approach of this type - we take OnCalculate out of the indicator, modify it slightly under a new name, add it to the indicator code and now we can pull this function as a bibilio function, passing parameters from the Expert Advisor to it. Right?

I think we can make the source code of any indicator to be connected to the Expert Advisor via an enclode. And then work with them as with a function.

We need to make a little fiddling with macros and other peculiarities. This solution will not cover all indicators. But it should cover most of them.

 

Attempted recovery:

I am posting my version of the measurement. MACD Sample One value at a 5 was taken as the basic version of the Expert Advisor. Small changes were made in it. Since all indicator values are collected in one place, it was not difficult to make a simple macro substitution: I think the conclusions are obvious: when calling indicators in MQL4 mode, the speed is lower by 40%.

 
Yuriy Koptsev:


look at /results/... open - stop is set, then the trawl pulls it over.


here is a section of the chart. and the report on this place. how can it set a stop of 150 pips for almost all lots with ATR * 0.5 (ATR = 80pp)? with such a setup, the stop should NEVER be further than about 40 pips from the opening price of the lot.... and only when it gets above this level, it will be traded on it already.
Files:
 
Didn't get a chance to read it.
 
Updated, man....
 
 
Vasiliy Sokolov:

Recovery attempt:

I am posting my version of the measurement. MACD Sample One value at a 5 was taken as the basic version of the Expert Advisor. Small changes were made in it. Since all indicator values are collected in one place, it was not difficult to make a simple macro substitution: I think the conclusions are obvious: when calling indicators in MQL4 mode, the speed is lower by 40%.

You can restore (edit) your post:

calling indicators in MQL4 mode, the speed is 40% slower.


- You had a beautiful code and description of the measurement results.