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
No announcement yet.
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,
-ChrisL
-
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 thread
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Yesterday, 05:17 AM
|
0 responses
54 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
130 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
71 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
44 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
49 views
0 likes
|
Last Post
|

Comment