NexusFi: Find Your Edge


Home Menu

 





Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?


Discussion in Traders Hideout

Updated
      Top Posters
    1. looks_one ratfink with 9 posts (15 thanks)
    2. looks_two resist with 8 posts (10 thanks)
    3. looks_3 gregid with 5 posts (7 thanks)
    4. looks_4 vincentmarchi with 5 posts (0 thanks)
      Best Posters
    1. looks_one Tasker_182 with 4 thanks per post
    2. looks_two ratfink with 1.7 thanks per post
    3. looks_3 gregid with 1.4 thanks per post
    4. looks_4 resist with 1.3 thanks per post
    1. trending_up 21,310 views
    2. thumb_up 38 thanks given
    3. group 6 followers
    1. forum 32 posts
    2. attach_file 4 attachments




 
 

Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?

 
 
vincentmarchi's Avatar
 vincentmarchi 
Dallas Texas United States
 
Experience: Master
Platform: NinjaTrader
Trading: Emini ES, Crude CL, Gold GC, Currency Futures
Posts: 9 since Feb 2013

Really need a Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?

Follow me on Twitter Started this thread

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - April 2024
Feedback and Announcements
Are there any eval firms that allow you to sink to your …
Traders Hideout
Futures True Range Report
The Elite Circle
Deepmoney LLM
Elite Quantitative GenAI/LLM
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
 
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623



vincentmarchi View Post
Really need a Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?

I gather you are talking about NinjaTrader? If so then you can current unrealized PnL with:
 
Code
double curPosition = (double)Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
If you are looking to include all strategies' positions on the account try this:
 
Code
double allPositions = 0;
foreach (Position position in Account.Positions)
{
    allPositions += position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
}
or with LINQ:
 
Code
double allPositionsLinq = Account.Positions.Cast<Position>().Sum(position => position.GetProfitLoss(Close[0], PerformanceUnit.Currency));

 
 
vincentmarchi's Avatar
 vincentmarchi 
Dallas Texas United States
 
Experience: Master
Platform: NinjaTrader
Trading: Emini ES, Crude CL, Gold GC, Currency Futures
Posts: 9 since Feb 2013

I am trying to get the UNREALIZED PnL of all instruments that have an open trade for instance - I have 6E open trade that at this current tick is at +$100 and I also have ES open at -$75 then I want to show the currentTotalUnrealizedPnL = +$25

Your second code snippet looks almost like what I need except that the calculation is going way off when I have a second position open in another instrument please see me screenshot below of the output of the code. This code:

 
Code
double allPositions = 0;
foreach (Position position in Account.Positions)
{
    allPositions += position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
	
}
		
Print("TotalUnrealized: " +allPositions.ToString()  );

Follow me on Twitter Started this thread
 
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623


vincentmarchi View Post
I am trying to get the UNREALIZED PnL of all instruments that have an open trade for instance - I have 6E open trade that at this current tick is at +$100 and I also have ES open at -$75 then I want to show the currentTotalUnrealizedPnL = +$25

Your second code snippet looks almost like what I need except that the calculation is going way off when I have a second position open in another instrument please see me screenshot below of the output of the code. This code:

 
Code
double allPositions = 0;
foreach (Position position in Account.Positions)
{
    allPositions += position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
	
}
		
Print("TotalUnrealized: " +allPositions.ToString()  );

The problem seems to be that it takes the Close[0] from current strategy (and its instrument) and applies the calculation to other instruments. For this to work you would have to provide the right close from right instrument for each of positions. At the moment I see no other way than by using multi instrument strategy with multiple bar series. I'll try to think if there is any alternative option tomorrow as I am now getting close to horizontal...

Thanked by:
 
 bltdavid 
San Jose, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader
Posts: 112 since Dec 2013
Thanks Given: 35
Thanks Received: 103

I was just looking at ChartTraderCustomBehavior and there is probably code in there to get you started at the lower level you seek.

It's an Elite-only download.

Search for OnBarUpdate() look for the commented out code where he thanks Bertrand ... perhaps you can modify it to do what you're asking for.

Good luck!

 
 
vincentmarchi's Avatar
 vincentmarchi 
Dallas Texas United States
 
Experience: Master
Platform: NinjaTrader
Trading: Emini ES, Crude CL, Gold GC, Currency Futures
Posts: 9 since Feb 2013

Still need to figure out a way to calculate the UNREALIZED profit 4 multiple instruments... any ideas or directions would be appreciated. I've hunted and hunted and tried alot of my own code, but I can't seem to get this. I must be missing something. Can someone please assist or if there is anyone else who has already done this, please let me know. Thanks so much! The basic idea is to have all trades close out when the total PnL is > $1,000 automatically so I don't have to calculate in my head when I have 4-5 trades open at once across multiple instruments and wont miss the opportunity to take the profit.

Follow me on Twitter Started this thread
 
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623


vincentmarchi View Post
Still need to figure out a way to calculate the UNREALIZED profit 4 multiple instruments... any ideas or directions would be appreciated. I've hunted and hunted and tried alot of my own code, but I can't seem to get this. I must be missing something. Can someone please assist or if there is anyone else who has already done this, please let me know. Thanks so much! The basic idea is to have all trades close out when the total PnL is > $1,000 automatically so I don't have to calculate in my head when I have 4-5 trades open at once across multiple instruments and wont miss the opportunity to take the profit.

have you tried adding multiple instruments as suggested in my second post?

 
 
vincentmarchi's Avatar
 vincentmarchi 
Dallas Texas United States
 
Experience: Master
Platform: NinjaTrader
Trading: Emini ES, Crude CL, Gold GC, Currency Futures
Posts: 9 since Feb 2013


gregid View Post
have you tried adding multiple instruments as suggested in my second post?

Oh so like, add all 7 instruments I'm trading to a single chart and add the strategy to that chart and try to pull each unrealized PnL separately and sum them in a variable? I will try to work on that today to see how that works. I was not aware I could add a bunch of instruments to a chart. Figured that would look really weird. heh!

Follow me on Twitter Started this thread
 
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623



vincentmarchi View Post
Oh so like, add all 7 instruments I'm trading to a single chart and add the strategy to that chart and try to pull each unrealized PnL separately and sum them in a variable? I will try to work on that today to see how that works. I was not aware I could add a bunch of instruments to a chart. Figured that would look really weird. heh!

The problem is in this line:
 
Code
allPositions += position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
What you need to do is provide the right price (Close[0]) for the instrument.
So in your code in the loop you will do something like (pseudocode):
 
Code
double currentPrice = 0;
if (position.Instrument.Name == "ES") currentPrice = Closes[0][0];
if (position.Instrument.Name == "6E") currentPrice = Closes[1][0];
if (position.Instrument.Name == "6A") currentPrice = Closes[2][0];

allPositions += position.GetProfitLoss(currentPrice, PerformanceUnit.Currency);
Search for multiinstrument strategies or indicators to learn how to add secondary series - these will be invisible, ie not visible on the chart so you don't need to worry about weird looking chart

Thanked by:

 



Last Updated on March 21, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts