BBANDS
Download scriptThis tool is designed to help traders identify trend directions and manage their risk by automatically calculating potential "stop-loss" levels based on market volatility. It uses the Bollinger Bands—a common technical analysis indicator that measures price movement—as its primary foundation to detect when the market is shifting from an upward to a downward trend, or vice versa.
Core Logic and Functionality
Trend Identification The script constantly monitors whether the current market price has broken out above or below the Bollinger Bands.
- Upward Trend: If the price closes above the upper band, the script recognizes an upward movement.
- Downward Trend: If the price closes below the lower band, the script identifies a downward movement.
Dynamic Stop-Loss Calculation Unlike standard indicators that provide a simple line, this script calculates a "Stop" level that adjusts based on the distance between the upper and lower bands. It incorporates a "Money Risk" factor (an offset), which allows you to customize how much breathing room is given to the trade. This ensures that the stop-loss moves dynamically with the market's volatility—widening when the market is volatile and tightening when it becomes stable.
Smart Trailing Mechanism To protect against sudden reversals, the logic includes a "ratchet" effect. In an uptrend, for example, the stop-loss level will only move higher; if the market pulls back slightly, the stop remains at its highest achieved point. This prevents the stop-loss from moving backward into a zone of higher risk.
Visual Alerts and Signaling The script provides three primary ways to interact with the data:
- Visual Lines: It can draw continuous lines on your chart representing the current trend's safety level.
- Signal Arrows: It can place specific markers (arrows) on the chart at the exact moment a new trend is confirmed and a stop level is established.
- Audio/Pop-up Alerts: You can configure the script to play sounds or trigger system alerts the moment the trend switches, allowing you to monitor the market without constantly staring at the screen.
Summary of User Experience
For a trader, this means less manual calculation and more consistent risk management. The tool identifies the trend for you, calculates a mathematically derived "safety zone" based on current volatility, and tracks that safety zone as the price moves in your favor, providing both visual cues and audible notifications when the market dynamics change.
//+------------------------------------------------------------------+
//| BBands_Stop_v1.mq4 |
//| Copyright © 2006, TrendLaboratory Ltd. |
//| http://finance.groups.yahoo.com/group/TrendLaboratory |
//| E-mail: igorad2004@list.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, TrendLaboratory Ltd."
#property link "http://finance.groups.yahoo.com/group/TrendLaboratory"
#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 LightSkyBlue
#property indicator_color2 MistyRose
#property indicator_color3 LightSkyBlue
#property indicator_color4 MistyRose
#property indicator_color5 LightSkyBlue
#property indicator_color6 MistyRose
#property indicator_width1 4
#property indicator_width2 4
#property indicator_width3 4
#property indicator_width4 4
#property indicator_width5 4
#property indicator_width6 4
//---- input parameters
extern int Length=20; // Bollinger Bands Period
extern int Deviation=1; // Deviation was 2
extern double MoneyRisk=1.00; // Offset Factor
extern int Signal=1; // Display signals mode: 1-Signals & Stops; 0-only Stops; 2-only Signals;
extern int Line=1; // Display line mode: 0-no,1-yes
extern int Nbars=1000;
//---- indicator buffers
double UpTrendBuffer[];
double DownTrendBuffer[];
double UpTrendSignal[];
double DownTrendSignal[];
double UpTrendLine[];
double DownTrendLine[];
extern bool SoundON=false;
extern bool PlaysoundON=false;
double alertBar;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicator line
SetIndexBuffer(0,UpTrendBuffer);
SetIndexBuffer(1,DownTrendBuffer);
SetIndexBuffer(2,UpTrendSignal);
SetIndexBuffer(3,DownTrendSignal);
SetIndexBuffer(4,UpTrendLine);
SetIndexBuffer(5,DownTrendLine);
SetIndexStyle(0,DRAW_ARROW);
SetIndexStyle(1,DRAW_ARROW);
SetIndexStyle(2,DRAW_ARROW);
SetIndexStyle(3,DRAW_ARROW);
SetIndexStyle(4,DRAW_LINE);
SetIndexStyle(5,DRAW_LINE);
SetIndexArrow(0,159);
SetIndexArrow(1,159);
SetIndexArrow(2,108);
SetIndexArrow(3,108);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
short_name="BBands Stop("+Length+","+Deviation+")";
IndicatorShortName(short_name);
SetIndexLabel(0,"UpTrend Stop");
SetIndexLabel(1,"DownTrend Stop");
SetIndexLabel(2,"UpTrend Signal");
SetIndexLabel(3,"DownTrend Signal");
SetIndexLabel(4,"UpTrend Line");
SetIndexLabel(5,"DownTrend Line");
//----
SetIndexDrawBegin(0,Length);
SetIndexDrawBegin(1,Length);
SetIndexDrawBegin(2,Length);
SetIndexDrawBegin(3,Length);
SetIndexDrawBegin(4,Length);
SetIndexDrawBegin(5,Length);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Bollinger Bands_Stop_v1 |
//+------------------------------------------------------------------+
int start()
{
int i,shift,trend;
double smax[25000],smin[25000],bsmax[25000],bsmin[25000];
for (shift=Nbars;shift>=0;shift--)
{
UpTrendBuffer[shift]=0;
DownTrendBuffer[shift]=0;
UpTrendSignal[shift]=0;
DownTrendSignal[shift]=0;
UpTrendLine[shift]=EMPTY_VALUE;
DownTrendLine[shift]=EMPTY_VALUE;
}
for (shift=Nbars-Length-1;shift>=0;shift--)
{
smax[shift]=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_UPPER,shift);
smin[shift]=iBands(NULL,0,Length,Deviation,0,PRICE_CLOSE,MODE_LOWER,shift);
if (Close[shift]>smax[shift+1]) trend=1;
if (Close[shift]<smin[shift+1]) trend=-1;
if(trend>0 && smin[shift]<smin[shift+1]) smin[shift]=smin[shift+1];
if(trend<0 && smax[shift]>smax[shift+1]) smax[shift]=smax[shift+1];
bsmax[shift]=smax[shift]+0.5*(MoneyRisk-1)*(smax[shift]-smin[shift]);
bsmin[shift]=smin[shift]-0.5*(MoneyRisk-1)*(smax[shift]-smin[shift]);
if(trend>0 && bsmin[shift]<bsmin[shift+1]) bsmin[shift]=bsmin[shift+1];
if(trend<0 && bsmax[shift]>bsmax[shift+1]) bsmax[shift]=bsmax[shift+1];
if (trend>0)
{
if (Signal>0 && UpTrendBuffer[shift+1]==-1.0)
{
UpTrendSignal[shift]=bsmin[shift];
UpTrendBuffer[shift]=bsmin[shift];
if(Line>0) UpTrendLine[shift]=bsmin[shift];
if (SoundON==true && shift==0 && Bars>alertBar)
{
Alert("#MTF BBands going Up on ",Symbol(),"-",Period());
alertBar = Bars;
}
if (PlaysoundON==true && shift==0 && Bars>alertBar)
{
PlaySound("photonup.wav");
alertBar = Bars;
}
}
else
{
UpTrendBuffer[shift]=bsmin[shift];
if(Line>0) UpTrendLine[shift]=bsmin[shift];
UpTrendSignal[shift]=-1;
}
if (Signal==2) UpTrendBuffer[shift]=0;
DownTrendSignal[shift]=-1;
DownTrendBuffer[shift]=-1.0;
DownTrendLine[shift]=EMPTY_VALUE;
}
if (trend<0)
{
if (Signal>0 && DownTrendBuffer[shift+1]==-1.0)
{
DownTrendSignal[shift]=bsmax[shift];
DownTrendBuffer[shift]=bsmax[shift];
if(Line>0) DownTrendLine[shift]=bsmax[shift];
if (SoundON==true && shift==0 && Bars>alertBar)
{
Alert("#MTF BBands going Down on ",Symbol(),"-",Period());
alertBar = Bars;
}
if (PlaysoundON==true && shift==0 && Bars>alertBar)
{
PlaySound("photondown.wav");
alertBar = Bars;
}
}
else
{
DownTrendBuffer[shift]=bsmax[shift];
if(Line>0)DownTrendLine[shift]=bsmax[shift];
DownTrendSignal[shift]=-1;
}
if (Signal==2) DownTrendBuffer[shift]=0;
UpTrendSignal[shift]=-1;
UpTrendBuffer[shift]=-1.0;
UpTrendLine[shift]=EMPTY_VALUE;
}
}
return(0);
}
Comments