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

Multi-Threaded and OnBarUpdate and Account.OrderUpdate

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

    Multi-Threaded and OnBarUpdate and Account.OrderUpdate

    If I have an indicator that attaches to Account.OrderUpdate, PositionUpdate and ExecutionUpdate events do I need to be handle these event handlers being called in different threads from the thread calling OnBarUpdate?

    #2
    Hello ntbone,

    From the description it would be unlikely that is needed, are you seeing some kind of threading error?

    A good indication at when multi threading is a factor would be if you get a threading related error in the output window. If what you are doing is not getting a threading error you very likely do not need to have any specific handling in that use case. The subscription to the account methods would not be a threading concern because that is not going to be on a UI thread. Generally you would only encounter threading concerns when dealing with the UI.

    Please let me know if I may be of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      If my indicator has internal data and I am modifying that data from both the event handlers and the OnBarUpdate I can run in to problems if one of the handlers modifies this data at the same time OnBarUpdate is also reading or modifying the data. Thus it is important to know if these calls can happen in different threads.

      Comment


        #4
        Hello ntbone,

        I can run in to problems if one of the handlers modifies this data at the same time OnBarUpdate is also reading or modifying the data
        What problems specifically?
        If you have a specific question or situation please provide that. You may need to use a C# tactic like locking a collection, from the given details I couldn't really say what the solution may need to be.

        If you mean "out of sync" instead of threading here, the method is called out of sync from the other script events. That is expected and you would need to work around that in your logic if that affects it somehow. OnBarUpdate and your indicator have no pre-existing knowledge that you are using the account directly, those event are separate from the indicators events. A Order Update event also has nothing to do with a Bar update event so those two events have no relation for syncing or be called in a specific order.

        Please let me know if I may be of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I want to know if the event handlers can be called from a different thread than the OnBarUpdate. If so then for any data I modify (like member variables) I will need to provide a lock or some other multi-threaded mechanism to be sure that the variables are not modified at the same time that they are being read in a different thread. I am preparing to write code that uses both OnBarUpdate and these event handlers at the same time and want to be sure I write thread safe code.

          Comment


            #6
            Hello ntbone,

            In general there is no expectation for OnBarUpdate to be called in order with any type of Order events.

            In any situation using those two events would require you to plan for the Order events to happen at any given time. The OnbarUpdate event is called only for bar events, that has no relation to order events. You would run into the same situation using a strategy and its order event overrides. If you are doing a modification from both the order event and OnBarUpdate you may need to use C# tactics like a lock if the use case requires it. There are not enough specific details here for me to say if that would be needed, you would just need to try and program it and see if you have a problem or not.

            Thread safe code generally only applies toward UI items or where you access an existing internal subscription like the level2 indicator reference sample. If you see a specific threading error in the output window please provide a more specific example of what code was used and what error was presented.


            Please let me know if I may be of further assistance.

            JesseNinjaTrader Customer Service

            Comment


              #7
              My concern is not with the order in which events happen. I am specifically more concerned with OnBarUpdate being called in Thread 1 while the OrderUpdate event handler being called in Thread 2 and both are being executed at the exact same time. In this case, if I have data structures in my indicator that I am reading/modifying in both functions I can get issues due to both threads reading/modifying the same data at the same time. This is a very classic multi-threaded problem. It sounds like OnBarUpdate and Account.OrderUpdate, PositionUpdate and ExecutionUpdate can be called on different threads and thus execute simultaneously.

              Code:
              public class MultiThreadedIndicator: Indicator
              {
                  protected override void OnStateChange()
                  {
                      switch(State)
                      {
                          case State.SetDefaults:
                              {
                                  Account account; // = some account
                                  account.OrderUpdate+=Account_OrderUpdate;
              
                                  someFlag = false;
                              }
                              break;
                      }
                  }
              
                  private void Account_OrderUpdate(object sender, OrderEventArgs e)
                  {
                      if(e.OrderState == OrderState.Accepted)
                      {
                          someFlag = true;
                      }
                  }
              
                  protected override void OnBarUpdate()
                  {
                      if(someFlag)
                      {
                          // do something                
                      }
              
              
                      if(Close[0] > Open[0])
                      {
                          someFlag = false;  
                      }
                  }
              
                  bool someFlag;
              }
              In the above code snippet someFlag can be potentially modified by by OnBarUpdate and Account_OrderUpdate at the same time. Further, it could be modified by Account_OrderUpdate at the same time that OnBarUpdate is reading it.

              Comment


                #8
                Hello ntbone,

                As I had mentioned previously the order event is not tied to OnBarUpdate so they can be called independently, you should plan your logic around that. The order update could come through at any given time when the broker provides that update.

                This is a very classic multi-threaded problem
                If you are hitting a situation where you have two methods working independently and not executing in a specific order you may need to use C# tactics like locks or other logic to handle what you are doing.

                In the above code snippet someFlag can be potentially modified by by OnBarUpdate and Account_OrderUpdate at the same time. Further, it could be modified by Account_OrderUpdate at the same time that OnBarUpdate is reading it.
                Sure, that would be expected to see these called at the same time or not one after the other, there is no order which these methods should be called when both being used. If you need your logic to work in a specific order based on the two events you would have to make your logic work in that way with the events being independent. If the order event updates during the middle of an OnBarUpdate you may need to just wait for the next OBU to see that updated value.


                Please let me know if I may be of further assistance.





                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Again, this is not so much about the order as it is about the fact that both can execute simultaneously. Two threads modifying the same variables at the same time would not cause any sort of exception to be thrown or obvious error. They would simply both attempt to modify the value, and which ever one executes last will be the values that end. Further, if the data structure is more complicated, it could cause the data to have partial answer from one thread and partial answer from the other thread.

                  To clarify for anyone who reads this thread, if code has the possibility to be called by multiple threads as in the case of OnBarUpdate along side the event handlers, you need to protect your data structures from being changed by more than one thread at the same time or you will end up with hard to find and fix bugs in which your data will take on unexpected values.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by OllieFeraher, 05-09-2024, 11:14 AM
                  6 responses
                  18 views
                  0 likes
                  Last Post OllieFeraher  
                  Started by PaulMohn, 05-02-2024, 06:59 PM
                  2 responses
                  42 views
                  0 likes
                  Last Post PaulMohn  
                  Started by ETFVoyageur, Today, 02:10 AM
                  0 responses
                  10 views
                  0 likes
                  Last Post ETFVoyageur  
                  Started by rayyyu12, Today, 12:47 AM
                  0 responses
                  8 views
                  0 likes
                  Last Post rayyyu12  
                  Started by ETFVoyageur, 05-07-2024, 07:05 PM
                  17 responses
                  137 views
                  0 likes
                  Last Post ETFVoyageur  
                  Working...
                  X