volatility_stepchannel

Author: Copyright 2015, fxborg
0 Views
0 Downloads
0 Favorites
volatility_stepchannel
ÿþ//+------------------------------------------------------------------+

//|                                       Volatility_StepChannel.mq5 |

//| Volatility StepChannel v1.03              Copyright 2015, fxborg |

//|                                   http://fxborg-labo.hateblo.jp/ |

//+------------------------------------------------------------------+

#property copyright "Copyright 2015, fxborg"

#property link      "http://fxborg-labo.hateblo.jp/"

#property version   "1.03"

//---

#include <MovingAverages.mqh>

//---

#property indicator_buffers 12

#property indicator_plots   3

#property indicator_chart_window

#property indicator_type1 DRAW_LINE

#property indicator_type2 DRAW_LINE

#property indicator_type3 DRAW_LINE

//---

#property indicator_color1 DeepPink

#property indicator_width1 2

#property indicator_style1 STYLE_SOLID

#property indicator_color2 DarkGray

#property indicator_width2 1

#property indicator_style2 STYLE_DOT

#property indicator_color3 DarkGray

#property indicator_width3 1

#property indicator_style3 STYLE_DOT



//--- input parameters

input double InpScaleFactor=2.25; // Scale factor

input int    InpMaPeriod=3;       // Smooth Period

input int    InpVolatilityPeriod=70; //  Volatility Period

//---

int    InpFastPeriod=int(InpVolatilityPeriod/7); //  Fast Period

int    InpMode=2; //  1:Simple Mode 2:hybrid Mode

ENUM_MA_METHOD InpMaMethod=MODE_SMMA; // Ma Method 



//---- will be used as indicator buffers

double UpperBuffer[];

double MiddleBuffer[];

double LowerBuffer[];

//---

double UpperMaBuffer[];

double MiddleMaBuffer[];

double LowerMaBuffer[];

//---

double BaseBuffer[];

//---

double HighBuffer[];

double LowBuffer[];

double CloseBuffer[];

double StdDevBuffer[];

double StdDevCalcBuffer[];

//---- declaration of global variables

int min_rates_total;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//---- Initialization of variables of data calculation starting point

   min_rates_total=1+InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod+1;

//--- indicator buffers mapping

//--- indicator buffers

   SetIndexBuffer(0,MiddleMaBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,UpperMaBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,LowerMaBuffer,INDICATOR_DATA);

//---

   SetIndexBuffer(3,UpperBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,BaseBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(5,MiddleBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,LowerBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,HighBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,LowBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,CloseBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(10,StdDevBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(11,StdDevCalcBuffer,INDICATOR_CALCULATIONS);

//---

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(8,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(9,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(10,PLOT_EMPTY_VALUE,0);

   PlotIndexSetDouble(11,PLOT_EMPTY_VALUE,0);

//---

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,min_rates_total);

   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,min_rates_total);

//---

   string short_name="CCI on StepChannel";

//---

   IndicatorSetString(INDICATOR_SHORTNAME,short_name);

//---

   return(INIT_SUCCEEDED);

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

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,j,first;

//--- check for bars count

   if(rates_total<=min_rates_total)

      return(0);

//---

   MathSrand(int(TimeLocal()));

//--- indicator buffers

   ArraySetAsSeries(UpperMaBuffer,false);

   ArraySetAsSeries(LowerMaBuffer,false);

   ArraySetAsSeries(MiddleMaBuffer,false);

   ArraySetAsSeries(UpperBuffer,false);

   ArraySetAsSeries(LowerBuffer,false);

   ArraySetAsSeries(MiddleBuffer,false);

   ArraySetAsSeries(HighBuffer,false);

   ArraySetAsSeries(LowBuffer,false);

   ArraySetAsSeries(CloseBuffer,false);

   ArraySetAsSeries(StdDevBuffer,false);

   ArraySetAsSeries(BaseBuffer,false);

//--- rate data

   ArraySetAsSeries(high,false);

   ArraySetAsSeries(low,false);

   ArraySetAsSeries(close,false);

   ArraySetAsSeries(time,false);

//---

//+----------------------------------------------------+

//| Set Median Buffeer                                 |

//+----------------------------------------------------+

   first=InpFastPeriod+InpVolatilityPeriod+InpMaPeriod+InpMaPeriod;

   if(first+1<prev_calculated)

      first=prev_calculated-2;

   else

     {

      for(i=0; i<first; i++)

        {

         MiddleBuffer[i]=close[i];

         HighBuffer[i]=high[i];

         LowBuffer[i]=low[i];

        }

     }

//---

   for(i=first; i<rates_total && !IsStopped(); i++)

     {

      StdDevBuffer[i]=calcStdDev(close,InpFastPeriod,i);

      //---

      double h,l,c,hsum=0.0,lsum=0.0,csum=0.0;

      //---

      for(j=0;j<InpMaPeriod;j++)

        {

         hsum += high[i-j];

         lsum += low[i-j];

         csum += close[i-j];

        }

      //---

      h=hsum/InpMaPeriod;

      l=lsum/InpMaPeriod;

      c=csum/InpMaPeriod;

      //--- Base Volatility

      double sd=0.0;

      for(j=(i-InpVolatilityPeriod+1);j<=i;j++)

         sd+=StdDevBuffer[j];

      //--- Ma Buffer

      double v=sd/InpVolatilityPeriod;

      BaseBuffer[i]=v;

      double base=v*InpScaleFactor;

      //--- Hybrid Mode

      if((h-base)>HighBuffer[i-1]) HighBuffer[i]=h;

      else if(h+base<HighBuffer[i-1]) HighBuffer[i]=h+base;

      else HighBuffer[i]=HighBuffer[i-1];

      //---

      if(l+base<LowBuffer[i-1]) LowBuffer[i]=l;

      else if((l-base)>LowBuffer[i-1]) LowBuffer[i]=l-base;

      else LowBuffer[i]=LowBuffer[i-1];

      //---

      if((c-base/2)>CloseBuffer[i-1]) CloseBuffer[i]=c-base/2;

      else if(c+base/2<CloseBuffer[i-1]) CloseBuffer[i]=c+base/2;

      else CloseBuffer[i]=CloseBuffer[i-1];

      //---

      MiddleBuffer[i]=(HighBuffer[i]+LowBuffer[i]+CloseBuffer[i]*2)/4;

      UpperBuffer[i]=HighBuffer[i] + base/2;

      LowerBuffer[i]=LowBuffer[i]  - base/2;

      //---

      hsum=0.0;

      lsum=0.0;

      csum=0.0;

      //---

      for(j=0;j<InpMaPeriod;j++)

        {

         hsum += UpperBuffer[i-j];

         lsum += LowerBuffer[i-j];

         csum += MiddleBuffer[i-j];

        }

      //---

      UpperMaBuffer[i]=hsum/InpMaPeriod;

      LowerMaBuffer[i]=lsum/InpMaPeriod;

      MiddleMaBuffer[i]=csum/InpMaPeriod;

     }

//----    

   return(rates_total);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

double calcStdDev(const double  &close[],int span,int i)

  {

   if(i<span-1)   return 0.0;

//---

   if(i==span-1)

      StdDevCalcBuffer[i]=SimpleMA(i,span,close);

   else

      StdDevCalcBuffer[i]=SmoothedMA(i,span,StdDevCalcBuffer[i-1],close);

//---

   double sum=0.0;

   for(int j=0;j<span;j++)

      sum+=MathPow(close[i-j]-StdDevCalcBuffer[i],2);

   return MathSqrt(sum/span);

  }

//+------------------------------------------------------------------+

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