can't access previous candles rsi data in my indicator

To add comments, please log in or register
Hossein Zandi
476
Hossein Zandi  

Hello,

Please take a look at this code and tell me why I can't access to  previous candle's rsi  RSIBuf[i+1] , 

I tried this AAA[]  Array but still not working when I try to comment (AAA[i+1]) in the strategy tester I get array out of range error please help me how can i use previous candle's data into my calculations


//------------------------------------------------------------------
//
//------------------------------------------------------------------

#property indicator_chart_window
#property strict

int    RSIPeriod=14;
double RSIBuf[],AAA[],ma[];
//------------------------------------------------------------------
//
//------------------------------------------------------------------

int
init()
  {
   IndicatorBuffers(3);
   SetIndexBuffer(0,RSIBuf);
   SetIndexBuffer(1,AAA);
   SetIndexBuffer(2,ma);

   return(0);
  }
//------------------------------------------------------------------
//
//------------------------------------------------------------------

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[])
  {

   for(int i=rates_total-prev_calculated-1; i>=0; i--)
     {
      RSIBuf[i]=iRSI(NULL,0,RSIPeriod,PRICE_WEIGHTED,i);
      ma[i]=iMAOnArray(RSIBuf,0,20,0,MODE_SMA,i);

      AAA[i]=0;
      if(RSIBuf[i]>ma[i]) AAA[i] =  1;
      if(RSIBuf[i]<ma[i]) AAA[i] = -1;

      Comment(AAA[i+1]);

     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
William Roeder
20210
William Roeder  
Hossein Zandi: , it's because of the loop it should be from the right to the left not from the left to the right 
  1. Wrong. Direction of the loop changes nothing (except breaking your iMAOnArray.)

  2.    for(int i=rates_total-prev_calculated-1; i>=0; i--){
          :
          Comment(AAA[i+1]);
    When prev_calculated was zero, i becomes rates_total - 1. but your comment access one past that, thus array exceeded.

  3. After that, prev_calculated equals rate_total so your loop becomes i=-1; i>=0, and thus does not update bar zero on new ticks.

  4.       RSIBuf[i]=iRSI(NULL,0,RSIPeriod,PRICE_WEIGHTED,i);
          ma[i]=iMAOnArray(RSIBuf,0,20,0,MODE_SMA,i);
    You can't start getting RSI values until you have RSIPeriod bars. You can't start computing SMA until you have 20 RSI values.
              How to do your lookbacks correctly.
Hossein Zandi
476
Hossein Zandi  

Thanks a lot, problem solved.

I changed it to below if someone is interested.

int lookback = movingaverageperiod // as maximum look bak
for(int i = Bars - 1 - MathMax(lookback, counted); i >= 0; i--)

To add comments, please log in or register