Untitled

 avatar
unknown
plain_text
8 days ago
3.4 kB
7
Indexable
//+------------------------------------------------------------------+
//|                                            FVG_Strategy.mq4      |
//|   Fair Value Gap Auto-Trader with Visuals (Boxes & Labels)      |
//+------------------------------------------------------------------+
#property strict
#property indicator_chart_window

//--- Settings
input double Slippage = 3;
input int    MagicNumber = 123456;
input int    LookbackBars = 500;
input int    ExtensionBars = 3;   // Box extends this many bars to the right

double LotSize = 0.01;  // Fixed lot size

//+------------------------------------------------------------------+
void OnTick()
{
   if (Bars < 3) return;
   if (OrdersTotal() > 0) return; // Only one trade at a time

   int i = 2;

   datetime startTime = Time[i - 2];
   datetime endTime = Time[MathMax(i - 2 - ExtensionBars, 0)];

   // Check Bullish FVG
   if (Low[i] > High[i - 2])
   {
      double entry = Ask;
      double sl = Low[i - 1];
      double tp = entry + (entry - sl);

      if (entry - sl > 0)
      {
         int ticket = OrderSend(Symbol(), OP_BUY, LotSize, entry, Slippage, sl, tp, "Bullish FVG Buy", MagicNumber, 0, clrGreen);
         if (ticket > 0)
         {
            DrawFVGBox("BullFVG_" + TimeToString(Time[i], TIME_MINUTES), startTime, endTime, High[i - 2], Low[i], clrLime);
            LabelTrade("Buy", Time[i], entry, clrGreen);
         }
      }
   }

   // Check Bearish FVG
   else if (High[i] < Low[i - 2])
   {
      double entry = Bid;
      double sl = High[i - 1];
      double tp = entry - (sl - entry);

      if (sl - entry > 0)
      {
         int ticket = OrderSend(Symbol(), OP_SELL, LotSize, entry, Slippage, sl, tp, "Bearish FVG Sell", MagicNumber, 0, clrRed);
         if (ticket > 0)
         {
            DrawFVGBox("BearFVG_" + TimeToString(Time[i], TIME_MINUTES), startTime, endTime, High[i], Low[i - 2], clrRed);
            LabelTrade("Sell", Time[i], entry, clrRed);
         }
      }
   }
}

//+------------------------------------------------------------------+
//| Draws FVG rectangle on chart                                     |
//+------------------------------------------------------------------+
void DrawFVGBox(string name, datetime startTime, datetime endTime, double top, double bottom, color boxColor)
{
   if (ObjectFind(0, name) >= 0) ObjectDelete(0, name);

   ObjectCreate(0, name, OBJ_RECTANGLE, 0, startTime, top, endTime, bottom);
   ObjectSetInteger(0, name, OBJPROP_COLOR, boxColor);
   ObjectSetInteger(0, name, OBJPROP_WIDTH, 1);
   ObjectSetInteger(0, name, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSetInteger(0, name, OBJPROP_BACK, true);
   ObjectSetInteger(0, name, OBJPROP_RAY_RIGHT, false);
}

//+------------------------------------------------------------------+
//| Labels the trade entry point                                     |
//+------------------------------------------------------------------+
void LabelTrade(string type, datetime t, double price, color labelColor)
{
   string label = "Label_" + IntegerToString(t);
   if (ObjectFind(0, label) >= 0) ObjectDelete(0, label);

   ObjectCreate(0, label, OBJ_TEXT, 0, t, price);
   ObjectSetInteger(0, label, OBJPROP_COLOR, labelColor);
   ObjectSetInteger(0, label, OBJPROP_FONTSIZE, 10);
   ObjectSetString(0, label, OBJPROP_TEXT, type + " @ " + DoubleToString(price, Digits));
}
Editor is loading...
Leave a Comment