Errors, bugs, questions - page 2930

 

Translating an indicator from mt4 to mt5

The problem is inDRAW_HISTOGRAM size

When I set "1" or "2" the size changes, but "3" or "4" = size is still "2".


Why everything works in MT4, but there is such a problem with MT5?

#property indicator_separate_window
#property indicator_plots   4
#property indicator_buffers 4
#property indicator_color1 clrDodgerBlue
#property indicator_color2 clrDodgerBlue
#property indicator_color3 clrLimeGreen
#property indicator_color4 clrFireBrick
#property indicator_width1 3
#property indicator_width2 3
#property indicator_width3 3
#property indicator_width4 3

Result in mt4


Result in mt5, shows only "2" thickness, although I have "3" in settings



Also, setting the property in OnInit() does not help

PlotIndexSetInteger(0,PLOT_LINE_WIDTH, 3);

 

Critical error during debugging:

struct sA
  {
   int               i;
   string            s;
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class cA
  {
public:
   sA                my_array[];
  };
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
   cA *ca;
   ca=new cA();
   ArrayResize(ca.my_array,1);
   ZeroMemory(ca.my_array);
   ArrayPrint(ca.my_array);
  }

Result:

2020.12.27 17:04:26.966 1 (EURUSD,M1)   Access violation at 0x000001FE5AF10199 read to 0xFFFFFFFFFFFFFFFF in 'D:\Alpari MT5\MQL5\Scripts\Test\1.ex5'
2020.12.27 17:04:26.967 1 (EURUSD,M1)      crash -->  000001FE5AF10199 8B4318            mov        eax, [rbx+0x18]
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF1019C 4089442420        mov        [rsp+0x20], eax
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF101A1 41B910000000      mov        r9d, 0x10
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF101A7 49B8B807F15AFE01  mov        r8, 0x1fe5af107b8
2020.12.27 17:04:26.967 1 (EURUSD,M1)                                  0000
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF101B1 488D17            lea        rdx, [rdi]
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF101B4 48B998D45F5FFE01  mov        rcx, 0x1fe5f5fd498
2020.12.27 17:04:26.967 1 (EURUSD,M1)                                  0000
2020.12.27 17:04:26.967 1 (EURUSD,M1)                 000001FE5AF101BE 49FF96A8120000    call       qword near [r14+0x12a8]  ; #11378 (terminal64.exe)
2020.12.27 17:04:26.967 1 (EURUSD,M1)   
2020.12.27 17:04:26.967 1 (EURUSD,M1)   00: 0x000001FE5AF10199
2020.12.27 17:04:26.967 1 (EURUSD,M1)   01: 0x000001FE633F0010
2020.12.27 17:04:26.967 1 (EURUSD,M1)   
Latest beta version at the moment
Документация по MQL5: Программы MQL5 / Ошибки выполнения
Документация по MQL5: Программы MQL5 / Ошибки выполнения
  • www.mql5.com
Ошибки выполнения - Программы MQL5 - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Has anyone figured out a way to get an indicator handle to display a property in the main window or subwindow? I'm talking about arbitrary third-party indicators that are created using IndicatorCreate and then could be added to the chart usingChartIndicatorAdd at user's request.
 
Stanislav Korotky:
Has anyone figured out a way to get the indicator handle to display property in the main window or subwindow? I am talking about custom indicators which are created using IndicatorCreate and then they can be added to the chart using ChartIndicatorAdd.

You could probably shove the indicator into OBJ_CHART and see where it is located.

 
Stanislav Korotky:
Has anyone figured out a way to get a display property from an indicator handle in the main window or subwindow? I am talking about custom indicators which are created using IndicatorCreate and then they can be added to the chart by ChartIndicatorAdd at user's request.

did something similar.
Here's one I found. Can't remember if it works or not. Please check it out.

bool FindIndicatorByHandle(long handle, string &symbol, ENUM_TIMEFRAMES &timeframe, long &ChartId, int &sub_win, string &Name) {
   long chart_id =ChartFirst();
   while (chart_id!=-1) {
      int total_sub = (int)ChartGetInteger(chart_id,CHART_WINDOWS_TOTAL);
      int total_ind; 
      for(int i=0; i<total_sub; i++) {
         total_ind = ChartIndicatorsTotal(chart_id,i);
         for (int j=0; j<total_ind; j++) {
            string name = ChartIndicatorName(chart_id,i,j);
            if (ChartIndicatorGet(chart_id,i,name)== handle) {
               ChartId=chart_id;
               symbol= ChartSymbol(chart_id);
               timeframe=ChartPeriod(chart_id);
               sub_win=i;
               Name=name;
               return true;
            }
         }
      }
      chart_id=ChartNext(chart_id);
   }
   return false;
}
The code searches all open charts for an indicator by its handle. Returns symbol, timeframe, chart id, subwindow and indicator name.
 
fxsaber:

I guess you could put the indicator in OBJ_CHART and see where it is located.

I'll give it a try, for now I had to make the option to specify it by the user.

 
Nikolai Semko:

did something similar.
Here's one I found. Can't remember if it works or not. Please check it out.

Judging by the code, it searches all open charts for an indicator by its handle. Returns symbol, timeframe, chart id, subwindow and indicator name.

It does not fit your question: indicators created by IndicatorCreate are not placed in any window - they "sit" inside it, and the task is to place such an indicator by its handle in the window. Now, MQL5 allows you to add an arbitrary handle to the main window or subwindow using the ChartIndicatorAdd function, regardless of the "properties" of the indicator (no errors!), and the result looks very strange when the adding is not performed as intended.

 

There's a bigger problem with indicators.

Some of them implement the OnCalculate handler according to the simplified version with one input array, and you can select the price type for their calculation. How do you know from the third-party MQL5 program that the indicator expects this price type, which must be passed after all parameters during the creation of the indicator? It seems that there is no way. And if you don't, the indicator is drawn by #property indicator_applied_price that cannot be accessed from the outside. Here is a simple Expert Advisor that adds the indicator/AMA to the chart programmatically:

int OnInit()
{
  const int h = iCustom(NULL, 0, "Examples/AMA");
  if(h == INVALID_HANDLE)
  {
    Print("iCustom failed: ", _LastError);
    return INIT_FAILED;
  }

  if(!ChartIndicatorAdd(0, 0, h))
  {
    Print("ChartIndicatorAdd failed: ", _LastError);
    return INIT_FAILED;
  }
  ChartRedraw();
  
  return INIT_SUCCEEDED;
}

If after that we switch the chart timeframe, we will get 2 indicators AMA: one at close price (programmatically created) and one at open price (generated due to timeframe change and #property indicator_applied_price PRICE_OPEN; it is not clear why this property is not caught by iCustom as well).

 

To continue the theme of indicators, I am attaching the Expert Advisor that I used to find out and avoid the problem with MqlParam parameters.

The task of the Expert Advisor is to add the indicator "Examples/Price_Channel" to the chart, if it is not there yet, with the same parameters.

To do this, we create an instance using iCustom/IndicatorCreate, get an array of its parameters through IndicatorParameters, and then call in the loop IndicatorParameters for indicators that already exist in the chart: if there is no match of the array parameters, the indicator is added to the chart, if there is a match, the new handle is simply destroyed.

The problem is that equality of parameters doesn't always work as expected. In particular, there is such a failsafe test case. We place the Expert Advisor on any empty chart and click - it creates the first instance of the indicator. Then we switch to any other chart timeframe (the old indicator remains) and click again. Strangely enough, the second instance of the indicator is created.

From the analysis of the execution and the log it is clear that this is due to the wrong equalization of strings of the indicator name in parameters. The strings there are 260 characters long, but the terminal 0 is located much earlier. After it there is either some service information or rubbish. So string comparison "==" or StringCompare takes this "rubbish" into account and we get inequality of strings.

If we convert strings into a char array, the arrays get a full match. If you convert to a short array, you get rubbish. All in all, some non-consistent behavior not described in the documentation.

Window N: 1
   0, Price Channel(22), 12
    [type] [integer_value] [double_value]                                                                                                                                                                                                                                                         [string_value]
[0]     14               0          0.000 "Indicators\Examples/Price_Channel"                                                                                                                                                                                                                                   
[1]      7              22          0.000 null                                                                                                                                                                                                                                                                  
ind1: 'Indicators\Examples/Price_Channel
ind2: 'Indicators\Examples/Price_Channel
260 0 / 260 0
Char arrays 34 34
 73 110 100 105  99  97 116 111 114 115  92  69 120  97 109 112 108 101 115  47  80 114 105  99 101  95  67 104  97 110 110 101 108   0
 73 110 100 105  99  97 116 111 114 115  92  69 120  97 109 112 108 101 115  47  80 114 105  99 101  95  67 104  97 110 110 101 108   0
Short arrays 260 260
[  0]    73   110   100   105    99    97   116   111   114   115    92    69   120    97   109   112   108   101   115    47    80   114   105    99
[ 24]   101    95    67   104    97   110   110   101   108     0   103   110    97   108     0     0     0     0     0     0     0     0     0     0
[ 48]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[ 72]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[ 96]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[120]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[144]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[168]     0     0     0     0     0     0     0     0     0     0     0     0    13  3328 58074    86 13696    54     0     0 35120  3210     0     0
[192]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[216]     0     0     0     0 61841 38184 17648 16370     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[240]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[  0]    73   110   100   105    99    97   116   111   114   115    92    69   120    97   109   112   108   101   115    47    80   114   105    99
[ 24]   101    95    67   104    97   110   110   101   108     0   103   110    97   108     0    32    32    77    32    65    32    82    32    75
[ 48]    32    83     0    32    99   111   109   109    97    32   115   101   112    97   114    97   116   101   100    32   108   105   115   116
[ 72]    41     0    32   112   114   111   116   111   116   121   112   101   115    41     0     0   115     0     0     0     0     0     0     0
[ 96]   528     0     0     0     0    45     0     0     3     0     0     0   512     0     0     0  1160    45     0     0  7256 30655     0     0
[120]     0     0     0     0   512     0     0     0     0 65535  2272     0 29648    45     0     0     0     0     0     0     0     0     0     0
[144]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[168]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[192]     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
[216]     3     0     0     0 26341 16284     1     0  1308    16     0     0 49814 30635     0     0     0     0     0     0 32016    37     0     0
[240]     2     0     0     0 50336 16481     1     0     2     0     0     0     3     0     0     0 45520    37     0     0
1 0

I'm not sure what exactly the bug is, whether it's in the principle of string processing in MQL or something else.

If anyone knows the nuances, please give me a hint.

Files:
 
Stanislav Korotky:

If anyone is aware of the nuances, please give me a tip.

This is the kind of feature that, even if you knew it, you have to bring up the whole layer in your head all over again.

Here did the definition of self availability. It seemed to work when switching TFs.

Init_Sync
Init_Sync
  • www.mql5.com
Библиотека делает синхронизированными Init/Deinit индикаторов
Reason: