Author: Copyright 2024, hieuhoangcntt@gmail.com

This script is designed to identify and visually highlight specific types of price action on a trading chart known as "Fair Value Gaps" (FVG). These gaps represent moments where the market moves so rapidly that it leaves behind an imbalance, which traders often watch for potential price reactions or returns.

How the Logic Works

The script automatically scans your chart's historical data to find these imbalances and draws clear visual boxes on your screen to mark them. Here is the step-by-step breakdown of the logic:

1. Identifying Market Imbalances The tool looks at the relationship between three consecutive candles (price bars) to determine if a gap has formed:

  • Bullish Gaps (Buy Ranges): The script checks for a strong upward move where the "low" price of a current candle is higher than the "high" price of a previous candle. If this happens alongside a green candle, it marks a potential area of interest for buyers.
  • Bearish Gaps (Sell Ranges): Conversely, it looks for sharp downward moves where the "high" price of a current candle is lower than the "low" price of a previous candle. If this occurs with a red candle, it identifies a potential area of interest for sellers.

2. Visual Representation Once these gaps are detected, the script automatically draws geometric rectangles on your chart:

  • Blue Boxes: These represent "Buy Ranges." They highlight zones where the market moved up quickly, potentially leaving a "gap" that price might revisit.
  • Red Boxes: These represent "Sell Ranges." They highlight zones where the market dropped aggressively.

3. Dynamic Chart Management To keep your trading workspace clean and organized, the script includes several automated maintenance features:

  • Real-Time Updates: The indicator is designed to refresh as new data comes in, ensuring that the marks stay relevant to the current price action.
  • Smart Cleanup: It manages the memory of the chart by deleting old objects automatically. This prevents your screen from becoming cluttered with outdated information and ensures the platform runs smoothly without lag.
  • Automatic Removal: When you remove the indicator from your chart, it is programmed to clean up all the boxes it drew, leaving your chart exactly as you found it.

Summary for Traders Instead of manually hunting for price gaps, this script automates that process for you. It filters the "noise" of the market to highlight specific imbalances, providing you with a visual roadmap of where significant momentum occurred and where the market might react in the future.

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

//|                                         Fair Value Gap Indicator |

//|                                       Copyright 2024, Hieu Hoang |

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

#property copyright "Copyright 2024, hieuhoangcntt@gmail.com"

#property indicator_chart_window

#property indicator_buffers 0

double _low, _high;

string object_names[];

datetime pre_time = 0;

const int bull = 5;

const int bear = 5;

int c_bull;

int c_bear;

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

//|                                                                  |

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

bool isGreenCandle(double open, double close)

  {

   return open < close;

  }



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

//|                                                                  |

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

bool isRedCandle(double open, double close)

  {

   return !isGreenCandle(open, close);

  }



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

//|                                                                  |

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

bool isBullFVG(int index,

               const double &open[],

               const double &high[],

               const double &low[],

               const double &close[])

  {



   if(isGreenCandle(open[index-1], close[index-1]) &&

      high[index-2] < low[index] && high[index-2] < _low

     )

      return true;

   return false;

  }



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

//|                                                                  |

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

bool isBearFVG(int index,

               const double &open[],

               const double &high[],

               const double &low[],

               const double &close[])

  {



   if(isRedCandle(open[index-1], close[index-1]) &&

      low[index-2] > high[index] && low[index-2] > _high

     )

      return true;

   return false;

  }

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

   return(INIT_SUCCEEDED);

  }



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

//|                                                                  |

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

void create_object(string name, color clrColor,const datetime time1, const datetime time2, const double low, const double high)

  {

   ArrayResize(object_names, ArraySize(object_names) + 1);

   object_names[ArraySize(object_names) - 1] = name;

   ObjectCreate(0, name, OBJ_RECTANGLE, 0, time1, low, time2, high);

   ObjectSetInteger(0, name, OBJPROP_COLOR, clrColor);

   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);

   ObjectSetInteger(0, name, OBJPROP_FILL, true);

   ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, true);

  }



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

//| 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[])

  {

   if(pre_time == time[rates_total - 1])

      return(rates_total);

   pre_time = time[rates_total - 1];

   delete_objects();

   _low = low[rates_total - 1];

   _high = high[rates_total - 1];

   c_bull = 0;

   c_bear = 0;

   for(int i = rates_total - 2; i >= 2; i--)

     {

      _low = _low < low[i] ? _low : low[i];

      _high = _high > high[i] ? _high: high[i];

      if(c_bull < bull && isBullFVG(i, open, high, low, close))

        {

         create_object("Buy range " + high[i-2] + " —! " + low[i], clrBlue, time[i-2], time[rates_total-1], low[i], high[i-2]);

         c_bull ++;

         if(c_bull >= bull && c_bear >= bear)

            break;

        }

      else if(c_bear < bear && isBearFVG(i, open, high, low, close))

           {

            create_object("Sell range " + low[i-2] + " ˜! " + high[i], clrRed, time[i-2], time[rates_total-1], high[i], low[i-2]);

            c_bear ++;

            if(c_bull >= bull && c_bear >= bear)

              break;

           }

     }

   return(rates_total);

  }

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

void delete_objects()

  {

   for(int i = 0; i < ArraySize(object_names); i++)

      ObjectDelete(0, object_names[i]);

   ArrayResize(object_names, 0);

  }

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

//|                                                                  |

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

void OnDeinit(const int reason)

  {

   pre_time = 0;

   delete_objects();

  }

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

Comments