Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto close all positions if loss >$50

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

    Auto close all positions if loss >$50

    How do I make this indicator:
    If my Loss is greater than $50, close all open positions.
    Thanks​

    #2
    Hello Rheiverson,

    From an indicator you will need an account object. From this you can use <Account>.Get() with AccountItem.RealizedPnL and .UnrealizedPnL.

    Code:
    private Account selectedAccount;
    // in State.DataLoaded
    selectedAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
    
    // in OnBarUpdate() or other market data driven method
    double currentPnL = selectedAccount.Get(AccountItem.RealizedProfitLoss, Currency.UsDollar) + selectedAccount.Get(AccountItem.UnrealizedProfitLoss, Currency.UsDollar);
    
    if (currentPnL <= LossLimit)
    {​// Close all open positions
    selectedAccount.Flatten(selectedAccount.Positions.Where(pos => pos.MarketPosition != MarketPosition.Flat).Select(pos => pos.Instrument).ToList());
    
    Print(string.Format("{0} | Closed all positions due to exceeding loss threshold.", Core.Globals.Now));​
    }
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      602 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      347 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      103 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      559 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      559 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X