bollinger bands b% indicator does not work when tested

To add comments, please log in or register
Hugo Del Castillo
133
Hugo Del Castillo  
I have programmed an indicator but it doesnt work when tested. It keeps returning 0. Can you help me figure out whats wrong with the code please?
//+------------------------------------------------------------------+
//|                                                  b%bollinger.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   1

#property indicator_label1  "Bollinger bands %b"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_level1 0.0
#property indicator_level2 0.5
#property indicator_level3 1.0

input int    BBPeriod=20;        //Period
input int    BBShift=0;         // Shift
input double StdDeviation=2.0;  //Standard Deviation

double         UpperBuffer[];
double         LowerBuffer[];
double         BLGBuffer[];
int    bbhandle;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BLGBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,UpperBuffer ,INDICATOR_CALCULATIONS);
   SetIndexBuffer(2,LowerBuffer ,INDICATOR_CALCULATIONS);
   ArraySetAsSeries(BLGBuffer,true);
   ArraySetAsSeries(UpperBuffer,true);
   ArraySetAsSeries(LowerBuffer,true);
   
   bbhandle=iBands(NULL,0,BBPeriod,BBShift,StdDeviation,PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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[])
  {
//---
   ArraySetAsSeries(close,true);
   CopyBuffer(bbhandle,1,0,rates_total,UpperBuffer);
   CopyBuffer(bbhandle,2,0,rates_total,LowerBuffer);
   
   for(int i = 0; i < rates_total - prev_calculated - 20; i++){
   
       BLGBuffer[i]=(close[i]-LowerBuffer[i])/(UpperBuffer[i]-LowerBuffer[i]);
   
   } 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

?
Naguisa Unada
416
Naguisa Unada  

//---      ArraySetAsSeries(close,true);    int limit;    if(prev_calculated==0)       limit=rates_total-BBPeriod;    else       limit=rates_total-prev_calculated;    int count=(limit>1 ? rates_total : 1),copied=0;    copied=CopyBuffer(bbhandle,1,0,count,UpperBuffer);    if(copied!=count) return 0;    copied=CopyBuffer(bbhandle,2,0,count,LowerBuffer);    if(copied!=count) return 0;    for(int i=0; i<=limit; i++){ BLGBuffer[i]=(close[i]-LowerBuffer[i])/(UpperBuffer[i]-LowerBuffer[i]); }

Hugo Del Castillo
133
Hugo Del Castillo  
Naguisa Unada:

thanks

To add comments, please log in or register