how do I usde MA whith rsi as the aplied price and refer to past events?

 

Hi I am pretty new to coding.


I am trying to take the moving average of the RSI.

I tried it tis way:

double r = iRSI(NULL,0,rsiPeriod,0,0);    


double fastMA1 = iMA(NULL,0,lengthrsifast,0,MODE_SMA,r,0);

double slowMA1 = iMA(NULL,0,lenghtrsislow,0,MODE_SMA,r,0);


but it is not working returns 0.0.

and the other problem I have is how do I refere to a own created value like

double slowMA = (r + r + iRSI(NULL,0,rsiPeriod,0,1) + iRSI(NULL,0,rsiPeriod,0,2) + iRSI(NULL,0,rsiPeriod,0,3) + iRSI(NULL,0,rsiPeriod,0,4) + iRSI(NULL,0,rsiPeriod,0,5))/7;

and check if for example  slowMA < 50 happened in the past 5 candles maybe an array of  slowMA might help but I dont know how to do that.

 

iMAOnArray should be used. So create an indicator first:

//--- input parameters
input int      RSIPeriod=14;
input int      FastMAPeriod=3;
input int      SlowMAPeriod=9;
input ENUM_MA_METHOD MAMethod=MODE_SMA;
input int      RSIPrice;
//--- indicator buffers
double         RSIBuffer[];
double         MAABuffer[];
double         MABBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   string name0=StringFormat("RSI(%d)",RSIPeriod);
//--- indicator buffers mapping
   SetIndexBuffer(0,RSIBuffer);
   SetIndexLabel(0,name0);
   string name1=StringFormat(" %s(%d)",StringSubstr(EnumToString(MAMethod),5),FastMAPeriod);
   SetIndexBuffer(1,MAABuffer);
   SetIndexLabel(1,name1);
   string name2=StringFormat(" %s(%d)",StringSubstr(EnumToString(MAMethod),5),SlowMAPeriod);
   SetIndexBuffer(2,MABBuffer);
   SetIndexLabel(2,name2);
//---
   IndicatorSetString(INDICATOR_SHORTNAME,name0+name1+name2);
//---
   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 begin=(rates_total!=prev_calculated)?rates_total-1-prev_calculated:0;
//---
   for(int i=begin;i>=0 && !_StopFlag;i--)
      RSIBuffer[i]=iRSI(_Symbol,_Period,RSIPeriod,RSIPrice,i);
   for(int i=begin;i>=0 && !_StopFlag;i--)
      MAABuffer[i]=iMAOnArray(RSIBuffer,rates_total,FastMAPeriod,0,MAMethod,i);
   for(int i=begin;i>=0 && !_StopFlag;i--)
      MABBuffer[i]=iMAOnArray(RSIBuffer,rates_total,SlowMAPeriod,0,MAMethod,i);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Then call iCustom in the EA:

//--- input parameters
input int      RSIPeriod=14;
input int      FastMAPeriod=3;
input int      SlowMAPeriod=9;
input ENUM_MA_METHOD MAMethod=MODE_SMA;
input int      RSIPrice;
//---
double iRSI(int shift)
  {
   return(iCustom(_Symbol,_Period,"MA RSI",RSIPeriod,FastMAPeriod,SlowMAPeriod,MAMethod,RSIPrice,0,shift));
  }
//---
#define FastMA 1
#define SlowMA 2
double iMA(int buffer,int shift)
  {
   return(iCustom(_Symbol,_Period,"MA RSI",RSIPeriod,FastMAPeriod,SlowMAPeriod,MAMethod,RSIPrice,buffer,shift));
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double rsi0=iRSI(0);
   double fastMA1=iMA(FastMA,1);
   double slowMA5=iMA(SlowMA,5);
//---
  }
//+------------------------------------------------------------------+
Files:
MA_RSI.mq4  4 kb
MA_RSI.mq4  2 kb
 
Ernst Van Der Merwe:

iMAOnArray should be used. So create an indicator first:

Then call iCustom in the EA:

Am I doing something wrong or why does 
fastMA1
slowMA5

and 

rsi0s

still all return 0.0?

I copied all your code but it does not work.

 
sasax7:
Am I doing something wrong or why does 

and 

still all return 0.0?

I copied all your code but it does not work.

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   double rsi0=iRSI(0);
   double fastMA1=iMA(FastMA,1);
   double slowMA5=iMA(SlowMA,5);
   Print("rsi0: ",DoubleToStr(rsi0,1));
   Print("fastMA1: ",DoubleToStr(fastMA1,1));
   Print("slowMA5: ",DoubleToStr(slowMA5,1));
//---
  }
//+------------------------------------------------------------------+

2021.02.22 13:16:17.446 MA RSI EURUSD,H1: slowMA5: 55.9
2021.02.22 13:16:17.445 MA RSI EURUSD,H1: fastMA1: 47.7
2021.02.22 13:16:17.445 MA RSI EURUSD,H1: rsi0: 59.8
2021.02.22 13:16:16.776 MA RSI EURUSD,H1: slowMA5: 55.9
2021.02.22 13:16:16.776 MA RSI EURUSD,H1: fastMA1: 47.7
2021.02.22 13:16:16.776 MA RSI EURUSD,H1: rsi0: 59.8

Working this side. Please download the attached files as is and try again.