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

locks and Account.Positions

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

    locks and Account.Positions

    The code in PositionAvgPrice.cs, PositionSize.cs, ProfitLoss.cs and UnrealizedProfitLoss.cs reference Account.Positions. When they do, they put a lock on Account.Positions.

    I assume that code in NinjaTrader locks on this collection whenever it is adding/removing positions from the collection. I also need to iterate over all the positions but I want it do it from an event handler connectione dto Account.PositionUpdate.

    How do I know that NinjaTrader will never have already acquired a lock on this collection when it calls my event handler? If there is some condition for which this event handler could be called with the lock already in place then the code will deadlock.

    #2
    Hello ntbone,

    Thanks for your post.

    When accessing these collections we should follow what is presented in the Help Guide, and also what you see in our system indicators. (Use lock before accessing the collection.)

    https://ninjatrader.com/support/help...ns_account.htm

    Accessing in Account.PositionUpdate may be a different case, but I have not heard of any dead lock scenarios to provide much additional detail. Are you stating that you are encountering dead locks here? If you attach a small example showing how the deadlock is encountered, we may be able to provide additional insight.

    Speaking objectively, and not just in terms of NinjaTrader, it is possible that a thread releases a lock and then the lock gets taken again almost immediately after. For cases like that, you could try using a passthrough lock like Monitor.TryEnter which will allow execution to continue if the lock cannot be taken. If it is crucial that the lock/information be obtained, you could consider combining with a loop to attempt grabbing the lock several times if it could not be captured the first time.

    Publicly available information on Monitor.TryEnter can be found below.

    https://docs.microsoft.com/en-us/dot...tframework-4.8

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment


      #3

      Hi ntbone,

      When the market would hit an impulse I also had significant deadlock issues when using locks on Account.Positions as described in the example in the guide.

      "I assume that code in NinjaTrader locks on this collection whenever it is adding/removing positions from the collection."

      I came to this same conclusion.

      So what did I do?

      I first tried the route of adding more complexity around locking but eventually concluded with an overall design principle:
      Execute a bias against adding more wait states into repetitive event operations.


      "Keep it Simple" ... what I am doing now.

      1) Switched calls to Account.Positions from tick data series to seconds based data series to reduce the likelihood that a market impulse drives a sudden flury of competitive locking activity on Account.Positions from both position changes and my own calls to Account.Positions.

      2) The market might be stalled more frequently than it is moving. Lets use this to our advantage. Only allow calls like you mention above with the market is stalled and mostly likely there will be no competitive locking from NT8's underlying operations. Something simple like:
      "If tickspeed is less than 35% of tickspeed's short moving average allow calls to Account.Positions using methods recommend by the NT team."


      HedgePlay

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by futurenow, Yesterday, 06:13 PM
      1 response
      21 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by trader3000a, Today, 07:34 AM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Started by focus333, Yesterday, 11:28 PM
      1 response
      22 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by futurenow, Yesterday, 07:03 PM
      1 response
      13 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by Balage0922, 10-01-2019, 11:09 AM
      17 responses
      214 views
      0 likes
      Last Post NinjaTrader_PaulH  
      Working...
      X