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?
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!
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
NinjaTrader
Strategy position concurrent access
Collapse
X
-
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?
Tags: None
-
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,
-ChrisLChris L.NinjaTrader Customer Service
-
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
-
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- Called by ThreadPool in historical processing.
- Called by InstrumentThread on each update in realtime processing.
OnOrderUpdate, OnExecutionUpdate, OnAccountUpdate, OnPositionUpdate, OnConnectionStatus - called by adapter threadChris L.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Is it possible to have a drawing, such as a line, cycle through different colors?
by gentlebenthebear
Started by gentlebenthebear, Today, 01:30 AM
|
2 responses
13 views
0 likes
|
Last Post Today, 02:05 PM | ||
Started by Kaledus, Today, 01:29 PM
|
2 responses
8 views
0 likes
|
Last Post
by Kaledus
Today, 02:04 PM
|
||
Started by frankthearm, Yesterday, 09:08 AM
|
13 responses
45 views
0 likes
|
Last Post
by frankthearm
Today, 01:52 PM
|
||
Started by PaulMohn, Today, 12:36 PM
|
2 responses
16 views
0 likes
|
Last Post
by PaulMohn
Today, 01:48 PM
|
||
Started by Conceptzx, 10-11-2022, 06:38 AM
|
2 responses
56 views
0 likes
|
Last Post
by PhillT
Today, 01:47 PM
|
Comment