Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Monitor open position code doesn't work when target is met.

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

    Monitor open position code doesn't work when target is met.

    I tried to modify the PositionDisplayIndicator code to monitor a position that was opened manually, when a loss of $100 is reached but it doesn't work. If you reload the script on a chart then it just closes the open position regardless of any amount. Is there any way to change this script to auto close a manually opened position when a target is met? Full indicator code I found from the Ninjatrader ecosystem is attached and here is my code addition:

    private void OnAccountItemUpdate(object sender, AccountItemEventArgs e)
    {
    if (e.AccountItem == AccountItem.RealizedProfitLoss)
    realizedPnL = e.Value;
    if (e.AccountItem == AccountItem.CashValue)
    cashValue = e.Value;
    if (e.AccountItem == AccountItem.UnrealizedProfitLoss)
    unRealizedPnL = e.Value;
    if (unRealizedPnL > -100.00)
    {
    position.Close();
    }
    }

    Thanks for you help.​
    Attached Files

    #2
    JohnZee It is the logic here if (unRealizedPnL > -100.00) that is problematic. That logic says "if the UNrealized PnL is GREATER THAN $100 LOSS, do the following". In other words, "if I am still at least ABOVE $100 LOSS (not yet reached $100 LOSS), do this ...".

    I think what you really mean is this: if (unRealizedPnL < -100.00)​ which means "when the UNrealized PnL as a LOSS is EVEN MORE THAN $100, do this ...". It's purely a matter of comparing to a negative number. You want to know if your PnL is LESS than a negative number representing a LOSS already.

    Quite simply, always compare for LOSS to any absolute value (positive or negative) by comparing for LESS THAN. So, EVEN MORE NEGATIVE requires a LESS THAN comparison. Hope that helps.

    Thanks.
    Multi-Dimensional Managed Trading
    jeronymite
    NinjaTrader Ecosystem Vendor - Mizpah Software

    Comment


      #3
      Hello JohnZee,

      Thanks for your post.

      jeronymite is correct. The if (unRealizedPnL > -100.00)​ will check if the Unrealized PnL is greater than -100 but not less than or equal to -100.

      You could consider changing the Greater than comparison to a Less than comparison if you want to compare if unRealizedPnL is less than -100.

      Let me know if I may assist further/
      <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 NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 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