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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    134 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X