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

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?

    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by frankthearm, Today, 09:08 AM
    5 responses
    10 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by jeronymite, 04-12-2024, 04:26 PM
    3 responses
    43 views
    0 likes
    Last Post jeronymite  
    Started by yertle, Today, 08:38 AM
    5 responses
    15 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by adeelshahzad, Today, 03:54 AM
    3 responses
    18 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by bill2023, Yesterday, 08:51 AM
    6 responses
    27 views
    0 likes
    Last Post NinjaTrader_Erick  
    Working...
    X