Help needed coding RSI,MA indicator....

 

Hi all,

I've only been coding for a week but have found my way around most things i want to do so far. However this one particular seemingly simple task is causing me no end of grief. i have searched this site and web and found various different solutions all of which i have attempted to make work. i seem to be close.... the RSI values i know are good but the MA values seem way out hence my indicator arrows in wrong location. I notice if i change the buffer size my arrows move...? Where am i going wrong...Thanks

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_color2 Red
 
//--- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,217);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,218);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);

             
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit, i, s, t;
   
   int    counted_bars=IndicatorCounted();
   
   double
          RSIBuffer[2000], MAofRSIBuffer[2000];  
                  
  
   //-- recount last bar  
   
 if(counted_bars>0) counted_bars--;
      
      limit=Bars-counted_bars; 
     
          
   for (s=0; s<=limit; s++)
   
   RSIBuffer[s]=iRSI(0,0,7,PRICE_CLOSE,s);
   
   for (t=0; t<=limit; t++)
   
   MAofRSIBuffer[t]=iMAOnArray(RSIBuffer,0,3,0,MODE_EMA,t);
   
           
   for (i=0; i<=limit; i++)   
   
      {      
         if(RSIBuffer[i+1]<MAofRSIBuffer[i+1] && RSIBuffer[i]>MAofRSIBuffer[i])
         ExtMapBuffer1[i]=Low[i]-80*Point;
        
         
         if(RSIBuffer[i+1]>MAofRSIBuffer[i+1] && RSIBuffer[i]<MAofRSIBuffer[i])
         ExtMapBuffer2[i]=High[i]+80*Point;
         
   }
//----
   return(0);
  }
 
   for (s=0; s<=limit; s++)  RSIBuffer[s]=iRSI(0,0,7,PRICE_CLOSE,s);
   for (t=0; t<=limit; t++)  MAofRSIBuffer[t]=iMAOnArray(RSIBuffer,0,3,0,MODE_EMA,t);
  1. Buffers auto-size and their contents auto-shift. Arrays don't.
  2. After the first run limit will usually be zero, so you are replacing just RSI[0] and creating a MA with old unrelated bars
  3. Replace your arrays with two buffers
  4. Contradictory information on IndicatorCounted() - MQL4 forum



 
cdsmart:

Hi all,

I've only been coding for a week but have found my way around most things i want to do so far. However this one particular seemingly simple task is causing me no end of grief. i have searched this site and web and found various different solutions all of which i have attempted to make work. i seem to be close.... the RSI values i know are good but the MA values seem way out hence my indicator arrows in wrong location. I notice if i change the buffer size my arrows move...? Where am i going wrong...Thanks

Read the documentation for iMAOnArray() very carefully, it's pretty poor I admit, but you need to take notice of this . . .

Calculation of the Moving Average on data stored in a numeric array. Unlike iMA(...), the iMAOnArray function does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries function.

 
Hey thanks fellas, the arraysetasseries fixed it straight up and the other stuff makes for interesting reading as i am still to understand it 100pc even though i got it working!