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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 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
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      71 views
      0 likes
      Last Post PaulMohn  
      Working...
      X