Negative Indices

 

hi everyone, i have an indicator and there is a variable "r" in that code that starts from -1 . if you change it to zero, the first candle doesnt have amount.  i want to know why is  started "r" from -1 . i want to write this code to another language , can someone help me ? Thank you :).

//+------------------------------------------------------------------+
//|                                       Better Bollinger bands.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "mladenfx@gmail.com"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 clrDeepSkyBlue
#property indicator_color2 clrDeepSkyBlue
#property indicator_color3 clrNONE
#property indicator_width1 2
#property indicator_width2 2
//
//
//
//
//

extern int    BandsLength    =  20;
extern double BandsDeviation = 2.0; 
extern int    AppliedPrice   = PRICE_CLOSE;

//
//
//
//
//

double MaBuffer[];
double UpperBand[];
double LowerBand[];
double tBuffer[][4];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   SetIndexBuffer(0,UpperBand);
   SetIndexBuffer(1,LowerBand);
   SetIndexBuffer(2,MaBuffer);
   return(0);
}
int deinit()
{
   return(0);
}


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

#define iMt1 0
#define iMt2 1
#define iUt1 2
#define iUt2 3

//
//
//
//
//

int start()
{
   int    counted_bars=IndicatorCounted();
   int    limit,i,r;


   if(counted_bars < 0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = Bars-counted_bars;
         if (ArrayRange(tBuffer,0) != Bars) ArrayResize(tBuffer,Bars);         
         
   //
   //
   //
   //
   //
   
   double alpha =  2.0/(BandsLength+1.0);
      for (i=limit, r=Bars-i-1; i>=0; i--,r++)
      {
         double price = iMA(NULL,0,1,0,MODE_SMA,AppliedPrice,i);
         Comment("Bars: ",Bars,"\nLimit: ",limit,"\nr : ",r) ;
         //
         //
         //
         //
         //
         
            tBuffer[r][iMt1] = tBuffer[r-1][iMt1] + alpha*(price           -tBuffer[r-1][iMt1]);
            tBuffer[r][iUt1] = tBuffer[r-1][iUt1] + alpha*(tBuffer[r][iMt1]-tBuffer[r-1][iUt1]);
               double dt  = ((2-alpha)*tBuffer[r][iMt1]-tBuffer[r][iUt1])/(1-alpha);
           
            tBuffer[r][iMt2] = tBuffer[r-1][iMt2] + alpha*(MathAbs(price-dt)-tBuffer[r-1][iMt2]);
            tBuffer[r][iUt2] = tBuffer[r-1][iUt2] + alpha*(tBuffer[r][iMt2] -tBuffer[r-1][iUt2]);
               double dt2 = ((2-alpha)*tBuffer[r][iMt2]-tBuffer[r][iUt2])/(1-alpha);

         //
         //
         //
         //
         //
         
         MaBuffer[i]  = dt;
         UpperBand[i] = dt+BandsDeviation*dt2;
         LowerBand[i] = dt-BandsDeviation*dt2;
      }            
   return(0);
}
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. momet: i have an indicator and there is a variable "r" in that code that starts from -1 .
         for (i=limit, r=Bars-i-1; i>=0; i--,r++)

    It doesn't. It starts at Bars-1, the oldest bar on chart. Then you increment it and indicator breaks.

                tBuffer[r][iMt1] = tBuffer[r-1][iMt1] + alpha*(price           -tBuffer[r-1][iMt1]);

    This r-1 is non-series indexing. Broken because r doesn't start at zero.

    Your code is garbage.

 

MQL4 and MetaTrader 4, has it's own section on the forum. I have moved your topic to the correct section.

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. It doesn't. It starts at Bars-1, the oldest bar on chart. Then you increment it and indicator breaks.

    This r-1 is non-series indexing. Broken because r doesn't start at zero.

    Your code is garbage.

it works properly. i also see in debugging section and see its -1. at least please help me to write the code in your way. i worked on it three days and i didnt understant something.  im new in mql4. please write these some codes. Thank you

 
Fernando Carreiro #:

MQL4 and MetaTrader 4, has it's own section on the forum. I have moved your topic to the correct section.

ok thank you. i wasnt here for a while and didnt know that :)

 

Consider learning to use the more modern MQL4+ syntax and event handlers ... Updated MQL4 - Language Basics - MQL4 Reference

 
Fernando Carreiro #: Consider learning to use the more modern MQL4+ syntax and event handlers ... Updated MQL4 - Language Basics - MQL4 Reference

Thanks again Mr Carreiro