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:
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
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?
Comment