Same Indicator Handle value for different indicators on same chart. Bug? - page 3

 
Ryan L Johnson #:

Have you tried putting your calls to indicator handles inside OnCalculate(), conditionally, as suggested by Airton Gomes Finoti in the other thread that you found?

"My work-around way is that EA load indicator A and indicator B in OnInit() process. Indicator A searches the handle of indicator B in the event of OnCalcute() for the first time. it shouldn't be done in the event of OnInit(). This work-around works well" (https://www.mql5.com/en/forum/98373#comment_18353294).

Yes, i tried, no luck.
 
Alain Verleyen #:
No. Using iCustom alone (Not using ChartIndicatorAdd) doesn't lead to duplicate indicators on a live chart. 

I can confirm that i have double logs changing EA code to this: 

int OnInit()
  {
      int B = iCustom(_Symbol,_Period, "B");
      //ChartIndicatorAdd(0,0,B);
  
      int A = iCustom(_Symbol,_Period, "A");
      //ChartIndicatorAdd(0,0,A);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }
  
void OnTick()
  {
  }


I get this log when i attach EA to chart:

2026.02.28 12:34:17.392 B (USTEC,M5)    B [128734579]-> 0/291815
2026.02.28 12:34:17.392 B (USTEC,M5)    B [128732873]-> 0/291815

Also refreshing che chart (right click-> refresh), i have same double logs.

 
antony23 #:

I can confirm that i have double logs changing EA code to this: 


I get this log when i attach EA to chart:

Also refreshing che chart (right click-> refresh), i have same double logs.

Your way to detect double instances is not correct.

OnCalculate() can be called twice in a row with prev_calculated = 0.

2026.02.28 08:35:01.574    B (EURAUD,M1)    OnInit: ChartID 134160922411012695. RND=13803
2026.02.28 08:35:01.574    B (EURAUD,M1)    13803: B [731]-> 0/100000

--> Refresh
2026.02.28 08:35:12.502    B (EURAUD,M1)    13803: B [10928101]-> 0/100000
2026.02.28 08:35:12.627    B (EURAUD,M1)    13803: B [11053560]-> 0/100000
2026.02.28 08:35:22.038    B (EURAUD,M1)    OnDeinit: ChartID 134160922411012695. RND=13803

 
Alain Verleyen #:

Your way to detect double instances is not correct.

OnCalculate() can be called twice in a row with prev_calculated = 0.

2026.02.28 08:35:01.574    B (EURAUD,M1)    OnInit: ChartID 134160922411012695. RND=13803
2026.02.28 08:35:01.574    B (EURAUD,M1)    13803: B [731]-> 0/100000

--> Refresh
2026.02.28 08:35:12.502    B (EURAUD,M1)    13803: B [10928101]-> 0/100000
2026.02.28 08:35:12.627    B (EURAUD,M1)    13803: B [11053560]-> 0/100000
2026.02.28 08:35:22.038    B (EURAUD,M1)    OnDeinit: ChartID 134160922411012695. RND=13803

I did test this by random initialization of a static variable - it's different in the logs, so these are 2 instances, not 2 events in the same instance.
 
Stanislav Korotky #:
I did test this by random initialization of a static variable - it's different in the logs, so these are 2 instances, not 2 events in the same instance.

There is a random variable in my code and log, it's from the same instance.

I can't reproduce what you said.

 
Alain Verleyen #:

Your way to detect double instances is not correct.

OnCalculate() can be called twice in a row with prev_calculated = 0.

2026.02.28 08:35:01.574    B (EURAUD,M1)    OnInit: ChartID 134160922411012695. RND=13803
2026.02.28 08:35:01.574    B (EURAUD,M1)    13803: B [731]-> 0/100000

--> Refresh
2026.02.28 08:35:12.502    B (EURAUD,M1)    13803: B [10928101]-> 0/100000
2026.02.28 08:35:12.627    B (EURAUD,M1)    13803: B [11053560]-> 0/100000
2026.02.28 08:35:22.038    B (EURAUD,M1)    OnDeinit: ChartID 134160922411012695. RND=13803

I made these other tests, in Terminal Chart (NO tester).

EA

int OnInit()
  {

      int B = iCustom(_Symbol,_Period, "B");
      //ChartIndicatorAdd(0,0,B);
  
      int A = iCustom(_Symbol,_Period, "A");
      //ChartIndicatorAdd(0,0,A);

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
  }
  
void OnTick()
  {
  }

A

#property indicator_chart_window

int OnInit()
  {
      int B = iCustom(_Symbol,_Period, "B");

   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int32_t rates_total,
                const int32_t 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 int32_t &spread[])
  {
   return(rates_total);
  }

B

#property indicator_chart_window

int OnInit()
  {
  
   MathSrand(GetTickCount());
  
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int32_t rates_total,
                const int32_t 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 int32_t &spread[])
  {
   
      if(prev_calculated<rates_total)
         Print("B ["+GetMicrosecondCount()+"]-> "+prev_calculated+"/"+rates_total+" -> RND: "+MathRand());
  
   return(rates_total);
  }


I got really weird behaviour.


Test 1 - Random variable different, it seems to be two instances here.

11:20:45.947    B (USTEC,M1)    B [757]-> 0/1000681 -> RND: 31038
11:20:45.967    B (USTEC,M1)    B [758]-> 0/1000681 -> RND: 31091
11:20:57.939    B (USTEC,M1)    B [11972113]-> 1000681/1000682 -> RND: 26275
11:20:57.939    B (USTEC,M1)    B [11992491]-> 1000681/1000682 -> RND: 18140
11:21:57.946    B (USTEC,M1)    B [71979760]-> 1000682/1000683 -> RND: 22335
11:21:57.946    B (USTEC,M1)    B [72000099]-> 1000682/1000683 -> RND: 31420

Test 2 - Random variable is the same, but double logs....

11:23:25.831    B (DE40,M1)     B [1010]-> 0/50394 -> RND: 28887
11:23:25.832    B (DE40,M1)     B [820]-> 0/50394 -> RND: 28887
11:23:59.648    B (DE40,M1)     B [33818205]-> 50394/50395 -> RND: 8290
11:23:59.648    B (DE40,M1)     B [33817143]-> 50394/50395 -> RND: 8290
11:24:58.142    B (DE40,M1)     B [92311584]-> 50395/50396 -> RND: 30635
11:24:58.142    B (DE40,M1)     B [92310522]-> 50395/50396 -> RND: 30635
11:26:10.228    B (EURGBP,M1)   B [734]-> 0/107201 -> RND: 8660
11:26:10.229    B (EURGBP,M1)   B [720]-> 0/107201 -> RND: 8660
11:26:57.931    B (EURGBP,M1)   B [47703865]-> 107201/107202 -> RND: 22720
11:26:57.931    B (EURGBP,M1)   B [47703101]-> 107201/107202 -> RND: 22720
11:27:57.924    B (EURGBP,M1)   B [107696811]-> 107202/107203 -> RND: 20854
11:27:57.924    B (EURGBP,M1)   B [107696045]-> 107202/107203 -> RND: 20854
 
antony23 #:

I made these other tests, in Terminal Chart (NO tester).

EA

A

B


I got really weird behaviour.


Test 1 - Random variable different, it seems to be two instances here.

Test 2 - Random variable is the same, but double logs....

Try this please as I can't reproduce that behaviour in any way.

The random number should be allocated globally, only once. I used Xoshiro256 library to be sure to get different random number in case there are several instances.

2026.03.02 08:39:06.690    EA505693 (EURUSD,M1)    EA: B launched through iCustom, handle 10
2026.03.02 08:39:06.705    A (EURUSD,M1)    A: B launched through iCustom, handle 10
2026.03.02 08:39:06.706    B (EURUSD,M1)    OnInit: ChartID 134160922411012695. RND=1234155352
2026.03.02 08:39:06.706    B (EURUSD,M1)    1234155352: B [696]-> 0/100003
2026.03.02 08:40:01.315    B (EURUSD,M1)    1234155352: B [54609529]-> 100003/100004
2026.03.02 08:41:01.857    B (EURUSD,M1)    1234155352: B [115151607]-> 100004/100005
2026.03.02 08:42:01.474    B (EURUSD,M1)    1234155352: B [174768490]-> 100005/100006
2026.03.02 08:43:01.310    B (EURUSD,M1)    1234155352: B [234604503]-> 100006/100007

2026.03.02 08:48:57.134    EA505693 (USTEC,M1)    EA: B launched through iCustom, handle 10
2026.03.02 08:48:57.140    A (USTEC,M1)    A: B launched through iCustom, handle 10
2026.03.02 08:48:57.141    B (USTEC,M1)    OnInit: ChartID 134169322501958094. RND=192256192
2026.03.02 08:48:57.141    B (USTEC,M1)    192256192: B [754]-> 0/39848
2026.03.02 08:49:01.332    B (USTEC,M1)    192256192: B [4191697]-> 39848/39849
2026.03.02 08:50:01.314    B (USTEC,M1)    192256192: B [64173855]-> 39849/39850
2026.03.02 08:50:03.027    B (USTEC,M1)    192256192: B [65887322]-> 0/39850
2026.03.02 08:51:01.391    B (USTEC,M1)    192256192: B [124251143]-> 39850/39851

Please also provide your Journal log showing the config used.
Files:
B.mq5  3 kb
Xoshiro256.mqh  44 kb
 

Using your B indicator code:

Experts log:

2026.03.02 17:26:52.620 B (USTEC,M1)    OnInit: ChartID 134060885355397815. RND=1542698133
2026.03.02 17:26:52.620 B (USTEC,M1)    1542698133: B [1058]-> 0/1001047
2026.03.02 17:26:52.651 B (USTEC,M1)    OnInit: ChartID 134060885355397815. RND=295955088
2026.03.02 17:26:52.651 B (USTEC,M1)    295955088: B [856]-> 0/1001047
2026.03.02 17:27:00.098 B (USTEC,M1)    295955088: B [7446933]-> 1001047/1001048
2026.03.02 17:27:00.098 B (USTEC,M1)    1542698133: B [7478263]-> 1001047/1001048
2026.03.02 17:28:00.080 B (USTEC,M1)    295955088: B [67429691]-> 1001048/1001049
2026.03.02 17:28:00.080 B (USTEC,M1)    1542698133: B [67460999]-> 1001048/1001049

Journal log:

2026.03.02 17:25:45.998 Terminal        MetaTrader x64 build 5660 started for MetaQuotes Software Corp.
2026.03.02 17:25:45.998 Terminal        Windows 10 build 19045, 8 x Intel Core i7-8565U  @ 1.80GHz, AVX2, 5 / 15 Gb memory, 116 / 475 Gb disk, UAC, GMT+1
2026.03.02 17:25:45.998 Terminal        C:\Users\xxxxxxxxx\AppData\Roaming\MetaQuotes\Terminal\xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
.....
.....
.....
2026.03.02 17:26:51.286 Experts expert EA (USTEC,M1) loaded successfully
2026.03.02 17:26:52.610 Indicators      custom indicator B (USTEC,M1) loaded succesfully
2026.03.02 17:26:52.618 Indicators      custom indicator A (USTEC,M1) loaded succesfully
2026.03.02 17:26:52.651 Indicators      custom indicator B (USTEC,M1) loaded succesfully


Can you send me also EA and A code you use?

 
antony23 #:

Using your B indicator code:

Experts log:

Journal log:

Strange. I am on Windows 11, only noticeable difference, I have a Window 10 too, I will try.

Can you send me also EA and A code you use?

Yes but there is no major difference.
Files:
EA505693.mq5  2 kb
A.mq5  3 kb
 
antony23 #:

Using your B indicator code:

Experts log:

Journal log:


Can you send me also EA and A code you use?

Ok I found the problem and reproduced it.

I moved the indicators in MQL5/Indicators.

2026.03.02 12:03:40.247    EA505693 (USTEC,M1)    EA: B launched through iCustom, handle 10
2026.03.02 12:03:40.252    B (USTEC,M1)    OnInit: ChartID 134169322501958094. RND=1805330128
2026.03.02 12:03:40.252    B (USTEC,M1)    1805330128: B [748]-> 0/40043
2026.03.02 12:03:40.262    A (USTEC,M1)    A: B launched through iCustom, handle 10
2026.03.02 12:03:40.263    B (USTEC,M1)    OnInit: ChartID 134169322501958094. RND=821904471
2026.03.02 12:03:40.263    B (USTEC,M1)    821904471: B [763]-> 0/40043
2026.03.02 12:04:01.537    B (USTEC,M1)    821904471: B [21274811]-> 40043/40044
2026.03.02 12:04:01.537    B (USTEC,M1)    1805330128: B [21286213]-> 40043/40044
2026.03.02 12:05:01.411    B (USTEC,M1)    821904471: B [81149159]-> 40044/40045
2026.03.02 12:05:01.411    B (USTEC,M1)    1805330128: B [81160561]-> 40044/40045

Previously I had not the problem because the indicators were in the SAME FOLDER as the EA.

Thanks for your help.