View Full Version : Indicator Initialize - Dispose
10-12-2009, 07:31 AM
I would like to understand better the sequence of Initialize and Dispose of an Indicator in NT7.
I have one chart window open and on this chart MyIndicator is loaded.
So it shows up as MyIndicator(ES 12-09,1Min,..)
Now I put some Print statements in Dispose and Initialize
Then I change the symbol in the chart window from ES 12-09 to FDAX 12-09.
From the Print statements I get.
MyIndicator(FDAX(12-09).Dispose ( ????? My expectation: MyIndicator(ES 12-09)
Now I am puzzled:
I would expect the first .Dispose to be associated with the (ES 12-09) Instrument, which is the one beeing disposed.
Could you please enlight what is happening?
P.S. Please enable debugging for the scripts, it is almost impossible to debug only via Print statements.
10-12-2009, 08:04 AM
This is not documented but we no longer advise using Dispose(). Please use the following to dispose of your resources.
protected override void OnTermination()
// Dispose logic here
10-12-2009, 08:13 AM
Thank you, I will try...
just to know: Same for Strategies, OnTermination() instead of Dispose()
what about calling the base class implementation, necessary? i guess no longer needed otherwise you could well stick with Dispose
10-12-2009, 08:18 AM
Right it's not needed. OnTermination only would hold your "custom cleanup" code.
10-12-2009, 09:33 AM
so OnTermination is called before Dispose, right?
Probably you want to clean up as much as possible outside the GC Dispose?
My problem before was Instrument.FullName related.
Same as in Initialize, Instrument.MasterInstrument.Name now.
10-12-2009, 09:35 AM
What exact issue do you see?
10-12-2009, 10:07 AM
please read again my first post , the behaviour with OnTermination is the same as with Dispose.
My point is:
On a chart window , when I change the instrument from ES 12-09 to FDAX 12-09 I expect something like this:
Instead all calls are with the new instrument, hence I see
This does not make sense to me, especially I need the correct instrument informnation because I save and load instrument relative data in Initialize and OnTerminate.
How can I get the instrument.FullName in Initialize()?
GetBarsInProgress is not allowed, so this means that there are changes due to the multinstrument charts,
any doc around or should I just dissassmble ...?
I get only MasterInstrument.Name and the expiry, so I can by hand construct me the Fullname, but does all this make sense?
What am I missing?
Sorry if I am less clear than usual , but I am sick and therfore a bit confused.
10-12-2009, 11:03 AM
on 1) Not sure why you keep referring to particular instruments. This is irrelevant. The actual indicator instance is relevant and nothing else. If you wanted to understand the sequence of events, then I suggest marking your indicator instances so that they would be unique.
on 2) Unfortunately there is no way to retrieve the instrument name on Initialize() since the actual instrument/series is assigned AFTER Initialize(). This was the case NT6.5 and nothing has changed since.
Hope you'll feel better soon
10-12-2009, 04:05 PM
finally used the Visual Studio Debugger on NT7, and what I need can be done inside the
string ovverride ToString()
01-23-2010, 09:06 AM
i dont understand everithing connected to this.
- This is what we want users to overload to dispose of their resources
- Do not overload Dispose() any longer
- Reason that dispose could be much later that you might expect
- Any reference sample updates required
// Overload this method to handle the termination of an indicator. Use this
// method to dispose of any resources vs overloading the Dispose() method.
1) question: the code is about indicator. Is it right for the strategy as well?
2) question: i did not find any exact sample, how to use it corretly. Can you help me? Now, this in the strategy, how would that be in OnTermination() method?
public override void Dispose()
// Disposes resources
if (MyClass1 = null)
MyClass1.PropertyChanged -= MyClass1 _PropertyChanged;
if (MyClass2 != null)
Thanks in advance, Tamas
01-23-2010, 01:43 PM
Same as for a strategy.
public override void Dispose()
public override void OnTermination()
01-24-2010, 03:38 AM
Ohhh :) thank you Ray
01-25-2010, 06:51 PM
there is a problem with NT creating instances of custom classes and holding them for no apparent reason.
if I compile an indicator but dont add to a chart NT already has an instance.
I have an indicator wich writes to a file. I create a stream in Initialize an close it on terminate.
this doesnt work because once I compiled it NT has initialised the code and it will not release until the NT shut. a new instance on the chart cant use the same file name as it is locked.
I had to do a dirty trick with random file names to avoid the problem.
why do you do it? what is the purpose of idle indicator?
seems as resources leak to me... I think same was in NT 6.5
01-25-2010, 09:48 PM
P.S. a year ago it was promised that Ninja would resolve problem with releasing custom dlls as well..
same problem as with indicators. If you reference a custom dll and you are making changes, compile it - you cant copy it other as Ninja is always holding dll until completely shut (no mater if it used anywhere or not)....
In NT7 beta is still not addressing this and I think will not.
01-26-2010, 06:05 AM
>> there is a problem with NT creating instances of custom classes and holding them for no apparent reason.
Incorrect. Those instances are created so you can properly manage them on the UI before they e.g. get placed on chart.
01-26-2010, 06:13 AM
I dont know what does it mean for a developer. What I see is that if I need to initialise some resource (I have file stream and I have an external window) - once I added one indicator to a chart - I have several of copies being instantiated but only one is used...
If you need some instanses for inner job you had to have 2 separate sets of methods to init and dispose custom classes.. One you use for what you use and one is for the developers... And the use case should be clearly explained.
I hope that will happen in NT7.
01-26-2010, 06:16 AM
Unfortunately nothing will change with NT7 in that regard as all supported features behave well in Initialize().
01-26-2010, 06:35 AM
Yeah I got it.
01-26-2010, 10:12 AM
I use Initialize and Dispose a lot in my code.
In my indicators when I subsitute Dispose with OnTermination my code breaks because OnTermination is never called. Is this a bug or intended?
Dispose is called as usual.
Therefore I will continue to use Dispose.
are there any plans to delete Dispose ?
01-26-2010, 10:19 AM
>> In my indicators when I subsitute Dispose with OnTermination my code breaks because OnTermination is never called
Could you please provide a simple a possible scenario where you feel OnTermination should be called but wasn't. Thanks
01-26-2010, 10:41 AM
just click F5 (reload ninjascript) should bring you there
01-26-2010, 11:05 AM
I copied the SMA indicator and added this simple method
protected override void OnTermination()
It's triggered every time I hit F5.
01-26-2010, 11:41 AM
you are correct, OnTermination is called in SMA.
But if you put a function public override void Dispose() in SMA,
then OnTermination is no longer called.
I overlooked this before because the changes will only be visible at the SECOND F5 click (at the first click the old SMA code Dispose/OnTerminate is called, only at the second f5 click the current code base is executed)
01-26-2010, 11:43 AM
Please use OnTermination as advised with NT7 to avoid any further confusion.
01-26-2010, 11:53 AM
I have code like
... my stuff
and base can be a strategy, indicator or marketanalyzer column.
All works perfectly, and in this way I can control my resources properly.
Why don' t you publish the STRUCTURE of your Dispose - OnTermination sequence.
OnTermination(); // this calls cleanup in custom indicator
would give exactly the same behaviour.
Only difference is that if a user forgets to call base.Dispose() he could cause exceptions in NT.
Is this the idea behind OnTermination?
01-26-2010, 11:57 AM
We do not provide any support for Dispose() with NT7. Nor should you ever call OnTermination() explicitely. Please clean up your ressources in OnTermination(). Thanks
01-26-2010, 12:14 PM
Thank you for your reply.
Not supported doesn' t necessarily mean bad code. Sometimes on the contrary, functionality becomes better...
Actually I am only interested to have Dispose() also available in the future. I will use OnTermination where I decide it is the better choice.
From your samples .cs files I see a massive use of Dispose(), so I feel on the safe side...
01-26-2010, 12:47 PM
Reason reference samples use Dispose() is because they are from NT6.5 and OnTermination() did not exist back then.