ATR + RSI code

 

Hello Guys,

Im new to metatrader but would really like to dive into this MQL4 code as my backgrounds in IT but not development/coding.

I was wondering if someone could help me with an example....

I'm trying to take the VALUE of ATR, and put that as the period in RSI... I know this doesn't make a whole lot of sense, but if someone could show me how to do this, i'll be able to do the rest...

Thanks for your help!

 
trader346 wrote >>

Hello Guys,

Im new to metatrader but would really like to dive into this MQL4 code as my backgrounds in IT but not development/coding.

I was wondering if someone could help me with an example....

I'm trying to take the VALUE of ATR, and put that as the period in RSI... I know this doesn't make a whole lot of sense, but if someone could show me how to do this, i'll be able to do the rest...

Thanks for your help!

I guess what im trying to do is take the already code there for RSI, and as the databuffer, put iATR ... is that right?

 

Ok, so here is my code... I add my indicator, but I don't see anything drawn on the subchart... any ideas?

#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=21;
//---- buffers

double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   double var2=iATR(0,0,21,0);
//---- 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("+var2+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//----
   SetIndexDrawBegin(0,var2);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,counted_bars=IndicatorCounted();
   double rel,negative,positive;
   double var2=iATR(0,0,21,0);
//----
   if(Bars<=var2) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=var2;i++) RSIBuffer[Bars-i]=0.0;
//----
   i=Bars-var2-1;
   if(counted_bars>=var2) i=Bars-counted_bars-1;
   while(i>=0)
     {
      double sumn=0.0,sump=0.0;
      if(i==Bars-var2-1)
        {
         int k=Bars-2;
         //---- initial accumulation
         while(k>=i)
           {
            rel=Close[k]-Close[k+1];
            if(rel>0) sump+=rel;
            else      sumn-=rel;
            k--;
           }
         positive=sump/var2;
         negative=sumn/var2;
        }
      else
        {
         //---- smoothed moving average
         rel=Close[i]-Close[i+1];
         if(rel>0) sump=rel;
         else      sumn=-rel;
         positive=(PosBuffer[i+1]*(var2-1)+sump)/var2;
         negative=(NegBuffer[i+1]*(var2-1)+sumn)/var2;
        }
      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);
  }
//+------------------------------------------------------------------+
Reason: