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:


    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 Sparkyboy, Today, 10:57 AM
          0 responses
          1 view
          0 likes
          Last Post Sparkyboy  
          Started by TheMarlin801, 10-13-2020, 01:40 AM
          21 responses
          3,916 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by timmbbo, 07-05-2023, 10:21 PM
          3 responses
          152 views
          0 likes
          Last Post grayfrog  
          Started by Lumbeezl, 01-11-2022, 06:50 PM
          30 responses
          809 views
          1 like
          Last Post grayfrog  
          Started by xiinteractive, 04-09-2024, 08:08 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Erick  
          Working...
          X