Download MetaTrader 5
To add comments, please log in or register
Added support for the Spanish language. Come and check it out!
Tosh5457
223
Tosh5457 2014.11.21 02:12 

Hello, I got an indicator that is working fine, but I want to reproduce it to a script, and in the scripts I'm getting wrong values. It's a kind of a moving average.

 In the indicator, ind_buffer0 will have the moving average values. Indicator code

int start()
  {
   int limit,i;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<1)
     {
      for(i=1;i<=draw_begin0;i++) ind_buffer0[Bars-i]=0;
      for(i=1;i<=HMA_Period;i++) ind_buffer1[Bars-i]=0;
     }
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- MA difference counted in the 1-st buffer
   for(i=0; 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);
//---- HMA counted in the 0-th buffer
   for(i=0; i<limit; i++)
      ind_buffer0[i]=iMAOnArray(ind_buffer1,0,MathFloor(MathSqrt(HMA_Period)),0,MODE_LWMA,i);
//---- done
   return(0);
  }

 

Script code 

//+------------------------------------------------------------------+
//|                                                          HMA.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

#define HISTORY 1000
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   Alert(DoubleToStr(HMA(21), 5));
   
  }
//+------------------------------------------------------------------+

double HMA(int HMA_Period)
{
   int i;
   double buffer[HISTORY+1];
   for (i = 0; i <= HISTORY; i++)
   {
      buffer[i] = iMA(NULL,0,(int) MathFloor(HMA_Period/2),0,MODE_LWMA,PRICE_CLOSE,i)*2-
      iMA(NULL,0,HMA_Period,0,MODE_LWMA,PRICE_CLOSE,i);   
   }
   
   
   return(iMAOnArray(buffer, 0, (int) MathFloor(MathSqrt(HMA_Period)), 0, MODE_LWMA, 0));
}

 

For a given symbol, I'm getting 184.9759 value in the indicator, and ~182.14 in the script, which is way off from the close price. Can't find what I'm doing wrong.. Might have something to do with HISTORY, since if I change that value, the value I get also changes.

Carl Schreiber
5934
Carl Schreiber 2014.11.21 07:55  

I didn't look detailed at your script-code.

But a script is executed only once.

Why don't you create a duplicate of the indicator with an extra buffer for your function.

Then you can modify until the lines are one above the other and you can use the debugger to see where and why it differs.

But be aware sometimes the debugger button is greyed and isn't working (I don't know why, I have told the service - no answer so far).

In this case just save the indicator under a different name and its working again.

whroeder1
13596
whroeder1 2014.11.21 14:54  
Jasus5457: , I got an indicator that is working fine, but I want to reproduce it to a script, and in the scripts I'm getting wrong values.
  1. Set your buffer to asSeries before filling it, so OnArray knows that zero is the last entry instead of the first.
  2. Don't duplicate code, just get the value from the indicator. Detailed explanation of iCustom - MQL4 forum
gooly: Why don't you create a duplicate of the indicator with an extra buffer for your function.
Because there are no buffers (auto-sizing auto-initializing, series arrays) and no IndicatorCounted in scripts.
Carl Schreiber
5934
Carl Schreiber 2014.11.21 15:20  
WHRoeder:
  1. Because there are no buffers (auto-sizing auto-initializing, series arrays) and no IndicatorCounted in scripts.

??

That's why I suggested an indicator-dublicate with an add. buffer (and not a script) what I did not mention explicetly is to paste his indi.-function in the dublicate-indicator and fill the add. buffer by his function results.

Not possible?

/
To add comments, please log in or register