Code
#property indicator_separate_window
#property indicator_color1 Blue
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Indicator buffer
return INIT_SUCCEEDED; // Return success
}
//+------------------------------------------------------------------+
//| 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[])
{
// Define the time and price for the rectangle
datetime startTime = Time[10]; // Start time for the rectangle
datetime endTime = Time[0]; // End time for the rectangle
double topPrice = High[10]; // Top price for the rectangle
double bottomPrice = Low[0]; // Bottom price for the rectangle
// Create a rectangle
string rectName = "MyRectangle";
ObjectCreate(rectName, OBJ_RECTANGLE, 0, startTime, topPrice, endTime, bottomPrice);
// Set properties for the rectangle
ObjectSetInteger(0, rectName, OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, rectName, OBJPROP_STYLE, STYLE_SOLID);
ObjectSetInteger(0, rectName, OBJPROP_WIDTH, 2);
// Create a text label for the rectangle name
string labelName = "RectangleLabel";
double labelY = topPrice; // Position label at the top of the rectangle
ObjectCreate(labelName, OBJ_LABEL, 0, startTime, labelY);
// Set properties for the label
ObjectSetString(0, labelName, OBJPROP_TEXT, rectName);
ObjectSetInteger(0, labelName, OBJPROP_COLOR, clrWhite);
ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 12);
ObjectSetInteger(0, labelName, OBJPROP_XSIZE, 0); // Prevents shifting with size change
ObjectSetInteger(0, labelName, OBJPROP_YSIZE, 0);
// Adjust position slightly for visibility
ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, 5); // Move text slightly below the top
}
//+------------------------------------------------------------------+
text label is not showing on rectangle. Could you help me to adjust code?
The text label is not showing on the rectangle because I am using OBJ_LABEL, which positions text relative to the chart window (not within the chart's price-time coordinates). To make the label appear at a specific position within the chart (aligned with the rectangle), I should use OBJ_TEXT instead of OBJ_LABEL.