Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

More Efficient - Check Account Position

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

    More Efficient - Check Account Position

    I have the below code in OnBarUpdate and worried that it isn't the most efficient especially if it's being called multiple times per minute. Specifically I'm worried about the "LOCK". What is the impact of LOCK and is there a better way of dynamically getting the account's position.

    // check if the account position is flat.

    private bool isAccountPositionFlat()
    {

    int accountLength = getInstrumentName().Length;

    lock (myAccount.Positions)
    {
    foreach (Position position in myAccount.Positions)
    {
    if (getInstrumentName() == position.Instrument.ToString )
    {
    return false;
    }
    }
    }
    return true;
    }

    #2
    Hello bc24fl,

    Thanks for your post.

    The "lock" will prevent you from accessing that object until the lock is released. For example, NinjaTrader may lock this collection when adding to it or manipulating it, and lock would be used in the script to ensure that it is accessed when it is free.

    Can you clarify what information you are looking for with your code and what you are trying to accomplish?

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    47 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    23 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    33 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    51 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Working...
    X