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 NullPointStrategies, Today, 05:17 AM
    0 responses
    43 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    65 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X