Trying to draw Rectangle for FVGs

 

I feel like this shouldnt be this hard, but i am just learning in my journey and trying to work out how to draw a rectangle when there is imbalance there.

I know i could just use an indicator already in the market but then i don't learn anything.

I have gone through the forums and tried to understand but i am just not getting it.

Once i understand this concept, it will help with drawing rectangles around all sorts of boxes.. but i just need that leg up now on how to find the coordinates and then to translate that to what i need specifically 

I understand the first time and price is the starting place of the rectangle and the second time and price is the end...
But how would i use those in this case?

Price 1 and Time 1 are candle [3] and Price 2 and Time 2 are current candle and time [0]
But i also need to add in... If Low[4]>High[2] then draw the rectangle from Low[4] and High[2] starting at candle 3 until we delete the rectangle

No where can i find something that is simple and says... here do this

my brain is literally going to fall out....

Here is what i have

and my errors


void OnTick(){
//A - Candle 3's low is higher than candle 1's high so there is a gap which needs to have a rectangle drawn there
//B - Candles 3's high is lower than candle 1's low so there is a gap which needs to have a rectangle drawn there

//define Candle OHLC Prices
  double CLow = PRICE_LOW;
  double CHigh = PRICE_HIGH;
  double COpen = PRICE_OPEN;
  double CClose = PRICE_CLOSE;

//define Bull and Bear Candles
  double Bull = PRICE_OPEN<PRICE_CLOSE;
  double Bear = PRICE_OPEN>PRICE_CLOSE;
  
  // Create variable for Highs and Lows for A or B Scenario
  int FVGHigh,FVGLow;
  
  // create array for CandleHighs and Lows
  double High[],Low[];
  
  // sort arrays downwards from the current candle
  ArraySetAsSeries(High,true);
  ArraySetAsSeries(Low,true);
 
  //Fill arrays with data for FVGs for 3 candles for FVG idenitification
  CopyHigh(_Symbol,_Period,0,3,High);
  CopyLow(_Symbol,_Period,0,3,Low); 
  
  //calculate the high and lows for the FVGs
  FVGHigh = ArrayMaximum(High,0,3);     // uses data for the last 3 candles
  FVGLow = ArrayMaximum(Low,0,3);       // uses data for the last 3 candles

  //Create an array for prices and sort it from current to oldest candles
  MqlRates Price[];
  ArraySetAsSeries(Price,true);
  
  //Copy price data into the array
  int Data=CopyRates(_Symbol,_Period,0,Bars(_Symbol,_Period),Price);
  
  // delete any previous rectangles
  ObjectDelete(_Symbol,"Rectangle");
  }
  
  // if candle 3 is a bear and candle 2 and candle 1 are both bulls.. and if there is a gap 
  if(Bear[3] and Bull[2] and Bull[1] and CLow[3]>CHigh[1]){
  
  //create the new rectangle using candle3s low, starting at candle 2, using candle 1s high and ending at candle 0
  ObjectCreate(_Symbol,"Rectangle",OBJ_RECTANGLE,0,Price[2].time,Price[FVGLow].Low[3],Price[0].time,Price[FVGHigh].high[1]);

  //set Object line color
  ObjectSetInteger(0,"Rectangle",OBJPROP_COLOR,clrRed);
  
  //Set the Object fill color
  ObjectSetInteger(0,"Rectangle",OBJPROP_FILL,clrRed);
   } 

  //check if we have a Bull FVG
  // if candle 3 is a Bull and candle 2 and candle 1 are both bears.. and if there is a gap 
  if(Bull[3] and Bear[2] and Bear[2] and High[3] < Low[1]){
  
  //create the new rectangle using candle3s high, starting at candle 2, using candle 1s low and ending at candle 0
  ObjectCreate(_Symbol,"Rectangle",OBJ_RECTANGLE,0,Price[2].time,Price[FVGHigh].CHigh[3],Price[0].time,Price[FVGLow].CLow[1]);

  //set Object line color
  ObjectSetInteger(0,"Rectangle",OBJPROP_COLOR,clrLime);
  
  //Set the Object fill color
  ObjectSetInteger(0,"Rectangle",OBJPROP_FILL,clrLime);
   } 
  
}

where i want the rectangle to draw 


I tried to follow this script...https://www.mql5.com/en/docs/constants/objectconstants/enum_object/obj_rectangle

I also followed the below YT and tried to adapt it.. 
where it is collecting the High and Low for the last 30 candles, sorting the arrays from highest to lowest and storing the highest and lowest prices of the last 30 candles in the array
Then when its actually drawing the box its using time1 as 10 candles back, price1 as the highest price from 30 candles back, time2 as candle 0 (now) and price2 as the lowest price from the last 30 candles


