GetUnrealizedProfitLoss() Calculates the unrealized PnL for the strategy position.
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
average fill price
Collapse
X
-
Hi Gaby,
your suggestions were very helpful, thank you!
Re Account Stats: I didn't get along with the Unrealised PnL and its returning Points for some reason, so this is my solution for anyone who may be interested:
if (Position.MarketPosition != MarketPosition.Flat){
//pnl=Math.Round(Position.GetUnrealizedProfitLoss(Pe rformanceUnit.Points, Close[0])/TickSize)*TickSize;//it works but somehow I struggled with it so did it a different way.
quant=Position.Quantity;
avgP=Math.Round(Position.AveragePrice/TickSize)*TickSize;
if (Position.MarketPosition != MarketPosition.Long){pnl=(Close[0]-avgP)/TickSize*5*quant;}
else if (Position.MarketPosition != MarketPosition.Short){pnl=(avgP-Close[0])/TickSize*5*quant;}
}
And with regard to the Dictionaries and Limit orders, this is what I did following your suggestions but it's not accurate and not updating with every order for some reason:
//Check for new Limit Orders
if(orderState==OrderState.Accepted || orderState==OrderState.Working){
//If it's a Buy Limit order, ,add it to the LongOrder Dictionary with its entry price
if(order.OrderType==OrderType.Limit && order.OrderAction==OrderAction.Buy){
if(!positionOrdersLong.ContainsKey(order.Name)){
positionOrdersLong.Add(order.Name, order);
size=positionOrdersLong.Count;Print("Size Long plus = "+size);//Print updated increased Dict Size
Print(string.Format( "{0} | Long Entries after addition: {1}", Time[0], string.Join(", ", positionOrdersLong.Select(x => String.Format("{0}: {1}", x.Key, x.Value.LimitPrice))) ));
}}
//If it's a sell Limit order, add it to the shortOrders Dictionary
if(order.OrderType==OrderType.Limit && order.OrderAction==OrderAction.Sell){
if(!positionOrdersShort.ContainsKey(order.Name)){
positionOrdersShort.Add(order.Name, order);
size=positionOrdersShort.Count;Print("Size Short plus = "+size);//Print updated increased Dict Size
Print(string.Format( "{0} | Short Entries after addition: {1}", Time[0], string.Join(", ", positionOrdersShort.Select(x => String.Format("{0}: {1}", x.Key, x.Value.LimitPrice))) ));
}}
}
//if an actual position gets opened (or cancelled or rejected)
else if(orderState==OrderState.Filled || orderState==OrderState.Cancelled || orderState==OrderState.Rejected){
//if it's a buy order, remove the corresponding BuyLimit order from the BuyOrders Dictionary
if(order.OrderAction==OrderAction.Buy){
if(order.FromEntrySignal != null && positionOrdersLong.ContainsKey(order.FromEntrySign al)){
positionOrdersLong.Remove(order.FromEntrySignal);
size=positionOrdersLong.Count;Print("Size Long minus = "+size);//Print updated reduced Dict Size
Print(string.Format( "{0} | Long entries after removal: {1}", Time[0], string.Join(", ", positionOrdersLong.Select(x => String.Format("{0}: {1}", x.Key, x.Value.LimitPrice))) ));
}}
//if it's a sell order,, remove the corresponding SellLimit order from the ShortOrders Dictionary
if(order.OrderAction==OrderAction.Sell){
if(order.FromEntrySignal != null && positionOrdersShort.ContainsKey(order.FromEntrySig nal)){
positionOrdersShort.Remove(order.FromEntrySignal);
size=positionOrdersShort.Count;Print("Size Short minus = "+size);//Print updated reduced Dict Size
Print(string.Format( "{0} | Short entries after removal: {1}", Time[0], string.Join(", ", positionOrdersShort.Select(x => String.Format("{0}: {1}", x.Key, x.Value.LimitPrice))) ));
}}
}
so for example, right now I had 5 open buylimits and 3 sell limits and now have 3 buylimits and 0 sell limits. but the only printouts i get is
Size Short plus = 4
28/06/2024 11:45:20 | Short Entries after addition: ShortPos1: 20126.5, ShortPos0: 20127, ShortPos3: 20123.5, ShortPos2: 20125.75
Size Long plus = 4
28/06/2024 11:47:01 | Long Entries after addition: LongPos2: 20120.75, LongPos3: 20121.5, LongPos4: 20123.25, LongPos1: 20124.25
and it hasn't updated for some time in the printout window... not sure what's happening
I think the removing of Dictionary itemms isn't working properly. I tried the same as with the adding "if it is a Limit order" but that didn't work either. Is this the correct way to recognise that an order has been filled and distinguish between buys and sells? I recon that's where the error may reside
Thanks
K
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
558 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
324 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
101 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
545 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
547 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment