Help needed with drawing Rectangle Object

 
So, i made this code and it does't throw any errors, but i don't see any rectangle on my chart.
What have i done wrong ?

         // Calculate rectangle coordinates
         datetime startTime = iTime(symbol, period, 1);
         datetime endTime = iTime(symbol, period, 2);
         double startY = MathMin(open, previousClose);
         double endY = MathMax(open, previousClose);
         
         // Draw rectangle around the gap
         int rectangle = ObjectCreate(0, "GapRectangle", OBJ_RECTANGLE, 0, startTime, startY, endTime, endY);
         if (rectangle != -1)
         {
            printf("Rectangle added");
            ObjectSetInteger(0, "GapRectangle", OBJPROP_COLOR, clrRed); // Set rectangle color
            ObjectSetInteger(0, "GapRectangle", OBJPROP_WIDTH, 2); // Set rectangle line width
            ObjectSetInteger(0, "GapRectangle", OBJPROP_BACK, false); // Make the rectangle background empty
         }
         else
         {
            Print("Error creating rectangle: ", GetLastError());
         }

 
Toyuu:
What have i done wrong ?

The easiest way to find out is to find your rectangle in the list of objects and check its properties


 

Toyuu:
So, i made this code and it does't throw any errors, but i don't see any rectangle on my chart.
What have i done wrong ?


Return value of the ObjectCreate(); function is boolen. either true or false. the issue contains in your if statement. put this code into chart and you will see. 


int OnInit()
  {
   double High1 = iHigh(NULL,PERIOD_CURRENT,1);
   double Low1 = iLow(NULL,PERIOD_CURRENT,1);
   
   datetime Start = iTime(NULL,PERIOD_CURRENT,1); 
   datetime End = iTime(NULL,PERIOD_CURRENT,1) + 60*60*24; // add a one day to the 15 min candle open price
   
   ObjectDelete(0,"Rectangle"); // delete the old one to avoid over creating.  

   bool Created = ObjectCreate(0,"Rectangle",OBJ_RECTANGLE,0,Start,High1,End,Low1); 
   
   if(Created == true)
   {
   ObjectSetInteger(0,"Rectangle",OBJPROP_BACK,1); // set to the background
   ObjectSetInteger(0,"Rectangle",OBJPROP_COLOR,clrWhite);
   
   Print("Object Rectangle has created"); 
   }
   else
   {
   Print(GetLastError()); // prints the last error. 
   ResetLastError(); // reset the last error is nessery. 
   
   }
   return (INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
{
 ObjectDelete(0,"Rectangle"); // clears the chart. 

}
 
Hapu Arachchilage Tharindu Lakmal #:
int OnInit()   {    double High1 = iHigh(NULL,PERIOD_CURRENT,1);
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:
Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

thanks for the information, bro, it's Technically true. but in this case just put it into a fully loaded chart. 

 

Well, i checked all that, and now i see that the Rectangle is drawn, but on the wrong chart.
But why is that ? It always create it on the AUDCAD 1H chart, but i want it on the WS30 15M chart (which is the chart in which the code is running on).


bool rectangleCreated = ObjectCreate(0, "GapRectangle", OBJ_RECTANGLE, 0, startTime, startY, endTime, endY);
 
Oh, nevermind, it was in my Debugger settings, the chart was wrongly set.
Well, thanks for your help.