void OnTick(){
  // Create variable for highest and lowest candle
  int HighestCandle,LowestCandle;

  
  // create array for highest and lowest candle
  double High[],Low[];
  
  // sort arrays downwards from the current candle
  ArraySetAsSeries(High,true);
 
  // sort array downwards from the current candle
  ArraySetAsSeries(Low,true);
  
  //Fill array with data for 30 candles
  CopyHigh(_Symbol,_Period,0,30,High);
  
  //Fill array with data for 30 candles
  CopyLow(_Symbol,_Period,0,30,Low); 
  
  //calculate the highest candle
  HighestCandle = ArrayMaximum(High,0,30);         //uses the data for the last 30 candles
   
  //calculate the lowest candle
  LowestCandle = ArrayMinimum(Low,0,30);           // uses data for last 30 candles
  
  //Create an array for prices
  MqlRates PriceInformation[];
  
  // sort it from current candle to oldest candle
  ArraySetAsSeries(PriceInformation,true);
  
  //Copy price data into the array
  int Data=CopyRates(_Symbol,_Period,0,Bars(_Symbol,_Period),PriceInformation);
  
  // delete former rectangles
  ObjectDelete(_Symbol,"Rectangle");
  
  //create the object
  ObjectCreate(_Symbol,"Rectangle",OBJ_RECTANGLE,0,PriceInformation[10].time,
               PriceInformation[HighestCandle].high,PriceInformation[0].time,
               PriceInformation[LowestCandle].low);
               
  //set Object line color
  ObjectSetInteger(0,"Rectangle",OBJPROP_COLOR,clrBlue);
  
  //Set the Object fill color
  ObjectSetInteger(0,"Rectangle",OBJPROP_FILL,clrBlue);
  
}

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_RECTANGLE
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types / OBJ_RECTANGLE
  • www.mql5.com
OBJ_RECTANGLE - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Simply rectangle:
//+------------------------------------------------------------------+
//|                                             IND_RECTANGLE_RP.mq5 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   string strObjName = "Rectangle";
   ObjectDelete(0, strObjName);
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
   string strObjName = "Rectangle";
   // Vertex 1
   datetime dt_1 = time[rates_total - 1];
   double pr_1 = high[rates_total - 1];
   
   // Vertex 2
   datetime dt_2 = time[rates_total - 4];
   double pr_2 = low[rates_total - 4];
   
   ObjectCreate(0, strObjName, OBJ_RECTANGLE, 0, dt_1, pr_1, dt_2, pr_2);
   ObjectSetInteger(0, strObjName, OBJPROP_FILL, true);
   ObjectSetInteger(0, strObjName, OBJPROP_COLOR, clrLightGreen);
    
   return(rates_total);
}
//+------------------------------------------------------------------+


Determining the specific corners of the rectangle:

//+------------------------------------------------------------------+
//|                                             IND_RECTANGLE_RP.mq5 |
//+------------------------------------------------------------------+
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots 0
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   string strObjName = "Rectangle";
   ObjectDelete(0, strObjName);
   
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 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[])
{
   string strObjName = "Rectangle";
   
   int i_Bars = 10;

   datetime dt_1 = time[rates_total - i_Bars];
   datetime dt_2 = time[rates_total - 1];
   
   // Min, Max
   int i_Count = i_Bars;
   int i_Start = 0;
   int i_Min = iLowest(Symbol(), PERIOD_CURRENT, MODE_LOW, i_Count, i_Start);
   int i_Max = iHighest(Symbol(), PERIOD_CURRENT, MODE_HIGH, i_Count, i_Start);
   
   double pr_1 = low[rates_total - 1 - i_Min];
   double pr_2 = high[rates_total - 1 - i_Max];
   
   ObjectCreate(0, strObjName, OBJ_RECTANGLE, 0, dt_1, pr_1, dt_2, pr_2);
   ObjectSetInteger(0, strObjName, OBJPROP_FILL, true);
   ObjectSetInteger(0, strObjName, OBJPROP_COLOR, clrLightGreen);
    
   return(rates_total);
}
//+------------------------------------------------------------------+
https://ibb.co/LQzLMgw
123 hosted at ImgBB
123 hosted at ImgBB
  • ibb.co
Image 123 hosted in ImgBB
 
pebs85:

  double CLow = PRICE_LOW;
  double CHigh = PRICE_HIGH;
  double COpen = PRICE_OPEN;
  double CClose = PRICE_CLOSE;

//define Bull and Bear Candles
  double Bull = PRICE_OPEN<PRICE_CLOSE;
  double Bear = PRICE_OPEN>PRICE_CLOSE;


Those tokens in red are constants not values. Either:

  1. Perhaps you should read the manual.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. Or Stop using ChatGPT.
              Help needed to debug and fix an AI EA - Trading Systems - MQL5 programming forum #2 (2023)

Reason: