Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Determine PnL for last trade of a specific instrument

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

    Determine PnL for last trade of a specific instrument

    Hello,

    I'm trying to determine what the PnL was of the last trade in a strategy. Problem is, I'm running the strategy for multiple instruments, so right now the "last trade" PnL may be for a different instrument, not the one I'm using the calculation for.

    So for example, if the strategy is running for ES and GC, I want to check what the PnL was for the last GC trade, even if the actual last trade was for ES. Is there a way to do this?

    Currently I'm using the following code to check the last trade but don't know how to identify which instrument it was for. Appreciate any help.

    Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];

    #2
    Hello,

    Thank you for the post.

    The GetTrades() function takes an instrument name as a string and an instance as an integer. An instance of 1 will return the most recent position taken. This function will return a trade collection object with all Trade objects that make up the position.

    https://ninjatrader.com/support/help...?gettrades.htm - GetTrades()

    Alternatively, you can loop through the AllTrades TradeCollection backwards to find the last trade on a particular instrument.

    Code:
    for(int i = SystemPerformance.AllTrades.Count - 1; i >= 0; --i){
        
            if(SystemPerformance.AllTrades[i].Exit.Instrument.FullName == "GC 12-17"){
    	        Print("Found last GC trade");
    		break;
    	 }
    			    
    }
    I have included links to all the material used here.

    https://ninjatrader.com/support/help...collection.htm -TradeCollection object
    https://ninjatrader.com/support/help...-us/?trade.htm -Trade object
    https://ninjatrader.com/support/help...?execution.htm - Execution object (inside of Trade object)
    https://ninjatrader.com/support/help...instrument.htm - Instrument object (inside of Execution object)

    If we may be of any further assistance, please let us know.
    Last edited by NinjaTrader_ChrisL; 11-16-2017, 11:56 AM. Reason: Spelling

    Comment


      #3
      Thanks Chris.

      That's helpful but I'm still a little bit unclear on how I can use that. Once I find the trade that matches my instrument name, how can I then identify that trade in another statement to check the PnL of it?

      Comment


        #4
        Hello fiddich,

        Thank you for the follow-up.

        You can make a private Trade object a member of the class and store the trade that you find with the loop I posted.

        Code:
        public class MyStrategy : Strategy
        {
        
        private Trade myTrade;
        ...
        for(int i = SystemPerformance.AllTrades.Count - 1; i >= 0; --i){
            
                if(SystemPerformance.AllTrades[i].Exit.Instrument.FullName == "GC 12-17"){
        	        Print("Found last GC trade");
                        myTrade = SystemPerformance.AllTrades[i];
        		break;
        	 }
        			    
        }
        
        ...
        
        Print(myTrade.Entry.Price);
        Please let me know if I may assist further.

        Comment


          #5
          Thanks Chris, works great.

          Once last question for you. Is there a way to determine and store the current instrument's name? For example, you use '== "GC 12-17"' below but since I'm using multiple instruments, I'd like to put in a variable there that will pull the current chart's instrument dynamically, if possible.

          Thanks.

          Comment


            #6
            Hello fiddich,

            Thank you for the reply.

            You can use the Instruments[] array, then get the full name from each index.

            public List<string> MyInstrumentNames = new List<string>();

            foreach (Instrument i in Instruments)
            {
            MyInstrumentNames.Add(i.FullName);
            }

            Here is the help guide page on the instruments array:


            If we may be of any further assistance, please let us know.
            Last edited by NinjaTrader_ChrisL; 11-17-2017, 10:13 AM. Reason: syntax

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            52 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            130 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            70 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            44 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            49 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X