Author: Integer

This script is a specialized technical analysis tool designed to visualize market trends by combining Bollinger Bands with dynamic trend detection. It provides a clear, color-coded visual layout on your chart to help distinguish between active upward movements, downward shifts, and periods of consolidation (flat markets).

Core Logic Overview

The primary goal of this indicator is to automatically identify the "state" of the market based on how price interacts with Bollinger Bands. Instead of just showing you the bands as static lines, it interprets them as boundaries to determine the prevailing momentum.

How it Works

The logic operates through three distinct phases:

1. Data Processing The tool begins by calculating standard Bollinger Bands—a common set of lines used to measure market volatility and potential price extremes. It tracks the Upper Band (resistance), the Lower Band (support), and the Middle Line (the average price).

2. Trend Identification The script constantly monitors the relationship between the current closing price and these three bands to assign a "Trend State":

  • Upward Trend: When the price closes above the Upper Band, the script identifies a strong bullish movement.
  • Downward Trend: When the price closes below the Lower Band, the script identifies a strong bearish movement.
  • Neutral/Flat Market: If the price fails to break out of the bands or falls back toward the Middle Line, it is treated as a "Flat" market.

3. Dynamic Filtering & Color Coding To ensure the visual display remains clean and accurate, the script includes a logic gate:

  • If the market was previously in an Upward Trend but the price closes below the Middle Line, it flips the status to Neutral.
  • If the market was previously in a Downward Trend but the price closes above the Middle Line, it also flips the status to Neutral.

Visual Output

The final result is a "filled" visual effect on your chart:

  • Blue Shading: Represents active Upward Trends.
  • Red Shading: Represents active Downward Trends.
  • Grey Shading: Indicates a Flat or neutral market where there is no clear direction.
  • Line Overlay: You will also see the standard Bollinger Bands (Upper, Lower, and Middle) plotted as lines to provide context for the shaded zones.

By using this logic, the indicator filters out "noise," allowing you to see at a glance whether the market is currently trending in a specific direction or if it is simply moving sideways.

Indicators Used
Bollinger bands indicator
9 Views
0 Downloads
0 Favorites
ibbfill
//+------------------------------------------------------------------+
//|                                                      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 Label2
#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

#property indicator_label4  "BB MA"
#property indicator_type4   DRAW_LINE
#property indicator_color4  Yellow
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1

#property indicator_label5  "BB Upper"
#property indicator_type5   DRAW_LINE
#property indicator_color5  DodgerBlue
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1

#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
// Bollinger Bands Period
input int                              BBPeriod=20;
// Bollinger Bands Width
input double                           BBDeviation=2;
// Bollinger Bands applied price
input ENUM_APPLIED_PRICE               BBPrice=PRICE_CLOSE;

//--- 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,limit1,limit2;
   if(prev_calculated>0)
     {
      limit=prev_calculated-1;
      limit=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