Hull Moving average of RSI, displays incorrect values on chart

 

What I am trying to do is to have an indicator that will calculate Hull MA of RSI to use in EA. I am very new to MQL5 and not a good programmer in general, so I found this Hull MA script in code library that calculates Hull MA and was trying to substitute price data for RSI values. Here is the original code https://www.mql5.com/en/code/25257.

What I've done with the code is added RSI handle and changed that line that calculates Hull MA value:

#property copyright "© mladen, 2019"
#property link      "mladenfx@gmail.com"
//------------------------------------------------------------------
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Hull"
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
#property indicator_width1  2

//
//
//
//
//

input int                inpPeriod  = 20;          // Period
input int                rsiPeriod  = 618;          // RSI Period
input double             inpDivisor = 2.0;         // Divisor ("speed")
input ENUM_APPLIED_PRICE inpPrice   = PRICE_CLOSE; // Price

double val[],valc[];

int rsiHandle;
double rsiVal[];

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//

int OnInit()
{
   SetIndexBuffer(0,val,INDICATOR_DATA);
      iHull.init(inpPeriod,inpDivisor);
         IndicatorSetString(INDICATOR_SHORTNAME,"Hull ("+(string)inpPeriod+")");
         
         
         
   rsiHandle = iRSI(_Symbol,_Period,rsiPeriod,inpPrice);
   
   if(rsiHandle<0)
   {
    Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
   }
   
   return (INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
IndicatorRelease(rsiHandle);
}

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
   ArraySetAsSeries(rsiVal, true);
   
   if(CopyBuffer(rsiHandle,0,0,inpPeriod,rsiVal)<0)
   {
     Alert("Error copying indicator Buffers - error:",GetLastError(),"!!");
   }
  
   int i= prev_calculated-1; if (i<0) i=0; for (; i<inpPeriod && !_StopFlag; i++)
   {
      //val[i]  = iHull.calculate(getPrice(inpPrice,open,high,low,close,i),i,rates_total);
      val[i]  = iHull.calculate(rsiVal[i],i,rates_total);
      double x=val[i];
   }
   return(i);
}

When debugging, val[i] returns correct Hull MA value, however if applied to chart its all wrong. Most of the times it equals 0. It doesn't plot the graph, but i dont need it to, I simply need the value.

I would really appreciate your help.

Hull moving average
Hull moving average
  • www.mql5.com
Hull moving average
 
#property indicator_separate_window
#property indicator_buffers 2
input int                rsiPeriod  = 14;          // RSI Period
SetIndexBuffer(0,val,INDICATOR_DATA);
SetIndexBuffer(1,rsiVal,INDICATOR_CALCULATIONS);
   //ArraySetAsSeries(rsiVal, true);
   
   if(CopyBuffer(rsiHandle,0,0,rates_total,rsiVal)<0)
   {
     Alert("Error copying indicator Buffers - error:",GetLastError(),"!!");
   }
  
   int i= prev_calculated-1; if (i<0) i=inpPeriod; for (; i<rates_total && !_StopFlag; i++)
   {
      //val[i]  = iHull.calculate(getPrice(inpPrice,open,high,low,close,i),i,rates_total);
      val[i]  = iHull.calculate(rsiVal[i],i,rates_total);
      //double x=val[i];
   }
It is not a good practice to copy everything every time.
 
Nagisa Unada #:
It is not a good practice to copy everything every time.

I know, its just I cannot wrap my head around OnCalculate() function. My code works, iHull.calculate with my parameters returns correct value. When debugging x is the Hull of RSI. I don't understand how to make indicator return this value

Files:
2.png  55 kb
 
"val" is a global variable, so it can be used as is within other functions. 
double x=val[i];



Reason: