Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Get Last Order value

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

    Get Last Order value

    Hello NT Team

    Ask: How to get the last closed trade is loss or profit.

    I would like to check a condition based on the Last closed trade. If the Last closed trade is in PROFIT...do this, if not do something else...

    Example:

    The Last Closed Trade Name = L1 and it Closed in profit(>0), then.....
    The Last Closed Trade Name = L1 and it Closed in loss(<0), then.....


    I tried the below and getting errors:

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string signalName, string fromEntrySignal)
    {
    if (execution.Order.OrderState == OrderState.Filled && fromEntrySignal == "L1")
    {
    Trade lastTrade = SystemPerformance.AllTrades.Count > 0 ? SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] : null;
    if (lastTrade != null && lastTrade.Entry.Name == "L1" && lastTrade. IsTradeClosed)
    {
    lastTradeProfit = lastTrade.ProfitCurrency;
    }
    }}


    Also, tried this:
    protected override void OnExecution(Execution execution)

    {
    if (execution.Order.OrderState == OrderState.Filled && execution.Order.Name == "L1")
    {
    // Update the last trade profit if the trade is closed
    Trade lastTrade = SystemPerformance.AllTrades.Count > 0 ? SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1] : null;
    if (lastTrade != null && lastTrade.Exit != null)
    {
    lastTradeProfit = lastTrade.ProfitCurrency;
    }
    }
    }

    Help Appreciated.

    Thanks​​

    #2
    Hello NRITV,

    If you would like assistance with an error message please provide the full error message.

    The code looks mostly correct. fromEntrySignal isn't shown where this is declared or assigned a value. Where you wanting to get the from entry signal when the execution order is an exit order?

    if ((execution.Order.OrderAction == OrderAction.Sell || execution.Order.OrderAction == OrderAction.BuyToCover) && execution.Order.FromEntrySignal == "L1")



    You won't need lastTrade. IsTradeClosed as all trades in the SystemPerformance collection are closed trades with both an entry and exit.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by kinfxhk, 07-13-2026, 10:18 AM
    0 responses
    58 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-13-2026, 09:50 AM
    0 responses
    41 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-13-2026, 07:21 AM
    0 responses
    46 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by kinfxhk, 07-11-2026, 02:11 AM
    0 responses
    37 views
    0 likes
    Last Post kinfxhk
    by kinfxhk
     
    Started by SalmaTrader, 07-07-2026, 10:26 PM
    0 responses
    156 views
    0 likes
    Last Post SalmaTrader  
    Working...
    X