Author: Copyright � 2011, Serg Deev
Indicators Used
Moving average indicatorStandard Deviation indicator
Miscellaneous
Implements a curve of type %1
0 Views
0 Downloads
0 Favorites
SqrtMA-v2
//+------------------------------------------------------------------+
//|                                          Sqrt Moving Average     |
//|                                      Copyright © 2011, Serg Deev |
//|                                       http://www.work2it.ru      |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Serg Deev"
#property link      "http://www.work2it.ru"

#property indicator_chart_window
#property indicator_buffers 7
#property indicator_color1 Green
#property indicator_color2 Yellow
#property indicator_color3 Red  
#property indicator_color4 Black
#property indicator_color5 Brown 
#property indicator_color6 Teal 
#property indicator_color7 White
//---- indicator parameters
extern int MA_Period          =6;
extern int MA_Method          =0; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
extern int MA_Price           =0; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int Std_Period         =6;
extern double Std_Level       =0.001;
//---- indicator buffers
double M[],MS[],MX[],MD[],MX_Up[],MX_Middle[],MX_Down[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
   SetIndexBuffer(0,MX_Up);
   SetIndexBuffer(1,MX_Middle);
   SetIndexBuffer(2,MX_Down);
   SetIndexBuffer(3,M);
   SetIndexBuffer(4,MS);
   SetIndexBuffer(5,MX);
   SetIndexBuffer(6,MD);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);
   SetIndexStyle(3,DRAW_NONE);
   SetIndexStyle(4,DRAW_NONE);
   SetIndexStyle(5,DRAW_NONE);
   SetIndexStyle(6,DRAW_NONE);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   if(MA_Period<=1) MA_Period=5;
   if(MA_Method < 0) MA_Method = 0;
   if(MA_Method > 3) MA_Method = 3;
   if(MA_Price < 0) MA_Price = 0;
   if(MA_Price > 6) MA_Price = 6;
   switch(MA_Method)
     {
      case 0 : short_name="SqrtSMA("; break;
      case 1 : short_name="SqrtEMA("; break;
      case 2 : short_name="SqrtSMMA("; break;
      case 3 : short_name="SqrtLWMA("; break;
     }
   IndicatorShortName(short_name+MA_Period+")");
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start() 
  {
   int i,j;
   double sum,prev,x;
   if(Bars<=MA_Period) return(0);

   int counted_bars = IndicatorCounted();
   if(counted_bars < 0)  return(-1);
   if(counted_bars > 0)   counted_bars--;
   int limit = Bars - counted_bars;
   if(counted_bars==0) limit-=1+MA_Period;   
   
   for(i=limit; i>=0; i--) 
     {
      switch(MA_Price) 
        {
         case 0: x = Open[i]; break;
         case 1: x = Close[i]; break;
         case 2: x = High[i]; break;
         case 3: x = Low[i]; break;
         case 4: x = (High[i]+Low[i])/2; break;
         case 5: x = (High[i]+Low[i]+Close[i])/3; break;
         case 6: x = (High[i]+Low[i]+Close[i]*2)/4; break;
        }
      M[i]=x;
     }
   for(i=limit; i>=0; i--) 
     {
      switch(MA_Method) 
        {
         case 0: x = iMAOnArray(M,0,MA_Period,0,MODE_SMA,i); break;
         case 1: x = iMAOnArray(M,0,MA_Period,0,MODE_EMA,i); break;
         case 2: x = iMAOnArray(M,0,MA_Period,0,MODE_SMMA,i); break;
         case 3: x = iMAOnArray(M,0,MA_Period,0,MODE_LWMA,i); break;
        }
      MS[i]=x;
     }

   for(i=limit; i>=0; i--) 
     {
      sum = 0;
      for(j=i; j<(i+MA_Period); j++) 
        {
         x=MS[j]-M[j];
         if(x>=0) sum=sum+x*x;
         else sum=sum-x*x;
        }
      x=iStdDev(NULL,0,Std_Period,0,MA_Method,MA_Price,i);
      if(sum>=0) 
        {
         MX[i] = MS[i]-MathSqrt(sum/MA_Period);
         MD[i] = MS[i];
         if(x>Std_Level) MD[i]=MD[i]-2*x;
        }
      else if(sum<0) 
        {
         MX[i] = MS[i]+MathSqrt(MathAbs(sum)/MA_Period);
         MD[i] = MS[i];
         if(x>Std_Level) MD[i]=MD[i]+2*x;
        }
     }
   for(i=limit; i>=0; i--) 
     {
      if((MX[i]>MS[i]) && (MD[i]>MX[i])) 
        {
         MX_Up[i]=MX[i];
         if((MX_Middle[i+1]>0) || (MX_Down[i+1]>0)) MX_Up[i+1]=MX[i+1];
        }
      else if((MX[i]<MS[i]) && (MD[i]<MX[i])) 
        {
         MX_Down[i]=MX[i];
         if((MX_Middle[i+1]>0) || (MX_Up[i+1]>0)) MX_Down[i+1]=MX[i+1];
        }
      else 
        {
         MX_Middle[i]=MX[i];
         if((MX_Down[i+1]>0) || (MX_Up[i+1]>0)) MX_Middle[i+1]=MX[i+1];
        }
     }

   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 ---