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 Ripoz4200, 01-29-2024, 03:21 PM
      6 responses
      2,309 views
      0 likes
      Last Post Lancer
      by Lancer
       
      Started by paulmuldoon, 09-03-2024, 08:24 PM
      5 responses
      58 views
      0 likes
      Last Post Lancer
      by Lancer
       
      Started by markjaffe, Today, 12:57 PM
      0 responses
      6 views
      0 likes
      Last Post markjaffe  
      Started by arbeydario, 09-03-2024, 08:37 PM
      4 responses
      31 views
      0 likes
      Last Post arbeydario  
      Started by Christopher Leggit, Today, 11:13 AM
      0 responses
      10 views
      0 likes
      Last Post Christopher Leggit  
      Working...
      X