Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calculating cost of current holding

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

    Calculating cost of current holding

    I want to calculate avg cost of my current holding/trades that i have longed.

    is it right way to do it? is there any in built version of the api?

    Code:
            private double CalculateAverageFillPrice(Account account)
            {
    
                double totalFillPrice = 0;
                int tradeCount = 0;
                lock (account.Executions){
                    Print("CalculateAverageFillPrice " + account.Executions.Count);
                    foreach (Execution execution in account.Executions)
                    {
                        if (execution.Instrument.FullName != Instrument.FullName || execution.Order.OrderAction != OrderAction.Buy) {
                            continue;
                        }
                        Print("execution " + execution.Instrument.FullName + " " +  execution.Time.Date + " price " + execution.Price + " today " + DateTime.Now.Date);
                        DateTime executionTime = execution.Time;
                        if (executionTime.Date == DateTime.Now.Date)
                        {
                            totalFillPrice += execution.Price;
                            tradeCount++;
                        }
                    }
    
                    if (tradeCount > 0)
                    {
                        return totalFillPrice / tradeCount;
                    }
                    else
                    {
                        return 0.0;
                    }
                }
                return 0.0;
            }​

    #2
    Hello, thanks for writing in. This is already built into the scripting library, see the Position.AveragePrice property:

    Comment


      #3
      Does position represent overall average of the instrument? Lets say i have long term position of a stock which from $50 and i am also trading the stock today at $70? what would position return?
      I want price from today's cost price

      Comment


        #4
        The notional value of the position would be Position.Quantity * Instrument.MasterInstrument.PointValue * Close[0]... but if it's a futures position the margin requirements are not that high of course.
        Bruce DeVault
        QuantKey Trading Vendor Services
        NinjaTrader Ecosystem Vendor - QuantKey

        Comment


          #5
          Hi, the Position.AveragePrice represents the total average position, so it will return the average between entry @50$ and entry @70$. If you need the position price for only the day trade, you will need to calculate this manually from within your script.
          Last edited by NinjaTrader_ChrisL; 05-25-2023, 08:56 AM.

          Comment


            #6
            I see you posted that this is an equity - if that's the case "point value" is 1 so it's just Position.Quantity * Close[0].
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            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
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X