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
    Chris L.NinjaTrader Customer Service

    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.
        Chris L.NinjaTrader Customer Service

        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:
            https://ninjatrader.com/support/help...rumenttype.htm

            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
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by blar58, Today, 03:16 PM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Melissa  
            Started by alcorr247, Today, 02:52 PM
            2 responses
            7 views
            0 likes
            Last Post robforee2  
            Started by Radano, Today, 02:30 PM
            1 response
            4 views
            0 likes
            Last Post NinjaTrader_ChrisL  
            Started by DrummerBoy, Today, 01:34 PM
            3 responses
            29 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by kangethe, Today, 02:11 PM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Working...
            X