View Full Version : Indicator Initialize - Dispose
zweistein
10-12-2009, 07:31 AM
Hello,
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,..)
fine.
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)
MyIndicaor(FDAX(12-09).Initialize
MyIndicator(FDAX(12-09).Dispose
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.
Best regards
Andreas
NinjaTrader_Ray
10-12-2009, 08:04 AM
Hi,
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
}
zweistein
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
OnTermination(){
base.OnTermination().... needed?
}
Best regards
andreas
NinjaTrader_Dierk
10-12-2009, 08:18 AM
Right it's not needed. OnTermination only would hold your "custom cleanup" code.
zweistein
10-12-2009, 09:33 AM
OK,
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.
regards
NinjaTrader_Dierk
10-12-2009, 09:35 AM
What exact issue do you see?
zweistein
10-12-2009, 10:07 AM
Dierk,
please read again my first post , the behaviour with OnTermination is the same as with Dispose.
First issue:
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:
MyIndicator(ES 12-09).OnTerminate
MyIndicator(FDAX 12-09).Initilialize
Instead all calls are with the new instrument, hence I see
MyIndicator(FDAX 12-09).OnTerminate.
MyIndicator(FDAX) Initialize
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.
Second issue;
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?
Best regards,
Sorry if I am less clear than usual , but I am sick and therfore a bit confused.
NinjaTrader_Dierk
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
zweistein
10-12-2009, 04:05 PM
OK,
finally used the Visual Studio Debugger on NT7, and what I need can be done inside the
string ovverride ToString()
function.
Andreas
tamas
01-23-2010, 09:06 AM
Hello,
i dont understand everithing connected to this.
In help:
- 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
In code:
// Summary:
// 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()
{
try
{
// Disposes resources
if (MyClass1 = null)
{
MyClass1.PropertyChanged -= MyClass1 _PropertyChanged;
MyClass1.Dispose();
}
if (MyClass2 != null)
MyClass2.Dispose();
}
...
}
Thanks in advance, Tamas
NinjaTrader_Ray
01-23-2010, 01:43 PM
Same as for a strategy.
Just replace:
public override void Dispose()
with
public override void OnTermination()
tamas
01-24-2010, 03:38 AM
Just replace:
Ohhh :) thank you Ray
maxima
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
maxima
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.
NinjaTrader_Dierk
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.
maxima
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.
NinjaTrader_Dierk
01-26-2010, 06:16 AM
Unfortunately nothing will change with NT7 in that regard as all supported features behave well in Initialize().
maxima
01-26-2010, 06:35 AM
Yeah I got it.
zweistein
01-26-2010, 10:12 AM
Hello,
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 ?
NinjaTrader_Dierk
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
zweistein
01-26-2010, 10:41 AM
just click F5 (reload ninjascript) should bring you there
NinjaTrader_Dierk
01-26-2010, 11:05 AM
I copied the SMA indicator and added this simple method
protected override void OnTermination()
{
Print("OnTermination");
}
It's triggered every time I hit F5.
zweistein
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)
regards
NinjaTrader_Dierk
01-26-2010, 11:43 AM
Please use OnTermination as advised with NT7 to avoid any further confusion.
zweistein
01-26-2010, 11:53 AM
I have code like
Dispose(){
... my stuff
base.Dispose()
}
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.
Is it?
IndicatorBase.Dispose(){
OnTermination(); // this calls cleanup in custom indicator
DoNTInternalDispose();
}
Instread
MyIndicator.Dispose(){
DoMyDispose();
base.Dispose();
}
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?
NinjaTrader_Dierk
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
zweistein
01-26-2010, 12:14 PM
"No support..."
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...
NinjaTrader_Josh
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.