Navigation:Home > Content >

CCI_Woodie_-_averages.mq4

Time: 2015-01-22 | Download file:CCI_Woodie_-_averages.mq4

//+------------------------------------------------------------------+
//|                                                  CCI Woodie like |
//|                                                CCI woodie wy.mq4 |
//|                                                           mladen |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1  Green
#property indicator_color2  Red
#property indicator_color3  LimeGreen
#property indicator_color4  Orange
#property indicator_color5  LimeGreen
#property indicator_color6  Orange
#property indicator_color7  DimGray
#property indicator_color8  YellowGreen
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property indicator_width5  2
#property indicator_width6  2
#property indicator_width7  2

//
//
//
//
//

extern int   CCIPeriod      = 14;
extern int   TrendPeriod    =  6;
extern int   TurboCCIPeriod =  0;
extern int   Ma1Period      = 25;
extern int   Ma1Method      = 13;
extern int   Ma1Price       = PRICE_CLOSE;
extern double Ma1VShift     = 15;
extern int   Ma2Period      = 32;
extern int   Ma2Method      =  1;
extern int   Ma2Price       = PRICE_CLOSE;
extern double Ma2VShift     = -15;
extern bool  ShowMa1        = true;
extern bool  ShowMa2        = true;
extern int   YellowBarBack  = 500;
extern int   YellowBarWidth = 2;
extern color YellowBarColor = Yellow;
extern bool  ShowZones      = true;
extern color ZonesColor     = C'30,33,36';
extern color LinesColor     = C'50,53,56';
extern string UniqueID      = "CCI woodie 1";
extern string MaMethods   = "";
extern string __0         = "SMA";
extern string __1         = "EMA";
extern string __2         = "Double smoothed EMA";
extern string __3         = "Double EMA (DEMA)";
extern string __4         = "Triple EMA (TEMA)";
extern string __5         = "Smoothed MA";
extern string __6         = "Linear weighted MA";
extern string __7         = "Parabolic weighted MA";
extern string __8         = "Alexander MA";
extern string __9         = "Volume weghted MA";
extern string __10        = "Hull MA";
extern string __11        = "Triangular MA";
extern string __12        = "Sine weighted MA";
extern string __13        = "Linear regression (LSMA)";
extern string __14        = "IE/2";
extern string __15        = "NonLag MA";
extern string __16        = "Zero lag EMA";
extern string __17        = "Leader EMA";
extern string __18        = "Super smoother";
extern string __19        = "Smoother";

//
//
//
//
//

double buffer1[];
double buffer2[];
double buffer3[];
double buffer4[];
double buffer5[];
double buffer6[];
double buffer7[];
double buffer8[];
string ShortName;
int    Window;
int    NameLen;

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

int init()
{
   int ma1Style = DRAW_NONE; if (ShowMa1)  ma1Style = DRAW_ARROW;
   int ma2Style = DRAW_NONE; if (ShowMa2)  ma2Style = DRAW_ARROW;
   SetIndexBuffer(0,buffer1); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,buffer2); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,buffer3); SetIndexStyle(2,ma1Style); SetIndexArrow(2,159);
   SetIndexBuffer(3,buffer4); SetIndexStyle(3,ma1Style); SetIndexArrow(3,159);
   SetIndexBuffer(4,buffer5); SetIndexStyle(4,ma2Style); SetIndexArrow(4,159);
   SetIndexBuffer(5,buffer6); SetIndexStyle(5,ma2Style); SetIndexArrow(5,159);
   SetIndexBuffer(6,buffer7);
   SetIndexBuffer(7,buffer8);
         SetIndexLabel(0,"Trend up");
         SetIndexLabel(1,"Trend down");
         SetIndexLabel(2,"Close above "+getAverageName(Ma1Method));
         SetIndexLabel(3,"Close bellow "+getAverageName(Ma1Method));
         SetIndexLabel(4,"Close above "+getAverageName(Ma2Method));
         SetIndexLabel(5,"Close bellow "+getAverageName(Ma2Method));
         SetIndexLabel(6,"CCI");
         SetIndexLabel(7,"Turbo CCI");

   if (YellowBarBack==0) YellowBarBack=Bars;

   //
   //
   //
   //
   //

   TrendPeriod = MathMax(3,TrendPeriod);   
   ShortName   = UniqueID+" ("+CCIPeriod+","+TurboCCIPeriod+")";
   NameLen     = StringLen(ShortName);
   IndicatorShortName(ShortName);
   return(0);
}
int deinit()
{
   DeleteBounds();
   for (int i = ObjectsTotal(); i>=0; i--)
   {
         string name = ObjectName(i);
         if (StringSubstr(name,0,NameLen) == ShortName) ObjectDelete(name);
   }         
   return(0);
}

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

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

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
           limit=Bars-counted_bars;
           Window=WindowFind(ShortName);           

   //
   //
   //
   //
   //
   
   for (i=limit;i>=0;i--)
   {
      if (TurboCCIPeriod>1) buffer8[i] = iCCI(NULL,0,TurboCCIPeriod,PRICE_TYPICAL,i); 
                            buffer7[i] = iCCI(NULL,0,CCIPeriod     ,PRICE_TYPICAL,i);
            SetHisto(i);
            if (ShowMa1 || ShowMa2) SetMa(i);
   }
   
   //
   //
   //
   //
   //
   
   if (ShowZones) UpdateBounds();
   return(0);
}
  
  
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

