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 argusthome, 03-08-2026, 10:06 AM
    0 responses
    116 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    61 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    43 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    82 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X