Download MetaTrader 5

Label at the end of the indicator line

To add comments, please log in or register
Find out the latest MQL5.community news!
user_123
172
user_123 2012.07.09 19:00 

Dear community,

I would be very  grateful if somebody could help me with the position of the label. I would like to create some labels at the end of the indicator lines like on the picture below. The labels should move to right, if we get new candles. 

 

 

I try to solve the problem with the "price" and "time" property of the CChartObjectLabel object, but the label is always shown at the left upper corner?

Thank you very much! 

 

#property version   "1.00"
#property indicator_chart_window
#include <ChartObjects\ChartObjectsTxtControls.mqh>
CChartObjectLabel myLabel;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
//--- indicator buffers mapping
   string labelName = "MyLabel";
   myLabel.Create(0,labelName,0,0,0);
//---
   return(0);
  }
  
int OnDeinit(){
//--- indicator buffers mapping
   myLabel.Delete();
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[]){
//---

   int limit;
   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;
   
   string text = "Level " + DoubleToString(close[limit]);
   myLabel.Time(time[limit]);
   myLabel.FontSize(20);
   myLabel.Price(close[limit]);
   myLabel.Color(White);
   myLabel.Font("Tahoma");
   myLabel.SetString(OBJPROP_TEXT,0,text);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
Documentation on MQL5: Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines
  • www.mql5.com
Standard Constants, Enumerations and Structures / Indicator Constants / Indicators Lines - Documentation on MQL5
Andrei Moraru
1157
Andrei Moraru 2012.07.09 20:12  
I'd change time[limit] and close[limit] to time[rates_total-1] and close[rates_total-1].
user_123
172
user_123 2012.07.09 23:09  

Dear enivid,

yes you have have right with the index. I changed it, like you say, but it does not help. The label is still in the left upper corner. 

onewithzachy
2271
onewithzachy 2012.07.10 02:51  
user_123:

Dear community,

I would be very  grateful if somebody could help me with the position of the label. I would like to create some labels at the end of the indicator lines like on the picture below. The labels should move to right, if we get new candles. 

I try to solve the problem with the "price" and "time" property of the CChartObjectLabel object, but the label is always shown at the left upper corner?

Thank you very much! 

Hi user_123,

1. Don't create object in OnInit(). There's a risk that the object gets deleted and never will be created again - unless we open the property window.

2. If we create multiple objects, make all of them have different names, for example, My_Lines_1, My_Lines_2, My_Lines_3, ... etc. If we have single name when creating multiple objects, the last created object is the winner, all of previous created objects are all dead.

3. Use CChartObjectText::Create (OBJ_TEXT) and not CChartObjectLabel::Create (OBJ_LABEL). Object text will stick with the chart with price time coordinates while object label will stick with chart window with x, y pixel coordinate - that is why you have one text on left upper corner because you creating text label with x = 0, y = 0 coordinates on upper left = 0 corner with the same name !.

:D 

user_123
172
user_123 2012.07.10 12:07  

Dear onewithzachy,

thank you very much! 

onewithzachy
2271
onewithzachy 2012.07.10 16:52  
user_123:

Dear onewithzachy,

thank you very much! 

Hi user_123,

You're welcome :) 

You may want to anchor it to the left (ENUM_ANCHOR_POINT : ANCHOR_LEFT) and add that time[limit] with (n*PeriodSeconds(PERIOD_CURRENT)). n is the number of bar to the right.

:D 

Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Timeseries and Indicators Access / Bars - Documentation on MQL5
To add comments, please log in or register