void SetHisto(int i)
{
   int k,s;

   buffer1[i] = EMPTY_VALUE;
   buffer2[i] = EMPTY_VALUE;
   
   //
   //
   //
   //
   //

      if (buffer7[i]>0)
         if (buffer1[i+1] != EMPTY_VALUE) s = TrendPeriod;
         else
            for (k=1,s= 1; k 0) && (i < YellowBarBack) && (MathAbs(s) == (TrendPeriod-1)))
                     drawYellowBar(YellowName,i);
}

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

void SetMa(int i)
{
   double maValue1 = iCustomMa(Ma1Method,iMA(NULL,0,1,0,MODE_SMA,Ma1Price,i),Ma1Period,i,0);
   double maValue2 = iCustomMa(Ma2Method,iMA(NULL,0,1,0,MODE_SMA,Ma2Price,i),Ma2Period,i,1);
   buffer3[i] = EMPTY_VALUE;
   buffer4[i] = EMPTY_VALUE;
   buffer5[i] = EMPTY_VALUE;
   buffer6[i] = EMPTY_VALUE;
      if (Close[i] > maValue1) buffer3[i] = Ma1VShift;
      if (Close[i] < maValue1) buffer4[i] = Ma1VShift;
      if (Close[i] > maValue2) buffer5[i] = Ma2VShift;
      if (Close[i] < maValue2) buffer6[i] = Ma2VShift;
}

//
//
//
//
//

void DeleteBounds()
{
   ObjectDelete(ShortName+"-1"); ObjectDelete(ShortName+"-11"); ObjectDelete(ShortName+"-12");
   ObjectDelete(ShortName+"-2"); ObjectDelete(ShortName+"-21"); ObjectDelete(ShortName+"-22");
   ObjectDelete(ShortName+"-3"); ObjectDelete(ShortName+"-31"); ObjectDelete(ShortName+"-32");
}
void UpdateBounds()
{
   SetUpBound(ShortName+"-1", 300, 200);
   SetUpBound(ShortName+"-2", 100,-100);
   SetUpBound(ShortName+"-3",-200,-300);
}

//
//
//
//
//

void SetUpBound(string name, double up, double down)
{
   if (ObjectFind(name) == -1)
      {
         ObjectCreate(name,OBJ_RECTANGLE,Window,0,0);
            ObjectSet(name,OBJPROP_PRICE1,up);
            ObjectSet(name,OBJPROP_PRICE2,down);
            ObjectSet(name,OBJPROP_COLOR,ZonesColor);
            ObjectSet(name,OBJPROP_BACK,true);
            ObjectSet(name,OBJPROP_TIME1,iTime(NULL,0,Bars-1));
      }
   if (ObjectGet(name,OBJPROP_TIME2) != iTime(NULL,0,0))
       ObjectSet(name,OBJPROP_TIME2,    iTime(NULL,0,0));
      
   //
   //
   //
   //
   //
          
   if (ObjectFind(name+"1") == -1) drawLine(name+"1",up);
   if (ObjectGet(name+"1",OBJPROP_TIME2) != iTime(NULL,0,0))
       ObjectSet(name+"1",OBJPROP_TIME2,    iTime(NULL,0,0));
   if (ObjectFind(name+"2") == -1) drawLine(name+"2",down);
   if (ObjectGet(name+"2",OBJPROP_TIME2) != iTime(NULL,0,0))
       ObjectSet(name+"2",OBJPROP_TIME2,    iTime(NULL,0,0));
}

//
//
//
//
//

void drawLine(string name, double current)
{
   ObjectCreate(name,OBJ_TREND,Window,iTime(NULL,0,Bars-1),current,Time[0],current);
      ObjectSet(name,OBJPROP_COLOR,LinesColor);
      ObjectSet(name,OBJPROP_RAY,false);
      ObjectSet(name,OBJPROP_BACK,true);
}

//
//
//
//
//

void drawYellowBar(string name, int i)
{
   if (ObjectFind(name) == -1)
      ObjectCreate(name,OBJ_TREND,Window,Time[i],0,Time[i],buffer7[i]);
         ObjectSet(name,OBJPROP_COLOR,YellowBarColor);
         ObjectSet(name,OBJPROP_WIDTH,YellowBarWidth);
         ObjectSet(name,OBJPROP_RAY,false);
         ObjectSet(name,OBJPROP_BACK,true);
         ObjectSet(name,OBJPROP_PRICE2,buffer7[i]);
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

string methodNames[] = {"SMA","EMA","Double smoothed EMA","Double EMA","Tripple EMA","Smoothed MA","Linear weighted MA","Parabolic weighted MA","Alexander MA","Volume weghted MA","Hull MA","Triangular MA","Sine weighted MA","Linear regression","IE/2","NonLag MA","Zero lag EMA","Leader EMA","Super smoother","Smoothed"};
string getAverageName(int& method)
{
   int max = ArraySize(methodNames)-1;
      method=MathMax(MathMin(method,max),0); return(methodNames[method]);
}

//
//
//
//
//

#define _maWorkBufferx1 2
#define _maWorkBufferx2 4
#define _maWorkBufferx3 6
#define _maWorkBufferx5 10

double iCustomMa(int mode, double price, double length, int i, int instanceNo=0)
{
   int r = Bars-i-1;
   switch (mode)
   {
      case 0  : return(iSma(price,length,r,instanceNo));
      case 1  : return(iEma(price,length,r,instanceNo));
      case 2  : return(iDsema(price,length,r,instanceNo));
      case 3  : return(iDema(price,length,r,instanceNo));
      case 4  : return(iTema(price,length,r,instanceNo));
      case 5  : return(iSmma(price,length,r,instanceNo));
      case 6  : return(iLwma(price,length,r,instanceNo));
      case 7  : return(iLwmp(price,length,r,instanceNo));
      case 8  : return(iAlex(price,length,r,instanceNo));
      case 9  : return(iWwma(price,length,r,instanceNo));
      case 10 : return(iHull(price,length,r,instanceNo));
      case 11 : return(iTma(price,length,r,instanceNo));
      case 12 : return(iSineWMA(price,length,r,instanceNo));
      case 13 : return(iLinr(price,length,r,instanceNo));
      case 14 : return(iIe2(price,length,r,instanceNo));
      case 15 : return(iNonLagMa(price,length,r,instanceNo));
      case 16 : return(iZeroLag(price,length,r,instanceNo));
      case 17 : return(iLeader(price,length,r,instanceNo));
      case 18 : return(iSsm(price,length,r,instanceNo));
      case 19 : return(iSmooth(price,length,r,instanceNo));
      default : return(0);
   }
}

//------------------------------------------------------------------
//                                                                  
//------------------------------------------------------------------
//
//
//
//
//

double workSma[][_maWorkBufferx2];
double iSma(double price, int period, int r, int instanceNo=0)
{
   if (ArrayRange(workSma,0)!= Bars) ArrayResize(workSma,Bars); instanceNo *= 2;

   //
   //
   //
   //
   //
      
   workSma[r][instanceNo] = price;
   if (r>=period)
          workSma[r][instanceNo+1] = workSma[r-1][instanceNo+1]+(workSma[r][instanceNo]-workSma[r-period][instanceNo])/period;
   else { workSma[r][instanceNo+1] = 0; for(int k=0; k=0; k++) workSma[r][instanceNo+1] += workSma[r-k][instanceNo];  
          workSma[r][instanceNo+1] /= k; }
   return(workSma[r][instanceNo+1]);
}

//
//
//
//
//

double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars);

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workEma[r][instanceNo] = workEma[r-1][instanceNo]+alpha*(price-workEma[r-1][instanceNo]);
   return(workEma[r][instanceNo]);
}

//
//
//
//
//

double workDsema[][_maWorkBufferx2];
#define _ema1 0
#define _ema2 1

double iDsema(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workDsema,0)!= Bars) ArrayResize(workDsema,Bars); instanceNo*=2;

   //
   //
   //
   //
   //
      
   double alpha = 2.0 /(1.0+MathSqrt(period));
          workDsema[r][_ema1+instanceNo] = workDsema[r-1][_ema1+instanceNo]+alpha*(price                         -workDsema[r-1][_ema1+instanceNo]);
          workDsema[r][_ema2+instanceNo] = workDsema[r-1][_ema2+instanceNo]+alpha*(workDsema[r][_ema1+instanceNo]-workDsema[r-1][_ema2+instanceNo]);
   return(workDsema[r][_ema2+instanceNo]);
}

//
//
//
//
//

double workDema[][_maWorkBufferx2];
#define _ema1 0
#define _ema2 1

double iDema(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workDema,0)!= Bars) ArrayResize(workDema,Bars); instanceNo*=2;

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workDema[r][_ema1+instanceNo] = workDema[r-1][_ema1+instanceNo]+alpha*(price                        -workDema[r-1][_ema1+instanceNo]);
          workDema[r][_ema2+instanceNo] = workDema[r-1][_ema2+instanceNo]+alpha*(workDema[r][_ema1+instanceNo]-workDema[r-1][_ema2+instanceNo]);
   return(workDema[r][_ema1+instanceNo]*2.0-workDema[r][_ema2+instanceNo]);
}

//
//
//
//
//

double workTema[][_maWorkBufferx3];
#define _ema1 0
#define _ema2 1
#define _ema3 2

double iTema(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3;

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price                        -workTema[r-1][_ema1+instanceNo]);
          workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]);
          workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]);
   return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo]));
}

//
//
//
//
//

double workSmma[][_maWorkBufferx1];
double iSmma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workSmma,0)!= Bars) ArrayResize(workSmma,Bars);

   //
   //
   //
   //
   //

   if (r=0; k++)
      {
         double weight = period-k;
                sumw  += weight;
                sum   += weight*workLwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workLwmp[][_maWorkBufferx1];
double iLwmp(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workLwmp,0)!= Bars) ArrayResize(workLwmp,Bars);
   
   //
   //
   //
   //
   //
   
   workLwmp[r][instanceNo] = price;
      double sumw = period*period;
      double sum  = sumw*price;

      for(int k=1; k=0; k++)
      {
         double weight = (period-k)*(period-k);
                sumw  += weight;
                sum   += weight*workLwmp[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workAlex[][_maWorkBufferx1];
double iAlex(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workAlex,0)!= Bars) ArrayResize(workAlex,Bars);
   if (period<4) return(price);
   
   //
   //
   //
   //
   //

   workAlex[r][instanceNo] = price;
      double sumw = period-2;
      double sum  = sumw*price;

      for(int k=1; k=0; k++)
      {
         double weight = period-k-2;
                sumw  += weight;
                sum   += weight*workAlex[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workTma[][_maWorkBufferx1];
double iTma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workTma,0)!= Bars) ArrayResize(workTma,Bars);
   
   //
   //
   //
   //
   //
   
   workTma[r][instanceNo] = price;

      double half = (period+1.0)/2.0;
      double sum  = price;
      double sumw = 1;

      for(int k=1; k=0; k++)
      {
         double weight = k+1; if (weight > half) weight = period-k;
                sumw  += weight;
                sum   += weight*workTma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workSineWMA[][_maWorkBufferx1];
#define Pi 3.14159265358979323846264338327950288

double iSineWMA(double price, int period, int r, int instanceNo=0)
{
   if (period<1) return(price);
   if (ArrayRange(workSineWMA,0)!= Bars) ArrayResize(workSineWMA,Bars);
   
   //
   //
   //
   //
   //
   
   workSineWMA[r][instanceNo] = price;
      double sum  = 0;
      double sumw = 0;
  
      for(int k=0; k=0; k++)
      { 
         double weight = MathSin(Pi*(k+1.0)/(period+1.0));
                sumw  += weight;
                sum   += weight*workSineWMA[r-k][instanceNo]; 
      }
      return(sum/sumw);
}

//
//
//
//
//

double workWwma[][_maWorkBufferx1];
double iWwma(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workWwma,0)!= Bars) ArrayResize(workWwma,Bars);
   
   //
   //
   //
   //
   //
   
   workWwma[r][instanceNo] = price;
      int    i    = Bars-r-1;
      double sumw = Volume[i];
      double sum  = sumw*price;

      for(int k=1; k=0; k++)
      {
         double weight = Volume[i+k];
                sumw  += weight;
                sum   += weight*workWwma[r-k][instanceNo];  
      }             
      return(sum/sumw);
}

//
//
//
//
//

double workHull[][_maWorkBufferx2];
double iHull(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workHull,0)!= Bars) ArrayResize(workHull,Bars);

   //
   //
   //
   //
   //

      int HmaPeriod  = MathMax(period,2);
      int HalfPeriod = MathFloor(HmaPeriod/2);
      int HullPeriod = MathFloor(MathSqrt(HmaPeriod));
      double hma,hmw,weight; instanceNo *= 2;

         workHull[r][instanceNo] = price;

         //
         //
         //
         //
         //
               
         hmw = HalfPeriod; hma = hmw*price; 
            for(int k=1; k=0; k++)
            {
               weight = HalfPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][instanceNo];  
            }             
            workHull[r][instanceNo+1] = 2.0*hma/hmw;

         hmw = HmaPeriod; hma = hmw*price; 
            for(k=1; k=0; k++)
            {
               weight = HmaPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][instanceNo];
            }             
            workHull[r][instanceNo+1] -= hma/hmw;

         //
         //
         //
         //
         //
         
         hmw = HullPeriod; hma = hmw*workHull[r][instanceNo+1];
            for(k=1; k=0; k++)
            {
               weight = HullPeriod-k;
               hmw   += weight;
               hma   += weight*workHull[r-k][1+instanceNo];  
            }
   return(hma/hmw);
}

//
//
//
//
//

double workLinr[][_maWorkBufferx1];
double iLinr(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workLinr,0)!= Bars) ArrayResize(workLinr,Bars);

   //
   //
   //
   //
   //
   
      period = MathMax(period,1);
      workLinr[r][instanceNo] = price;
         double lwmw = period; double lwma = lwmw*price;
         double sma  = price;
         for(int k=1; k=0; k++)
         {
            double weight = period-k;
                   lwmw  += weight;
                   lwma  += weight*workLinr[r-k][instanceNo];  
                   sma   +=        workLinr[r-k][instanceNo];
         }             
   
   return(3.0*lwma/lwmw-2.0*sma/period);
}

//
//
//
//
//

double workIe2[][_maWorkBufferx1];
double iIe2(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workIe2,0)!= Bars) ArrayResize(workIe2,Bars);

   //
   //
   //
   //
   //
   
      period = MathMax(period,1);
      workIe2[r][instanceNo] = price;
         double sumx=0, sumxx=0, sumxy=0, sumy=0;
         for (int k=0; k0)
   {
      double sum = 0;
           for (k=0; k < nlm.values[_len][instanceNo]; k++) sum += nlm.alphas[k][instanceNo]*nlm.prices[r-k][instanceNo];
           return( sum / nlm.values[_weight][instanceNo]);
   }
   else return(0);           
}

Recommend