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!
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
NinjaTrader
how is Position.AveragePrice calculated?
Collapse
X
-
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()Tags: None
-
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 FilesJimNinjaTrader Customer Service
-
Comment
-
Originally posted by cincai View Postit 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
-
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
-
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 orderState, int qty, double 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(qty, avgFillPrice));
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(0, 1), calculatedPosition.Quantity, calculatedPosition.AveragePrice);
}
Last edited by sidlercom80; 01-08-2021, 05:59 AM.
- Likes 1
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Brevo, Today, 01:45 AM
|
0 responses
6 views
0 likes
|
Last Post
by Brevo
Today, 01:45 AM
|
||
Started by aussugardefender, Today, 01:07 AM
|
0 responses
3 views
0 likes
|
Last Post Today, 01:07 AM | ||
Started by pvincent, 06-23-2022, 12:53 PM
|
14 responses
242 views
0 likes
|
Last Post
by Nyman
Today, 12:47 AM
|
||
Started by TraderG23, 12-08-2023, 07:56 AM
|
9 responses
384 views
1 like
|
Last Post
by Gavini
Today, 12:29 AM
|
||
Started by oviejo, Today, 12:28 AM
|
0 responses
6 views
0 likes
|
Last Post
by oviejo
Today, 12:28 AM
|
Comment