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 NullPointStrategies, Today, 05:17 AM
            0 responses
            39 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            124 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            64 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            41 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