Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1018

 
Alexey Viktorov:

It's hard to understand anything with these names. I have no problems with connecting indicators as resources.


Is presence of a source code (.mq5) necessary?

Alexey Viktorov:

What does it have to do with

Does it affect the connection of a resource?

 
Сергей Таболин:

Is the availability of source (.mq5) compulsory?

it's quicker to check, here I made a connection through a resource in MT5, I don't see any problems

// close.mql5
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

double buff[];

int OnInit()
  {
   SetIndexBuffer(0,buff,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[])
  {
   int i,limit;
   if(prev_calculated==0) limit=0; else limit=prev_calculated-1;
   for(i=limit;i<rates_total;i++) buff[i]=close[i];
   return(rates_total);
  }

and connect this indicator as a resource, then I removed this indicator to check if the call is really coming from the resources:

#property indicator_chart_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  1

#resource "close.ex5"

//--- indicator buffers
double         Label1Buffer[];
int handle;

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   handle = iCustom(Symbol(),0,"::close.ex5");
   if(handle == INVALID_HANDLE)                                         // проверяем наличие хендла индикатора
   {
      Print("Не удалось получить хендл индикатора handle_MA");             // если хендл не получен, то выводим сообщение в лог об ошибке
      return(INIT_FAILED);                                                 // завершаем работу с ошибкой
   }
//---
   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 copy=CopyBuffer(handle,0,0,rates_total,Label1Buffer); 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 
Igor Makanu:

it's quicker to check, here I made a connection in MT5 via a resource, I see no problems

and connect this indicator as a resource, then I removed this indicator to check if the call is really coming from resources:

//+------------------------------------------------------------------+
//|                                                       myHMA5.mq5 |
//|                                          Copyright 2019.03, Test |
//|                                         /ru |
//+------------------------------------------------------------------+
#property   copyright               "Copyright 2019, Test"
#property   link                    "/ru"
#property   version                 "1.21"
//----------------------------------------------------------------------------------------------
#resource   "\\Indicators\\Market\\HMA5.ex5"
//----------------------------------------------------------------------------------------------
//#property   strict
#property   indicator_chart_window
#property   indicator_buffers       3
#property   indicator_plots         1

#property   indicator_label1        "myHMA5"
#property   indicator_type1         DRAW_COLOR_LINE
#property   indicator_color1        clrLime,clrRed
#property   indicator_style1        STYLE_SOLID
#property   indicator_width1        2 
//----------------------------------------------------------------------------------------------
//--- input parameters
input int      InpPeriodMA          = 45; // MA period
//--- indicator buffers
double         Buffer0[];
double         Buffer1_Color[];
int            handle_MA;                 // переменная для хранения хэндла индикатора HMA5
double         buffer_MA[];               // массив для хранения значений индикатора HMA5
int            ma_bars_calculated   = 0; 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArraySetAsSeries(Buffer0         ,true);
   ArraySetAsSeries(Buffer1_Color   ,true);
   ArraySetAsSeries(buffer_MA       ,true);

   SetIndexBuffer(0,Buffer0         ,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer1_Color   ,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,buffer_MA       ,INDICATOR_CALCULATIONS);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,-1);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,-1);
   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,-1);
