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 PaulMohn, Today, 05:00 AM
            0 responses
            6 views
            0 likes
            Last Post PaulMohn  
            Started by ZenCortexAuCost, Today, 04:24 AM
            0 responses
            6 views
            0 likes
            Last Post ZenCortexAuCost  
            Started by ZenCortexAuCost, Today, 04:22 AM
            0 responses
            3 views
            0 likes
            Last Post ZenCortexAuCost  
            Started by SantoshXX, Today, 03:09 AM
            0 responses
            16 views
            0 likes
            Last Post SantoshXX  
            Started by DanielTynera, Today, 01:14 AM
            0 responses
            5 views
            0 likes
            Last Post DanielTynera  
            Working...
            X