Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

lastTrade

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

    lastTrade

    I am attempting to implement some money management approaches within my strategy but can not seem to get it. For simplicity, let's say I just want to check whether the last trade was a winner or looser. What is wrong with this code?
    Code:
    if (Performance.AllTrades.Count > 1)
    
       			 {
    
            		Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
    
    				double lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
     
            		Print("The last trade profit is " + lastProfit);
    
    
       			 }
    
                // Condition set 1
                if (
    				lastProfit > 0
    				&& Close[0] == High[0]
                    
    				)
                {
                    EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry");
                }

    #2
    Hello Tdschulz,
    The trade collection will add an object to it only when you have a trade. However a trade will never occur since your coding filter specifies lastProfit > 0 (which will never happens unless there is a trade).

    Its like a circular reference, and thus you are witnessing the scenario.
    Code:
    double lastProfit = 0;
    
    if (Performance.AllTrades.Count >= 1)
    
     {
    	Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
    	lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    	Print("The last trade profit is " + lastProfit);
     }
    
    // Condition set 1
    if ((Performance.AllTrades.Count == 0 || lastProfit > 0)
    	&& Close[0] == High[0]
        
    	)
    {
        EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry");
    }
    Please note, if any trade results in a loss you will not have any further trade since lastProfit > 0.
    JoydeepNinjaTrader Customer Service

    Comment


      #3
      The problem is not that it's not executing as I expected but I can't get it to compile. Error says lastProfit does not exist. Do I need to add that in the Properties section?

      Comment


        #4
        Sorry. I didn't set the double = 0; at the beginning. I have it now.

        Next issue. The lastProfit is only recording the profit from a single contract rather that the amount for the entire quantity. Do you see anything wrong here?
        Code:
         protected override void OnBarUpdate()
                {	
        			double lastProfit = 0;
        			if (CurrentBar < 250)
        			{
        				return;
        			}
        			
        			if (Performance.AllTrades.Count >= 1)
        
           			{
        
                		Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
        
        				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
         
                		
        			}	
        
        
           			Print("The last trade profit is " + lastProfit); 
        
                    // Condition set 1
                    if ((Performance.AllTrades.Count == 0 || lastProfit > 0)
        			
        				&& Close[0] == High[0]
                        
        				)
                    {
                        EnterLongLimit(1, Close[0] + 1 * TickSize, "LongEntry");
                    }
        			
        			if ((lastProfit < 0 && lastProfit > -140)
        			
        				&& Close[0] == High[0]
                        
        				)
                    {
                        EnterLongLimit(2, Close[0] + 1 * TickSize, "LongEntry");
                    }
        			if ((lastProfit < -140)
        			
        				&& Close[0] == High[0]
                       
        				)
                    {
                        EnterLongLimit(6, Close[0] + 1 * TickSize, "LongEntry");
                    }

        Comment


          #5
          Hello Tdschulz,
          Profit for the last trade is taken into account as per the below code.

          Code:
          Performance.AllTrades[Performance.AllTrades.Count - 1]
          If you need the total profit then you have to change the above and calculate it further.
          JoydeepNinjaTrader Customer Service

          Comment


            #6
            isn't that what I'm calculating as:
            lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;

            Comment


              #7
              Hello Tdschulz,
              To assist you further may I know, are you trying to figure out the total profit for the entire period or the profit for the last trade only.
              JoydeepNinjaTrader Customer Service

              Comment


                #8
                Profit for last trade only

                Comment


                  #9
                  Hello Tdschulz,
                  To assist you further may I know, are you getting the trade part-filled?

                  I look forward to assisting you further.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    Full fills. It's occurring in replay and SIM. Maybe there is a better way to accomplish my goal. I want to trade one contract unless I get a looser. If a looser occurs when trading one contract, then trade two on the next trade. If that looses, trade 6 contracts. I was attempting to do that using dollar loss amounts but maybe you know of a better way.

                    Comment


                      #11
                      Hello Tdschulz,
                      If the trade gets fully filled at one shot then you will get the correct volume.

                      What value do you get if you print,
                      Code:
                      Print(lastTrade.Quantity);
                      JoydeepNinjaTrader Customer Service

                      Comment


                        #12
                        If lastTrade.Quantity only looks at the individual fills, then I'll need to figure out another way. Do you have a method to suggest to have the strategy look for a sequence of looses? As I mentioned, my end goal is to increase contracts as looses occur.

                        Comment


                          #13
                          I tried Printing lastTrade.Quantity and I receive an error stating 'lastTrade' does not exist in the current context. It compiles fine when used in the lastProfit statement but gives an error when trying to print it. Any ideas?

                          Comment


                            #14
                            Originally posted by Tdschulz View Post
                            I tried Printing lastTrade.Quantity and I receive an error stating 'lastTrade' does not exist in the current context. It compiles fine when used in the lastProfit statement but gives an error when trying to print it. Any ideas?
                            It might help if you posted the bit of code that you're having trouble with.

                            Comment


                              #15
                              Hello Tdschulz,
                              The trade collection is the best way to retrieve the PnL.

                              As an alternative you can store the positions PnL in a variable when you are in a position and use it later to determine the quantity size.
                              Code:
                              Position.GetProfitLoss(...)
                              JoydeepNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              651 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              370 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              109 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              574 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              577 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X