Need Assistance with Text Alignment on Candlestick Chart

 

Hello forum members,

I'm seeking help regarding text alignment displayed on a candlestick chart. I've encountered an issue where the labels are overlapping with the right candlestick and not displaying properly (please refer to the attached image).

Upon reviewing my code, I'm struggling to identify the root cause of this problem. I would greatly appreciate any guidance or suggestions to rectify this issue and ensure that the labels for Open and Close Prices are displayed at the center of the low and high candlesticks.

Thank you in advance for your assistance.

#property indicator_chart_window

input color FontColorOpen = clrGreen; // Font color for OPEN price
input color FontColorClose = clrRed;  // Font color for CLOSE price
input int FontSize = 10;              // Adjusted Font size
input int LabelCorner = 10;           // Value for OBJPROP_CORNER
input int LabelXDistance = 25;        // Value for OBJPROP_XDISTANCE

void DrawLabel(string text, datetime x1, double vPrice, color vcolor, string description)
{
    // Label
    string label = description +" - "+DoubleToStr(x1, 0);
    ObjectCreate(label, OBJ_TEXT, 0, x1, vPrice);
    ObjectSetText(label, text, FontSize, "Tahoma", vcolor); // Adjusted font size here
    ObjectSet(label, OBJPROP_ANGLE, 90);  
    ObjectSet(label, OBJPROP_BACK, false);
    ObjectSet(label, OBJPROP_CORNER, LabelCorner); // Set the label to the specified corner
    ObjectSet(label, OBJPROP_XDISTANCE, LabelXDistance); // Set the distance from the left edge of the chart
}

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[])
{
    for (int i = 0; i < rates_total; i++)
    {
        string openText = "Open: " + DoubleToStr(open[i], _Digits);
        string closeText = "Close: " + DoubleToStr(close[i], _Digits);
        
        // Remove previous labels to avoid duplicates
        ObjectDelete("OpenLabel_" + IntegerToString(i));
        ObjectDelete("CloseLabel_" + IntegerToString(i));
        
        // Draw OPEN price label below the shadow
        double openLabelX = time[i];
        double openLabelY = low[i] - MarketInfo(Symbol(), MODE_POINT) * 50;
        DrawLabel(openText, openLabelX, openLabelY, FontColorOpen, "OpenLabel_" + IntegerToString(i));
        
        // Draw CLOSE price label above the shadow
        double closeLabelX = time[i];
        double closeLabelY = high[i] + MarketInfo(Symbol(), MODE_POINT) * 50;
        DrawLabel(closeText, closeLabelX, closeLabelY, FontColorClose, "CloseLabel_" + IntegerToString(i));
    }
    
    return(rates_total);
}
Files:
text_align.PNG  46 kb
 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893