SuperTrend_002

Author: Copyright � 2010, Cyril Dumas.
SuperTrend_002
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
SuperTrend_002
//+------------------------------------------------------------------+
//|                                              SuperTrend.mq4 v1   |
//|                   Copyright © 2008, Cyril Dumas                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Cyril Dumas."

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Lime
#property indicator_width1 1
#property indicator_color2 Red
#property indicator_width2 1
/*
#property indicator_color3 Blue
#property indicator_width3 2
#property indicator_color4 Green
#property indicator_width4 2
*/

//---extern
extern int  CalcPriceType = 1;    // type of price calculation over which average will be calculated
extern int  TrendPriceType = 1;   // type of price calculation over which trend direction will be calculated
extern int  VolatPeriod = 3;      // period to look for volatility
extern double  VolatCoeff = 1.8;     // distance from average, multiply of volatility
extern int  AvgToVolatRatio = 10; // period to calculate average, multiply of volatility period
extern int  UpDnCalcType = 6;     // type of calculation for stops

//---- buffers
double AvgPrice[],Stop[];
//double Up[],Dn[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
 //  IndicatorBuffers(11);
   
   SetIndexBuffer(0, AvgPrice);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexLabel(0, "Average Price");
   SetIndexBuffer(1, Stop);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexLabel(1, "Stop");
   string short_name = "SuperTrend";
   IndicatorShortName(short_name);
/* 
   SetIndexBuffer(2, Up);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexLabel(2, "Up");
   SetIndexBuffer(3, Dn);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexLabel(3, "Dn");
   */
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
      
   int limit;
   int i;
   double VolatUpCoeff;
   double VolatDnCoeff;
   double HH[];
   double LL[];
   double CalcPrice[];
   double TrendPrice[];
   double Volat[];
   double AvgVolat[];
   double AvgPeriod;
   double Up[];
   double Dn[];
   double Trend[];
   double temp1[];
   double temp2[];

   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if(counted_bars<0) return(-1);
   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   VolatUpCoeff = MathAbs(VolatCoeff);
   VolatDnCoeff = -MathAbs(VolatCoeff);
   AvgPeriod = AvgToVolatRatio*VolatPeriod;
   //limit=Bars-counted_bars+AvgPeriod+2;
   limit=Bars;
   
   //limit=Bars;
   //---- main loop  
   
   ArrayResize( HH, limit); //resize the array to the number of bars
   ArraySetAsSeries(HH,true);
   ArrayResize( LL, limit); //resize the array to the number of bars
   ArraySetAsSeries(LL,true);
   ArrayResize( CalcPrice, limit); //resize the array to the number of bars
   ArraySetAsSeries(CalcPrice,true);
   ArrayResize( TrendPrice, limit); //resize the array to the number of bars
   ArraySetAsSeries(TrendPrice,true);
   ArrayResize( Volat, limit); //resize the array to the number of bars
   ArraySetAsSeries(Volat,true);
   ArrayResize( AvgVolat, limit); //resize the array to the number of bars
   ArraySetAsSeries(AvgVolat,true);
   ArrayResize( Up, limit); //resize the array to the number of bars
   ArraySetAsSeries(Up,true);
   ArrayResize( Dn, limit); //resize the array to the number of bars
   ArraySetAsSeries(Dn,true);
   ArrayResize( Trend, limit); //resize the array to the number of bars
   ArraySetAsSeries(Trend,true);
   ArrayResize( temp1, limit); //resize the array to the number of bars
   ArraySetAsSeries(temp1,true);
   ArrayResize( temp2, limit); //resize the array to the number of bars
   ArraySetAsSeries(temp2,true);
   
  
   
  if(VolatPeriod>0)
  {   
      if (limit<=0)
      {
         HH[limit] = High[limit];
         LL[limit] = Low[limit];
      }

      for (i=0;i<limit;i++)
      {    
         HH[i] = High[iHighest(NULL,0,MODE_HIGH,VolatPeriod,i)];
         LL[i] = Low[iLowest(NULL,0,MODE_LOW,VolatPeriod,i)];
      }      
   }
  
  if (CalcPriceType == 1)
  {
      for (i=0;i<limit;i++)
         CalcPrice[i]=Close[i];
  }
  
  if (CalcPriceType == 2)
  {
      for (i =0;i<limit;i++)
         CalcPrice[i]=(HH[i]+LL[i])/2;
  }
  
  if (CalcPriceType == 3)
  {
      for (i =0;i<limit;i++)
         CalcPrice[i]=(HH[i]+LL[i]+Close[i])/3;
  }
  
  if (CalcPriceType == 4)
  {
         for (i=0;i<limit;i++)
            CalcPrice[i]=Close[i];
         for (i=(limit-VolatPeriod+1);i>=0;i--) 
         {
            CalcPrice[i] = (HH[i]+LL[i]+Close[i]+Low[i+VolatPeriod-1])/4;
         }
  }    
  
  if (CalcPriceType == 5)
  {
         for (i =0;i<limit;i++)
            CalcPrice[i]=Close[i];
         for (i=(limit-VolatPeriod);i>=0;i--) 
         {
            CalcPrice[i] = (HH[i]+LL[i]+Close[i+VolatPeriod])/3;
         }
  }    
      
  if (CalcPriceType == 6)
  {
         for (i=0;i<limit;i++)
            CalcPrice[i]=Close[i];
         for (i=(limit-VolatPeriod+1);i>=0;i--) 
         {
            CalcPrice[i] = (HH[i]+LL[i]+Close[i+VolatPeriod]+Low[i+VolatPeriod-1])/4;
         }
         
  }    
    
  if (TrendPriceType == 1)
  {
      for (i=0;i<limit;i++)
         TrendPrice[i]=Close[i];
  }
  if (TrendPriceType == 2)
  {
      for (i=0;i<limit;i++)
         TrendPrice[i]=(HH[i]+LL[i])/2;
  }
  if (TrendPriceType == 3)
  {
      for (i=0;i<limit;i++)
         TrendPrice[i]=(HH[i]+LL[i]+Close[i])/3;
  }
  if (TrendPriceType == 4)
  {
         for (i =0;i<limit;i++)
            TrendPrice[i]=Close[i];
         for (i=(limit-VolatPeriod+1);i>=0;i--) 
         {
            TrendPrice[i] = (HH[i]+LL[i]+Close[i]+Low[i+VolatPeriod-1])/4;
         }
  }    
  if (TrendPriceType == 5)
  {
         for (i=0;i<limit;i++)
            TrendPrice[i]=Close[i];
         for (i=(limit-VolatPeriod);i>=0;i--) 
         {
            TrendPrice[i] = (HH[i]+LL[i]+Close[i+VolatPeriod])/3;
         }
  }  
  if (TrendPriceType == 6)
  {
         for (i=0;i<limit;i++)
            TrendPrice[i]=Close[i];
         for (i=(limit-VolatPeriod+1);i>=0;i--) 
         {
            TrendPrice[i] = (HH[i]+LL[i]+Close[i+VolatPeriod]+Low[i+VolatPeriod-1])/4;
         }
  }   



  for (i =0; i <limit; i++)
  {
      Volat[i] = (HH[i] - LL[i])/2;
      Up[i] = CalcPrice[i];
      Dn[i] = CalcPrice[i];
      Trend[i]=0;
  }
  
   
  if (AvgToVolatRatio > 1)
  {
      if (UpDnCalcType==1 || UpDnCalcType==2)
      {
         for (i=0;i<limit;i++)
         {
            AvgPrice[i] = iMAOnArray(CalcPrice,0,AvgPeriod,0,MODE_SMA,i); 
            AvgVolat[i] = iMAOnArray(Volat,0,AvgPeriod,0,MODE_SMA,i); 
            Up[i] = AvgPrice[i]+VolatUpCoeff*AvgVolat[i]; 
            Dn[i] = AvgPrice[i]+VolatDnCoeff*AvgVolat[i];
         }
      }
      if (UpDnCalcType==3 || UpDnCalcType==4)
      {
         for (i=0;i<limit;i++)
         {
            AvgPrice[i] = iMAOnArray(CalcPrice,0,AvgPeriod,0,MODE_EMA,i);
            AvgVolat[i] = iMAOnArray(Volat,0,AvgPeriod,0,MODE_EMA,i); 
            Up[i] = AvgPrice[i]+VolatUpCoeff*AvgVolat[i]; 
            Dn[i] = AvgPrice[i]+VolatDnCoeff*AvgVolat[i];
         }
      }
      if (UpDnCalcType==5 || UpDnCalcType==6)
      {
         for (i=0;i<limit;i++)
         {
            AvgPrice[i] = iMAOnArray(CalcPrice,0,AvgPeriod,0,MODE_LWMA,i);
            AvgVolat[i] = iMAOnArray(Volat,0,AvgPeriod,0,MODE_LWMA,i);
            Up[i] = AvgPrice[i]+VolatUpCoeff*AvgVolat[i];
            Dn[i] = AvgPrice[i]+VolatDnCoeff*AvgVolat[i];
         }
      }
      if (UpDnCalcType==7 || UpDnCalcType==8)
      {
         for (i=0;i<limit;i++)
            temp1[i] = 2*iMAOnArray(CalcPrice,0,MathRound(AvgPeriod/2),0,MODE_LWMA,i)-iMAOnArray(CalcPrice,0,AvgPeriod,0,MODE_LWMA,i);
         for (i=0;i<limit;i++)
            temp2[i] = 2*iMAOnArray(Volat,0,MathRound(AvgPeriod/2),0,MODE_LWMA,i)-iMAOnArray(Volat,0,AvgPeriod,0,MODE_LWMA,i);  
         for (i=0;i<limit;i++)
         {
            AvgPrice[i] = iMAOnArray(temp1,0,MathRound(MathSqrt(AvgPeriod)),0,MODE_LWMA,i); 
            AvgVolat[i] = iMAOnArray(temp2,0,MathRound(MathSqrt(AvgPeriod)),0,MODE_LWMA,i); 
            Up[i] = AvgPrice[i]+VolatUpCoeff*AvgVolat[i]; 
            Dn[i] = AvgPrice[i]+VolatDnCoeff*AvgVolat[i];
         }
      }
  }
  else
  {
      for (i=0;i<limit;i++)
      {
         AvgPrice[i] = CalcPrice[i]; 
         Up[i] = CalcPrice[i]+VolatUpCoeff*Volat[i]; 
         Dn[i] = CalcPrice[i]+VolatDnCoeff*Volat[i];
      } 
  }

    for (i=(limit-AvgPeriod-1);i>=0;i--)
    {
         if (AvgToVolatRatio > 1 && (UpDnCalcType == 2 || UpDnCalcType == 4 || UpDnCalcType == 6 || UpDnCalcType == 8))
         {
            if (Trend[i+1] < 0)
            {
               if (Up[i] > Up[i+1])
                  Up[i] = Up[i+1];
            }
            if (Trend[i+1] >= 0)
            {
               if (Dn[i]<Dn[i+1])
                  Dn[i] = Dn[i+1];
            }
         }
         
         Trend[i] = Trend[i+1];
         if (TrendPrice[i] > Up[i])
            Trend[i] = 1;
         if (TrendPrice[i] < Dn[i]) 
            Trend[i] = -1;
    }
      
    for (i=(limit-AvgPeriod);i>=0;i--)
    {
      if (Trend[i] >= 0)
         Stop[i] = Dn[i];
      else
         Stop[i] = Up[i];        
    }  
  
//----
   return(0);
  }
//+------------------------------------------------------------------+


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---