Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Strategy if Max Daily Loss is hit?

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

    Stop Strategy if Max Daily Loss is hit?

    Hi there,

    Coding a strategy and I want to stop the strategy from trading IF a specific max daily loss has been hit on the day. How would I go about doing that?

    I have lifetime license so I know I can use:
    Account.GetAccountItem(AccountItem.RealizedProfitL oss, NinjaTrader.Cbi.Currency.UsDollar).Value;

    But that returns the "realtime" value so will not work when I backtest. Is there a way to get the cumulative running net or at least gross PnL for the "session".

    For a bonus point, can it have a total of both realized AND unrealized?


    Thanks!
    Last edited by focus333; 11-30-2020, 09:33 PM.

    #2
    Hello focus333,

    Thanks for your post.

    For the strategy Realized PNL you can use: SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit
    Reference: https://ninjatrader.com/support/help...?cumprofit.htm

    For the strategy Unrealized PNL you can use: Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])
    Reference: https://ninjatrader.com/support/help...profitloss.htm

    UnrealizedPNL is the value of the current trade only.

    For clarity, the RealizedPNL is the strategies accumulated PNL, it does not reset on a daily basis. To create a daily PNL, you would need to create a variable to hold the accumulated PNL at the beginning of each day/session. Then you can check the difference between the accumulated PNL and the stored PNL to find the daily PNL.

    For example, assume you have created a double variable called StartOfDayPNL and a double variable called currentPNL

    if (Bars.IsFirstBarOfSession)
    {
    StartOfDayPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit; // save the strategies accumulated realized PNL at the beginning of the session
    }


    // to determine the PNL through the day

    CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit - StartOfDayPNL;

    if (CurrentPNL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) < myDailyStoplevel)
    {
    // do something
    }


    Comment


      #3
      Paul, Thanks for this info. A couple questions:

      If I start the strategy in the morning before the start of the regular session, with it automatically start at 0, or will it look for historical trades?

      Is it possible to create a strategy that only monitors this process and will accept a command to flatten all positions in that account if triggered, or does each position strategy need to have this code?

      Comment


        #4
        Hello Mathwiz,

        Thanks for your post.

        If I start the strategy in the morning before the start of the regular session, with it automatically start at 0, or will it look for historical trades? If your strategy performs historical trades it will accumulate the PNL. If you wish to avoid accumulated PNL and you start your strategy every day, then don't perform historical trades. To prevent historical trades add: if (State != State.Realtime) return; into your OnBarUpdate(). This means that you would not be able to observe any historical trades so if you use the strategy analyzer, you would need to comment out that line (or use a user input bool to control).

        Is it possible to create a strategy that only monitors this process and will accept a command to flatten all positions in that account if triggered, or does each position strategy need to have this code?" If you monitor PNL in the strategy you can stop trading in the strategy.

        Comment


          #5
          Paul,

          So will code like this:

          CurrentPNL = SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit;

          if (CurrentPNL + Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0])) < myDailyStoplevel)
          {
          Flatten()
          }

          running as its own strategy will monitor my entire account and flatten all positions if it reaches its trigger?

          Comment


            #6
            hello MathWiz,

            Thanks for your post.

            It looks like you have started another thread on this same topic here: https://ninjatrader.com/support/foru...loss-or-profit

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            87 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            132 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            68 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            118 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            67 views
            0 likes
            Last Post PaulMohn  
            Working...
            X