Determining the instance number of a Custom Indicator that is attached multiple times to a chart

 

I have written a Custom Indicator that places a number of graphical objects on a chart and allows the geometry to be altered by dragging various anchor points using the OnChartEvent() entry point.  Think of a MQL4 implementation of the Cycle Lines or Andrews Pitchfork tools and you will get the idea, though the geometry in this case is quite different.

I would like to be able to manage multiple instances of the indicator placed on a single main chart so I think I need a way of the code determining which instance it is - some sort of ordinal number would suffice.

I'm probably missing something obvious but I've not found this done in the code of any of the custom indicators I have looked at and I've not spotted a suitable function in the API.

There are probably other approaches like kludging some sort of election mechanism together with global variables.

It is not necessary for an instance to retain its association with a set of annotations through things like compiles, symbol changes and timeframe changes as long as each instance can associate itself with exactly one set of annotations.

Any suggestions greatly appreciated.

 
LawrenceIpsum:

I have written a Custom Indicator that places a number of graphical objects on a chart and allows the geometry to be altered by dragging various anchor points using the OnChartEvent() entry point.  Think of a MQL4 implementation of the Cycle Lines or Andrews Pitchfork tools and you will get the idea, though the geometry in this case is quite different.

I would like to be able to manage multiple instances of the indicator placed on a single main chart so I think I need a way of the code determining which instance it is - some sort of ordinal number would suffice.

I'm probably missing something obvious but I've not found this done in the code of any of the custom indicators I have looked at and I've not spotted a suitable function in the API.

There are probably other approaches like kludging some sort of election mechanism together with global variables.

It is not necessary for an instance to retain its association with a set of annotations through things like compiles, symbol changes and timeframe changes as long as each instance can associate itself with exactly one set of annotations.

Any suggestions greatly appreciated.

pass the serial in one of indicator parameters and make the indicator create a short name ( INDICATOR_SHORTNAME) which will be read by the ea (ChartIndicatorName()), or the ea just read the parameters based on indicator handle (IndicatorParametrers() ). 
 
  1. Perhaps
    uint instance;
    int OnInit(){
       instance=GetMicrosecondCount();
    :

  2. It makes no sense to have two identical indicators running on the same chart. Use a hash of the input parameters.
 
whroeder1:
  1. Perhaps

  2. It makes no sense to have two identical indicators running on the same chart. Use a hash of the input parameters.

Thanks @whroeder1 !

1. There are a number of ways of obtaining a globally unique instance ID that allows me to name the graphical objects uniquely and that part is not problematic and works fine - I have been working with code that is quite similar to your suggestion.

The difficulty is when the code is recompiled or the time period or symbol are changed or the chart is closed and reopened we are back at OnInit() and I don't see how the code can determine its instance ID at that point.

2. I think maybe it does.  If you imagine the indicator places an Andrews Pitchfork on the chart (its a different geometry but this suffices for a discussion as it is familiar) and allows it to be manually dragged to align to features in the data series, surely it is reasonable to want to be able to place more than one Pitchfork on the series?  The indicator is only concerned with placing graphical object annotations on the screen and so has:

#property indicator_buffers 0

Ideally I'd like the indicator to "do the right thing" if it is dragged onto a chart multiple times and the default parameters are accepted.  Such instances would have identical parameters but would control different sets of graphical objects (Pitchforks).

That said, forcing the user to provide the instance ordinal as an input parameter would work I think - it would be relatively simple to prevent the indicator running if an attempt is made to run with a duplicate instance ordinal and I believe the input parameters will be stable across OnDeInit()/OnInit() so that certainly seems like a clean approach that I can test.

The problem is I can't manage the input parameters programatically , as I understand it, and so the user has to manually adjust the instance ordinal for each instance after the first.  It would be nicer to manage that automatically in the code.

I guess that's an acceptable compromise though if no other approaches emerge.

Thank you for your insights.

 
Amir Yacoby:
pass the serial in one of indicator parameters and make the indicator create a short name ( INDICATOR_SHORTNAME) which will be read by the ea (ChartIndicatorName()), or the ea just read the parameters based on indicator handle (IndicatorParametrers() ). 

Thanks @Amir Yacoby,

I don't have an EA involved in this, just a CI that can be dragged onto a chart window multiple times.

I did wonder about setting the INDICATOR_SHORTNAME with instance ID values but rejected the idea for some reason.  Looking at it again after reading your reply, I think I can see now how to get the behavior I need - I'll give it a try.

Many thanks for your reply.

 
LawrenceIpsum: Ideally I'd like the indicator to "do the right thing" if it is dragged onto a chart multiple times and the default parameters are accepted.

Makes no sense. The "right thing" is same parameters, same results.