BollingerUpperBand and BollingerUpperBandPrevious

 

Hy everybody,


First, I m french and I want to apologize for bad englisg skills ....


Since September, I m trying to do something appearing as easy, but doesn't.


I need to calculate the variation for example between BollingerUpperBand for the current bar and the previous bar, and then calculating on SMA on this variations.

But firstly, my variation formula seems to be wrong, but I can t understand why :(

I m using the default Bands.mq4 files like attached.

I m trying with this formula, but unsuccessfull :(


//--- starting calculation
   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
      pos=0;
//--- main cycle
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      //--- middle line
      ExtMovingBuffer[i]=SimpleMA(i,InpBandsPeriod,close);
      //--- calculate and write down StdDev
      ExtStdDevBuffer[i]=StdDev_Func(i,close,ExtMovingBuffer,InpBandsPeriod);
      //--- upper line
      ExtUpperBuffer[i]=ExtMovingBuffer[i]+InpBandsDeviations*ExtStdDevBuffer[i];
      //--- lower line
      ExtLowerBuffer[i]=ExtMovingBuffer[i]-InpBandsDeviations*ExtStdDevBuffer[i];
      //---
      VARUP[i]= ((ExtUpperBuffer[i]-ExtUpperBuffer[i+1])/ExtUpperBuffer[i+1]);
      VARDOWN[i]= 10000*((ExtLowerBuffer[i]-ExtLowerBuffer[i+1])/ExtLowerBuffer[i+1]);

    }
//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }


but I have an ARRAY OUT OF RANGE error and after reading explnations on forums, I can t find why.


Maybe could be easy or already done by one of us :)


thanks a lot for further help,


Nice evening to all of you


Soukenson

MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
  • www.mql4.com
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
Files:
Bands.mq4  5 kb
 
SoukDiaby: but I have an ARRAY OUT OF RANGE error and after reading explnations on forums, I can t find why.
  1. When you post code please use the SRC button! Please edit your post.
               General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. ExtMovingBuffer[i]=
    ExtStdDevBuffer[i]=
    ExtUpperBuffer[i]=
    ExtLowerBuffer[i]=
    VARUP[i]=
    VARDOWN[i]= 10000
    You appear to have 6 arrays. How can we possibly know wether you have resized them properly or if they are buffers? Show all relevant code.
 
whroeder1:
  1. When you post code please use the SRC button! Please edit your post.
               General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. You appear to have 6 arrays. How can we possibly know wether you have resized them properly or if they are buffers? Show all relevant code.

Hi,


Thanks for your reply, I ll correct my mistakes.


Thanks whroeder1

 

So,


here is the code with OUT OF RANGE problem.

I dont understand why this OUT OF RANGE, buffer are limitless if I didn t do something wrong (ex : VARUP[])

Many thanks for further help :)


Have a nice day everybody


//+------------------------------------------------------------------+
//|                                                        Bands.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Bollinger Bands"
#property strict

#include <MovingAverages.mqh>

#property indicator_chart_window
#property indicator_buffers 10
#property indicator_color1 LightSeaGreen
#property indicator_color2 LightSeaGreen
#property indicator_color3 LightSeaGreen
#property indicator_color4 Red
#property indicator_color5 Red
#property indicator_color6 Red
#property indicator_color7 Red


//--- indicator parameters
input int    InpBandsPeriod=20;      // Bands Period
input int    InpBandsShift=0;        // Bands Shift
input double InpBandsDeviations=1.5; // Bands Deviations
//--- buffers
double ExtMovingBuffer[];
double ExtUpperBuffer[];
double ExtLowerBuffer[];
double ExtStdDevBuffer[];
double VARUP[];
double VARDOWN[];
double MAUP[];
double MADOWN[];
double PrevBufferUpper[];
double PrevBufferLower[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- 1 additional buffer used for counting.
   IndicatorBuffers(10);
   IndicatorDigits(Digits);
//--- middle line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMovingBuffer);
   SetIndexShift(0,InpBandsShift);
   SetIndexLabel(0,"Bands SMA");
//--- upper band
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtUpperBuffer);
   SetIndexShift(1,InpBandsShift);
   SetIndexLabel(1,"Bands Upper");
//--- lower band
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtLowerBuffer);
   SetIndexShift(2,InpBandsShift);
   SetIndexLabel(2,"Bands Lower");
//--- work buffer
   SetIndexBuffer(3,ExtStdDevBuffer);
   SetIndexLabel(3,"ExtStdDevBuffer");
   
   //SetIndexStyle(4,DRAW_LINE);//,0,1);
   SetIndexBuffer(4,VARUP);
   SetIndexLabel(4,"Variation UP");
   
   SetIndexBuffer(5,VARDOWN);
   //SetIndexStyle(5,DRAW_LINE);//,0,1);
   SetIndexLabel(5,"Variation DOWN");
   
   SetIndexBuffer(6,MAUP);
   //SetIndexStyle(6,DRAW_LINE);//,0,1);
   SetIndexLabel(6,"MAUP");
   
   SetIndexBuffer(7,MADOWN);
   //SetIndexStyle(7,DRAW_LINE);//,0,1);
   SetIndexLabel(7,"MADOWN");
   
   SetIndexBuffer(8,PrevBufferUpper);
   //SetIndexStyle(8,DRAW_LINE);
   SetIndexLabel(8,"PrevBufferUpper");
   
   SetIndexBuffer(9,PrevBufferLower);
   //SetIndexStyle(9,DRAW_LINE);  
   SetIndexLabel(9,"PrevBufferLower");
   
   
//--- check for input parameter
   if(InpBandsPeriod<=0)
     {
      Print("Wrong input parameter Bands Period=",InpBandsPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpBandsPeriod+InpBandsShift);
   SetIndexDrawBegin(1,InpBandsPeriod+InpBandsShift);
   SetIndexDrawBegin(2,InpBandsPeriod+InpBandsShift);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
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[])
  {
   int i,pos;
//---
   if(rates_total<=InpBandsPeriod || InpBandsPeriod<=0)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtMovingBuffer,false);
   ArraySetAsSeries(ExtUpperBuffer,false);
   ArraySetAsSeries(ExtLowerBuffer,false);
   ArraySetAsSeries(ExtStdDevBuffer,false);
   ArraySetAsSeries(close,false);
   ArraySetAsSeries(VARUP,false);
   ArraySetAsSeries(MAUP,false);
   ArraySetAsSeries(MADOWN,false);
   ArraySetAsSeries(VARDOWN,false);
   ArraySetAsSeries(PrevBufferUpper,false);
   ArraySetAsSeries(PrevBufferLower,false);
//--- initial zero
   if(prev_calculated<1)
     {
      for(i=0; i<InpBandsPeriod; i++)
        {
         ExtMovingBuffer[i]=EMPTY_VALUE;
         ExtUpperBuffer[i]=EMPTY_VALUE;
         ExtLowerBuffer[i]=EMPTY_VALUE;
         VARUP[i] = EMPTY_VALUE;
         VARDOWN[i] = EMPTY_VALUE;
         MAUP[i] = EMPTY_VALUE;
         MADOWN[i] = EMPTY_VALUE;
         PrevBufferUpper[i] = EMPTY_VALUE;
         PrevBufferLower[i] = EMPTY_VALUE;
        }
     }
//--- starting calculation
   if(prev_calculated>1)
      pos=prev_calculated-1;
   else
      pos=0;
//--- main cycle
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      //--- middle line
      ExtMovingBuffer[i]=SimpleMA(i,InpBandsPeriod,close);
      //--- calculate and write down StdDev
      ExtStdDevBuffer[i]=StdDev_Func(i,close,ExtMovingBuffer,InpBandsPeriod);
      //--- upper line
      ExtUpperBuffer[i]=ExtMovingBuffer[i]+InpBandsDeviations*ExtStdDevBuffer[i];
      //--- lower line
      ExtLowerBuffer[i]=ExtMovingBuffer[i]-InpBandsDeviations*ExtStdDevBuffer[i];
      //---
     
         VARUP[i]= 10000*((ExtUpperBuffer[i]-ExtUpperBuffer[i+])/ExtUpperBuffer[i+1]); // Variation for (Upper band[0]  - Upperband[1])/ Upperband[1]
         VARDOWN[i]= 10000*((ExtLowerBuffer[i]-ExtLowerBuffer[i+1])/ExtLowerBuffer[i+1]); // same variation for lower band
         MAUP[i]= iMAOnArray(VARUP,0,100,0,0,i); // moving average 100 period of VARUP
         MADOWN[i]= iMAOnArray(VARDOWN,0,100,0,0,i); // idem for VARDOWN
         
         Print ("varup : "+VARUP[i], " // vardw : "+VARDOWN[i], " // maup : "+MAUP[i], " // MADOWN : "+MADOWN[i]);
         //Print("i = " + i, " // ", "pos = " + pos, " // ", "rates-total = " + rates_total, " // ", "VarUp = " + VARUP[i], " // ", "VarDown = " + VARDOWN[i], " // " + "MAUp = " + MAUP[i], " // ", "MADown = " + MADOWN[i]);
      }

     //}
//---- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Calculate Standard Deviation                                     |
//+------------------------------------------------------------------+
double StdDev_Func(int position,const double &price[],const double &MAprice[],int period)
  {
//--- variables
   double StdDev_dTmp=0.0;
//--- check for position
   if(position>=period)
     {
      //--- calcualte StdDev
      for(int i=0; i<period; i++)
         StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2);
      StdDev_dTmp=MathSqrt(StdDev_dTmp/period);
     }
//--- return calculated value
   return(StdDev_dTmp);
  }
//+------------------------------------------------------------------+
 
I dont understand why this OUT OF RANGE, buffer are limitless if I didn t do something wrong (ex : VARUP[])
VARDOWN[i]= 10000*((ExtLowerBuffer[i]-ExtLowerBuffer[i+1])/ExtLowerBuffer[i+1]
  1. Buffers are not "limitless." Their size is rates_total. i goes up to rates_total therefor i+1 is rates_total which does not exist.
  2. Why are you looking at future values to calculate past ones? Especially when you haven't even calculated them yet.
  3. Don't hard code numbers (10000.) Code fails on JPY pairs. If you want it in Points, divide by _Point. If you want it in PIPs compute what a PIP is and divide by that.
              How to manage JPY pairs with parameters? - MQL4 and MetaTrader 4 - MQL4 programming forum
Reason: