Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Access Order Profit/Loss

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

    Access Order Profit/Loss

    Hi,

    I am using OnOrderUpdate to follow the order set (connected Enter/Stop/Target orders) and update the order data on my end. I managed to get the order averageFillPrice, the update of either the stop or target order and cancelation of the opposite order (target/stop), but I don't get the averageFillPrice for the Stop/Target order. Therefore I can't figure out the result of the order (Profit/Loss).

    Is there a way to get the results of the active order?

    Of maybe how to get to the Trades related to the order?

    Here is my OnOrderUpdate code. SOrder is my order class with data on the order. sOrders is a list of active orders:

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
        string fromSignalName = (order.FromEntrySignal!="")?order.FromEntrySignal:order.Name;            
        SOrder sOrder = sOrders.Find(o=> o.name==fromSignalName);
        if (sOrder!=null)
        {
            if (order.Name==fromSignalName) // the Enter order
            {
                sOrder.orderId = order.OrderId;
                sOrder.SetStatus(orderState);
                sOrder.entryOrder = order;
                if (quantity>0)     sOrder.qty = quantity;
                if (limitPrice>0)     sOrder.enterPrice = limitPrice;
                if (orderState==OrderState.Filled || orderState==OrderState.PartFilled)
                {
                    SetAvgSlippage(sOrder.enterPrice,averageFillPrice);
                    sOrder.filledEnterPrice    = averageFillPrice;
                    sOrder.filledQty = filled;
                }
            }
            else if (order.Name.ToLower().Contains("stop")) // Stop Order
            {
                sOrder.stopOrder = order;
                sOrder.stopPrice = stopPrice;
            }
            else if (order.Name.ToLower().Contains("target"))    // Target Order
            {
                sOrder.targetOrder = order;
            }
    
            if (orderState==OrderState.Cancelled)
            {
                if (order.Name!=fromSignalName)
                    sOrder.Completed();  // either Stop or Target were cancelled, the order is comapleted
                else
                    sOrder.Cancelled(); // Enter order was canccelled
            }
        }    
    }
        ​
    Last edited by Shai Samuel; 04-21-2023, 09:52 AM.

    #2
    I believe I found it... using GetTrades() with the entrySignalName.


    Please let me know if there is anything else.

    Thanks

    Comment


      #3
      Hello, thanks for writing in. It would appear your code is doing something wrong. I am able to print out the average fill price of the stop or target order using this example:


      On the lines in OnOrderUpdate where this script prints out order information, I added one print to make sure we can retrieve this information:
      Code:
      if (stopLossOrders.Contains(order))
      {
      ...
      Print(order);
      Print(order.AverageFillPrice + " **** STOP ORDER FILL PRICE ****");​
      }
      Please test this on your PC and compare/contrast your strategy to this example code.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      43 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      20 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      30 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      47 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      38 views
      0 likes
      Last Post CarlTrading  
      Working...
      X