Creating a Fair Value Gap (FVG)/Imbalances Expert Advisor (EA) step-by-step: A Smart Money concept approach

Creating a Fair Value Gap (FVG)/Imbalances Expert Advisor (EA) step-by-step: A Smart Money concept approach

29 July 2024, 18:33
Allan Munene Mutiiria
1
48

In this article, we discussed the fundamental steps required to craft and develop an expert advisor (EA) rooted in the Fair Value Gap (FVG) strategy, enhanced by the Smart Money concept approach. This journey merged art and science, demanding a trader's ability to analyze candlestick patterns and visualize concept levels effectively. We unraveled the mysteries of smart money and harnessed its transformative power within the realm of algorithmic trading. Our EA development journey covered essential topics such as the imbalance definition, trading strategy description, strategy blueprint, MQL5 trading system, and strategy tester results.

Example:

IMG 1

We first explored the Fair Value Gap (FVG) to highlight the imbalances caused by buying and selling pressures during periods of high volatility. These imbalances manifested as significant uni-directional movements in the market, often characterized by long candlesticks. By identifying these gaps, we exploited potential trading opportunities and created a robust strategy. We discussed the methods for spotting FVGs on price charts, focusing on identifying large candlesticks and examining adjacent ones to determine the fair value differences.

Example of illustrative figures used is as below:

IMG 2

Our FVG trading strategy integrated fair value assessments with candlestick imbalances to identify trading opportunities. We conducted comprehensive analyses, utilizing patterns such as bullish and bearish engulfing and doji, to determine market sentiment and potential shifts in momentum. Entry and exit signals were executed based on the identified fair value gaps and significant candlestick imbalances, with risk management techniques employed to mitigate potential losses. The strategy distinguished between bearish and bullish FVGs, each indicating different market conditions and trading opportunities.

After outlining the strategy, we developed a detailed blueprint to trade the FVG strategy effectively. This involved identifying bullish or bearish candlesticks with significant price movements and assessing neighboring candles. Once an FVG was identified, it was documented in the algorithm, and visual cues were added to the chart for easy identification. Trades were executed based on predefined conditions, with specific take profit and stop loss levels set to maintain a favorable risk-to-reward ratio.

Example of visual cues considered for easy identification:

IMG 3


Finally, we automated the FVG trading strategy by crafting an Expert Advisor (EA) in MQL5 for MetaTrader 5. The MetaQuotes Language Editor environment was used to write the EA, incorporating all the theoretical aspects discussed.

IMG 4

Sample of the code used in their identification is as below:

   for (int i=0; i<=visibleBars; i++){
      //Print("Bar Index = ",i);
      double low0 = iLow(_Symbol,_Period,i);
      double high2 = iHigh(_Symbol,_Period,i+2);
      double gap_L0_H2 = NormalizeDouble((low0 - high2)/_Point,_Digits);
      
      double high0 = iHigh(_Symbol,_Period,i);
      double low2 = iLow(_Symbol,_Period,i+2);
      double gap_H0_L2 = NormalizeDouble((low2 - high0)/_Point,_Digits);
      
      bool FVG_UP = low0 > high2 && gap_L0_H2 > minPts;
      bool FVG_DOWN = low2 > high0 && gap_H0_L2 > minPts;
      
      if (FVG_UP || FVG_DOWN){
         Print("Bar Index with FVG = ",i+1);
         datetime time1 = iTime(_Symbol,_Period,i+1);
         double price1 = FVG_UP ? high2 : high0;
         datetime time2 = time1 + PeriodSeconds(_Period)*FVG_Rec_Ext_Bars;
         double price2 = FVG_UP ? low0 : low2;
         string fvgNAME = FVG_Prefix+"("+TimeToString(time1)+")";
         color fvgClr = FVG_UP ? CLR_UP : CLR_DOWN;
         CreateRec(fvgNAME,time1,price1,time2,price2,fvgClr);
         Print("Old ArraySize = ",ArraySize(totalFVGs));
         ArrayResize(totalFVGs,ArraySize(totalFVGs)+1);
         ArrayResize(barINDICES,ArraySize(barINDICES)+1);
         Print("New ArraySize = ",ArraySize(totalFVGs));
         totalFVGs[ArraySize(totalFVGs)-1] = fvgNAME;
         barINDICES[ArraySize(barINDICES)-1] = i+1;
         ArrayPrint(totalFVGs);
         ArrayPrint(barINDICES);
      }
   }
   
   for (int i=ArraySize(totalFVGs)-1; i>=0; i--){
      string objName = totalFVGs[i];
      string fvgNAME = ObjectGetString(0,objName,OBJPROP_NAME);
      int barIndex = barINDICES[i];
      datetime timeSTART = (datetime)ObjectGetInteger(0,fvgNAME,OBJPROP_TIME,0);
      datetime timeEND = (datetime)ObjectGetInteger(0,fvgNAME,OBJPROP_TIME,1);
      double fvgLOW = ObjectGetDouble(0,fvgNAME,OBJPROP_PRICE,0);
      double fvgHIGH = ObjectGetDouble(0,fvgNAME,OBJPROP_PRICE,1);
      color fvgColor = (color)ObjectGetInteger(0,fvgNAME,OBJPROP_COLOR);
      
      Print("FVG NAME = ",fvgNAME," >No: ",barIndex," TS: ",timeSTART," TE: ",
            timeEND," LOW: ",fvgLOW," HIGH: ",fvgHIGH," CLR = ",fvgColor);
      for (int k=barIndex-1; k>=(barIndex-FVG_Rec_Ext_Bars); k--){
         datetime barTime = iTime(_Symbol,_Period,k);
         double barLow = iLow(_Symbol,_Period,k);
         double barHigh = iHigh(_Symbol,_Period,k);
         //Print("Bar No: ",k," >Time: ",barTime," >H: ",barHigh," >L: ",barLow);
         
         if (k==0){
            Print("OverFlow Detected @ fvg ",fvgNAME);
            UpdateRec(fvgNAME,timeSTART,fvgLOW,barTime,fvgHIGH);
            break;
         }
         
         if ((fvgColor == CLR_DOWN && barHigh > fvgHIGH) ||
            (fvgColor == CLR_UP && barLow < fvgLOW)
         ){
            Print("Cut Off @ bar no: ",k," of Time: ",barTime);
            UpdateRec(fvgNAME,timeSTART,fvgLOW,barTime,fvgHIGH);
            break;
         }
      }

We also tested it and we got the below results:

IMG 5

This automation streamlined the trading process, allowing for efficient execution and monitoring of trades. Through this journey, we equipped traders with the tools and knowledge needed to harness the potential of the Fair Value Gap strategy in the world of algorithmic trading.

Detailed explanations can be found in Learn how to trade the Fair Value Gap (FVG)/Imbalances step-by-step: A Smart Money concept approach. We do hope you will find the article detailed and easy to understand. Thank you. Cheers to many more coming our way.



Files:
FVG SMC EA.ex5  84 kb