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 CarlTrading, 03-31-2026, 09:41 PM
      1 response
      37 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, Yesterday, 02:41 AM
      0 responses
      17 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      25 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      40 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      34 views
      0 likes
      Last Post CarlTrading  
      Working...
      X