Bands Level

Download script
Author: Copyright � 2009, Darkkiller
Bands Level

This script is a technical analysis tool designed for the MetaTrader 4 platform. It functions by calculating and displaying specific price boundaries—known as Bollinger Bands—to help traders visualize market volatility and identify potential trend shifts or exhaustion points.

Core Functionality

The script analyzes historical price data to determine a "normal" range for an asset. It accomplishes this through three primary components:

  • The Middle Band: This represents the baseline average price over a specific period (determined by the user). It serves as the equilibrium point for the market.
  • The Upper and Lower Bands: These are calculated based on the distance of current prices from the middle average. They act as "envelopes" that expand when the market is volatile and contract when the market is calm.

How the Logic Works

  1. Data Processing: The script looks back at a set number of previous price candles (the BandsPeriod). It calculates a Simple Moving Average to establish the core trend.
  2. Volatility Calculation: It measures how much the price has deviated from that average over the specified timeframe. By applying a "Deviation" multiplier, it creates the outer boundaries.
  3. Real-Time Updates: As new price data enters the chart, the script constantly recalculates these values. It then draws dynamic horizontal lines and text labels on your screen to show exactly where the current Upper, Middle, and Lower levels sit.

Visual Features and Customization

To ensure the tool is useful for different trading styles, the logic includes several user-friendly features:

  • Visibility Toggles: You can choose to show or hide the individual bands (Upper, Lower, or Middle) depending on your preference.
  • Visual Customization: The script allows for personalized styling, including the ability to change colors (e.g., Red for Upper, Blue for Lower), line thickness, and text sizes.
  • Timeframe Awareness: The tool automatically identifies which timeframe you are viewing (such as 1-minute, Hourly, or Daily charts) and adjusts its labels accordingly to ensure clarity on the display.
  • Dynamic Labeling: In addition to drawing lines, the script places text labels on the chart that identify each band clearly, providing an organized workspace for the trader.

In summary, this script automates the complex math required to visualize market volatility, providing a clear visual "channel" that helps traders see where price is currently trading relative to its historical average.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
5 Views
0 Downloads
0 Favorites
Bands Level
//+------------------------------------------------------------------+
//|                                                   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; i<limit; i++)
      MovingBuffer[i]=iMA(NULL,TimeFrame,BandsPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);
//----
   i=Bars-BandsPeriod+1;
   if(counted_bars>BandsPeriod-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);
}

Comments