Download MetaTrader 5

Problem getting the indicator short name within an EA (MQL5)

To add comments, please log in or register
Dr Matthias Hammelsbeck
593
Dr Matthias Hammelsbeck  

Hello everybody,

I need some help because I have trouble in getting the indicator short name within an EA.

Here an example of a short indicator mql5 code (draws simply a horizontal line):

#property  copyright "Copyright 2015, Bobcat"
#property  strict
#property  indicator_chart_window
#property  indicator_buffers 1
#property  indicator_plots   1

#property  indicator_color1  clrRed
#property  indicator_width1  1
#property  indicator_type1   DRAW_LINE
#property  indicator_style1  STYLE_SOLID

input int  periods     = 20;
double     line[];

int OnInit() {
  PlotIndexSetString  (0, PLOT_LABEL, "Linie");
  PlotIndexSetInteger (0, PLOT_DRAW_BEGIN, periods);
  if (SetIndexBuffer(0,line) == false) return INIT_FAILED;
  IndicatorSetInteger (INDICATOR_DIGITS,_Digits);
  IndicatorSetString (INDICATOR_SHORTNAME, "Simple_Line("+IntegerToString(periods)+")");
  return INIT_SUCCEEDED;
}
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 (rates_total < periods) return 0;
   if (prev_calculated == 0) limit = periods;
   else limit = prev_calculated;
   for (int i = limit; (i <= rates_total) && (!IsStopped()); i++) {
     line[i-1] = 1.05000;
   }
   return rates_total;
}

 

 Please note the statement

IndicatorSetString (INDICATOR_SHORTNAME, "Simple_Line("+IntegerToString(periods)+")");



 Here a short EA which tries to get the indicator short name:

#include <Indicators/Custom.mqh>

CiCustom g_Custom_Ind;

int OnInit() {
  // Indicator Creation
  MqlParam p[2];
  p[0].type          = TYPE_STRING;
  p[0].string_value  = "Own\\Name";
  p[1].type          = TYPE_INT;
  p[1].integer_value = 25;
  bool ok = g_Custom_Ind.Create (Symbol(), 0, IND_CUSTOM, 2, p);
  if (ok == false) return INIT_FAILED;

  // Add the indicator to the chart
  int handle = g_Custom_Ind.Handle();
  ChartIndicatorAdd (0, 0, handle);

  // Get the short name of the indicator
  string shortname = ChartIndicatorName (0,0,ChartIndicatorsTotal(0,0)-1);
  Print ("Shortname = ", shortname);
  return INIT_SUCCEEDED;
}

 The output of ChartIndicatorName is "Name"; but I had expected the indicator short name "Simple_Line(25)".

 

Question:

how can I get the indicator short name ?

Thanks for any help

Matthias 

Alain Verleyen
Moderator
35129
Alain Verleyen  

You need to let time for the indicator to be initialized.

Not very nice, but it works :

  // Add the indicator to the chart
  int handle = g_Custom_Ind.Handle();
  ChartIndicatorAdd (0, 0, handle);

  Sleep(100);
  // Get the short name of the indicator
  string shortname = ChartIndicatorName (0,0,ChartIndicatorsTotal(0,0)-1);
  Print ("Shortname = ", shortname," ",total);
I am letting you find a more elegant way ;-)
Dr Matthias Hammelsbeck
593
Dr Matthias Hammelsbeck  
Alain Verleyen:

You need to let time for the indicator to be initialized.

Not very nice, but it works :

  // Add the indicator to the chart
  int handle = g_Custom_Ind.Handle();
  ChartIndicatorAdd (0, 0, handle);

  Sleep(100);
  // Get the short name of the indicator
  string shortname = ChartIndicatorName (0,0,ChartIndicatorsTotal(0,0)-1);
  Print ("Shortname = ", shortname," ",total);
I am letting you find a more elegant way ;-)

Hello Alain,

thanks for your answer. It works now.

In consequence the definition of the function  

bool CIndicator::AddToChart(const long chart,const int subwin)

in the file Indicator.mqh in Include/Indicators should be changed accordingly: 

 

//+------------------------------------------------------------------+
//| Adds indicator to chart                                          |
//+------------------------------------------------------------------+
bool CIndicator::AddToChart(const long chart,const int subwin)
  {
   if(ChartIndicatorAdd(chart,subwin,m_handle))
     {
       /* Hint from Alain Verleyen in https://www.mql5.com/en/forum/165475 */
       Sleep (100);
       m_name=ChartIndicatorName(chart,subwin,ChartIndicatorsTotal(chart,subwin)-1);
       return(true);
     }
//--- failed
   return(false);
  }



 May be an issue for the service desk??

BTW: I'm using the Metatrader 5  build 1502

 

Matthias/Bobcat 

Alain Verleyen
Moderator
35129
Alain Verleyen  
Dr Matthias Hammelsbeck:

Hello Alain,

thanks for your answer. It works now.

In consequence the definition of the function  

bool CIndicator::AddToChart(const long chart,const int subwin)

in the file Indicator.mqh in Include/Indicators should be changed accordingly: 

 

//+------------------------------------------------------------------+
//| Adds indicator to chart                                          |
//+------------------------------------------------------------------+
bool CIndicator::AddToChart(const long chart,const int subwin)
  {
   if(ChartIndicatorAdd(chart,subwin,m_handle))
     {
       /* Hint from Alain Verleyen in https://www.mql5.com/en/forum/165475 */
       Sleep (100);
       m_name=ChartIndicatorName(chart,subwin,ChartIndicatorsTotal(chart,subwin)-1);
       return(true);
     }
//--- failed
   return(false);
  }



 May be an issue for the service desk??

BTW: I'm using the Metatrader 5  build 1502

 

Matthias/Bobcat 

Yes it should be reported to ServiceDesk. Please keep us posted.
Dr Matthias Hammelsbeck
593
Dr Matthias Hammelsbeck  
Alain Verleyen:
Yes it should be reported to ServiceDesk. Please keep us posted.

Thanks.

I reported the problem to the ServiceDesk. I keep you informed as soon I get an answer.

Matthias

Dr Matthias Hammelsbeck
593
Dr Matthias Hammelsbeck  

Unfortunately I have no answer from the service desk :-(

I'm waiting. Sorry.

To add comments, please log in or register