//--- set shortname and change label
   string short_name="myHMA5("+
                              IntegerToString(InpPeriodMA)
                              +")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpPeriodMA);
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpPeriodMA);
   PlotIndexSetInteger(2,PLOT_DRAW_BEGIN,InpPeriodMA);

   handle_MA = iCustom(Symbol(),0,"::Indicators\\Market\\HMA5.ex5",InpPeriodMA);
   if(handle_MA == INVALID_HANDLE)                                         // проверяем наличие хендла индикатора
   {
      Print("Не удалось получить хендл индикатора handle_MA");             // если хендл не получен, то выводим сообщение в лог об ошибке
      return(INIT_FAILED);                                                 // завершаем работу с ошибкой
   }

   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 ma_values_to_copy; 
   int ma_calculated = BarsCalculated(handle_MA); 
   if(ma_calculated <= 0){ 
      PrintFormat("BarsCalculated() вернул %d, код ошибки %d",ma_calculated,GetLastError()); 
      return(0); 
     }  
   if(prev_calculated == 0 || ma_calculated != ma_bars_calculated || rates_total > prev_calculated + 1){ 
      if(ma_calculated > rates_total) ma_values_to_copy = rates_total; 
      else ma_values_to_copy = ma_calculated; 
     } else { 
      ma_values_to_copy = (rates_total - prev_calculated) + 1; 
     } 
     
   if(CopyBuffer(handle_MA,0,0,ma_values_to_copy,buffer_MA) < 0 ) // копируем данные из индикаторного массива в массив buffer_MA
   {                                                                                // если не скопировалось
      Print("Не удалось скопировать данные из индикаторного буфера в buffer_MA");   // то выводим сообщение об ошибке
      return(0);                                                                    // и выходим из функции
   }

   for(int i = 0; i < ma_values_to_copy; i++)
   {
      Buffer0[i]        = buffer_MA[i];
      
      Buffer1_Color[i]  = 0;
      if(NormalizeDouble(Buffer0[i],Digits()) == NormalizeDouble(Buffer0[i+1],Digits())) Buffer1_Color[i] = Buffer1_Color[i+1];
      else
      if(Buffer0[i] > Buffer0[i+1]) Buffer1_Color[i] = 0;
      else
      if(Buffer0[i] < Buffer0[i+1]) Buffer1_Color[i] = 1;
      else                          Buffer1_Color[i] = Buffer1_Color[i+1];
   }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

Will you try it?

 
Сергей Таболин:

Will you try it?

No problem at all, even without any edits to your code.


 
Igor Makanu:

Alas, this is not for me, I am not very friendly with MT5, but you can use my examples to find why your resource does not connect, delete the source code of close.mql5 indicator and then try to compile the main indicator using close.ex5.... you need to check why it is not working, i think that the protection from Market is preventing you to make the share

О! I'll try to connect my indicator with the resource.

 
Alexey Viktorov:

No problem at all, even without any edits to your code.


Funny... (((

What's wrong with me? ....

 
Igor Makanu:

Alas, this is not for me, I am not very friendly with MT5, but you can use my examples to find why your resource does not connect, delete the source code of close.mql5 indicator and then try to compile the main indicator using close.ex5.... you need to check why it is not working, i think that the protection from the Market is preventing you to make the share

No protection by the Marketplace prevents the indicator connected to the resource from working on one computer in different terminals. Example above.
 

It seems to be working. Thank you.

But now I've run into another error.

"array out of range" frowns on

   for(int i = 0; i < ma_values_to_copy; i++)
   {
      double   noise    = high[i]-low[i]+high[i+1]-low[i+1]+high[i+2]-low[i+2]; // array out of range

but the indicator is drawn...

 
Сергей Таболин:

It seems to be working. Thank you.

But now I've run into another error.

"array out of range" frowns on

but the indicator is drawn...

high[i+1]
low[i+1]
high[i+2]
low[i+2] 
при значении i >=  ma_values_to_copy даст такую ошибку

And here you have "two ways out":

1 .

for(int i = 0; i < (ma_values_to_copy-2); i++)
2.
for(int i = 2; i < ma_values_to_copy; i++)
{
        double   noise    = high[i-2]-low[i-2]+high[i-1]-low[i-1]+high[i]-low[i];
 
Oleg Peiko:

And here you have "two ways to go":

1 .

2.

The first way helped )))) Thanks.

One more question: I tear off 3 indicator instances in the indicator to get data from different TFs. But when I attach it to a chart, I get the same layout:

2019.03.23 20:59:05.531 my_HMA5_123 (USDCHF,M30)        Скопирован индикаторный буфер в buffer_MA
2019.03.23 20:59:05.531 my_HMA5_123 (USDCHF,M30)        Не удалось скопировать данные из индикаторного буфера в buffer_MA2
2019.03.23 20:59:05.703 my_HMA5_123 (USDCHF,M30)        Скопирован индикаторный буфер в buffer_MA
2019.03.23 20:59:05.704 my_HMA5_123 (USDCHF,M30)        Скопирован индикаторный буфер в buffer_MA2
2019.03.23 20:59:05.704 my_HMA5_123 (USDCHF,M30)        Скопирован индикаторный буфер в buffer_MA3

What would that mean?

Reason: