Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last trade Winner or Loser

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

    Last trade Winner or Loser

    I have been searching around looking for a way to see if there is a way to tell if the last trade is a winner or loser. Does anyone have some sample code I can take a look at, The closest thing I found was in a post from a long time ago and It doesn't work when I add it to the strategy.

    Performance.AllTrades[Performance.AllTrades.Count - 1].ProfitCurrency < 0

    Basically I would like to do is if the last trade is a loss, then an action.

    Any help would be greatly appreciated.

    #2
    Hello Kspot,

    Thank you for your post.

    You can actually find an example of this in the Help Guide at the following link for Trade: http://www.ninjatrader.com/support/h.../nt7/trade.htm

    Comment


      #3
      I was able to find that, but I am having trouble actually using it. How would I use that to accomplish what I am looking to do. I added that as a condition, but then how do I add a check to one of my other conditions? Do I first have to add "lastTrade" or "lastProfit" as a variable?

      Then would it be like:

      && last Profit > 0

      I have been playing around with it for a few days and haven't been able to sort it out. Thanks for the help on this.




      protected override void OnBarUpdate()
      {
      // Check to make sure there is at least one trade in the collection
      if (Performance.RealtimeTrades.Count > 0)
      {
      // Get the last completed real-time trade (at index 0)
      Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];

      // Calculate the PnL for the last completed real-time trade
      double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;

      // Pring the PnL to the Output window
      Print("The last trade's profit is " + lastProfit);
      }
      }

      Comment


        #4
        Hello Kspot,

        Thank you for your response.

        To call the lastProfit throughout the strategy we need to define lastProfit outside of the OnBarUpdate() method and inside of the variables section. So our code would reflect the following:
        Code:
                #region Variables		
                private double lastProfit = 0;
                #endregion
        
                
                protected override void Initialize()
                {
        			
                }
        		
        		protected override void OnBarUpdate()
        		{
        			// Check to make sure there is at least one trade in the collection
        			if (Performance.RealtimeTrades.Count > 0)
        			{
        				// Get the last completed real-time trade (at index 0)
        				Trade lastTrade = Performance.RealtimeTrades[Performance.RealtimeTrades.Count - 1];
        		
        				// Calculate the PnL for the last completed real-time trade
        				lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
        		
        				// Pring the PnL to the Output window
        				Print("The last trade's profit is " + lastProfit);
        			}
        		}

        Comment


          #5
          My code is now compiling, but It will only complete the first condition in real time trading(Sim). After 1.1 or 1.2 it should move on to either 1.3/1.4 or 2.1/2.2. The lastProfit doesn't seem to be working. Do you see anything I'm missing? Thanks again for your help.

          Code:
              CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      
          			{
              	// Check to make sure there is at least one trade in the collection
             		 if (Performance.AllTrades.Count > 0)
              	{
                   // Get the last completed real-time trade (at index 0)
                  Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
           
                   // Calculate the PnL for the last completed real-time trade
                  double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
           
                   // Pring the PnL to the Output window
                  Print("The last trade's profit is " + lastProfit);
              }
          }
          			
          			
          				// Condition set 1.1
                      if (RSI(2, 1)[0] > 85
                          && Position.MarketPosition == MarketPosition.Flat
          				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
                      {
                          EnterShort(QTY, "T1");
                      }
          
                      // Condition set 1.2
                      if (RSI(2, 1)[0] < 15
                          && Position.MarketPosition == MarketPosition.Flat
          				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
          				
          				
                      {
                          EnterLong(QTY, "T1");
                      }
          			
          				
          			// Condition set 1.3
                      if (RSI(2, 1)[0] > 85
                          && Position.MarketPosition == MarketPosition.Flat
          				&& LASTPROFIT > 0)	
                      {
                          EnterShort(QTY, "T1.1");
                      }
          
                      // Condition set 1.4
                      if (RSI(2, 1)[0] < 15
                          && Position.MarketPosition == MarketPosition.Flat
          				&& LASTPROFIT > 0)	
          				
          				
                      {
                          EnterLong(QTY, "T1.1");
                      }
          
                      // Condition set 2.1
                      if (Position.MarketPosition == MarketPosition.Flat
          				&& LASTPROFIT < 0				
          				&& RSI(2, 1)[0] > 85)
          
                      {
                          EnterShort(QTY2, "Q2");
                      }
          
                      // Condition set 2.2
                      if (Position.MarketPosition == MarketPosition.Flat
          				&& LASTPROFIT < 0				 
          				&& RSI(2, 1)[0] < 15)
                      {
                          EnterLong(QTY2, "Q2");
                      }
          Last edited by Kspot; 02-13-2014, 11:09 PM.

          Comment


            #6
            Bump for edit

            Comment


              #7
              Hello Kspot,

              In Conditions 1.3 and on you are trying to access "LASTPROFIT" instead of "lastProfit". NinjaScript (C#) is context sensitive meaning that capital letter(s) will mean a completely different variable.

              You may want to use "lastProfit" instead.

              Happy to be of further assistance.
              JCNinjaTrader Customer Service

              Comment


                #8
                Arg, still having problems with this. I get the following error:

                'lastProfit' conflicts with the decleration 'Ninjatrader.Strategy.AAAMart3.lastProfit'

                If I add lastProfit to the #region Properties, I don't get an error, but it just stops after either 1.1 or 1.2.

                Sorry, I do not have a programming background and I do really appreciate the help on this.



                Code:
                #region Variables
                
                private double lastProfit = 0;
                		
                		
                        #endregion
                
                        /// <summary>
                        /// This method is used to configure the strategy and is called once before any strategy method is called.
                        /// </summary>
                        protected override void Initialize()
                        {
                            Add(RSI(2, 1));
                            Add(RSI(2, 1));
                			SetProfitTarget("", CalculationMode.Ticks, PT);
                            SetStopLoss("", CalculationMode.Ticks, SL, false);
                            
                
                            CalculateOnBarClose = true;
                        }
                
                        /// <summary>
                        /// Called on each bar update event (incoming tick)
                        /// </summary>
                        protected override void OnBarUpdate()
                        {
                            
                			{
                    	// Check to make sure there is at least one trade in the collection
                   		 if (Performance.AllTrades.Count > 0)
                    	{
                         // Get the last completed real-time trade (at index 0)
                        Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                 
                         // Calculate the PnL for the last completed real-time trade
                        double lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
                 
                         // Pring the PnL to the Output window
                        Print("The last trade's profit is " + lastProfit);
                    }
                }
                			
                			
                				// Condition set 1.1
                            if (RSI(2, 1)[0] > 85
                                && Position.MarketPosition == MarketPosition.Flat
                				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
                            {
                                EnterShort(QTY, "T1");
                            }
                
                            // Condition set 1.2
                            if (RSI(2, 1)[0] < 15
                                && Position.MarketPosition == MarketPosition.Flat
                				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
                				
                				
                            {
                                EnterLong(QTY, "T1");
                            }
                			
                				
                			// Condition set 1.3
                            if (RSI(2, 1)[0] > 85
                                && Position.MarketPosition == MarketPosition.Flat
                				&& lastProfit > 0)	
                            {
                                EnterShort(QTY, "T1.1");
                            }
                
                            // Condition set 1.4
                            if (RSI(2, 1)[0] < 15
                                && Position.MarketPosition == MarketPosition.Flat
                				&& lastProfit > 0)	
                				
                				
                            {
                                EnterLong(QTY, "T1.1");

                Comment


                  #9
                  Hello Kspot,

                  You are already declaring that variable inside of the Strategy so you do not have to declare it once again inside of OnBarUpdate().

                  instead of declaring it again like double lastProfit you may want to change it to:

                  Code:
                  // since you are already declaring the variable you do not to declare again
                  lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
                  Let me know if that helps.
                  JCNinjaTrader Customer Service

                  Comment


                    #10
                    Thank you, that does help!

                    Now that I can tell if it was profitable or not, is there a way to know the quantity of the last trade? So if the last trade wasn't profitable and equaled a specific quantity.

                    Am I on the right track per the code below? Do I have to define lastTrade as a variable?(tried without success)

                    // Condition set 1.3
                    if (RSI(2, 1)[0] > 85
                    && Position.MarketPosition == MarketPosition.Flat
                    && lastProfit > 0
                    && lastTrade.Quantity = QTY2
                    )

                    Comment


                      #11
                      Hello Kspot,

                      Yes, you are on the right track. To check the quantity inside of a condition you will have to use a double equals sign "==" in your condition like when you are checking your MarketPosition.

                      Code:
                      // Condition set 1.3
                      if (RSI(2, 1)[0] > 85
                      && Position.MarketPosition == MarketPosition.Flat
                      && lastProfit > 0
                      && lastTrade.Quantity == QTY2
                      )
                      Here is a link to our Help Guide that goes over Syntax in C# that you may view under "Operations - Relational"
                      JCNinjaTrader Customer Service

                      Comment


                        #12
                        yea sorry that was a stupid mistake, but it didn't fix the problem. Any more thoughts?

                        Receiving two errors from condition 1.3,

                        'lastTrade' conflicts with the decleration 'NinjaTrader.Strategy.New1.lastTrade'

                        and

                        'double' does not contain a defintion for 'Quantity' and no extension method 'Quantity' accepting a first argument of type 'double' could be found (are you missing a using directive or an assembly reference)

                        Code:
                        private double lastProfit = 0;
                        		private double lastTrade = 0;
                        		
                                #endregion
                        
                                /// <summary>
                                /// This method is used to configure the strategy and is called once before any strategy method is called.
                                /// </summary>
                                protected override void Initialize()
                                {
                                    Add(RSI(2, 1));
                                    Add(RSI(2, 1));
                        			SetProfitTarget("", CalculationMode.Ticks, PT);
                                    SetStopLoss("", CalculationMode.Ticks, SL, false);
                                    
                        
                                    CalculateOnBarClose = true;
                                }
                        
                                /// <summary>
                                /// Called on each bar update event (incoming tick)
                                /// </summary>
                                protected override void OnBarUpdate()
                                {
                                    
                        			{
                            	// Check to make sure there is at least one trade in the collection
                           		 if (Performance.AllTrades.Count > 0)
                            	{
                                 // Get the last completed real-time trade (at index 0)
                                Trade lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                         
                                 // Calculate the PnL for the last completed real-time trade
                                lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;
                         
                                 // Pring the PnL to the Output window
                                Print("The last trade's profit is " + lastProfit);
                            }
                        }
                        			
                        			
                        				// Condition set 1.1
                                    if (RSI(2, 1)[0] > 85
                                        && Position.MarketPosition == MarketPosition.Flat
                        				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
                                    {
                                        EnterShort(QTY, "T1");
                                    }
                        
                                    // Condition set 1.2
                                    if (RSI(2, 1)[0] < 15
                                        && Position.MarketPosition == MarketPosition.Flat
                        				&& Performance.AllTrades.TradesPerformance.Currency.CumProfit == 0)
                        				
                        				
                                    {
                                        EnterLong(QTY, "T1");
                                    }
                        			
                        				
                        			// Condition set 1.3
                                    if (RSI(2, 1)[0] > 85
                                        && Position.MarketPosition == MarketPosition.Flat
                        				&& lastProfit > 0
                        				&& lastTrade.Quantity == QTY
                        				
                        				)	
                        				
                                    {
                                        EnterShort(QTY, "T1.1");
                                    }

                        Comment


                          #13
                          You're delcaring lastTrade in both the variables as well as the Performance count.

                          Instead of using
                          Code:
                          		private double lastTrade = 0;
                          Change this to

                          Code:
                                		private Trade lastTrade;
                          Then in the Trade count, just change it to

                          Code:
                                          lastTrade = Performance.AllTrades[Performance.AllTrades.Count - 1];
                          And you should be able to compile.
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            That did it, thank you!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by bmartz, 03-12-2024, 06:12 AM
                            4 responses
                            32 views
                            0 likes
                            Last Post bmartz
                            by bmartz
                             
                            Started by Aviram Y, Today, 05:29 AM
                            4 responses
                            12 views
                            0 likes
                            Last Post Aviram Y  
                            Started by algospoke, 04-17-2024, 06:40 PM
                            3 responses
                            28 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by gentlebenthebear, Today, 01:30 AM
                            1 response
                            8 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by cls71, Today, 04:45 AM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Working...
                            X