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 Mindset, 04-21-2026, 06:46 AM
    0 responses
    87 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    132 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X