Align label to top right of chart?

 

How can I align a label to the top right?

I made this helper class, using the Standard Library. I do set the Anchor property (using  https://www.mql5.com/en/docs/standardlibrary/chart_object_classes/obj_controls/cchartobjecttext/cchartobjecttextanchor), but it has no effect. The label is positioned at the top right, but it is still anchored top left.

#include <ChartObjects\ChartObjectsTxtControls.mqh>


const int padding = 10;
const int rowHeight = 15;


class Utils
{


    public:

        // Adds a label to the top right corner of the current chart.
        static void Utils::AddLabel(string text, string objectName, int row = 0)
        {
            // Create.
            CChartObjectLabel label;
            label.Create(ChartID(), objectName, 0, 0, 0.0);
            
            // Content.
            label.Description(text);

            // Style
            label.Color(clrWhite);
            label.FontSize(20);
            label.Font("Trebuchet MS");
            
            // Positioning.
            label.Anchor(ANCHOR_RIGHT_UPPER);
            label.Corner(CORNER_RIGHT_UPPER);
            label.X_Distance(padding);
            label.Y_Distance(padding + row * rowHeight);
        }
};

Interestingly enough, when I set the label anchor manually in the terminal (see screenshot below), it gets aligned just fine (!).

Bonus: It does the same with Button Objects.

Documentation on MQL5: Standard Library / Graphic Objects / Control Objects / CChartObjectText / Anchor
Documentation on MQL5: Standard Library / Graphic Objects / Control Objects / CChartObjectText / Anchor
  • www.mql5.com
Standard Library / Graphic Objects / Control Objects / CChartObjectText / Anchor - Reference on algorithmic/automated trading language for MetaTrader 5