Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last trade profit

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

    Last trade profit

    Hello,

    if I want to get the profit after closing a trade you help guide says to use the code:

    // Accesses the first/last trade in the strategy (oldest trade is at index 0)
    // and prints out the profit as a percentage to the output window

    if (Performance.AllTrades.Count > 1)
    {
    Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
    Trade firstTrade = Performance.AllTrades[0];


    Print("The last trade profit is " + lastTrade.ProfitPercent);
    Print("The first trade profit is " + firstTrade.ProfitPercent);

    }

    if the last trade is the last index number why the code is not?:

    Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count];

    #2
    Hello Guillembm,

    Thank you for your note.

    This is because of how the indexing is handled for Lists and Arrays.

    Lists and Arrays will always start indexing at value of 0, while Count will start at 1.

    When getting the entire Count and trying to index that value will produce an index out of range error, which is why we need to subtract 1 from the count so that we index accordingly.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      So what if I want to see not the first and last trade profit but rather the profit from just the latest 3 trades if they were losers or winners in a row? Then System.Perfromance in NT8 will be the wrong thing to use?
      Thank you.

      Comment


        #4
        Hello Trader17,

        Thank you for your reply.

        To clarify, are you asking about doing this in NT7 or NT8? In NinjaTrader 8 you would use SystemPerformance instead:



        To find the last three you could modify

        SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1]; //finds the most recent trade

        SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 2]; //finds the 2nd most recent trade

        SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 3]; //finds the 3rd most recent trade

        and so on for however many trades you want to look at.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Sorry. In Ninja Trader 8. And how would we differentiate between losers and winners in these last 3 trades? System Performance returns those values?

          Comment


            #6
            Hello Trader17,

            Yes, you can get the profit from the trade object returned:



            For example:

            double firstTradeProfitCurrency = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1].ProfitCurrency

            then you could check to see whether the returned value is positive or negative to determine whether the trade was a winning or losing trade.

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Perfect. That is what I was looking for as I wanted to trade for example only after X losers. And it can be done as currecy too as I only saw Percent.

              Comment


                #8
                And the above example you gave is from the Help Guide?

                Comment


                  #9
                  And -1 will always be the first trade and 0 the last in a strategy? Last as in the first taken by the strategy.

                  Comment


                    #10
                    Hello Trader17,

                    Thank you for your reply.

                    The above example is one I just created, so not in the help guide.

                    Yes, SystemPerformance.AllTrades.Count - 1 would always be the most recent trade and 0 would be the very first trade taken.

                    Please let us know if we may be of further assistance to you.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Thank you very much Kate. Wish they would make an example like you did for future reference in NT8.

                      Comment


                        #12
                        Hello Kate. Now if this is used on Bar Update, is the value of the profit/loss updated as soon as trade is closed or after the bar closes? I ask as I want to include that last trade also in the bar open calculation values.
                        Thank you/.

                        Comment


                          #13
                          Hello Trader17,

                          Thank you for your reply.

                          That would depend on the calculation method of the script. If you run it OnEachTick, it will be updated on the next tick that comes in after the trade is closed. If you run it on OnBarClose, it would update each time a new bar closes, and if the script is run OnPriceChange, it will update the next price change after the trade is closed.

                          Please let us know if we may be of further assistance to you.
                          Kate W.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you. So if I am using On Bar Close, it will still take into account that last trade profit/loss when it is also calculating the next signal, correct?
                            Thanks.

                            Comment


                              #15
                              Hello Trader17,

                              Thank you for your reply.

                              As long as you put the profit and loss calculation first before you check for a signal, yes, you should be fine there.

                              Please let us know if we may be of further assistance to you.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by fitspressoburnfat, Today, 04:25 AM
                              0 responses
                              2 views
                              0 likes
                              Last Post fitspressoburnfat  
                              Started by Skifree, Today, 03:41 AM
                              1 response
                              4 views
                              0 likes
                              Last Post Skifree
                              by Skifree
                               
                              Started by usazencort, Today, 01:16 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post usazencort  
                              Started by kaywai, 09-01-2023, 08:44 PM
                              5 responses
                              604 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by xiinteractive, 04-09-2024, 08:08 AM
                              6 responses
                              23 views
                              0 likes
                              Last Post xiinteractive  
                              Working...
                              X