Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Subscription to OnExecutionUpdate from OnStateChange

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

    Subscription to OnExecutionUpdate from OnStateChange

    Looking at examples for OnExecutionUpdate in the Indicator space suggests to subscribe and unsubscribe from within the OnStateChange method. However, either of these two lines of code:

    accountNow.ExecutionUpdate += OnExecutionUpdate;
    accountNow.ExecutionUpdate -= OnExecutionUpdate;

    are generating the "Object reference not set to instance of an object" error when executed from within OnStateChange. However (a) the indicator loads anyway and appears to be functioning as expected, regardless, and (b) I am within the same indicator using both lines of code within a dispatcher outside OnStateChange and that does not cause the error.

    #2
    Hello jvanelli,

    Thanks for your post.

    The error mentions that something you are referencing is null. Debugging prints can be used to confirm the line in which the error is seen (add prints after each line of code and check the output window to confirm the last line of code reached.)

    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

    Once the line is identified, you will need to check which object on that line is null. (Before that line is called, check each object that is used on that line to see if it is null. The object that is null would be the reason why the error is thrown.)

    As a guess, you may need to create an Account object in your indicator.

    I have attached an example indicator that can be referenced for how to subscribe and unsubscribe from account events.

    If this does not resolve your inquiry and you would like clarity on why you see an error one way, but not another, please implement just that code in a separate indicator so we have a small script that demonstrates the issue. You can then attach that indicator source code here so we may comment.

    Exporting as source code - https://ninjatrader.com/support/help...tAsSourceFiles

    Let us know if there is anything else we can do to help.
    Attached Files

    Comment


      #3
      Thanks. Yes, I believe the Account object is not available when the affected code executes, some sort of race issue between threads. This could be because account selection/creation occurs in a (too lengthy) locked sequence of code which tests several possible account names. I have been doing this account selection/creation in State.Configure, which may be problematic. Will move to State.DataLoaded, perhaps use dispatcher.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      607 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      353 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      560 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      561 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X