Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to get PL for a specific instrument in an indicator

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

    How to get PL for a specific instrument in an indicator

    I'm trying to calculate the PL for a specific instrument for the current day to show that in an indicator panel.
    Since the Account.Get(RealizedProfitLoss) gives me the total of all instruments, I'm trying to get it by summing the Executions.
    But I get no results.
    What could be wrong with the code below:

    Dictionary<Order, double> orders = new Dictionary<Order, double>();
    foreach(Execution ex in account.Executions) {
    if (ex.Instrument.FullName==Instrument.FullName) {
    //belongs to order, can be entry or exit
    if (!orders.ContainsKey(ex.Order))
    orders[ex.Order] = 0;

    orders[ex.Order] -= ex.Commission;
    if (ex.Order.OrderAction==OrderAction.Buy) {
    if (ex.MarketPosition==MarketPosition.Short)
    orders[ex.Order] += (ex.Price-ex.Order.AverageFillPrice)*ex.Quantity*50;
    } else {
    if (ex.MarketPosition==MarketPosition.Long)
    orders[ex.Order] += (ex.Order.AverageFillPrice-ex.Price)*ex.Quantity*50;
    }
    }

    double profitloss = 0;
    foreach(KeyValuePair<Order, double> kv in orders) {
    profitloss += kv.Value;
    }


    PS: ex.isEntry and ex.isExit don't help either.

    #2
    Hello wjadevries, thanks for writing in.

    Make sure you use the Print method to test your code. It is likely one or more of the variables you are using are 0, or there are no elements in the Executions collection. Note that the Executions collection does not hold all executions from the database, but only the "current executions" that can be seen from the Executions tab of the Control Center. If that tab has executions within, then the list will not be empty.

    I look forward to hearing of your results.

    Comment


      #3
      Oke, I see that the Execution.Order and Execution.OrderId aren't matching with Entry and Exit of an order.
      Every Execution has an unique order, so I can't use that.

      Now I simply sum up the executions like this (when I'm flat in the instrument):

      Code:
      double profitloss = 0;
      double commission = 0;
      
      foreach(Execution ex in account.Executions) {
          if (ex.Instrument.FullName==Instrument.FullName && ex.Account.Name==account.Name) {
      
              if (ex.MarketPosition==MarketPosition.Long)
                  profitloss += ex.Price * ex.Quantity;
              else
                  profitloss -= ex.Price * ex.Quantity;
      
              commission += ex.Commission;
          }
      }
      
      profitloss *= -Instrument.MasterInstrument.PointValue;
      profitloss -= commission;
      For ES and NQ this gives me the same results as in the Trade Performance window (so that's fine).
      But for 6E the (loss) result in the Trade Performance window is about 20% worse than the above code produces.
      Any idea why?
      Should I use the Execution.Slippage or something?
      Looks too much to be slippage.

      Comment


        #4
        Got it, I think.
        If I use Instrument.MasterInstrument.RoundToTickSize(ex.Pri ce) instead of ex.Price all instruments P/L's are correct.
        Can you confirm that this is the right way to do it?

        Comment


          #5
          Hello wjadevries, thanks for your reply.

          Yes, that would be correct. RoundToTickSize will remove any non-significant from the PnL that could lead to the total PnL being slightly different.

          Best regards.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          648 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          369 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          108 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          572 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          574 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X