Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Want to include lastProfit in SendMail()

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

    Want to include lastProfit in SendMail()

    Dear NT8 community,

    I would like to include the profit/loss of the trade in my Sendmail() message after a position has been closed.

    I use the following code to initiate an email:

    PHP Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,  double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    // Exit Long
    if (order.Name == "Exit Long" && orderState == OrderState.Filled)          
        {
        SendMail("[email protected]", "Trade Alert / Close: " + Instrument.MasterInstrument.Name, "Trade profit/loss was " + lastProfitCurrency);
        }         
    } 
    

    ..and the following code to derive "lastProfitCurrency", which I adapted from https://ninjatrader.com/support/help...-us/?trade.htm

    PHP Code:
    protected override void OnBarUpdate()
    {
    #region Calculate Trade P&L
    if (SystemPerformance.RealTimeTrades.Count > 0)
          {
              // Check to make sure there is at least one trade in the collection
              Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];
     
        // Calculate the PnL for the last completed real-time trade
              double lastProfitCurrency = lastTrade.ProfitCurrency;
     
             // Store the quantity of the last completed real-time trade
              double lastTradeQty = lastTrade.Quantity;                
        }
    #endregion
    } 
    

    It won't compile, as it gives me the error: "The name "lastProfitCurrency" does not exist in the current context." Can someone help me out how to fix the problem or find an easier solution to have my trade P&L mailed?

    #2
    Hello sagetrade,
    Thanks for your post.

    That error is the result of how your 'lastProfitCurrency' variable is being declared. Since it is declared inside a function nothing else can access it. To fix this you can simply declare the variable inside the class instead. Something similar to the following snippet should suffice.

    Code:
    [B]		private double lastProfitCurrency;[/B]
    		protected override void OnBarUpdate()
    		{
    			if (SystemPerformance.RealTimeTrades.Count > 0) 
    			{ 
    				Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1]; 
    				
    				lastProfitCurrency = lastTrade.ProfitCurrency;
    				
    				double lastTradeQty = lastTrade.Quantity;      
    			}
    Please let me know if you have any further questions.
    Josh G.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh,

      thx for your reply. I tried it with your code but "lastProfitCurrency" returns 0 for all trades closed.

      any idea where that comes from?

      Comment


        #4
        Hello sagetrade,
        Thanks for your note.

        Assuming that the previous snippet was used as-is then it should work as expected.
        lastTrade.ProfitCurrency will return "0" when there is not both an entry AND an exit execution, or if there was no profit or loss made from the trade. I should also note that my snippet was for real time trades which means there must be trades made to the account in realtime. For historical performance you should use SystemPerformance.AllTrades

        When you compare the orders placed by the strategy do you see both an entry and exit? Are your entry and exit orders profitable?

        I look forward to your reply.
        Josh G.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_JoshG View Post
          Hello sagetrade,
          Thanks for your note.

          Assuming that the previous snippet was used as-is then it should work as expected.
          lastTrade.ProfitCurrency will return "0" when there is not both an entry AND an exit execution, or if there was no profit or loss made from the trade. I should also note that my snippet was for real time trades which means there must be trades made to the account in realtime. For historical performance you should use SystemPerformance.AllTrades

          When you compare the orders placed by the strategy do you see both an entry and exit? Are your entry and exit orders profitable?

          I look forward to your reply.
          Dear Josh,

          the trade was not historical but realtime (Global Simulation Mode), it was opened via a market order and closed by a stop loss.

          // Stop Loss
          if (order.Name == "Stop loss" && orderState == OrderState.Filled)
          {
          SendMail("[email protected]", "Trade Alert / Initial Stop Loss: " + Instrument.MasterInstrument.Name, " Trade profit/loss was " + lastProfitCurrency);
          }

          The code: "private double lastProfitCurrency;" has been added above "protected override void OnStateChange()" and below "public class". The remainder of the snippet was already there below "protected override void OnBarUpdate()".

          Comment


            #6
            Hello sagetrade,

            Please send me your strategy so that I can investigate this matter further. Please write in to PlatformSupport(at)NinjaTrader(dot)com and reference this forum post.

            I look forward to assisting you.
            Josh G.NinjaTrader Customer Service

            Comment


              #7
              you have mail!

              Thanks for taking the time.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Today, 05:17 AM
              0 responses
              48 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              126 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              66 views
              0 likes
              Last Post NabilKhattabi  
              Started by Deep42, 03-06-2026, 12:28 AM
              0 responses
              42 views
              0 likes
              Last Post Deep42
              by Deep42
               
              Started by TheRealMorford, 03-05-2026, 06:15 PM
              0 responses
              46 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X