EA Buffers not matching indicator data window values (AMA Smoothe)

 
Hi,

I'm having issues matching up data from EA buffers to indicator data window values. The indicator I'm using is the AMA Smoothe by mladen (which is really cool).

You'll see from the below window that I printed the values from a backtest into the journal window. levu should correspond to Upper and levd should correspond to lower.

Would you guys have any idea what is going wrong please? Screenshot and code is as follows, with the indicator attached:


Thanks.



Here is my simple code to test it

//+------------------------------------------------------------------+
//|                                                        test5.mq5 |
//|                                  Copyright 2022, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

input group    "=======>AMA Smoothed RSI";
input int                inpRsiPeriod  = 14;          // RSI period
input int                inpPeriod     = 14;          // AMA Period
input int                inpFastPeriod =  2;          // AMA Fast end period
input int                inpSlowPeriod = 30;          // AMA Slow end period
input ENUM_APPLIED_PRICE inpPrice      = PRICE_CLOSE; // Price
enum enColorChangeOn
{
   cchange_onSlope,  // Change color on slope change
   cchange_onLevels, // Change color on outer levels cross
   cchange_onZero    // Change color on middle level cross
};
input int                inpFlPeriod    = 25;               // Floating levels period
input double             inpFlLevelUp   = 90;               // Floating levels up %
input double             inpFlLevelDn   = 10;               // Floating levels down %
input enColorChangeOn    inpColorChange = cchange_onLevels; // Color changing mode



//AMA Smoothed RSI Variables
double amasmoothe[];
double val[],valc[],prices[],levu[],levd[],levm[],rsi[];
int amasmootheHandle;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//AMA Smoothed RSI


   //ArraySetAsSeries(valc, true);
   //ArraySetAsSeries(prices, true);
   //ArraySetAsSeries(rsi, true);
   amasmootheHandle = iCustom(_Symbol,PERIOD_M1,"AMA Smoothed RSI (fl)",inpRsiPeriod,inpPeriod,inpFastPeriod,inpSlowPeriod,inpPrice,inpFlPeriod,inpFlLevelUp,inpFlLevelDn,inpColorChange);

   ChartIndicatorAdd(ChartID(), 1, amasmootheHandle);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   ArraySetAsSeries(levu, true);
   ArraySetAsSeries(levm, true);
   ArraySetAsSeries(levd, true);
   ArraySetAsSeries(val, true);
   CopyBuffer(amasmootheHandle,0,0,1,levu);
   CopyBuffer(amasmootheHandle,1,0,1,levm);
   CopyBuffer(amasmootheHandle,2,0,1,levd);
   CopyBuffer(amasmootheHandle,3,0,1,val);

   Print( "val0 " + val[0] + " levu " + levu[0] + " levd " + levd[0]);
   
  }
//+------------------------------------------------------------------+

Files:
 

Here is the correct code:

//+------------------------------------------------------------------+
//|                 iCustom AMA smoothed RSI (fl) value on chart.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property version   "1.000"
#property tester_indicator "AMA smoothed RSI (fl)"
//--- input parameters
input int                inpRsiPeriod  = 14;          // RSI period
input int                inpPeriod     = 14;          // AMA Period
input int                inpFastPeriod =  2;          // AMA Fast end period
input int                inpSlowPeriod = 30;          // AMA Slow end period
input ENUM_APPLIED_PRICE inpPrice      = PRICE_CLOSE; // Price
enum enColorChangeOn
  {
   cchange_onSlope,  // Change color on slope change
   cchange_onLevels, // Change color on outer levels cross
   cchange_onZero    // Change color on middle level cross
  };
input int                inpFlPeriod    = 25;               // Floating levels period
input double             inpFlLevelUp   = 90;               // Floating levels up %
input double             inpFlLevelDn   = 10;               // Floating levels down %
input enColorChangeOn    inpColorChange = cchange_onLevels; // Color changing mode
//---
int      handle_iCustom;                     // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMACD ([data folder]\MQL5\Indicators\AMA smoothed RSI (fl).mq5)
   handle_iCustom=iCustom(Symbol(),Period(),"AMA smoothed RSI (fl)",
                          inpRsiPeriod,
                          inpPeriod,
                          inpFastPeriod,
                          inpSlowPeriod,
                          inpPrice,
                          inpFlPeriod,
                          inpFlLevelUp,
                          inpFlLevelDn,
                          inpColorChange);
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the 'AMA smoothed RSI (fl)' indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iCustom!=INVALID_HANDLE)
      IndicatorRelease(handle_iCustom);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double levu[],levm[],levd[],val[];
   ArraySetAsSeries(levu,true);
   ArraySetAsSeries(levm,true);
   ArraySetAsSeries(levd,true);
   ArraySetAsSeries(val,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,levu) || !iGetArray(handle_iCustom,1,start_pos,count,levm) ||
      !iGetArray(handle_iCustom,2,start_pos,count,levd) || !iGetArray(handle_iCustom,3,start_pos,count,val))
     {
      return;
     }
//---
   string text=" Upper level | Middle level | Lower level | AMA smoothed RSI"+"\n";
   for(int i=count-1; i>=0; i--)
     {
      text=text+"#"+IntegerToString(i)+
           " | "+DoubleToString(levu[i],Digits()+1)+
           " | "+DoubleToString(levm[i],Digits()+1)+
           " | "+DoubleToString(levd[i],Digits()+1)+
           " | "+DoubleToString(val[i],Digits()+1)+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+

Result:


 
Vladimir Karputov #:

Here is the correct code:

Result:


Thank you Vladimir. This works perfectly!