Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

lastTrade

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

    #16
    Here is what I have. I'm not sure what is wrong here but the Print statement causes this code to fail. "//Print ("LastTrade Quantity is" + lastTrade.Quantity);"
    I receive the error "The name 'lastTrade' does not exist in the current context" although I'm using it in the 'lastProfit' code. Any ideas?
    I expect it to be the last filled quantity, not the intended lot size that I'm trying to fill. Ultimately I'm trying to monitor consecutive losses and skip trades, not stop the strategy as 'SampleTradeObject2' does. I'm not capable of modifying the SampleTradeObject2 to make it into a strategy that continues rather than stops so any help there would be appreciated also.

    Code:
            protected override void Initialize()
            {
                SetProfitTarget("LongEntry", CalculationMode.Ticks, Target);
                SetProfitTarget("ShortEntry", CalculationMode.Ticks, Target);
                SetStopLoss("LongEntry", CalculationMode.Ticks, Stop, false);
                SetStopLoss("ShortEntry", CalculationMode.Ticks, Stop, false);
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {	
    			double lastProfit = 0;
    			double lastProfit1 = 0;
    			double lastProfit2 = 0;
    			double lastProfit3 = 0;
    			double lastProfit4 = 0;
    			double priorTradesCount = 0;
    			double priorTradesCumProfit = 0;
    			
    			bool SkipTrade = false;
    			
    			if (CurrentBar < 250)
    			{
    				return;
    			}
    			// At the start of a new session
    			if (Bars.FirstBarOfSession)
    			{
    				// Store the strategy's prior cumulated realized profit and number of trades
    				priorTradesCount = Performance.RealtimeTrades.Count;
    				priorTradesCumProfit = Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit;
    				
    				/* NOTE: Using .AllTrades will include both historical virtual trades as well as real-time trades.
    				If you want to only count profits from real-time trades please use .RealtimeTrades. */
    			}
    			
    			/* Prevents further trading if the current session's realized profit exceeds $1000 or if realized losses exceed $400. */
    			if (Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit >= 10000
    				|| Performance.RealtimeTrades.TradesPerformance.Currency.CumProfit - priorTradesCumProfit <= -7000)
    			{
    				/* TIP FOR EXPERIENCED CODERS: This only prevents trade logic in the context of the OnBarUpdate() method. If you are utilizing
    				other methods like OnOrderUpdate() or OnMarketData() you will need to insert this code segment there as well. */
    				
    				// Returns out of the OnBarUpdate() method. This prevents any further evaluation of trade logic in the OnBarUpdate() method.
    				return;
    			}
    			if (Performance.RealtimeTrades.Count >= 1)
    
       			{
            		Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    			
    			}	
    			if (Performance.RealtimeTrades.Count >= 2)
    
       			{
    				Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    				Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
    				lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
    
    			}	
    			if (Performance.RealtimeTrades.Count >= 3)
    
       			{
    				Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    				Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
    				lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
    				Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
    				lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
    			}	
    			if (Performance.RealtimeTrades.Count >= 4)
    
       			{
    				Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    				Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
    				lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
    				Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
    				lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
    				Trade lastTrade3 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 4];
    				lastProfit3 = lastTrade3.ProfitCurrency*lastTrade3.Quantity;
    			}	
    //			if (Performance.RealtimeTrades.Count >= 5)
    //
    //   			{
    //				Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
    //				lastProfit = lastTrade.ProfitCurrency*lastTrade.Quantity;
    //				Trade lastTrade1 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 2];
    //				lastProfit1 = lastTrade1.ProfitCurrency*lastTrade1.Quantity;
    //				Trade lastTrade2 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 3];
    //				lastProfit2 = lastTrade2.ProfitCurrency*lastTrade2.Quantity;
    //				Trade lastTrade3 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 4];
    //				lastProfit3 = lastTrade3.ProfitCurrency*lastTrade3.Quantity;
    //				Trade lastTrade4 = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 5];
    //				lastProfit4 = lastTrade4.ProfitCurrency*lastTrade4.Quantity;
    //			}	
    			//Hault Trading after three loosers in a row.  lastProfit1 & 2 are the 2 contract order.  lastProfit 3 is one contract of the Qty 6//
    			if (lastProfit < 0
    				&& lastProfit1 < 0
    				&& lastProfit2 < 0
    				&& lastProfit3 <0
    //				&& lastProfit4 < 0
    				)
    			{
    				SkipTrade = true;
    			}
    				
    				
       			Print("The last trade profit is " + lastProfit); 
    			//Print ("LastTrade Quantity is" + lastTrade.Quantity);
    			Print ("LastProfit1 is" + lastProfit1);
    			Print ("LastProfit2 is" + lastProfit2);
    			Print ("LastProfit3 is" = lastProfit3);

    Comment


      #17
      ignore the last line in that code. I mistakenly put a '=' rather than '+"

      Same issue with 'lastTrade' still exists....

      Comment


        #18
        The problem is that you only declare the lastTrade variable inside your 'if' statements, so if none of those 'if' statements evaluate true, lastTrade won't exist. So what you should do is declare lastTrade at the beginning of OnBarUpdate, like where this block of code is:

        double lastProfit = 0;
        double lastProfit1 = 0;
        double lastProfit2 = 0;
        double lastProfit3 = 0;
        double lastProfit4 = 0;
        double priorTradesCount = 0;
        double priorTradesCumProfit = 0;

        Trade lastTrade;

        And then when you do your Print statement at the end, you want to make sure there is actually a trade in the collection, so you should do something like this:

        if (Performance.AllTrades.Count > 0) Print("something" + lastTrade);

        Comment


          #19
          Hello Tdschulz,
          The code is compiling fine at my end if I change the = to + in the last line of the code.
          JoydeepNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          649 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
          573 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          576 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X