Problem with custom indicator...

To add comments, please log in or register
Ricardo Luceac
43
Ricardo Luceac  

Hi my friends,


I'm new to programming indicators, and I'm having some problems, hope you can help me.

I'm trying to do a simple indicator of squeeze in bollinger bands.

The indicator will go blue when the bollinger is outside keltner channel and red when its inside (all or just one line).

The problem is that it not working well. By the picture bellow you can see that its different than bollinger keltner in graph.

Tela

In the strategy tester it even get printed....

This is my first custom indicator.

Any Help is appreciated

My code:


//+------------------------------------------------------------------+
//|                                            boil-kelt-squeeze.mq5 |
//|                                                   Ricardo Luceac |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Ricardo Luceac"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot Colorhistogram
#property indicator_label1  "Colorhistogram"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  clrRed,clrBlue,C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0'
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

#resource "\\indicators\\meta-inds\\Download\\keltner_channel.ex5"


//--- input parameters
input int      period_=20;
input int      shift_=0;
input double   deviation_=2;
input double   ratio_=2;
//--- indicator buffers
double         ColorhistogramBuffer[];
double         ColorhistogramColors[];

int b_handler_;
int k_handler_;

double upper_b_buffer[];
double lower_b_buffer[];
double upper_k_buffer[];
double lower_k_buffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ColorhistogramBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ColorhistogramColors,INDICATOR_COLOR_INDEX);
   
   b_handler_=iBands(Symbol(),Period(),period_,shift_,deviation_,PRICE_CLOSE);
   k_handler_=iCustom(Symbol(),Period(),"::indicators\\meta-inds\\Download\\keltner_channel.ex5",period_,MODE_SMA,ratio_,PRICE_CLOSE,shift_);

   ArraySetAsSeries(upper_b_buffer,true);
   ArraySetAsSeries(lower_b_buffer,true);
   ArraySetAsSeries(upper_k_buffer,true);
   ArraySetAsSeries(lower_k_buffer,true);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,       
                const int prev_calculated,   
                const int begin,            
                const double &price[])
  {
//---

   if(rates_total<period_)
      return(0); 
   
   
   int start;
   
    if(prev_calculated==0)
      start=1; 
    else 
      start=prev_calculated-1;   

   

   
       if(CopyBuffer(b_handler_,1,0,rates_total,upper_b_buffer)==-1)
         return rates_total;
         
       if(CopyBuffer(b_handler_,2,0,rates_total,lower_b_buffer)==-1)
          return rates_total;
      
      if(CopyBuffer(k_handler_,0,0,rates_total,upper_k_buffer)==-1)
         return rates_total;
         
       if(CopyBuffer(k_handler_,2,0,rates_total,lower_k_buffer)==-1)
          return rates_total;


   for(int i=0; i<rates_total; i++)
   {

      double b_size=upper_b_buffer[i]-lower_b_buffer[i];
      double k_size=upper_k_buffer[i]-lower_k_buffer[i];

      
      if(upper_b_buffer[i]>upper_k_buffer[i] && lower_b_buffer[i]<lower_k_buffer[i])
      {
         ColorhistogramBuffer[i]=b_size-k_size;
         ColorhistogramColors[i]=1;
      }
      else
      {
         if(b_size<k_size)
            ColorhistogramBuffer[i]=b_size-k_size;
         else
             ColorhistogramBuffer[i]=k_size-b_size;
         
         ColorhistogramColors[i]=0;
      }
      
   
   
   
   }
   
   
   
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Anthony Garot
1968
Anthony Garot  
Ricardo Luceac:

My code:

Try this:

        // ArraySetAsSeries(upper_b_buffer,true);
        // ArraySetAsSeries(lower_b_buffer,true);
        // ArraySetAsSeries(upper_k_buffer,true);
        // ArraySetAsSeries(lower_k_buffer,true);
Ricardo Luceac
43
Ricardo Luceac  
Thanks.... Thats it!
To add comments, please log in or register