IC Markets

Fai Value Gap indicator for your forex scalping strategy

Optimize MetaTrader 4, MetaTrader 5, cTrader, and TradingView for speed. Discuss Level II Market Depth (DOM), custom hotkeys, and volume indicators.
Post Reply
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Fai Value Gap indicator for your forex scalping strategy

Post by PTScalper »

Hi scalpers,

today i would like to share with you another interesting indicator, which can be usefull for your forex scalping.
It is called Fair Value Gap (FVG).

The Fair Value Gap (FVG) is a popular price action concept rooted in Smart Money Concepts (SMC), highly effective for forex scalping. An FVG represents an inefficiency or imbalance in the market, occurring when a sudden surge in buying or selling pressure causes the price to move so rapidly that liquidity isn't evenly distributed.

Visually, an FVG is identified using a three-candle sequence. A bullish FVG forms when the high of the first candle fails to overlap with the low of the third candle, leaving a gap across the body of the large second candle. Conversely, a bearish FVG occurs when the low of the first candle does not overlap with the high of the third candle during a sharp downtrend.

For forex scalpers operating on lower timeframes like the 1-minute (M1) or 5-minute (M5) charts, these gaps serve as magnetic zones. The core premise is that the market naturally seeks efficiency and will frequently retrace to "fill" or rebalance these gaps before continuing its original trend. Scalpers can place limit orders or wait for price action confirmation within the FVG zone to enter high-probability trades with tight stop losses.

When scalping, relying solely on FVGs can be risky due to market noise. The highest probability setups occur when an FVG aligns with other confluences, such as a liquidity sweep, a break of market structure (BMS), or an order block. By utilizing a custom indicator to automatically draw these zones, scalpers save crucial time, allowing them to focus purely on execution and risk management in fast-moving markets.

MT4 Fair Value Gap (FVG) Indicator Code
Here is a lightweight MQL4 indicator that automatically identifies and draws rectangles over Bullish and Bearish Fair Value Gaps on your chart.

To use this, open MetaEditor in MT4, create a new Custom Indicator, and paste the code below.

Code: Select all

//+------------------------------------------------------------------+
//|                                                          FVG.mq4 |
//|                                      Copyright 2026, Your Name   |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      ""
#property version   "1.00"
#property indicator_chart_window

//--- Input parameters
input color BullishColor = clrLightGreen; // Color of Bullish FVG
input color BearishColor = clrLightCoral; // Color of Bearish FVG
input int   MaxBars      = 500;           // Number of past bars to scan
input int   ExtendBars   = 10;            // How many bars forward to extend the box

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Remove all FVG boxes when indicator is removed
   ObjectsDeleteAll(0, "FVG_");
}

//+------------------------------------------------------------------+
//| 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[])
{
   // Determine how many bars to scan
   int limit = rates_total - prev_calculated;
   if(limit > MaxBars) limit = MaxBars;
   if(limit <= 2) return(rates_total);

   // Loop through historical bars
   for(int i = limit; i >= 1; i--)
   {
      // Ensure we have enough bars for a 3-candle formation
      if(i + 2 >= rates_total) continue;

      double gapTop = 0;
      double gapBottom = 0;
      bool isBullish = false;
      bool isBearish = false;

      // Check for Bullish FVG (Low of candle 1 > High of candle 3)
      // Note: In MT4, index 0 is the current candle. i=1, i+1=2, i+2=3
      if(low[i] > high[i+2])
      {
         isBullish = true;
         gapTop = low[i];
         gapBottom = high[i+2];
      }
      // Check for Bearish FVG (High of candle 1 < Low of candle 3)
      else if(high[i] < low[i+2])
      {
         isBearish = true;
         gapTop = low[i+2];
         gapBottom = high[i];
      }

      // Draw the rectangle if an FVG is found
      if(isBullish || isBearish)
      {
         string objName = "FVG_" + TimeToString(time[i+1]);
         
         // Only draw if it doesn't already exist
         if(ObjectFind(0, objName) < 0)
         {
            datetime endTime = time[i] + (PeriodSeconds() * ExtendBars);
            
            ObjectCreate(0, objName, OBJ_RECTANGLE, 0, time[i+2], gapTop, endTime, gapBottom);
            ObjectSetInteger(0, objName, OBJPROP_COLOR, isBullish ? BullishColor : BearishColor);
            ObjectSetInteger(0, objName, OBJPROP_BACK, true); // Keep behind candles
            ObjectSetInteger(0, objName, OBJPROP_FILL, true); // Fill the box with color
            ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false);
            ObjectSetInteger(0, objName, OBJPROP_HIDDEN, true);
         }
      }
   }
   
   return(rates_total);
}
//+------------------------------------------------------------------+
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: Fai Value Gap indicator for your forex scalping strategy

Post by PTScalper »

Plus i prepared for you version for MT5 as well.

MT5 handles historical data (arrays) differently than MT4, so we must tell the indicator to read the price data backwards using ArraySetAsSeries.
To use this, open MetaEditor in MT5, create a new Custom Indicator, and paste this code:

Code: Select all

//+------------------------------------------------------------------+
//|                                                          FVG.mq5 |
//|                                      Copyright 2026, Your Name   |
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots 0

