Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
New indicators added each time strategy is started
Collapse
X
-
New indicators added each time strategy is started
My strategy uses two indicators that I wrote. Each time I disable and then re-enable my strategy another instance of each indicator appears in the indicator dialog. When I try to remove them I get a dialog that says they cannot be removed because they are being used, even though the strategy has been removed.Tags: None
-
Where in the code are you adding the indicators?Originally posted by bernie_c View PostMy strategy uses two indicators that I wrote. Each time I disable and then re-enable my strategy another instance of each indicator appears in the indicator dialog. When I try to remove them I get a dialog that says they cannot be removed because they are being used, even though the strategy has been removed.
Comment
-
As suggested, I make a new strategy with the wizard and just added the lines of code that load my indicator and a strategyPlot. My code also does the load in Initialize(). Here is the edit:
This works fine. When I remove the strategy such that it no longer appears in the strategy tab the associated indicator and strategyPlot are also removed, meaning that they no longer appear in the indicator dialog.Code:private bcIi bc_ind; protected override void Initialize() { bc_ind = bcIi(); Add( bc_ind ); Add( StrategyPlot(0) ); }
This is exactly how I do it in my problematic strategy.
Just to be clear, in my original post I mentioned two indicators. I was actually referring to the one indicator (bcIi()) and the strategyPlot. They both persist after my strategy is removed.
Comment
-
The plot thickens...
My strategy has a tool strip button. I have this event handler in my strategy. When I comment this handler out the persistent indicators problem goes away.
What am I doing wrong?Code:protected override void OnTermination() { if( strip != null ) { if( butt_trade_ctrl != null ) strip.Items.Remove( butt_trade_ctrl ); } strip = null; butt_trade_ctrl = null; base.Dispose(); }
Comment
-
Try removing the call to Dispose().Originally posted by bernie_c View PostThe plot thickens...
My strategy has a tool strip button. I have this event handler in my strategy. When I comment this handler out the persistent indicators problem goes away.
What am I doing wrong?Code:protected override void OnTermination() { if( strip != null ) { if( butt_trade_ctrl != null ) strip.Items.Remove( butt_trade_ctrl ); } strip = null; butt_trade_ctrl = null; base.Dispose(); }
Comment
-
koganam, That appeared to do the trick. I no longer get the persistent indicators.
Backstory:
OnTermination() which is where the Dispose() call is is from the MAToggle example. MAToggle is an indicator. I liked the toolbar buttons, so I just copied all the button related logic. My implementation of the buttons is in a strategy. Do I really need to dispose in a strategy? Or do I need to do the equivalent in another way?
Comment
-
I will leave NinjaTrader Support to confirm it, but I believe the OnTermination() event handler was itself created partly because of these kinds of problems with Dispose(). I believe that OnTermination() cleans up properly first, then calls Dispose(), so calling Dispose() midsteam, so to speak, may cause the old problems that OnTermination() was designed to avoid.Originally posted by bernie_c View Postkoganam, That appeared to do the trick. I no longer get the persistent indicators.
Backstory:
OnTermination() which is where the Dispose() call is is from the MAToggle example. MAToggle is an indicator. I liked the toolbar buttons, so I just copied all the button related logic. My implementation of the buttons is in a strategy. Do I really need to dispose in a strategy? Or do I need to do the equivalent in another way?
Comment
-
Hello bernie_c,
Thank you for your response.
You can call Dispose() in OnTermination(). In general, any indicators called from the strategy are removed when the strategy is disabled, so this may complicate the process when you call Dispose() in OnTermination for the strategy.
ToolStrips are unsupported as well and there may be unexpected behavior when attempting to Dispose() any forms from a strategy when it is disabled.
Comment
-
I was not sure where I read the advice to not overload the Dispose() method in OnTermination(), which is why I left my statement up in the air. It turns out that that advice is in the NinjaTrader manual. Are you saying that your manual is providing the wrong warning?Originally posted by NinjaTrader_PatrickH View PostHello bernie_c,
Thank you for your response.
You can call Dispose() in OnTermination(). In general, any indicators called from the strategy are removed when the strategy is disabled, so this may complicate the process when you call Dispose() in OnTermination for the strategy.
ToolStrips are unsupported as well and there may be unexpected behavior when attempting to Dispose() any forms from a strategy when it is disabled.
ref: http://www.ninjatrader.com/support/h...rmination2.htm
Moreover, this particular case actually more or less proves the point, does it not?
Comment
-
Hello koganam.
Agreed, the Help Guide details it as the following:
However, the CustomPlotSample indicator in NinjaTrader does in fact use Dispose() for the textBrush and stringFormat:Note: Please do NOT overload the Dispose() method. Dispose() method could be triggered much later than expected resulting in resources not being released early enough.
Code:protected override void OnTermination() { textBrush.Dispose(); stringFormat.Dispose(); }
Comment
-
Not quite the same thing. What you write is calling the Dispose() method of the specific created object: what he wrote was calling the base.Dispose() method, which is the Dispose() method of the parent class, and hence, which even plainly called, is an override; albeit a trivial one.Originally posted by NinjaTrader_PatrickH View PostHello koganam.
Agreed, the Help Guide details it as the following:
However, the CustomPlotSample indicator in NinjaTrader does in fact use Dispose() for the textBrush and stringFormat:
Code:protected override void OnTermination() { textBrush.Dispose(); stringFormat.Dispose(); }
Comment
-
Maybe calling the Dispose() method of each specific created object is the ticket. I did some trial and error and came up with the following arrangement that appears to work.
No more persistent indicators and no more memory leakage as observed in the Processes tab in the Window Task Manager. I did have another arrangement that got rid of the persistent indicators, but there was sluggish erratic behavior. This is when I discovered the memory leakage. Scarry stuff.Code:protected override void OnTermination() { if( strip != null ) { if( butt_trade_ctrl != null ) strip.Items.Remove( butt_trade_ctrl ); butt_trade_ctrl.Dispose(); // strip.Dispose(); } }
Is strip.Items.Remove( butt_trade_ctrl ); really needed?
Patrick said: "ToolStrips are unsupported as well and there may be unexpected behavior when attempting to Dispose() any forms from a strategy when it is disabled."
Are any buttons (other than toolStrip buttons) supported? It seems like a natural thing to want to interact with a running strategy.
Comment
-
Yes. Disposing of created objects by calling their internally implemented method should be OK.Originally posted by bernie_c View PostMaybe calling the Dispose() method of each specific created object is the ticket. I did some trial and error and came up with the following arrangement that appears to work.
No more persistent indicators and no more memory leakage as observed in the Processes tab in the Window Task Manager. I did have another arrangement that got rid of the persistent indicators, but there was sluggish erratic behavior. This is when I discovered the memory leakage. Scarry stuff.Code:protected override void OnTermination() { if( strip != null ) { if( butt_trade_ctrl != null ) strip.Items.Remove( butt_trade_ctrl ); butt_trade_ctrl.Dispose(); // strip.Dispose(); } }
Is strip.Items.Remove( butt_trade_ctrl ); really needed?
Patrick said: "ToolStrips are unsupported as well and there may be unexpected behavior when attempting to Dispose() any forms from a strategy when it is disabled."
Are any buttons (other than toolStrip buttons) supported? It seems like a natural thing to want to interact with a running strategy.
Just be careful that they are objects that can get reinitialized. Remember that OnTermination() is also called when you refresh the chart (F5).
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
633 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
364 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
567 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
568 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment