Losing "Previous Indicator's Data" handle on Timeframe change & Subwindow scaling issues

 

Hello everyone,

I am working with custom indicators and have run into a very frustrating issue regarding indicator handles and subwindows. I hope someone here can shed some light on this.

Currently, I am calling Indicator_2  using iCustom , and I pass the handle of Indicator_1 as the data source (the last parameter). It works perfectly at first. When I open the indicator properties on the chart, the "Apply to" field correctly shows "Previous Indicator's Data".

However, I have three questions regarding this setup:

1. Passing "First Indicator's Data" via code: Is there a way to call iCustom and explicitly set it to use "First Indicator's Data" instead of "Previous Indicator's Data" without manually passing the Indicator_1 handle in the parameter?

2. The Timeframe Reset Issue (My biggest problem): When I change the timeframe on the chart, the indicator completely loses its data link. The "Apply to" parameter of Indicator_2 automatically resets to "Close" instead of maintaining "Previous Indicator's Data".

Is there any trick, workaround, or proper method to prevent MT5 from breaking this handle link and reverting to "Close" upon timeframe changes?

3. Subwindow Scaling: By the way, is there a way to force Indicator_2 to strictly follow the scale (min/max values) of Indicator_1 ? I want to attach multiple indicators (more than two) into the exact same subwindow.

Any guidance, code snippets, or advice would be highly appreciated!

Thank you very much in advance!

void ToggleMA(string Indicator_2="CustomMovingAverage", 
              string Indicator_1="CustomRSI",
              int ma1=20, int ma2=50, int ma3=100, int ma4=200)
{
   int total_windows = (int)ChartGetInteger(0, CHART_WINDOWS_TOTAL);
   if(total_windows <= 1) return;
   
   for(int w = 1; w < total_windows; w++)
   {
      int total_inds = ChartIndicatorsTotal(0, w);
      if(total_inds > 0)
      {
         int root_handle = INVALID_HANDLE;
         string found_name = "";

         for(int i = 0; i < total_inds; i++)
         {
            string ind_name = ChartIndicatorName(0, w, i);
            
            if(StringFind(ind_name, Indicator_1) >= 0)
            {
               found_name = ind_name;
               root_handle = ChartIndicatorGet(0, w, found_name);
               break;
            }
         }

         if(root_handle != INVALID_HANDLE)
         {
            int ma_handle = iCustom(NULL, _Period, Indicator_2, 
                                    ma1, ma2, ma3, ma4,
                                    root_handle);

            if(ma_handle != INVALID_HANDLE)
            {
               ChartIndicatorAdd(0, w, ma_handle);
            }
         }
      }
   }
}
Step on New Rails: Custom Indicators in MQL5
Step on New Rails: Custom Indicators in MQL5
  • 2009.11.23
  • www.mql5.com
I will not list all of the new possibilities and features of the new terminal and language. They are numerous, and some novelties are worth the discussion in a separate article. Also there is no code here, written with object-oriented programming, it is a too serous topic to be simply mentioned in a context as additional advantages for developers. In this article we will consider the indicators, their structure, drawing, types and their programming details, as compared to MQL4. I hope that this article will be useful both for beginners and experienced developers, maybe some of them will find something new.
 

Have a look at the code of the primary indicator file and the moving average subsidiary indicator file in the link below. When I was coding those indicators, I encountered a conflict between the primary separate window indicator and the main chart window moving average indicator. My solution was to change the moving average indicator #property to indicator_separate_window. This setup does not have the bugs that you mentioned.

Code Base

Brooky Trend Strength for MT5

Ryan L Johnson, 2025.04.29 19:52

This indicator calls 3 other subwindow indicators. All files go in your Indicators folder.

 
Ryan L Johnson #:

Have a look at the code of the primary indicator file and the moving average subsidiary indicator file in the link below. When I was coding those indicators, I encountered a conflict between the primary separate window indicator and a main chart window moving average indicator. My solution was to change the moving average indicator #property to indicator_separate_window. This setup does not have the bugs that you mentioned.


​Both indicator_separate_window and indicator_chart_window suffer from the exact same issue, and in my opinion, it makes no sense when calling them via iCustom.