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

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:


            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 DanielSanMartin, Yesterday, 02:37 PM
            2 responses
            13 views
            0 likes
            Last Post DanielSanMartin  
            Started by DJ888, 04-16-2024, 06:09 PM
            4 responses
            12 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by terofs, Today, 04:18 PM
            0 responses
            11 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by nandhumca, Today, 03:41 PM
            0 responses
            8 views
            0 likes
            Last Post nandhumca  
            Started by The_Sec, Today, 03:37 PM
            0 responses
            7 views
            0 likes
            Last Post The_Sec
            by The_Sec
             
            Working...
            X