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 NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      69 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X