Author: 2005-2014, MetaQuotes Software Corp.+ wellx
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
CCI_Band
//+------------------------------------------------------------------+
//|                                                     CCI_band.mq4 |
//|            Copyright 2005-2014, MetaQuotes Software Corp.+ wellx |
//|                                              http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp.+ wellx"
#property link        "http://www.mql4.com"
#property description "Commodity Channel Index High-Low Band"
#property strict

#include <MovingAverages.mqh>

#property indicator_separate_window
#property indicator_buffers    3
#property indicator_color1     LightSeaGreen
#property indicator_color2     Magenta
#property indicator_color3     Yellow
#property indicator_level1    -100.0
#property indicator_level2     100.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT



//--- input parameter
input int InpCCIPeriod=14; // CCI Period
input double koeff=0.015;

//--- buffers
double ExtCCIBuffer[];
double ExtCCI_H_Buffer[];
double ExtCCI_L_Buffer[];

double ExtPriceBuffer[];
double ExtPrice_H_Buffer[];
double ExtPrice_L_Buffer[];

double ExtMovBuffer[];
double ExtMov_H_Buffer[];
double ExtMov_L_Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(9);
   
   SetIndexBuffer(4,ExtPriceBuffer);
   SetIndexBuffer(5,ExtPrice_H_Buffer);
   SetIndexBuffer(6,ExtPrice_L_Buffer);
   
   SetIndexBuffer(3,ExtMovBuffer);
   SetIndexBuffer(7,ExtMov_H_Buffer);
   SetIndexBuffer(8,ExtMov_L_Buffer);
   
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexStyle(2,DRAW_LINE);
   
   SetIndexBuffer(0,ExtCCIBuffer);
   SetIndexBuffer(1,ExtCCI_H_Buffer);
   SetIndexBuffer(2,ExtCCI_L_Buffer);
   
//--- check for input parameter
   if(InpCCIPeriod<=1)
     {
      Print("Wrong input parameter CCI Period=",InpCCIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpCCIPeriod);
//--- name for DataWindow and indicator subwindow label
   short_name="CCI_Band("+IntegerToString(InpCCIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Commodity Channel Index                                          |
//+------------------------------------------------------------------+
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,k,pos;
   double dSum,dSumL,dSumH,dMul;
//---
   if(rates_total<=InpCCIPeriod || InpCCIPeriod<=1)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtCCI_H_Buffer,false);
   ArraySetAsSeries(ExtCCI_L_Buffer,false);
   ArraySetAsSeries(ExtCCIBuffer,false);
 
   ArraySetAsSeries(ExtPriceBuffer,false);
   ArraySetAsSeries(ExtPrice_L_Buffer,false);
   ArraySetAsSeries(ExtPrice_H_Buffer,false);
   
   ArraySetAsSeries(ExtMovBuffer,false);
   ArraySetAsSeries(ExtMov_L_Buffer,false);
   ArraySetAsSeries(ExtMov_H_Buffer,false);
   
   ArraySetAsSeries(high,false);
   ArraySetAsSeries(low,false);
   ArraySetAsSeries(close,false);
//--- initial zero
   if(prev_calculated<1)
     {
      for(i=0; i<InpCCIPeriod; i++)
        {
         ExtCCIBuffer[i]=0.0;
         ExtCCI_H_Buffer[i]=0.0;
         ExtCCI_L_Buffer[i]=0.0;
         
         ExtPriceBuffer[i]=(high[i]+low[i]+close[i])/3;
         ExtPrice_H_Buffer[i]=(high[i]+low[i]+high[i])/3;
         ExtPrice_L_Buffer[i]=(high[i]+low[i]+low[i])/3;
         
         ExtMov_L_Buffer[i]=0.0;
         ExtMov_H_Buffer[i]=0.0;
         ExtMovBuffer[i]=0.0;
        }
     }
//--- calculate position
   pos=prev_calculated-1;
   if(pos<InpCCIPeriod)
      pos=InpCCIPeriod;
//--- typical price and its moving average
   for(i=pos; i<rates_total; i++)
     {
      ExtPriceBuffer[i]=(high[i]+low[i]+close[i])/3;
      ExtPrice_H_Buffer[i]=(high[i]+low[i]+high[i])/3;
      ExtPrice_L_Buffer[i]=(high[i]+low[i]+low[i])/3;
      
      ExtMovBuffer[i]=SimpleMA(i,InpCCIPeriod,ExtPriceBuffer);
      ExtMov_H_Buffer[i]=SimpleMA(i,InpCCIPeriod,ExtPrice_H_Buffer);
      ExtMov_L_Buffer[i]=SimpleMA(i,InpCCIPeriod,ExtPrice_L_Buffer);
     }
//--- standard deviations and cci counting
   dMul=koeff/InpCCIPeriod;
   
   pos=InpCCIPeriod-1;
   
   if(pos<prev_calculated-1)
      pos=prev_calculated-2;
   i=pos;
   while(i<rates_total)
     {
      dSum=0.0;
	   dSumL=0.0;
	   dSumH=0.0;
	  
      k=i+1-InpCCIPeriod;
      while(k<=i)
        {
         dSum+=MathAbs(ExtPriceBuffer[k]-ExtMovBuffer[i]);
         
         dSumL+=MathAbs(ExtPrice_L_Buffer[k]-ExtMovBuffer[i]);
         // dSumL+=MathAbs(ExtPrice_L_Buffer[k]-ExtMov_L_Buffer[i]);
         dSumH+=MathAbs(ExtPrice_H_Buffer[k]-ExtMovBuffer[i]);
		   // dSumH+=MathAbs(ExtPrice_H_Buffer[k]-ExtMov_H_Buffer[i]);
         k++;
        }
      dSum*=dMul;
	   dSumL*=dMul;
	   dSumH*=dMul;
	  
      if(dSum==0.0)
         ExtCCIBuffer[i]=0.0;
      else
         ExtCCIBuffer[i]=(ExtPriceBuffer[i]-ExtMovBuffer[i])/dSum;
	 
  	  if(dSumH==0.0)
         ExtCCI_H_Buffer[i]=0.0;
      else
         //ExtCCI_H_Buffer[i]=(ExtPrice_H_Buffer[i]-ExtMov_H_Buffer[i])/dSumH;
         ExtCCI_H_Buffer[i]=(ExtPrice_H_Buffer[i]-ExtMovBuffer[i])/dSumH;

      if(dSumL==0.0)
         ExtCCI_L_Buffer[i]=0.0;
      else
         //ExtCCI_L_Buffer[i]=(ExtPrice_L_Buffer[i]-ExtMov_L_Buffer[i])/dSumL;	 
	      ExtCCI_L_Buffer[i]=(ExtPrice_L_Buffer[i]-ExtMovBuffer[i])/dSumL;
      i++;
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+

Comments