Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    }


    Paul H.NinjaTrader Customer Service

    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.

        Paul H.NinjaTrader Customer Service

        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
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by ender_wiggum, Today, 07:56 AM
            7 responses
            21 views
            0 likes
            Last Post NinjaTrader_Zachary  
            Started by renner1984, 06-16-2024, 01:50 PM
            7 responses
            24 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by hardworkinguy, 02-07-2024, 09:19 PM
            8 responses
            413 views
            0 likes
            Last Post DavidHunter  
            Started by admiralpanda, 02-16-2024, 06:59 AM
            2 responses
            135 views
            0 likes
            Last Post pacpac
            by pacpac
             
            Started by Vikuno1, 06-15-2024, 10:22 PM
            16 responses
            58 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X