• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AddOn & MarketData & Desubscribe to Update

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

    AddOn & MarketData & Desubscribe to Update

    Hello,

    In my Add-On I have built a MarketData object as this:
    http://ninjatrader.com/support/helpGuides/nt8/en-us/

    but I dont subscribe to Update event. This is the only difference. I want be secure other things work correctly before I recieve Level1 data.


    When I dispose my Add-On, in the CleanUp method, I get an exception when the next sentence is executed:
    Code:
    if( mymarketdata != null )
       mymarketdata.Update -= myeventhandler;
    As far as I know, in C# I can desubscribe to one event although I have not subscribed to it before.

    Thanks

    #2
    Hello,

    Thank you for the post.

    What specific error are you seeing?

    In the case you are not creating a subscription to the event at all, that line should likely be removed, it would not be needed as there is no subscription.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      The exception is: "Object reference not set to an instance of an object. at NinjaTrader.Data.MarketData.remove_Update(EventHan dler`1 value)"

      To simplify and reproduce the bug I have implemented an indicator to subscribe to MarketData by this way, instead override OnMarketData directly, because the Help says :
      "The concept can be carried over to any NinjaScript object you may be working on."


      Steps:
      1.- Load the indicator in a chart.
      2.- Close the chart to run the State.Terminated. (dont remove the indicator because then NT is frozen. This is other different bug).
      3.- Exception "Object reference not set to an instance of an object." is throwed.

      This sentence in State.Terminated throws the exception, but should not do it even if I am not subscribed to the event:
      Code:
      md.Update -= myOnMarketData;

      Code:
      public class Zi8Borrar04 : Indicator
      {
      	private Boolean firstTime = true;
      	private MarketData md;
      
      	protected override void OnStateChange()
      	{
      		Print( String.Format("{0}   State={1}", this.Name, State.ToString()));
      		
      		if (State == State.SetDefaults)
      		{
      			Description							= @"Enter the description for your new custom Indicator here.";
      			Name								= "Zi8Borrar04";
      			Calculate							= Calculate.OnBarClose;
      			IsOverlay							= true;
      			DisplayInDataBox					= true;
      			DrawOnPricePanel					= true;
      			DrawHorizontalGridLines				= true;
      			DrawVerticalGridLines				= true;
      			PaintPriceMarkers					= true;
      			ScaleJustification					= NinjaTrader.Gui.Chart.ScaleJustification.Right;
      			//Disable this property if your indicator requires custom values that cumulate with each new market data event. 
      			//See Help Guide for additional information.
      			IsSuspendedWhileInactive			= true;
      			Displacement = -10;
      		}
      		else if ( State == State.Realtime )
      		{
      			md = new MarketData(this.Instrument);
      			//md.Update += myOnMarketData;
      		}
      		else if (State == State.Terminated )
      		{
      			try
      			{
      				if (md != null)
      					md.Update -= myOnMarketData;
      			}
      			catch(Exception ex )
      			{
      				NinjaTrader.Code.Output.Process(String.Format("{0}{1}{2}",ex.Message, Environment.NewLine, ex.StackTrace), PrintTo.OutputTab1);
      			}
      		}
      	}
      	
      	protected override void OnBarUpdate()
      	{
      	}
      
      	private void myOnMarketData(object sender, MarketDataEventArgs e)
      	{
      		Print(e.ToString());
      	}
      }

      Comment


        #4
        Hello,

        Thank you for the reply.

        I believe this would be expected based on the object and the action being used, I do understand in general C# this does not raise an exception, but that also assumes we are dealing with a standard C# object. In this case we are referring to a custom object and event handler specific to NinjaTrader that is also associated to internal logic, the comparison is fairly different.

        One way we can confirm the difference would be to use a standard WPF event:

        Code:
        if (ChartControl != null)
        {
        	ChartControl.MouseRightButtonDown -= ChartControl_MouseRightButtonDown;
        	Print("no exception");
        }
        I can see this does get called but does not return an exception.

        I will confirm with product management if this should be expected or if anything could be done and report the details back when I have them.

        The solution to this would be that if you know you are not going to subscribe to the event, don't try to un subscribe from the event. Instead that code could just be removed. Otherwise if you had the option to subscribe to the event or not, you would likely need to add a bool to know it has been subscribed to know to call the un subscribe in this case.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello,

          I just wanted to reply back on this item.

          This is to be expected, unfortunately in some cases custom events can fail less gracefully than the standard events provided by the .net framework.

          In this case specifically an unsubscription is being used and based on the object not already having a subscription, this is reaching an null exception.

          The solution would to be simply not unsubscribe if there was no initial subscription. In general it is best not to rely on internal exception handling but instead to ensure the logic used is only what is needed.

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sdauteuil, Today, 10:22 AM
          0 responses
          1 view
          0 likes
          Last Post sdauteuil  
          Started by pjsmith, Today, 09:48 AM
          2 responses
          11 views
          0 likes
          Last Post pjsmith
          by pjsmith
           
          Started by bc24fl, Today, 10:03 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by Trader17, Yesterday, 07:50 PM
          1 response
          16 views
          0 likes
          Last Post NinjaTrader_Jim  
          Started by ct_fx, Today, 05:14 AM
          1 response
          16 views
          0 likes
          Last Post NinjaTrader_ChrisL  
          Working...
          X