Cascading iMA with iRSI

 
I want to calculate the EMA of the RSI with using handles as much as possible. So I just create one iRSI handle, one iMA handle which takes the Handle_RSI of course but then I do not know how to print only the iMA(iRSI) on the screen
//+------------------------------------------------------------------+
//|                                        Custom Moving Average.mq5 |
//|                   Copyright 2009-2020, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2020, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
//--- input parameters
input int            InpMAPeriod_RSI=13;         // Period
input int            InpMAPeriod=13;         // Period
input int            InpMAShift=0;           // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_EMA;  // Method
//--- indicator buffer
double buffer_EMA[];
int handle_EMA=0;
double buffer_RSI[];
int handle_RSI=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,buffer_EMA,INDICATOR_DATA);
//--- indicator buffers mapping
   SetIndexBuffer(1,buffer_RSI,INDICATOR_CALCULATIONS);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- set first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);
//--- line shifts when drawing
   PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);
//--- name for DataWindow
   string short_name;
   switch(InpMAMethod)
     {
      case MODE_EMA :
         short_name="EMA";
         break;
      case MODE_LWMA :
         short_name="LWMA";
         break;
      case MODE_SMA :
         short_name="SMA";
         break;
      case MODE_SMMA :
         short_name="SMMA";
         break;
      default :
         short_name="unknown ma";
     }
   IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod)+")");
//--- set drawing line empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
   
   handle_RSI=iRSI(_Symbol, _Period, InpMAPeriod_RSI, PRICE_CLOSE);
   handle_EMA=iMA(_Symbol, _Period, InpMAPeriod, InpMAShift, InpMAMethod, handle_RSI);
  }
//+------------------------------------------------------------------+
//|  Moving Average                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
      {
       IndicatorRelease(handle_EMA);
       IndicatorRelease(handle_RSI);
      }
//+------------------------------------------------------------------+
//|  Moving Average                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   if(rates_total<InpMAPeriod-1+begin)
      return(0);
//--- first calculation or number of bars was changed
//double buffer_RSI[];
int pos_buffer_RSI=0;
int cpt_buffer_RSI=0;
int nb_val_RSI=InpMAPeriod;
int start_buffer_RSI=0;
//double buffer_EMA[];
int pos_buffer_EMA=0;
int cpt_buffer_EMA=0;
int nb_val_EMA=1;
int start_buffer_EMA=0;
cpt_buffer_RSI= CopyBuffer(handle_RSI, pos_buffer_RSI, start_buffer_RSI, nb_val_RSI, buffer_RSI);
cpt_buffer_EMA= CopyBuffer(handle_EMA, pos_buffer_EMA, start_buffer_EMA, nb_val_EMA, buffer_EMA);
if(cpt_buffer_EMA < nb_val_EMA) return(0);
if(cpt_buffer_RSI < nb_val_RSI) return(0);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Enaud:

I want to calculate the EMA of the RSI with using as handles as much as possible. So I just create one iRSI handle, one iMA handle which takes the Handle_RSI of course but then I do not know how to print only the iMA(iRSI) on the screen


Does this line help you?

PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_NONE);
 

One thing to mention:

This is not how you calculate EMA of RSI.

You are calculating EMA and RSI.

 

are you sure it's not the proper way. I do give the Handle_RSi to the iMA like this



   handle_RSI=iRSI(_Symbol, _Period, InpMAPeriod_RSI, PRICE_CLOSE);
   handle_EMA=iMA(_Symbol, _Period, InpMAPeriod, InpMAShift, InpMAMethod, handle_RSI);


then I retrieve the right amount of values of the RSI in order to calculate its MA



cpt_buffer_RSI= CopyBuffer(handle_RSI, pos_buffer_RSI, start_buffer_RSI, nb_val_RSI, buffer_RSI);
cpt_buffer_EMA= CopyBuffer(handle_EMA, pos_buffer_EMA, start_buffer_EMA, nb_val_EMA, buffer_EMA)

so normally the buffer_EMA should be filled with the latest value of the iMA of the RSi, at each call of OnCalculate.

 
Enaud #:

are you sure it's not the proper way. I do give the Handle_RSi to the iMA like this



then I retrieve the right amount of values of the RSI in order to calculate its MA


so normally the buffer_EMA should be filled with the latest value of the iMA of the RSi, at each call of OnCalculate.

You are right. I made a mistake.

 
bump
 
Enaud #:
bump

Does the problem persist?

 
yes and i don't know what to change to make it work.
 

Okay i redid the code

this time i changed the required number of bars from copybuffer to be rates_total. It outputs a curve which looks like the right thing.


//+------------------------------------------------------------------+
//|                                               handle.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  Red
//--- input parameters
input int            InpMAPeriod_RSI=14;         // Period_RSI
input int            InpMAPeriod=13;         // Period
input int            InpMAShift=0;           // Shift
input ENUM_MA_METHOD InpMAMethod=MODE_SMMA;  // Method
//--- indicator buffer
int handle_ima=0;
double buffer_ima[];
int handle_rsi=0;
double buffer_rsi[];
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
 EventKillTimer();
 IndicatorRelease(handle_ima);
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
 handle_rsi=iRSI(_Symbol,_Period, InpMAPeriod_RSI, PRICE_CLOSE);
//--- indicator buffers mapping
 handle_ima=iMA(_Symbol,_Period, InpMAPeriod, InpMAShift, InpMAMethod, handle_rsi);
//--- indicator buffers mapping
 SetIndexBuffer(0,buffer_ima,INDICATOR_DATA);
//--- set accuracy
 IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- set first bar from what index will be drawn
 PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpMAPeriod);
//--- line shifts when drawing
 PlotIndexSetInteger(0,PLOT_SHIFT,InpMAShift);
//--- name for DataWindow
 string short_name;
 switch(InpMAMethod)
 {
 case MODE_EMA :
  short_name="EMA of RSI";
  break;
 case MODE_LWMA :
  short_name="LWMA of RSI";
  break;
 case MODE_SMA :
  short_name="SMA of RSI";
  break;
 case MODE_SMMA :
  short_name="SMMA of RSI";
  break;
 default :
  short_name="unknown ma";
 }
 IndicatorSetString(INDICATOR_SHORTNAME,short_name+"("+string(InpMAPeriod)+")");
//--- set drawing line empty value
 PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//---
 return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
{
//---
 int cnt_rsi=0;
 int required_rsi=rates_total;
 int start_rsi=0;
 int position_buffer_rsi=0;
 cnt_rsi=CopyBuffer(handle_rsi, position_buffer_rsi, start_rsi, required_rsi, buffer_rsi);
 if(cnt_rsi<required_rsi)return(0);
//---
 int cnt_ima=0;
 int required_ima=rates_total;
 int start_ima=0;
 int position_buffer_ima=0;
 cnt_ima=CopyBuffer(handle_ima, position_buffer_ima, start_ima, required_ima, buffer_ima);
 if(cnt_ima<required_ima)return(0);
 
 
 //Print(buffer_ima[0]);
 for(int i=prev_calculated; i<rates_total; i++)
 {
  Print(i, "  ", buffer_ima[i]);
 }
//--- return value of prev_calculated for next call
 return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
//---
}
//+------------------------------------------------------------------+
Reason: