Nesting indicators

 

Hello,

I need a little help to code the RSI of this expression
average ( (open+close)/2, length) instead of the close.
length being declared as an input.
getting confused and would really appreciate the push !

many thanks

Daniel77

 
Daniel77 wrote >>

Hello,

I need a little help to code the RSI of this expression
average ( (open+close)/2, length) instead of the close.
length being declared as an input.
getting confused and would really appreciate the push !

many thanks

Daniel77


here is how I modified the standard mt4 RSI,

//+------------------------------------------------------------------+
//| RSI.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| https://www.metaquotes.net// |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net//"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_buffers 1
#property indicator_color1 DodgerBlue
//---- input parameters
extern int RSIPeriod=14;
extern int RSISmooth=3; //I insert here the length of the moving average
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- 2 additional buffers are used for counting.
IndicatorBuffers(3);
SetIndexBuffer(1,PosBuffer);
SetIndexBuffer(2,NegBuffer);
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,RSIBuffer);
//---- name for DataWindow and indicator subwindow label
short_name="RSI("+RSIPeriod+")";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,RSIPeriod);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Relative Strength Index |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double price, AVGprice,rel,negative,positive;//I declare here the variables AVGprice & price
//----
if(Bars<=RSIPeriod) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=RSIPeriod;i++) RSIBuffer[Bars-i]=0.0;
//----
i=Bars-RSIPeriod-1;
if(counted_bars>=RSIPeriod) i=Bars-counted_bars-1;
while(i>=0)
{
double sumn=0.0,sump=0.0;
if(i==Bars-RSIPeriod-1)
{
int k=Bars-2;
//---- initial accumulation
while(k>=i)
{
price[k]= (open[k] + close[k])/2;
Avgprice[k]=iMAOnArray(price,Bars,RSISmooth,0,MODE_SMA,k);
rel=Avgprice[k]-Avgprice[k+1];
if(rel>0) sump+=rel;
else sumn-=rel;
k--;
}
positive=sump/RSIPeriod;
negative=sumn/RSIPeriod;
}
else
{
//---- smoothed moving average
price[i]= (open[i] + close[i])/2;
Avgprice[i]=iMAOnArray(price,Bars,RSISmooth,0,MODE_SMA,i);
rel= Avgprice[i]- Avgprice[i+1];
if(rel>0) sump=rel;
else sumn=-rel;
positive=(PosBuffer[i+1]*(RSIPeriod-1)+sump)/RSIPeriod;
negative=(NegBuffer[i+1]*(RSIPeriod-1)+sumn)/RSIPeriod;
}
PosBuffer[i]=positive;
NegBuffer[i]=negative;
if(negative==0.0) RSIBuffer[i]=0.0;
else RSIBuffer[i]=100.0-100.0/(1+positive/negative);
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+

 

Hello again,
Still swimming .... trying to find the pb.

I came up with the code below which compiles but nothing shows on the screen.

The idea is to use an average of (open+close)/2 and draw the RSI of that value.
Taking the RSI of an average seems to smoothe the price action better
than doing the reverse i.e. averaging an RSI value.

Thanks in advance for your help....
Dan

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

//+------------------------------------------------------------------+
//| RSIModified.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 DodgerBlue
//---- input parameters
extern int rsi_period = 14;
extern int ma_period = 3;
int ma_shift = 0;
//---- buffers
double Pricebuf[];
double Avgpricebuf[];
double RSImodbuf [];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,RSImodbuf);
return(0);
//---- name for DataWindow and indicator subwindow label
short_name="RSImod";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,rsi_period);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int repaintable_bars = Bars - IndicatorCounted();
ArraySetAsSeries(Pricebuf,true);
for(int shift = repaintable_bars; shift >= 0; shift--){
Pricebuf[shift] = (Open[shift]+Close[shift])/2;
Avgpricebuf[shift]=iMAOnArray(Pricebuf,Bars,ma_period,0,MODE_SMA,shift);
}
for(shift = repaintable_bars; shift >= 0; shift--){
RSImodbuf[shift] = iRSIOnArray(Avgpricebuf, 0, rsi_period,shift);
}
return(0);
}
//+------------------------------------------------------------------+

 

OK this time it plots a graph, but it is the RSI of the close, not the RSI of the average of the (open+close)/2...

A little help please would be welcome... thanks


//+------------------------------------------------------------------+
//| RSIModified.mq4 |
//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_color1 DodgerBlue
//---- input parameters
extern int rsi_period = 14;
extern int ma_period = 3;
//---- buffers
double Pricebuf[];
double Avgpricebuf[];
double RSImodbuf [];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,RSImodbuf);
return(0);
//---- name for DataWindow and indicator subwindow label
short_name="RSImod";
IndicatorShortName(short_name);
SetIndexLabel(0,short_name);
//----
SetIndexDrawBegin(0,rsi_period);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int repaintable_bars = Bars - IndicatorCounted();

for(int shift = repaintable_bars; shift >= 0; shift--)
{
Pricebuf[shift] = (Open[shift]+Close[shift])/2;
Avgpricebuf[shift]=iMA(NULL,0,ma_period,0,MODE_SMA,Pricebuf,shift);
}
for(shift = repaintable_bars; shift >= 0; shift--)
{
RSImodbuf[shift] = iRSI(NULL,0,rsi_period,Avgpricebuf,shift);
}
return(0);
}
//+------------------------------------------------------------------+

Reason: