Navigation:Home > Content >

Bands_Level.mq4

Time: 2015-05-22 | Download file:Bands_Level.mq4

//+------------------------------------------------------------------+
//|                                                   Band Level.mq4 |
//|                      Copyright © 2009, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, Darkkiller"
#property link      "http://www.metaquotes.net/"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 DimGray
#property indicator_color2 DimGray
#property indicator_color3 DimGray
//---- indicator parameters


extern int    BandsPeriod=365;
extern int    BandsShift=0;
extern double BandsDeviations=2.0;
extern bool   Show_Bands=false;

extern bool   Middle_Bands=true;
extern bool   Upper_Bands=true;
extern bool   Lower_Bands=true;
extern color uppercolor=Red;
extern color lowercolor=Blue;
extern color middlecolor=White;
extern color textcolor=DimGray;
extern int textsize=8;
extern int linewidth=1;
extern int linestyle=0;


int TimeFrame=0;
//---- buffers
double MovingBuffer[];
double UpperBuffer[];
double LowerBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

int deinit()
  {
   ObjectDelete("Upper"+TimeFrame);
   ObjectDelete("Lower"+TimeFrame);
   ObjectDelete("Middle"+TimeFrame);
   ObjectDelete("dk bandslevel");
      ObjectDelete("Uppertext"+TimeFrame);
   ObjectDelete("Lowertext"+TimeFrame);
   ObjectDelete("Middletext"+TimeFrame);
   return(0);
  }
  
int init()
  {
//---- indicators

   SetIndexBuffer(0,MovingBuffer);

   SetIndexBuffer(1,UpperBuffer);

   SetIndexBuffer(2,LowerBuffer);
  
   if (Show_Bands)
   SetIndexStyle(0,DRAW_LINE);
   else
   SetIndexStyle(0,DRAW_NONE);
      if (Show_Bands)
   SetIndexStyle(1,DRAW_LINE);
   else
   SetIndexStyle(1,DRAW_NONE);
      if (Show_Bands)
   SetIndexStyle(2,DRAW_LINE);
   else
   SetIndexStyle(2,DRAW_NONE);
//----
   SetIndexDrawBegin(0,BandsPeriod+BandsShift);
   SetIndexDrawBegin(1,BandsPeriod+BandsShift);
   SetIndexDrawBegin(2,BandsPeriod+BandsShift);
//----


   return(0);
  }
//+------------------------------------------------------------------+
//| Bollinger Bands                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k,counted_bars=IndicatorCounted();
   double deviation;
   double sum,oldval,newres;
//----
   if(Bars<=BandsPeriod) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=BandsPeriod;i++)
        {
         MovingBuffer[Bars-i]=EMPTY_VALUE;
         UpperBuffer[Bars-i]=EMPTY_VALUE;
         LowerBuffer[Bars-i]=EMPTY_VALUE;
        }
//----
   int limit=Bars-counted_bars;
   if(counted_bars>0) limit++;
   for(i=0; iBandsPeriod-1) i=Bars-counted_bars-1;
   while(i>=0)
     {
      sum=0.0;
      k=i+BandsPeriod-1;
      oldval=MovingBuffer[i];
      while(k>=i)
        {
         newres=Close[k]-oldval;
         sum+=newres*newres;
         k--;
        }
      deviation=BandsDeviations*MathSqrt(sum/BandsPeriod);
      UpperBuffer[i]=oldval+deviation;
      LowerBuffer[i]=oldval-deviation;
      i--;
     }
     
     if(TimeFrame==0) TimeFrame = Period();

   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr="TF0";
   } 
//----
if (Middle_Bands)
if (ObjectFind("Middle"+TimeFrame)==-1) CreateLine();
   ObjectSet("Middle"+TimeFrame,OBJPROP_TIME1,Time[0]);
   ObjectSet("Middle"+TimeFrame,OBJPROP_PRICE1,MovingBuffer[0]);
if (Middle_Bands)
ObjectCreate("Middletext"+TimeFrame, OBJ_TEXT, 0, Time[0], MovingBuffer[0]);
ObjectSetText("Middletext"+TimeFrame, "                              Middle Band  "+TimeFrameStr, textsize, "Arial", textcolor);

if (Upper_Bands)
if (ObjectFind("Upper"+TimeFrame)==-1) CreateLine1();
   ObjectSet("Upper"+TimeFrame,OBJPROP_TIME1,Time[0]);
   ObjectSet("Upper"+TimeFrame,OBJPROP_PRICE1,UpperBuffer[0]);
if (Upper_Bands)   
ObjectCreate("Uppertext"+TimeFrame, OBJ_TEXT, 0, Time[0], UpperBuffer[0]);
ObjectSetText("Uppertext"+TimeFrame, "                              Upper Band  "+TimeFrameStr, textsize, "Arial", textcolor);

if (Lower_Bands)   
   if (ObjectFind("Lower"+TimeFrame)==-1) CreateLine2();
   ObjectSet("Lower"+TimeFrame,OBJPROP_TIME1,Time[0]);
   ObjectSet("Lower"+TimeFrame,OBJPROP_PRICE1,LowerBuffer[0]);
if (Lower_Bands)   
ObjectCreate("Lowertext"+TimeFrame, OBJ_TEXT, 0, Time[0], LowerBuffer[0]);
ObjectSetText("Lowertext"+TimeFrame, "                              Lower Band  "+TimeFrameStr, textsize, "Arial", textcolor);   
   
   
   
      string signature = "dk bandslevel";
   ObjectDelete(signature);
   ObjectCreate( signature, OBJ_LABEL, 0, 0, 0 );
   ObjectSetText(signature,"|dk|",7, "Arial Bold", DimGray);
   ObjectSet( signature, OBJPROP_CORNER, 3 );
   ObjectSet( signature, OBJPROP_XDISTANCE, 10);
   ObjectSet( signature, OBJPROP_YDISTANCE, 1);
   return(0);
  }
  
  void CreateLine()
{
 SetIndexBuffer(0,MovingBuffer);
 ObjectCreate("Middle"+TimeFrame,OBJ_HLINE,0,0,0,0,0);
 ObjectSet("Middle"+TimeFrame,OBJPROP_COLOR,middlecolor);
 ObjectSet("Middle"+TimeFrame,OBJPROP_WIDTH,linewidth);
ObjectSet("Middle"+TimeFrame,OBJPROP_STYLE,linestyle);
}
void CreateLine1()
{
 SetIndexBuffer(1,UpperBuffer);
 ObjectCreate("Upper"+TimeFrame,OBJ_HLINE,0,0,0,0,0);
 ObjectSet("Upper"+TimeFrame,OBJPROP_COLOR,uppercolor);
 ObjectSet("Upper"+TimeFrame,OBJPROP_WIDTH,linewidth);
ObjectSet("Upper"+TimeFrame,OBJPROP_STYLE,linestyle);
}
  
void CreateLine2()
{
 SetIndexBuffer(2,LowerBuffer);
 ObjectCreate("Lower"+TimeFrame,OBJ_HLINE,0,0,0,0,0);
 ObjectSet("Lower"+TimeFrame,OBJPROP_COLOR,lowercolor);
 ObjectSet("Lower"+TimeFrame,OBJPROP_WIDTH,linewidth);
ObjectSet("Lower"+TimeFrame,OBJPROP_STYLE,linestyle);
}

Recommend