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

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
    Chris L.NinjaTrader Customer Service

    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
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          NinjaTrader_ChrisL thanks for the great info!

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by gentlebenthebear, Today, 01:30 AM
          2 responses
          13 views
          0 likes
          Last Post gentlebenthebear  
          Started by Kaledus, Today, 01:29 PM
          2 responses
          8 views
          0 likes
          Last Post Kaledus
          by Kaledus
           
          Started by frankthearm, Yesterday, 09:08 AM
          13 responses
          45 views
          0 likes
          Last Post frankthearm  
          Started by PaulMohn, Today, 12:36 PM
          2 responses
          16 views
          0 likes
          Last Post PaulMohn  
          Started by Conceptzx, 10-11-2022, 06:38 AM
          2 responses
          56 views
          0 likes
          Last Post PhillT
          by PhillT
           
          Working...
          X