Transfer Indicator to EA Code

 

Hi all,

For performance reasons in the backtesting I' trying to transfer an indicator to my EA code.

So reading the Topic on TRANSFERRING AN INDICATOR CODE INTO AN EXPERT ADVISOR CODE. INDICATOR STRUCTURE 

I'm still not getting a good result. 

The custom indicator that i'm trying to transfer is HMA in attachment.

Following the steps I've converted the code to this:

double HMAA ()
 {
   static int IndCounted;
   int    counted_bars=IndCounted;   
   int HMA_Period=12;
   int    limit;
   int    i;
   double     ind_buffer0[];
   double     ind_buffer1[]; 
   double HMAV;
   double HMAB;
   
   if(counted_bars>0)
      counted_bars--;
     {
      for(i=1;i<=HMA_Period;i++) ind_buffer1[Bars-i]=0;
     }
     
   int NewSize = iBars(NULL, PERIOD_CURRENT);
//----  Checking the change of the zero bar
   if(ArraySize(ind_buffer0) < NewSize)
     {
      //---- Set the direct indexing direction in the array
      ArraySetAsSeries(ind_buffer1, false);
      ArraySetAsSeries(ind_buffer0, false);
      
      //---- Change the size of the emulated indicator buffers
      ArrayResize(ind_buffer1, NewSize);
      ArrayResize(ind_buffer0, NewSize);
      
      //---- Set the reverse indexing direction in the array
      ArraySetAsSeries(ind_buffer1, true);
      ArraySetAsSeries(ind_buffer0, true);
      }
      
   int IBARS = iBars(NULL, PERIOD_CURRENT);
   IndCounted = IBARS - 1;
   
   if(counted_bars>0)
      counted_bars--;
      
   limit=Bars-counted_bars;
   
   for(i=1; i<limit; i++)
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
      HMAB= ind_buffer1[i];
      Print("HMAB ",HMAB);               
//---- HMA counted in the 0-th buffer
   for(i=1; i<limit; i++)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);
      HMAV= ind_buffer0[i];
      Print("HMAV ",HMAV);
      
//---- done
   return(HMAV);
      
 }  

I'm getting the correct value for the 

 HMAB= ind_buffer1[i];

but for the 

HMAV= ind_buffer0[i];

I'm always getting zero value. 

Any thoughts? 

Thank you in advance.

Best.

Files:
HMA2.mq4  6 kb
 
Pinto André: F or performance reasons in the backtesting I' trying to transfer an indicator to my EA code.
  1. Don't try to do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)

    Just get the value(s) of the indicator(s) into EA/indicator (using iCustom) and do what you want with it.
              Detailed explanation of iCustom - MQL4 programming forum (2017)


    1. EAs : Don't do per tick that you can do per bar, or on open.
      If you are waiting for a level, don't reevaluate, wait until price reaches it (or a new bar starts, and you recalculate.)
      If you are waiting for an order to close, only look when OrdersTotal (or MT5 equivalent) has changed.
                How to get backtesting faster ? - MT4 - MQL4 programming forum (2017)

    2. Indicators: Code it properly so it only recomputes bar zero (after the initial run.)
                How to do your lookbacks correctly. (2016)
      Or, reduce Tools → Options (control+O) → Charts → Max bars in chart to something reasonable (like 1K.)

 

Hi William

Sometimes the custom indicator buffer for a brief moment return 0 value when it shouldn't and this is a problem when you use it for exit/entry trigger.

Any thoughts in who solve this issue? 

Thank you in advance.

André

 
Pinto André:

Hi all,

For performance reasons in the backtesting I' trying to transfer an indicator to my EA code.

So reading the Topic on TRANSFERRING AN INDICATOR CODE INTO AN EXPERT ADVISOR CODE. INDICATOR STRUCTURE 

I'm still not getting a good result. 

The custom indicator that i'm trying to transfer is HMA in attachment.

Following the steps I've converted the code to this:

I'm getting the correct value for the 

but for the 

I'm always getting zero value. 

Any thoughts? 

Thank you in advance.

Best.

Is this the actual code you are testing with?

 I think you would want to change it to something like this

double HMAA(int shift)
  {
   static int IndCounted;
   int    counted_bars=IndCounted;
   int HMA_Period=12;
   int    limit;
   int    i;
   double     ind_buffer0[];
   double     ind_buffer1[];
   double HMAV;
   double HMAB;

   if(counted_bars>0)
      counted_bars--;

   int NewSize = iBars(NULL, PERIOD_CURRENT);
//----  Checking the change of the zero bar
   if(ArraySize(ind_buffer0) < NewSize)
     {
      //---- Set the direct indexing direction in the array
      ArraySetAsSeries(ind_buffer1, false);
      ArraySetAsSeries(ind_buffer0, false);

      //---- Change the size of the emulated indicator buffers
      ArrayResize(ind_buffer1, NewSize);
      ArrayResize(ind_buffer0, NewSize);

      //---- Set the reverse indexing direction in the array
      ArraySetAsSeries(ind_buffer1, true);
      ArraySetAsSeries(ind_buffer0, true);
     }

   for(i=1; i<=HMA_Period; i++)
      ind_buffer1[Bars-i]=0;

   int IBARS = iBars(NULL, PERIOD_CURRENT);
   IndCounted = IBARS - 1;

   if(counted_bars>0)
      counted_bars--;

   limit=Bars-counted_bars;

   for(i=1; i<limit-1; i++)
     {
      ind_buffer1[i]=iMA(NULL,0,MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
                     iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);
      HMAB= ind_buffer1[i];
      //Print("HMAB ",HMAB);
     }
//---- HMA counted in the 0-th buffer
   HMAV = iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,shift);
//---- done
   return(HMAV);

  }
 

Thank you Vitor! Works as it should.

Regarding William's comment, I'm confused now.

Every post that i've been reading, almost everybody is apologist of including the custom indicator in the actual code of the EA.

For one reason it helps you understand the actual meaning of the indicator, and avoids the repainting issue.

"No way to know if older bars have changed or been added (history update.)" -> Using a professional broker this could happen? I believed that the historical date provided by the brokers should be debbuged.

Again thank you for the tips. 

André P.

 
Pinto André #: "No way to know if older bars have changed or been added (history update.)" -> Using a professional broker this could happen? I believed that the historical date provided by the brokers should be debbuged.

Irrelevant. The default is 2K bars. Then if you scroll back, or download history, you now have more historical data. Most brokers have 32K or 64K.

Your code treats the added bars as new bars. So historical data have no indicator calculations, and you recalculate existing bars.

Reason: