Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

how is Position.AveragePrice calculated?

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

    how is Position.AveragePrice calculated?

    Hi, can someone tell me how the Position.AveragePrice is calculated (without FIFO/LIFO)? Is there somewhere formula for this? Reason for the question is because I would like to calculate the strategy Position.AveragePrice myself in a separate method()
    sidlercom80
    NinjaTrader Ecosystem Vendor - Sidi Trading

    #2
    Hello sidlercom80,

    Thanks for your post.

    I do not have the actual source code since that is internally calculated, but you could decipher this without too much trouble by monitoring prices of executions in OnExecutionUpdate and comparing them with Position.AveragePrice. Please see the example I have attached.

    We look forward to assisting.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi NinjaTrader_Jim, unfortunately the calculation is not correct for partial executions, see picture:

      Click image for larger version

Name:	Screenshot_1.jpg
Views:	320
Size:	25.0 KB
ID:	1135477
      sidlercom80
      NinjaTrader Ecosystem Vendor - Sidi Trading

      Comment


        #4
        it will divide by the quantity.

        For example, you buy stock at price, 100 with 100 quantity, however it will not always 100 quantity with price 100.
        It may sometime around that value.

        So, average is the price / quantity.

        Comment


          #5
          Originally posted by cincai View Post
          it will divide by the quantity.

          For example, you buy stock at price, 100 with 100 quantity, however it will not always 100 quantity with price 100.
          It may sometime around that value.

          So, average is the price / quantity.
          i realize that, it doesn't help me. for my purpose, i need to use exactly the calculation method of the position. AveragePrice and not inaccurate ;-)
          sidlercom80
          NinjaTrader Ecosystem Vendor - Sidi Trading

          Comment


            #6
            Hello slidercom80,

            The code that calculates AveragePrice is internal. Producing code that generates the same result would be best done by calculating the averages on your own and comparing the result with Positon.AveragePrice.

            With regards to partial fills, different entry prices and execution quantities will also have to be considered. For example, if you have Entry1 for 5 contracts, that fill with 3 executions of 1 @ 100, 2 @ 101, and 2 @ 102, you would want to keep track of each execution quantity and price so you can get the average entry price for that specific order, and then the average for the overall position would be very similar to what I have already demonstrated.

            You could consider using a Dictionary<double, int> for each order so you can track separate prices of executions and the quantities of those executions. The double can reflect the price of an execution and the int can reflect the quantity associated with that execution.

            We look forward to assisting.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello _Jim, thank you for your answer. unfortunately, I did not take into account that in the OnOrderUpdate() method, the calculation of the average price by the AverageFillPrice is partially automatic. For those who are interested, here is my solution. it certainly goes much nicer with the code, but for my purposes it is enough


              PHP Code:
              private void GetPositionAvgPrice(OrderState orderStateint qtydouble avgFillPrice)
              {
              ///Attention!!! position average prices are calculated via the OnOrderUpdate() method. The average price is taken into account here for each partial fill.
              ///Position average price is calculated only for incoming orders (entry orders).

              int qtyTotal 0;
              double priceTotal 0;
              double posAvgPrice 0;

              if (
              posAvgPriceList.Count 1)
              posAvgPrice avgFillPrice;
              if (
              orderState == OrderState.PartFilled && posAvgPriceList.Count 0)
              {
              foreach (var 
              pos in posAvgPriceList)
              {
              qtyTotal += pos.Item1;
              priceTotal += pos.Item1 pos.Item2;
              posAvgPrice priceTotal qtyTotal;
              }
              posAvgPrice = (posAvgPrice avgFillPrice) / 2;
              }
              if (
              orderState != OrderState.PartFilled)
              {
              posAvgPriceList.Add(Tuple.Create(qtyavgFillPrice));

              foreach (var 
              pos in posAvgPriceList)
              {
              qtyTotal += pos.Item1;
              priceTotal += pos.Item1 pos.Item2;
              posAvgPrice priceTotal qtyTotal;
              }
              }
              //Set calculatedPosition.AveragePrice
              if (calculatedPosition != null)
              calculatedPosition.AveragePrice Instrument.MasterInstrument.RoundToTickSize(posAvg Price);
              else
              calculatedPosition.AveragePrice 0.0;
              //PositionInfo
              if (calculatedPosition.MarketPosition == MarketPosition.Flat)
              PositionInfo string.Format("{0}"calculatedPosition.MarketPosition.ToString());
              else
              PositionInfo string.Format("{0} {1} @ {2}"calculatedPosition.MarketPosition.ToString().Subst ring(01), calculatedPosition.QuantitycalculatedPosition.AveragePrice);

              Last edited by sidlercom80; 01-08-2021, 05:59 AM.
              sidlercom80
              NinjaTrader Ecosystem Vendor - Sidi Trading

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by PaulMohn, Today, 03:49 AM
              0 responses
              3 views
              0 likes
              Last Post PaulMohn  
              Started by inanazsocial, Today, 01:15 AM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Jason  
              Started by rocketman7, Today, 02:12 AM
              0 responses
              10 views
              0 likes
              Last Post rocketman7  
              Started by dustydbayer, Today, 01:59 AM
              0 responses
              2 views
              0 likes
              Last Post dustydbayer  
              Started by trilliantrader, 04-18-2024, 08:16 AM
              5 responses
              23 views
              0 likes
              Last Post trilliantrader  
              Working...
              X