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

Properly implementing PnL shutdown on historicals

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

    Properly implementing PnL shutdown on historicals

    I have some simple code:

    Code:
                PandL = Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit;
                if (PandL < -500)
                    ProfitLossForDayReached = true;

    That shuts down trading in any day if I lose more than $500 on that strategy. However, this doesn't work while back testing.

    I've tried using AllTrades instead of RealtimeTrades, but then it shuts down the whole backtest when it loses more than $500.

    Any ideas on how I can get something that will work during back testing (and hopefully live at the same time)?

    Thanks

    #2
    Hello,

    Thanks you for your note.

    There is not a performance counter for daily PnL for a backtest. You would need to create a variable and then when a trade is done store the realized PnL or Unrelized into a variable and then reset that variable to 0 at the start of a new day.

    Let me know if I can be of further assistance.

    Comment


      #3
      Originally posted by NinjaTrader_Brett View Post
      Hello,

      Thanks you for your note.

      There is not a performance counter for daily PnL for a backtest. You would need to create a variable and then when a trade is done store the realized PnL or Unrelized into a variable and then reset that variable to 0 at the start of a new day.

      Let me know if I can be of further assistance.
      Hi Brett,

      I think I may be able to come up with something from your description. However, I was wondering, is there a way to get the PnL of a trade after it is exited (just that last exited trade)? I can't seem to find it in the documentation.

      Thanks

      Comment


        #4
        Hello,

        Sure no problem.

        To do this you would need to use the Example 1 on this page for the last trade:



        Let me know if I can be of further assistance.

        Comment


          #5
          In that example, it has

          Code:
          Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];

          While doing a backtest, this value seems to be resetting back to zero. But, I cannot figure out when it is happening? Does it reset back to zero at the end of a session? Or at the end of a day?

          If it is happening at the end of a session, does it reset at the end of the session I've set up in the Session Manager, or whatever the session the instrument uses?


          Thanks

          Comment


            #6
            lookOutBelow, it would update after a new 'last' trade is closed...if that's not the case and the strategy is for example flat you would see the Pnl of the last closed trade being persisted for each bar. In which code context are you using it?
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Thanks for such a good post.
              However. how can I write the syntax


              if unrealized pnl of signal 5 <=50;(ticks)
              Enter Long

              Comment


                #8
                Hello abuomararman,

                Thanks for your note.

                You would need to loop through the TradesCollection in order to locate the trade with the signal 5 entry signal name and pull the PnL from that trade object. You would also need to do a mathematical calculation in order to determine the profit in ticks from points:

                Code:
                for (i = 0; i < Performance.RealTimeTrades.Count; i++)
                {
                Trade signalFive = Performance.RealTimeTrades[i];
                // the "signalFive" string will need to be replaced with the entry/exit signal name.  I am multiplying the point value with the result of an equation to convert the profit reported in points to ticks
                if (signalFive.Entry.Name == "signalFive" && signalFive.ProfitPoints * (Instrument.MasterInstrument.PointValue/(Instrument.MasterInstrument.PointValue*TickSize) <= 50)
                EnterLong();
                }
                For more information on the Trade object, you can refer to our help guide here:
                http://ninjatrader.com/support/helpGuides/nt7/trade.htm

                Please let me know if I may be of any further assistance.
                Alan S.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by sidlercom80, 10-28-2023, 08:49 AM
                167 responses
                2,260 views
                0 likes
                Last Post jeronymite  
                Started by warreng86, 11-10-2020, 02:04 PM
                7 responses
                1,361 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by Perr0Grande, Today, 08:16 PM
                0 responses
                5 views
                0 likes
                Last Post Perr0Grande  
                Started by elderan, Today, 08:03 PM
                0 responses
                9 views
                0 likes
                Last Post elderan
                by elderan
                 
                Started by algospoke, Today, 06:40 PM
                0 responses
                10 views
                0 likes
                Last Post algospoke  
                Working...
                X