Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1342

 
Vladimir Karputov #:

In OnDeinit you should doObjectsDeleteAll- delete by prefix (in your case the prefix is "HLine")

What line should "HLine" be written in?

 
Green handsome #:

In which line should "HLine" be written?

What is not clear?

Forum on Trading, Automated Trading Systems and Strategy Tests

FAQ from Beginners MQL5 MT5 MetaTrader 5

Vladimir Karputov, 2021.09.10 15:53

You have to doObjectsDeleteAll in OnDeinit - delete by prefix(in your case, the prefix is "HLine")

int  ObjectsDeleteAll(
   long           chart_id,   // идентификатор графика
   const string     prefix,   // префикс имени объекта
   int       sub_window=-1,   // индекс окна
   int      object_type=-1    // тип объекта для удаления
   );


 
Vladimir Karputov #:

What exactly is not clear?


int  ObjectsDeleteAll(
   long           chart_id,   // идентификатор графика
   const string      HLine,   // префикс имени объекта
   int       sub_window=-1,   // индекс окна
   int      object_type=-1    // тип объекта для удаления
   
   );

Here I have done it, it still won't delete it.

 
void OnDeinit(const int reason)
  {
     {
      ObjectsDeleteAll(0,prefix,0);
     }
//---
   //ChartRedraw();
  }

the prefix contains the names of the graphic labels

do you know what prefix means?

 
Fast235 #:

the prefix contains the names of the graphic labels

do you know what prefix means?

I don't know MQL at all.... I thought I'd just fix a couple of lines and that's it.

 
Green handsome #:

I don't know anything about MQLs at all.... I thought I'd just fix a couple of lines and that's it.

the code above should help, otherwise look at the prefix with which objects are created

 

What's the reason for not displaying the short name in the subwindow in the top left corner?


Also, if I load one indicator on the chart, the second copy doesn't work anymore - OnInit() = nothing is triggered and it doesn't print. But if I change "Input parameters" to other - it works

#property indicator_separate_window

#property indicator_plots 0

input int MASlow = 21; // Период медленной МА
input int MAFast = 8;  // Период быстрой МА

//+------------------------------------------------------------------+
string prog_name,short_name;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit(void)
{
  prog_name=MQLInfoString(MQL_PROGRAM_NAME);
  short_name="=Set ("+(string)MASlow+"/"+(string)MAFast+")";
  IndicatorSetString(INDICATOR_SHORTNAME,short_name);
  Print("INIT_SUCCEEDED");
  return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| 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[])
{
//---

//--- return value of prev_calculated for next call
  return(rates_total);
}

---

What's wrong, in mt4 this code works.

 
Vitaly Muzichenko #:

What's the reason for not displaying the short name in the subwindow in the top left corner?


Also, if I load one indicator on the chart, the second copy doesn't work anymore - OnInit() = nothing is triggered and it doesn't print. But if I change "Input parameters" to other - it works

---

What's wrong, this code works in mt4.

Maybe it's just because it's too short? No mapping, no buffers... Why make a second copy of such an indicator, so the terminal gets arbitrary...

 
Vitaly Muzichenko #:

What's the reason for not displaying the short name in the subwindow in the top left corner?


Also, if I load one indicator on the chart, the second copy doesn't work anymore - OnInit() = nothing is triggered and it doesn't print. But if I change "Input parameters" to other - it works

---

What's wrong, this code works in mt4.

I had a problem with it too, but I built the indicator, everything is OK.

#property copyright "Copyright 2021, IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- indicator buffers
double         Label1Buffer[];
input int MASlow = 21; // Период медленной МА
input int MAFast = 8;  // Период быстрой МА
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   string short_name = MQLInfoString(MQL_PROGRAM_NAME) + "=Set (" + (string)MASlow + "/" + (string)MAFast + ")";
   IndicatorSetString(INDICATOR_SHORTNAME, short_name);
   SetIndexBuffer(0, Label1Buffer, INDICATOR_DATA);
//---
   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[])
{
   for(int i = prev_calculated == 0 ? 0 : prev_calculated - 1; i < rates_total; i++)
   {
      Label1Buffer[i] = close[i];
   }
   return(rates_total);
}
//+------------------------------------------------------------------+

I might comment it out to find out when it stops writing the indicator's name in the subwindow ... but too lazy

 
Igor Makanu #:

something is missing - probably some kind of property, also faced with this, but here I have sketched the indicator, all is OK

you can comment it to look for when it stops writing the indicator name in the subwindow... but lazy

The data is displayed, but where from, if theOnInit() function has not worked? We throw a copy on the chart and OnInit() may be triggered by accident, but if it is another copy, that's it, there is not even a print. But if we change input parameters - then it works.

#property copyright "Copyright 2021, IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2


input int MASlow = 21; // Период медленной МА
input int MAFast = 8;  // Период быстрой МА

//+------------------------------------------------------------------+
int wndNum;
string short_name;
double Label1Buffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
  short_name="Set ("+(string)MASlow+"/"+(string)MAFast+")";
  IndicatorSetString(INDICATOR_SHORTNAME,short_name);
  wndNum=ChartWindowFind();
  Print("INIT SUCCEEDED: "+(string)wndNum);
  SetIndexBuffer(0, Label1Buffer, INDICATOR_DATA);
//---
  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[])
{
   for(int i = prev_calculated == 0 ? 0 : prev_calculated - 1; i < rates_total; i++)
   {
      Label1Buffer[i] = close[i];
   }
   return(rates_total);
}

---

What's the solution, why OnInit doesn't work ?

Reason: