Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

New indicators added each time strategy is started

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    Hello Bernie_C,

    Thank you for your post.

    Has the strategy been removed from the Strategies tab as well?

    Additionally, can you test a new strategy and just add an indicator in the Initialize() and have the same behavior?
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by bernie_c View Post
      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.
      Where in the code are you adding the indicators?

      Comment


        #4
        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:

        Code:
        private bcIi 	bc_ind;
        
        protected override void Initialize()
                {	
        			bc_ind = bcIi();
        			
        			Add( bc_ind );
        			
        			Add( StrategyPlot(0) );
                }
        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.

        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


          #5
          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.

          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();
          }
          What am I doing wrong?

          Comment


            #6
            Originally posted by bernie_c View Post
            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.

            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();
            }
            What am I doing wrong?
            Try removing the call to Dispose().

            Comment


              #7
              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


                #8
                Originally posted by bernie_c View Post
                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?
                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.

                Comment


                  #9
                  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


                    #10
                    Originally posted by NinjaTrader_PatrickH View Post
                    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.
                    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?

                    ref: http://www.ninjatrader.com/support/h...rmination2.htm

                    Moreover, this particular case actually more or less proves the point, does it not?

                    Comment


                      #11
                      Hello koganam.

                      Agreed, the Help Guide details it as the following:
                      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.
                      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


                        #12
                        Originally posted by NinjaTrader_PatrickH View Post
                        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:
                        Code:
                        		protected override void OnTermination()
                        		{
                        			textBrush.Dispose();
                        			stringFormat.Dispose();
                        		}
                        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.

                        Comment


                          #13
                          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.

                          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();
                          	}
                          }
                          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.

                          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


                            #14
                            Originally posted by bernie_c View Post
                            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.

                            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();
                            	}
                            }
                            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.

                            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.
                            Yes. Disposing of created objects by calling their internally implemented method should be OK.

                            Just be careful that they are objects that can get reinitialized. Remember that OnTermination() is also called when you refresh the chart (F5).
                            Last edited by koganam; 04-01-2015, 02:55 PM. Reason: Corrected misspelling.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by AaronKoRn, Today, 09:49 PM
                            0 responses
                            1 view
                            0 likes
                            Last Post AaronKoRn  
                            Started by carnitron, Today, 08:42 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post carnitron  
                            Started by strategist007, Today, 07:51 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post strategist007  
                            Started by StockTrader88, 03-06-2021, 08:58 AM
                            44 responses
                            3,975 views
                            3 likes
                            Last Post jhudas88  
                            Started by rbeckmann05, Today, 06:48 PM
                            0 responses
                            9 views
                            0 likes
                            Last Post rbeckmann05  
                            Working...
                            X