Author: Awran5
istddev

This script is designed to automatically visualize mathematical price boundaries on your trading charts using a method known as Standard Deviation. In professional trading, Standard Deviation is used to measure volatility; it essentially tells you how far the current price is "drifting" away from its average.

By using this tool, you can see visual zones that highlight extreme price movements, helping you identify when a market is becoming overextended or potentially reaching a reversal point.

Core Functionality

The logic operates by identifying two distinct layers of data on your screen:

  • Primary Channel: This serves as the main boundary. It uses a specific deviation setting (defaulting to 1.618, a common ratio in technical analysis) to create a broad visual area that encompasses the "normal" range of price action.
  • Secondary Levels: These are plotted as additional reference lines using a different deviation (defaulting to 0.618). These provide a tighter perspective on price movement, allowing you to see more subtle shifts in volatility.

Key Features and Logic Flow

1. Dynamic Time Tracking The script doesn't just place lines at random spots. It calculates the specific starting point (First Bar) and the ending point (Last Bar) based on your chosen time frame and a user-defined range (e.g., looking back at the last 50 bars). This ensures the channels stay relevant to the most recent market data.

2. Automated Visual Drawing Every time the market moves, the script performs the following sequence:

  • It checks the current chart data to find the correct timeframe.
  • It clears out any old drawings to keep your chart clean.
  • It calculates the mathematical "spread" of the price and draws two separate channels simultaneously.

3. Customizable Visuals Because the script is built for clarity, it allows for high levels of personalization without requiring any technical knowledge. You can independently adjust:

  • Colors: Easily distinguish between the main channel and the internal levels.
  • Line Styles & Thickness: Make specific boundaries bolder or more subtle depending on your visual preference.
  • Ray Extension: The script includes a "Ray" feature, which allows the calculated channels to project forward into the future, helping you visualize potential price corridors as they develop in real-time.

Summary for the User

Instead of manually calculating complex math to figure out how volatile a currency or stock is, this script does the heavy lifting for you. It draws "fences" on your chart that automatically expand or contract based on market reality, giving you a clear visual guide of where the price typically stays and where it is pushing into "extreme" territory.

11 Views
0 Downloads
0 Favorites
istddev
ÿþ//+------------------------------------------------------------------+

//|                                   Standard Deviation Channel.mq4 |

//|                                           Copyright 2015, Awran5 |

//|                                                 awran5@yahoo.com |

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

#property copyright "Awran5"

#property link      "awran5@yahoo.com"

#property version   "1.00"

#property description "Simple indicator to show 2 levels of Standard Deviation Channel on your chart"

#property strict

#property indicator_chart_window



extern int    FirstBar              = 1;           // First Bar

extern int    LastBar               = 50;          // Last Bar

extern ENUM_TIMEFRAMES FixedPeriod  = 0;           // Time Frame to use

input string  lb_0                  = "";          // ----- CHANNEL --------------------------------------

extern double cDeviation            = 1.618;       // Channel Deviation

extern ENUM_LINE_STYLE cStyle       = 0;           // Channel Style

extern color  cColor                = clrRoyalBlue;// Channel Color

extern int    cWidth                = 1;           // Channel Width

input string  lb_1                  = "";          // ----- LEVELS ---------------------------------------

extern double lDeviation            = 0.618;       // Levels Deviation

extern ENUM_LINE_STYLE lStyle       = 0;           // Levels Style

extern color  lColor                = clrGold;     // Levels Color

extern int    lWidth                = 1;           // Levels Width

//---

string StdDevChannel="StdDev Channel",StdDevLevels="StdDev Levels";

datetime FirstBarTime,LastBarTime;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- indicator buffers mapping

//---- indicator short name

   IndicatorShortName("iStdDev");

//---

   return(INIT_SUCCEEDED);

  }

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

//| deinitialization function                                        |

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

void OnDeinit(const int reason)

  {

//---

   if(ObjectFind(StdDevChannel) >= 0)  ObjectDelete(StdDevChannel);

   if(ObjectFind(StdDevLevels)  >= 0)  ObjectDelete(StdDevLevels);

   Comment("");

//---

  }

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

//| 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 counted_bars=IndicatorCounted(),

   limit=Bars-counted_bars,

   i;

   if(counted_bars < 0)  return(-1);

   if(counted_bars>0) counted_bars--;



   for(i=limit-1; i>=0; i--)

     {

      FirstBarTime = iTime(NULL, FixedPeriod, i+FirstBar);

      LastBarTime  = iTime(NULL, FixedPeriod, i+LastBar);

     }



   DrawSDChannel(StdDevChannel,cDeviation,LastBarTime,FirstBarTime,cColor,cStyle,cWidth);

   DrawSDChannel(StdDevLevels,lDeviation,LastBarTime,FirstBarTime,lColor,lStyle,lWidth);



//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//|                                                                  |

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

void DrawSDChannel(string name,double deviation,datetime t1,datetime t2,color clr,int style,int width)

  {

//---

   ObjectDelete(name);

   ObjectCreate(name,OBJ_STDDEVCHANNEL,0,t1,0,t2,0);

   ObjectSet(name,OBJPROP_DEVIATION,deviation);

   ObjectSet(name,OBJPROP_COLOR,clr);

   ObjectSet(name,OBJPROP_STYLE,style);

   ObjectSet(name,OBJPROP_WIDTH,width);

   ObjectSet(name,OBJPROP_RAY,true);

//---

  }

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

Comments