//--- Input parameters
input color BullishColor = clrLightGreen; // Color of Bullish FVG
input color BearishColor = clrLightCoral; // Color of Bearish FVG
input int   MaxBars      = 500;           // Number of past bars to scan
input int   ExtendBars   = 10;            // How many bars forward to extend the box

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // Clean up FVG boxes on removal
   ObjectsDeleteAll(0, "FVG_");
}

//+------------------------------------------------------------------+
//| 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[])
{
   // MT5 requires arrays to be set as series to read right-to-left like MT4
   ArraySetAsSeries(time, true);
   ArraySetAsSeries(high, true);
   ArraySetAsSeries(low, true);

   int limit = rates_total - prev_calculated;
   if(limit > MaxBars) limit = MaxBars;
   if(prev_calculated == 0) limit = MaxBars; 
   if(limit < 1) limit = 1;

   for(int i = limit; i >= 1; i--)
   {
      if(i + 2 >= rates_total) continue;

      double gapTop = 0;
      double gapBottom = 0;
      bool isBullish = false;
      bool isBearish = false;

      // Bullish FVG
      if(low[i] > high[i+2])
      {
         isBullish = true;
         gapTop = low[i];
         gapBottom = high[i+2];
      }
      // Bearish FVG
      else if(high[i] < low[i+2])
      {
         isBearish = true;
         gapTop = low[i+2];
         gapBottom = high[i];
      }

      if(isBullish || isBearish)
      {
         string objName = "FVG_" + TimeToString(time[i+1]);
         
         if(ObjectFind(0, objName) < 0)
         {
            datetime endTime = time[i] + (PeriodSeconds() * ExtendBars);
            
            ObjectCreate(0, objName, OBJ_RECTANGLE, 0, time[i+2], gapTop, endTime, gapBottom);
            ObjectSetInteger(0, objName, OBJPROP_COLOR, isBullish ? BullishColor : BearishColor);
            ObjectSetInteger(0, objName, OBJPROP_BACK, true); 
            ObjectSetInteger(0, objName, OBJPROP_FILL, true); 
            ObjectSetInteger(0, objName, OBJPROP_SELECTABLE, false);
            ObjectSetInteger(0, objName, OBJPROP_HIDDEN, true);
         }
      }
   }
   return(rates_total);
}
//+------------------------------------------------------------------+
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: Fai Value Gap indicator for your forex scalping strategy

Post by PTScalper »

And i prepared for IC traders as well special version:

cTrader utilizes the cAlgo API written in C#. Its structure processes data naturally left-to-right (where index is the current bar).
To use this, open cTrader, go to the Automate tab, click New Indicator, and paste this code:

Code: Select all

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class FairValueGap : Indicator
    {
        [Parameter("Bullish Color", DefaultValue = "LightGreen")]
        public string BullColorStr { get; set; }

        [Parameter("Bearish Color", DefaultValue = "LightCoral")]
        public string BearColorStr { get; set; }

        [Parameter("Extend Bars", DefaultValue = 10)]
        public int ExtendBars { get; set; }

        [Parameter("Opacity (0-255)", DefaultValue = 70, MinValue = 0, MaxValue = 255)]
        public int BoxOpacity { get; set; }

        private Color _bullColor;
        private Color _bearColor;

        protected override void Initialize()
        {
            // Parse strings to cTrader Colors with custom opacity
            _bullColor = Color.FromArgb(BoxOpacity, Color.FromName(BullColorStr));
            _bearColor = Color.FromArgb(BoxOpacity, Color.FromName(BearColorStr));
        }

        public override void Calculate(int index)
        {
            // Need at least 3 candles to check for an FVG
            if (index < 2) return;

            double lowCurrent = Bars.LowPrices[index];
            double highCurrent = Bars.HighPrices[index];
            
            double highCandle1 = Bars.HighPrices[index - 2];
            double lowCandle1 = Bars.LowPrices[index - 2];

            bool isBullish = lowCurrent > highCandle1;
            bool isBearish = highCurrent < lowCandle1;

            if (isBullish || isBearish)
            {
                // Calculate how far forward to draw the box
                TimeSpan barDuration = Bars.OpenTimes[index] - Bars.OpenTimes[index - 1];
                DateTime endTime = Bars.OpenTimes[index].Add(TimeSpan.FromTicks(barDuration.Ticks * ExtendBars));

                string objName = "FVG_" + index;

                if (isBullish)
                {
                    var rect = Chart.DrawRectangle(objName, Bars.OpenTimes[index - 2], lowCurrent, endTime, highCandle1, _bullColor);
                    rect.IsFilled = true;
                }
                else if (isBearish)
                {
                    var rect = Chart.DrawRectangle(objName, Bars.OpenTimes[index - 2], highCurrent, endTime, lowCandle1, _bearColor);
                    rect.IsFilled = true;
                }
            }
        }
    }
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 210
Joined: Mon Jul 20, 2026 1:28 pm

Re: Fai Value Gap indicator for your forex scalping strategy

Post by PTScalper »

Please give me feedback, if you liked it or if it is usefull for you.
Take a care and have a great trades :-)
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Post Reply