//+------------------------------------------------------------------+
//| Bands.mq4 |
//| Copyright 2005-2014, MetaQuotes Software Corp. |
//| http://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "2005-2014, MetaQuotes Software Corp."
#property link "http://www.mql4.com"
#property description "Bollinger Bands"
#property strict
#include <MovingAverages.mqh>
#property indicator_chart_window
#property indicator_buffers 5 //此段落是定义画线的数量
#property indicator_color1 Blue
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Red
#property indicator_color5 Yellow
//--- indicator parameters
input int InpBandsPeriod=20; // Bands Period
input int InpBandsShift=0; // Bands Shift
input double InpBandsDeviations=2.0; // Bands Deviations
input int price_type=1 ; //1,代表用close;2代表high
//--- buffers
double ExtMovingBuffer[];
double ExtUpperBuffer[];
double ExtLowerBuffer[];
double ExtStdDevBuffer[];
double ExtHighUpBuffer[];
double ExtLowDownBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit(void)
{
//--- 1 additional buffer used for counting.
IndicatorBuffers(6); //此段落是定义的是程序使用的缓存数量
IndicatorDigits(Digits);
//--- middle line
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMovingBuffer);
SetIndexShift(0,InpBandsShift);
SetIndexLabel(0,"Bands SMA");
//--- upper band
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtUpperBuffer);
SetIndexShift(1,InpBandsShift);
SetIndexLabel(1,"Bands Upper");
//--- lower band
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtLowerBuffer);
SetIndexShift(2,InpBandsShift);
SetIndexLabel(2,"Bands Lower");
//--- HighUp band 属性设置,就是画线
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,ExtHighUpBuffer);
SetIndexShift(3,InpBandsShift);
SetIndexLabel(3,"Bands HighUp");
//--- LowDown band 属性设置,就是画线
SetIndexStyle(4,DRAW_LINE);
SetIndexBuffer(4,ExtLowDownBuffer);
SetIndexShift(4,InpBandsShift);
SetIndexLabel(4,"Bands LowDown");
//--- work buffer
SetIndexBuffer(5,ExtStdDevBuffer);
//--- check for input parameter
if(InpBandsPeriod<=0)
{
Print("Wrong input parameter Bands Period=",InpBandsPeriod);
return(INIT_FAILED);
}
//---
SetIndexDrawBegin(0,InpBandsPeriod+InpBandsShift);
SetIndexDrawBegin(1,InpBandsPeriod+InpBandsShift);
SetIndexDrawBegin(2,InpBandsPeriod+InpBandsShift);
//--- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Bollinger Bands |
//+------------------------------------------------------------------+
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,pos;
//---
if(rates_total<=InpBandsPeriod || InpBandsPeriod<=0)
return(0);
//--- counting from 0 to rates_total
ArraySetAsSeries(ExtMovingBuffer,false);
ArraySetAsSeries(ExtUpperBuffer,false);
ArraySetAsSeries(ExtLowerBuffer,false);
ArraySetAsSeries(ExtStdDevBuffer,false);
ArraySetAsSeries(close,false);
//--- initial zero
if(prev_calculated<1)
{
for(i=0; i<InpBandsPeriod; i++)
{
ExtMovingBuffer[i]=EMPTY_VALUE;
ExtUpperBuffer[i]=EMPTY_VALUE;
ExtLowerBuffer[i]=EMPTY_VALUE;
}
}
//--- starting calculation
if(prev_calculated>1)
pos=prev_calculated-1;
else
pos=0;
//--- main cycle
for(i=pos; i<rates_total && !IsStopped(); i++)
{
//--- middle line
ExtMovingBuffer[i]=SimpleMA(i,InpBandsPeriod,close);
//--- calculate and write down StdDev
if( price_type==1)
ExtStdDevBuffer[i]=StdDev_Func(i,close,ExtMovingBuffer,InpBandsPeriod);
else if( price_type==2)
ExtStdDevBuffer[i]=StdDev_Func(i,high,ExtMovingBuffer,InpBandsPeriod);//多增加的一条语句
//--- upper line
ExtUpperBuffer[i]=ExtMovingBuffer[i]+InpBandsDeviations*ExtStdDevBuffer[i];
//--- lower line
ExtLowerBuffer[i]=ExtMovingBuffer[i]-InpBandsDeviations*ExtStdDevBuffer[i];
//--- high up line
if( close[i] > ExtMovingBuffer[i] )
ExtHighUpBuffer[i]=ExtMovingBuffer[i]+MathAbs(high[i]-ExtMovingBuffer[i]);
//--- low up line
else if( close[i] < ExtMovingBuffer[i] )
ExtLowDownBuffer[i]=ExtMovingBuffer[i]-MathAbs(low[i]-ExtMovingBuffer[i]);
}
//---- OnCalculate done. Return new prev_calculated.
return(rates_total);
}
//+------------------------------------------------------------------+
//| Calculate Standard Deviation |
//+------------------------------------------------------------------+
double StdDev_Func(int position,const double &price[],const double &MAprice[],int period)
{
//--- variables
double StdDev_dTmp=0.0;
//--- check for position
if(position>=period)
{
//--- calcualte StdDev
for(int i=0; i<period; i++)
StdDev_dTmp+=MathPow(price[position-i]-MAprice[position],2);
StdDev_dTmp=MathSqrt(StdDev_dTmp/period);
}
//--- return calculated value
return(StdDev_dTmp);
}
//+------------------------------------------------------------------+
|