ibbfill2
Download scriptOverview of the iBBFill Indicator
The iBBFill script is a visual trading tool designed to analyze market trends by utilizing Bollinger Bands. Its primary purpose is to categorize price action into three distinct states—Upward Trends, Downward Trends, and Flat Markets—and visualize these states using color-coded shaded areas directly on your trading chart.
Core Logic and Functionality
The indicator operates by monitoring the relationship between the current market price and the standard Bollinger Band components (the Middle Line, Upper Band, and Lower Band). Here is a breakdown of how it determines market behavior:
1. Trend Identification
The script continuously evaluates the closing price of candles against the boundaries of the Bollinger Bands to define the market "mood":
- Upward Trend: If the closing price breaks above the Upper Band, the script identifies an active upward trend.
- Downward Trend: If the closing price drops below the Lower Band, the script identifies an active downward trend.
- Flat/Neutral Market: When the price remains within the bands (neither breaking above the top nor below the bottom), the market is considered to be in a neutral or "flat" state.
2. Trend Persistence and Reversal
A key feature of this logic is its ability to maintain a trend until it is officially invalidated. For example:
- If an Upward Trend is active, it will remain identified as such until the price closes below the middle line (the average).
- If a Downward Trend is active, it will remain identified until the price closes above the middle line.
This prevents "flickering" of signals and ensures that the trend remains consistent until a significant shift in momentum occurs.
Visual Representation
To make this data easy to read at a glance, the script uses a filling effect on the chart:
- Blue Shading: Highlights periods of identified Upward Trends.
- Red Shading: Highlights periods of identified Downward Trends.
- Grey Shading: Highlights periods where the market is considered Flat or Neutral.
- Reference Lines: The script also plots the actual Bollinger Bands (Upper, Lower, and Middle) as lines, providing a clear visual guide for price volatility and boundaries.
Summary for Traders
Instead of looking at raw numbers, this indicator provides a "weather map" of the market. It tells you where the current momentum is heading by shading the chart based on whether the price is aggressively pushing against the outer bands or consolidating in the middle. This allows for a clearer understanding of market structure and helps in identifying potential trend continuations or exhaustion points.
//+------------------------------------------------------------------+
//| iBBFill.mq5 |
//| Copyright Integer |
//| http://dmffx.com |
//+------------------------------------------------------------------+
#property copyright "Integer"
#property link "http://dmffx.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 10
#property indicator_plots 6
//--- plot Label1
#property indicator_label1 "UpTrend"
#property indicator_type1 DRAW_FILLING
#property indicator_color1 C'10,10,70'
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
//--- plot Label2
#property indicator_label2 "DnTrend"
#property indicator_type2 DRAW_FILLING
#property indicator_color2 C'70,10,10'
#property indicator_style2 STYLE_SOLID
#property indicator_width2 1
//--- plot Label3
#property indicator_label3 "Flat"
#property indicator_type3 DRAW_FILLING
#property indicator_color3 C'50,50,50'
#property indicator_style3 STYLE_SOLID
#property indicator_width3 1
//--- plot Label4
#property indicator_label4 "BB MA"
#property indicator_type4 DRAW_LINE
#property indicator_color4 Yellow
#property indicator_style4 STYLE_SOLID
#property indicator_width4 1
//--- plot Label5
#property indicator_label5 "BB Upper"
#property indicator_type5 DRAW_LINE
#property indicator_color5 DodgerBlue
#property indicator_style5 STYLE_SOLID
#property indicator_width5 1
//--- plot Label6
#property indicator_label6 "BB Lower"
#property indicator_type6 DRAW_LINE
#property indicator_color6 Red
#property indicator_style6 STYLE_SOLID
#property indicator_width4 1
//--- input parameters
input int BBPeriod = 20; // Bollinger Bands period
input double BBDeviation = 2; // Bollinger Bands width
input ENUM_APPLIED_PRICE BBPrice = PRICE_CLOSE; // Bollinger Bands applied price
//--- indicator buffers
double Upper1[];
double Lower1[];
double Upper2[];
double Lower2[];
double Upper3[];
double Lower3[];
double BCBuf[];
double BUBuf[];
double BLBuf[];
double Trend[];
int BBHand;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer(0,Upper1,INDICATOR_DATA);
SetIndexBuffer(1,Lower1,INDICATOR_CALCULATIONS);
SetIndexBuffer(2,Upper2,INDICATOR_DATA);
SetIndexBuffer(3,Lower2,INDICATOR_CALCULATIONS);
SetIndexBuffer(4,Upper3,INDICATOR_DATA);
SetIndexBuffer(5,Lower3,INDICATOR_CALCULATIONS);
SetIndexBuffer(6,BCBuf,INDICATOR_DATA);
SetIndexBuffer(7,BUBuf,INDICATOR_DATA);
SetIndexBuffer(8,BLBuf,INDICATOR_DATA);
SetIndexBuffer(9,Trend,INDICATOR_CALCULATIONS);
BBHand=iBands(NULL,PERIOD_CURRENT,BBPeriod,0,BBDeviation,BBPrice);
return(0);
}
//+------------------------------------------------------------------+
//| 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 limit=0,limit1=0;
if(prev_calculated>0)
{
limit=prev_calculated-1;
limit1=limit;
}
else
{
limit=0;
limit1=1;
}
CopyBuffer(BBHand,0,0,rates_total-limit,BCBuf);
CopyBuffer(BBHand,1,0,rates_total-limit,BUBuf);
CopyBuffer(BBHand,2,0,rates_total-limit,BLBuf);
for(int i=limit1;i<rates_total;i++)
{
Trend[i]=Trend[i-1];
Upper1[i]=0;
Lower1[i]=0;
Upper2[i]=0;
Lower2[i]=0;
Upper3[i]=0;
Lower3[i]=0;
if(Trend[i]==1 && close[i]<BCBuf[i])
{
Trend[i]=0;
}
if(Trend[i]==-1 && close[i]>BCBuf[i])
{
Trend[i]=0;
}
if(close[i]>BUBuf[i])
{
Trend[i]=1;
}
if(close[i]<BLBuf[i])
{
Trend[i]=-1;
}
switch((int)Trend[i])
{
case 1:
Upper1[i]=BUBuf[i];
Lower1[i]=BLBuf[i];
if(Trend[i-1]!=1)
{
Upper1[i-1]=BUBuf[i-1];
Lower1[i-1]=BLBuf[i-1];
}
break;
case -1:
Upper2[i]=BUBuf[i];
Lower2[i]=BLBuf[i];
if(Trend[i-1]!=-1)
{
Upper2[i-1]=BUBuf[i-1];
Lower2[i-1]=BLBuf[i-1];
}
break;
case 0:
Upper3[i]=BUBuf[i];
Lower3[i]=BLBuf[i];
if(Trend[i-1]!=0)
{
Upper3[i-1]=BUBuf[i-1];
Lower3[i-1]=BLBuf[i-1];
}
break;
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
Comments