Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy position concurrent access

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

    Strategy position concurrent access

    I have seen what looks like concurrent changes from another thread to the Position object within a strategy. Basically, I access the strategy Position object, perform some calculation based on the position quantity, and later when I check the position quantity again in the same function, it has already changed due to an execution. The calling function is OnMarketData().

    I am aware of the lock used to traverse the Positions/Accounts objects. Is there a similar lock to the strategy Position object itself? If not, what is the best way to prevent this from happening?


    #2
    Hi VolatyTrading, thanks for posting.

    Positions are updated from the order thread, OnMarketData is run from the data thread, so they can go at different speeds. One solution is to get your position data from OnPositionUpdate and use that data in OnMarketData or save the Position object to a variable in OnMarketData and use that variable, just note that data would be old by the time OnMarketData is finished.

    Kind regards,
    -ChrisL

    Comment


      #3
      Hi NinjaTrader_ChrisL,

      thanks for the clarification! I do have logic that not only checks positions but also sends orders due to a market data event from OnMarketData().

      Is it thread-safe to call SubmitOrder() or SubmitOrderUnmanaged() from OnMarketData()? From the explanation above, I assume not. In this case, what is the thread-safe way to do it?

      Sorry, I couldn't find a reference for the thread model in NT8. I did see this document https://ninjatrader.com/support/help...-threading.htm but it doesn't get into this level of detail explaining the existing threads.

      Comment


        #4
        Hi Volaty, thanks for your reply.

        SubmitOrderUnmanaged is thread safe to call anywhere from NinjaScript events. If the entry depends on the Position data you could either submit the order in OnPositionUpdate or only use data set from OnPositionUpdate in OnMarketData. A birds eye view of the threading model is: multiple UI threads, multiple Instrument threads, a Thread Pool for running random tasks in the background and Adapter threads that are spawned by the connection adapter.

        OnBarUpdate
        1. Called by ThreadPool in historical processing.
        2. Called by InstrumentThread on each update in realtime processing.
        OnMarketData - Called by Instrument Thread

        OnOrderUpdate, OnExecutionUpdate, OnAccountUpdate, OnPositionUpdate, OnConnectionStatus - called by adapter thread

        Comment


          #5
          NinjaTrader_ChrisL thanks for the great info!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          58 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          133 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          73 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X