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 kinfxhk, 07-14-2026, 09:39 AM
    0 responses
    15 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-13-2026, 10:18 AM
    0 responses
    59 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-13-2026, 09:50 AM
    0 responses
    42 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-13-2026, 07:21 AM
    0 responses
    47 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-11-2026, 02:11 AM
    0 responses
    38 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Working...
    X