How to draw rectangles on top of the chart?

 
How do I draw rectangles on the top of the chart? I also want them to stay there if I zoom or unzoom. 
 
Use the rectangle tool. What's the problem?
 

OBJ_RECTANGLE uses two coordinates and will move with the bars as you zoom in and out.

OBJ_LABEL uses 1 coordinate in pixels and will remain relative to the window as you zoom in and out. This is what you are looking for, right?

Try using OBJ_LABEL with Wingdings as a work around. This will draw a "rectangle" that remains relative to the window.

 

   ObjectCreate("rec1", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("rec1", "n", 200, "Wingdings", Gray);
   ObjectSet("rec1", OBJPROP_XDISTANCE, 10);
   ObjectSet("rec1", OBJPROP_YDISTANCE, 20);
   ObjectSet("rec1", OBJPROP_BACK, true);
   
   ObjectCreate("rec2", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("rec2", "n", 200, "Wingdings", Gray);
   ObjectSet("rec2", OBJPROP_XDISTANCE, 10);
   ObjectSet("rec2", OBJPROP_YDISTANCE, 120);
   ObjectSet("rec2", OBJPROP_BACK, true);
 
MisterDog:

OBJ_RECTANGLE uses two coordinates and will move with the bars as you zoom in and out.

OBJ_LABEL uses 1 coordinate in pixels and will remain relative to the window as you zoom in and out. This is what you are looking for, right?

Try using OBJ_LABEL with Wingdings as a work around. This will draw a "rectangle" that remains relative to the window.

 


 

Thanks. Now what I wanted to do was to draw a rectangle at the top of each bar, and keep it always on the top of the chart. Do you know how to do that?
 
You can NOT "draw a rectangle at the top of each bar"! Each rectangle must have a starting and stopping date/time (and price). Therefor the rectangle must cover one bar to another.
 
WHRoeder:
You can NOT "draw a rectangle at the top of each bar"! Each rectangle must have a starting and stopping date/time (and price). Therefor the rectangle must cover one bar to another.

Yes you can, using what MisterDog said. Now what I want to do is to keep them fixed on the top of the chart, for each bar.
 

You asked about drawing a rectangle. You can NOT use a rectangle object.

MisterDog said to use a LABEL. That would put a SQUARE on the window.  That means they will not stay with the bar as the chart scrolls. And you can't zoom the chart unless you repaint the objects each time.

If you want a SQUARE, you can use a wingding text object. Those will move with the bars.

 

Yes, well put WHRoeder. It took me awhile to wrap my head around the idea that some objects are oriented to the screen and positioned with pixels. While other objects are oriented to the chart positioned by bars and pips.

From your additional comments, it sounds like my recommendation is not your solution.

 
Tosh5457:
How do I draw rectangles on the top of the chart? I also want them to stay there if I zoom or unzoom. 

Try this

ChartSetInteger(ChartID(),CHART_FOREGROUND,0,false);

ObjectCreate(ChartID(),"obj_rect",OBJ_RECTANGLE_LABEL,0,0,0) ;
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_XDISTANCE,100);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_YDISTANCE,100);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_BACK,false);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_XSIZE,300);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_YSIZE,100);
 
Denny Muhammad Zein:

Try this

ChartSetInteger(ChartID(),CHART_FOREGROUND,0,false);

ObjectCreate(ChartID(),"obj_rect",OBJ_RECTANGLE_LABEL,0,0,0) ;
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_XDISTANCE,100);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_YDISTANCE,100);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_BACK,false);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_XSIZE,300);
ObjectSetInteger(ChartID(),"obj_rect",OBJPROP_YSIZE,100);

Thank you. Saved hours

Reason: