Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Event Handler not Unsubscribing

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

    Event Handler not Unsubscribing

    Hello,

    Upon refreshing the chart F5, the indicator is supposed to unsub the event, and when it loads back in, re-subscribe. For some reason, it's not unsubbing so it keeps duplicating the event handler. I tried using Dispatcher, but IIRC that has to do with custom buttons which this indicator does not have. Also tried to print "Unsubbing" to confirm it unsubbed but that print never occurs. Doesn't the indicator go to the temporary state of Terminated when refreshing the chart thus it should run through that bit of code?

    Code:
    else if (State == State.Historical)
    {
      if (ChartControl != null )
      {
        lock (Account.All)
        userAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
    
        count = 0;
        intrumentName = InstrumentInput.FullName;
    
        ChartControl.Dispatcher.InvokeAsync((Action)(() => {
          InitialOrderTransfer(); --> userAccount.ExecutionUpdate += OnExecutionUpdate;
        }));
      }
    else if (State == State.Terminated)
    {
      if (ChartControl == null)
        return;
      // Unsubscribe to events
      ChartControl.Dispatcher.InvokeAsync((() =>
      {
        userAccount.ExecutionUpdate -= OnExecutionUpdate;
        Print("Unsubing");
      }));
    }
    Unsuitable
    NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

    #2
    Hello Unsuitable,

    Thanks for your post.

    You may wish to close the platform and restart to ensure everything is cleared and there are not any leftover event handlers that are subscribed.

    What you have set up looks fine, but the dispatcher is not necessary. I have an example attached that demonstrates subscribing to various account level events.

    Let us know if you experience any issues with the example or setting up in the same fashion after restarting the platform.

    We look forward to assisting.
    Attached Files

    Comment


      #3
      Hello Jim,

      Turns out the terminated state was within the state.DataLoaded if statement. I notice the code uses DataLoaded while I use Historical, does it matter?
      Unsuitable
      NinjaTrader Ecosystem Vendor - Ocean Trading Indicators

      Comment


        #4
        Hello Unsuitable,

        DataLoaded and Historical are good states for subscribing events and resource creation. It doesn't really matter between the two of these. State.Configure would be less advised, especially for strategies since State.Configure would be processed when clicking OK in a Strategies dialog, without enabling the strategy.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        566 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        330 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        547 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        548 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X