Bug: ChartWindowFind always returns 0

 

If called without any arguments, ChartWindowFind will always return 0 even if the indicator is dragged to a subwindow.

I compiled the following code and dragged the indicator into the subwindow of the volume indicator:

#property version   "1.00"
#property indicator_chart_window

int OnInit() {
  IndicatorSetString(INDICATOR_SHORTNAME, "Some Indicator");
  
   // Prints 1
  Print(ChartWindowFind(ChartID(), "Some Indicator"));
  
   // Prints 0
  Print(ChartWindowFind());
  
  return INIT_SUCCEEDED;
}

int OnCalculate(
  int const TOTAL,
  int const CALCULATED,
  int const BEGIN,
  double const & VALUE[]
) {
  return TOTAL;
}

Files:
 
Alain Verleyen:

That's not a bug.

The ChartWindowFind() documentation states :

ChartWindowFind

With this statement, the indicator will ALWAYS work in the main window.

But there are situations where the indicator can work both in the main window or another indicator's subwindow using the signature:

int  OnCalculate(
   const int        rates_total,       // price[] array size
   const int        prev_calculated,   // number of handled bars at the previous call
   const int        begin,             // index number in the price[] array meaningful data starts from
   const double&    price[]            // array of values for calculation
   );

The property indicator_chart_window is only there because the compiler requires it and even add it if you remove.


For these indicators, the behavior of ChartWindowFind is incorrect.

 
Alain Verleyen:

What are you talking about ? 1 indicator is only able to draw ("work") in one window, always, whatever OnCalculate() method you are using.

A warning isn't something to ignore.

There is no problem with ChartWindowFind().

Using the signature:

int  OnCalculate(
   const int        rates_total,       // price[] array size
   const int        prev_calculated,   // number of handled bars at the previous call
   const int        begin,             // index number in the price[] array meaningful data starts from
   const double&    price[]            // array of values for calculation
   );

Allows this:


And it means that the indicator can calculate based on another indicator's value. In this situation, ChartWindowFind should return

the target subwindow that it is attached to since it is what is described in the documentation:


 

I don't know why you are being this aggressive, the docs clearly states ***SUB***WINDOW, you are the one not understanding here. It is possible

to work around this bug by giving the indicator a unique name but it still a bug.

 
Alexandre Borela: I don't know why you are being this aggressive, the docs clearly states ***SUB***WINDOW, you are the one not understanding here. It is possible to work around this bug by giving the indicator a unique name but it still a bug.

I understand your frustration! I have previously also looked into this and personally I do also believe it be a bug. These are my own findings:

  • When using the property "#property indicator_chart_window", even when one is applying the indicator to another indicator's data on a sub-window, for example applying a Moving Average indicator to an RSI sub window, the long version of the function gives the correct value, but the short version always gives a result of "0" no matter what.
  • However, when using the property "#property indicator_separate_window", and applying the indicator to another indicator's data on a sub-window, then both the long version and the short version gives the correct value. However, in this mode we are unable to apply it to the main chart window anymore, nor apply it to another indicator on the main window.

So, in my own view, I also consider this a bug of the short version, given that when using the long version in the following form (work-around), one is able to get the correct value:

int subWindow = ChartWindowFind( ChartID(), MQLInfoString( MQL_PROGRAM_NAME ) ); // Returns the Correct Value

EDIT: Please note that the above works because "MQLInfoString( MQL_PROGRAM_NAME )" returns the Indicator's "Short Name" and not the actual program file name as defined in the documentation (also a possible bug in my view, but that is another story).

EDIT2: Very important! Please note that I find the above work-around unreliable when there are multiple instances of the same indicator on the chart. As it will report on the first indicator it finds and not the current working indicator requesting the data.

 
Fernando Carreiro:

I understand your frustration! I have previously also looked into this and personally I do also believe it be a bug. These are my own findings:

  • When using the property "#property indicator_chart_window", even when one is applying the indicator to another indicator's data on a sub-window, for example applying a Moving Average indicator to an RSI sub window, the long version of the function gives the correct value, but the short version always gives a result of "0" no matter what.
  • However, when using the property "#property indicator_separate_window", and applying the indicator to another indicator's data on a sub-window, then both the long version and the short version gives the correct value. However, in this mode we are unable to apply it to the main chart window anymore, nor apply it to another indicator on the main window.

So, in my own view, I also consider this a bug of the short version, given that when using the long version in the following form (work-around), one is able to get the correct value:

EDIT: Please note that the above works because "MQLInfoString( MQL_PROGRAM_NAME )" returns the Indicator's "Short Name" and not the actual program file name as defined in the documentation (also a possible bug in my view, but that is another story).

EDIT2: Very important! Please note that I find the above work-around unreliable when there are multiple instances of the same indicator on the chart. As it will report on the first indicator it finds and not the current working indicator requesting the data.

Exactly, I've been using that workaround and generating unique names for indicators based on the time they were added to prevent conflicts but still, not ideal.
 
Alexandre Borela #:
Exactly, I've been using that workaround and generating unique names for indicators based on the time they were added to prevent conflicts but still, not ideal.

The bug is still here to stay.

I use this workaround in OnInit() to prevent attaching my tool to any of the sub windows:

   IndicatorSetString(INDICATOR_SHORTNAME,SHORTNAME);
   int subWin=ChartWindowFind(0,SHORTNAME);
   if(subWin!=0)
   {
      ChartIndicatorDelete(0,subWin,SHORTNAME);
      return(INIT_FAILED);
   }

And you can't attach the same tool multiple times if it has the same short name, so I don't have to check that case.

ChartWindowFind() still returns 0 if you manually drag a tool/indicator to a sub window (instead of the sub window it was dragged to), so you just have to use the full version.

 
Alexandre Borela #:
Exactly, I've been using that workaround and generating unique names for indicators based on the time they were added to prevent conflicts but still, not ideal.

Try GetMyUniqueName() from here.

Init_Sync
Init_Sync
  • www.mql5.com
The library makes indicators' Init/Deinit synchronized
Reason: