Using dynamic RSI in a script

 

Hello,

I would like to set Bollinger Bands on RSI in order to make a dynamic RSI in a script. I am stucked, I have these two function : iRSI and IBands but I dont know how to cross both.

Help me please !

 
Skowi:

Hello,

I would like to set Bollinger Bands on RSI in order to make a dynamic RSI in a script. I am stucked, I have these two function : iRSI and IBands but I dont know how to cross both.

Help me please !



If you need a solution in the code, it is important that the second indicator has an OnCalculate  of the following form:

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
But if you have a custom indicator, then it can be done only in MT5.
 
Sorry I just would like to know how to apply iBands() on iRSI()
 
Skowi:
Sorry I just would like to know how to apply iBands() on iRSI()

For MT4:

//--- indicator buffers
double         RSIBuffer[];
double         LowerBuffer[];
double         UpperBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RSIBuffer);
   SetIndexBuffer(1,LowerBuffer);
   SetIndexBuffer(2,UpperBuffer);
//---
   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=!prev_calculated?rates_total-RSIPeriod-1:rates_total-prev_calculated;
//---
   for(int i=begin;i>=0;i--)
      RSIBuffer[i]=iRSI(_Symbol,_Period,RSIPeriod,PRICE_CLOSE,i);
//---
   begin=!prev_calculated?rates_total-RSIPeriod-BandsPeriod-2:rates_total-prev_calculated;
//---
   for(int i=begin;i>=0;i--)
     {
      LowerBuffer[i]=iBandsOnArray(RSIBuffer,0,BandsPeriod,Deviation,0,MODE_LOWER,i);
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

For MT5:

//---
int handleRSI,handleBands;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,RSIBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,UpperBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,LowerBuffer,INDICATOR_DATA);
   if((handleRSI=iRSI(_Symbol,_Period,RSIPeriod,PRICE_CLOSE))==INVALID_HANDLE ||
      (handleBands=iBands(_Symbol,_Period,BandsPeriod,0,Deviation,handleRSI))==INVALID_HANDLE)
      return(INIT_FAILED);
   ArraySetAsSeries(RSIBuffer,true);
   ArraySetAsSeries(UpperBuffer,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   int toCopy=!prev_calculated?rates_total-RSIPeriod:rates_total-prev_calculated+1;
   
   if(CopyBuffer(handleRSI,0,0,toCopy,RSIBuffer)!=toCopy ||
      CopyBuffer(handleBands,1,0,toCopy,UpperBuffer)!=toCopy)
      return(0);
      
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Reason: