Navigation:Home > Content >

Dsergs-cSuperTrend_v2.3.mq4

Time: 2011-12-23 | Download file:Dsergs-cSuperTrend_v2.3.mq4

//+------------------------------------------------------------------+ 
//|                                           Dserg - Supertrend.mq4 | 
//|                                                                  | 
//|                                                                  | 
//+------------------------------------------------------------------+ 
#property copyright "" 
#property link      "" 

#property indicator_chart_window 
#property indicator_buffers 7 
#property indicator_color1 SpringGreen 
#property indicator_color2 Red 
#property indicator_color3 DeepSkyBlue 
#property indicator_color4 DeepSkyBlue 
#property indicator_color5 DeepSkyBlue 
#property indicator_color6 DeepPink 
#property indicator_color7 DarkOrange 


double TrendUp[]; 
double TrendDown[]; 
double ChannelUp[]; 
double ChannelDown[];

double ArrowUp[];
double ArrowDn[];
double Stop[];
 
int st = 0; 
extern double Dev = 2.0;
extern int DevPeriod = 20;
extern double r0 = 0.618;
extern double r1 = 0.382;
extern double t0 = 2.618;
extern bool constantChannel=true;
extern bool drawArrows = true;
extern bool reverseAtStop = false;

static bool NoUp;
static bool NoDn;

static double lastPrice; 

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 
//---- indicators 
   SetIndexStyle(0, DRAW_LINE, STYLE_SOLID, 2); 
   SetIndexBuffer(0, TrendUp); 
   SetIndexLabel(0, "TrendUP");
   SetIndexStyle(1, DRAW_LINE, STYLE_SOLID, 2); 
   SetIndexBuffer(1, TrendDown); 
   SetIndexLabel(1, "TrendDOWN");
    
   SetIndexStyle(2, DRAW_LINE, STYLE_DOT); 
   SetIndexBuffer(2, ChannelUp); 
   SetIndexLabel(2, "ChannelUP");
   SetIndexStyle(3, DRAW_LINE, STYLE_DOT); 
   SetIndexBuffer(3, ChannelDown); 
   SetIndexLabel(3, "ChannelDOWN");

   SetIndexStyle(4,DRAW_ARROW,0,2);
   SetIndexArrow(4, 233);        // стрелка для бай
   SetIndexBuffer(4, ArrowUp);      
   SetIndexLabel(4, "BUY");
   SetIndexEmptyValue(4, 0.0); 

   SetIndexStyle(5,DRAW_ARROW,0,2);
   SetIndexArrow(5, 234);        // стрелка для селл
   SetIndexBuffer(5, ArrowDn);   
   SetIndexLabel(5, "SELL");
   SetIndexEmptyValue(5, 0.0); 

   SetIndexStyle(6,DRAW_ARROW,0,2);
                                // стоп
   SetIndexBuffer(6, Stop);   
   SetIndexLabel(6, "STOP");
   SetIndexEmptyValue(6, 0.0); 

   NoUp = true;
   NoDn = true;
   lastPrice = 0;
   
//---- 
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
  { 
//---- 
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
  { 
    
   int limit, i, counter; 
   double Range, AvgRange, cciTrendNow, cciTrendPrevious, var; 

   int counted_bars = IndicatorCounted(); 
//---- check for possible errors 
   if(counted_bars < 0) return(-1); 
//---- last counted bar will be recounted 
   if(counted_bars > 0) counted_bars--; 

   limit=Bars-counted_bars; 
    
   for(i = limit; i >= 0; i--) { 
      cciTrendNow = iCCI(NULL, 0, 50, PRICE_TYPICAL, i); 
      cciTrendPrevious = iCCI(NULL, 0, 50, PRICE_TYPICAL, i+1); 
      
      //st = st * 100; 
      
      
      counter = i; 
      Range = 0; 
      AvgRange = 0; 
      for (counter = i; counter >= i-9; counter--) { 
         AvgRange = AvgRange + MathAbs(High[counter]-Low[counter]); 
      } 
      Range = AvgRange/10; 
      if (cciTrendNow >= st && cciTrendPrevious < st) { 
         TrendUp[i+1] = TrendDown[i+1]; 
      } 
      
      if (cciTrendNow <= st && cciTrendPrevious > st) { 
         TrendDown[i+1] = TrendUp[i+1]; 
      } 
      
      if (cciTrendNow >= st) { 
         TrendUp[i] = Low[i] - iATR(NULL, 0, 5, i);          
         if (TrendUp[i] < TrendUp[i+1]) { 
            TrendUp[i] = TrendUp[i+1]; 
         } 
      } 
      else if (cciTrendNow <= st) { 
         TrendDown[i] = High[i] + iATR(NULL, 0, 5, i); 
         if (TrendDown[i] > TrendDown[i+1]) { 
            TrendDown[i] = TrendDown[i+1]; 
         } 
      }
      
      //Рисуем каналы стандартного отклонения на основе линии супертренда
      double s0 = Dev*iStdDev(NULL,0,DevPeriod,0,MODE_SMA,PRICE_OPEN,i);
      if (TrendDown[i]==EMPTY_VALUE) {
         if (TrendUp[i+1]==TrendUp[i] && constantChannel) {
            ChannelUp[i] = ChannelUp[i+1]; 
            ChannelDown[i] = ChannelDown[i+1]; 
         } else {          
            ChannelUp[i] = TrendUp[i]+s0; 
            ChannelDown[i] = TrendUp[i]-s0; 
         }
      }
      if (TrendUp[i]==EMPTY_VALUE) {
         if (TrendDown[i+1]==TrendDown[i] && constantChannel) {
            ChannelUp[i] = ChannelUp[i+1]; 
            ChannelDown[i] = ChannelDown[i+1]; 
         } else {          
            ChannelUp[i] = TrendDown[i]+s0; 
            ChannelDown[i] = TrendDown[i]-s0; 
         }
      }
      //Рисуем сигналы
      if (drawArrows) {
         //Сигналы на покупку:
         //1. Цена отскочила от зелёной линии супертренда, докупаем по линии, и по фибе
         //2. Если нет сделок на покупку, то покупаем по пробитию ценой верхней границы канала (по закрытию бара)   
         double f1up = (1-r1)*TrendUp[i+1]+r1*ChannelDown[i+1];
         if (TrendDown[i+3]==EMPTY_VALUE && TrendDown[i+2]==EMPTY_VALUE && TrendDown[i+1]==EMPTY_VALUE &&
             TrendUp[i+1]==TrendUp[i+2] && TrendUp[i+2]==TrendUp[i+3] &&
             (Open[i]>f1up||Open[i+1]>f1up||Open[i+2]>f1up) && Low[i]f0up && ArrowUp[i+2]!=f0up && ArrowUp[i+3]!=f0up) {
            ArrowUp[i]=f0up;
            NoUp = false;
            NoDn = true;
         }

         if (TrendDown[i+1]==EMPTY_VALUE && Open[i]>ChannelUp[i+2] && NoUp) {
            ArrowUp[i]=Open[i];
            NoUp = false;
            NoDn = true;
         }

         //Сигналы на продажу:
         //1. Цена отскочила от красной линии супертренда, продаём по линии, и по фибе
         //2. Если нет сделок на продажу, то продаём по пробитию ценой нижней границы канала (по закрытию бара)    
         double f1dn = (1-r1)*TrendDown[i+1]+r1*ChannelUp[i+1];
         if (TrendUp[i+3]==EMPTY_VALUE && TrendUp[i+2]==EMPTY_VALUE && TrendUp[i+1]==EMPTY_VALUE &&
             TrendDown[i+1]==TrendDown[i+2] && TrendDown[i+2]==TrendDown[i+3] &&
             (Open[i]f1dn &&
             ArrowDn[i+2]==0.0 && ArrowDn[i+3]==0.0 ) {

            ArrowDn[i]=f1dn;
            NoDn = false;
            NoUp = true;
         }
         
         double f0dn = (1-r0)*TrendDown[i+1]+r0*ChannelUp[i+1];
         if (TrendUp[i+1]==EMPTY_VALUE && High[i]>f0dn && Low[i+1]TrendDown[i+1] && TrendDown[i+2]==EMPTY_VALUE) || (TrendDown[i+2]>TrendDown[i+1] && TrendUp[i+2]==EMPTY_VALUE) ))    )) {
            Stop[i]=Open[i];
            NoUp = true;
            if (reverseAtStop) {
               ArrowDn[i]=Open[i]-50*Point;
               NoDn=false;
            }
         }
         if (!NoDn && (Open[i]>ChannelUp[i+1] || (TrendDown[i+1]==EMPTY_VALUE &&
             ( (TrendDown[i+2]        

Recommend