Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem: Position.AveragePrice = 0

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

    Problem: Position.AveragePrice = 0

    I'd like to raise something regarding a potential problem with the position average prices update by the strategy builder.

    I am planning to use Position.AveragePrice for create a stoploss below the price when going long.
    However, the price wasn't update until the next 2 loops of OnBarUpdate.

    Is this a normal situation ? Is there any alternative function ?

    Please see the code example below

    protected override void OnBarUpdate()
    {
    // Order entry
    if(Low[0] > FastEMAT && Low[0] > FastEMAB && Position.MarketPosition == MarketPosition.Flat )
    {
    EnterLong(Convert.ToInt32(DefaultQuantity),"");
    Print("EnterLong");
    Print(Position.AveragePrice);
    }

    The code output will be as below.
    EnterLong
    0

    Thank you for your help!

    #2
    Hello ystnpw,

    Thanks for your post and welcome to the NinjaTrader Forums.

    If you print out Position.AveragePrice in the same bar update as calling EnterLong() to place the order, the order might not have had time to be Submitted, Accepted, Working, Filled so the Position.AveragePrice would return a value of 0.

    Note that it takes some time for the order to be Submitted, Accepted, Working, Filled.

    In your script, create a condition that checks if you are in a long market position by using Position.MarketPosition == MarketPosition.Long and then print out the Position.AveragePrice within that condition. By doing so, you ensure that the order has been filled and you are in a long position before printing out the average price of the position.

    Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    80 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    46 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    29 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    32 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    66 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X