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

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 orderdouble limitPricedouble stopPriceint quantityint filled,  double averageFillPriceOrderState orderStateDateTime timeErrorCode errorstring 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 KennyK, 05-29-2017, 02:02 AM
              3 responses
              1,278 views
              0 likes
              Last Post marcus2300  
              Started by fernandobr, Today, 09:11 AM
              0 responses
              0 views
              0 likes
              Last Post fernandobr  
              Started by itrader46, Today, 09:04 AM
              1 response
              3 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by timmbbo, Today, 08:59 AM
              0 responses
              2 views
              0 likes
              Last Post timmbbo
              by timmbbo
               
              Started by bmartz, 03-12-2024, 06:12 AM
              5 responses
              33 views
              0 likes
              Last Post NinjaTrader_Zachary  
              Working...
              X