Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account? - Traders Hideout | futures io social day trading
futures io futures trading


Code example for displaying UNREALIZED PnL for ALL open positions for 1 Account?
Updated: Views / Replies:3,979 / 31
Created: by vincentmarchi Attachments:4

Welcome to futures io.

(If you already have an account, login at the top of the page)

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

Reply
 4  
 
Thread Tools Search this Thread
 

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

  #1 (permalink)
Elite Member
Dallas Texas United States
 
Futures Experience: Advanced
Platform: NinjaTrader
Favorite Futures: ES
 
vincentmarchi's Avatar
 
Posts: 9 since Feb 2013
Thanks: 5 given, 3 received

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

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

Reply With Quote
 
  #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

 
  #3 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received



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));

Reply With Quote
The following 4 users say Thank You to gregid for this post:
 
  #4 (permalink)
Elite Member
Dallas Texas United States
 
Futures Experience: Advanced
Platform: NinjaTrader
Favorite Futures: ES
 
vincentmarchi's Avatar
 
Posts: 9 since Feb 2013
Thanks: 5 given, 3 received

Thanks so much! But there is some issue with the calculation:

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()  );
Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).

Reply With Quote
 
  #5 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


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()  );
Please register on futures.io to view futures trading content such as post attachment(s), image(s), and screenshot(s).

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...

Reply With Quote
The following user says Thank You to gregid for this post:
 
  #6 (permalink)
Elite Member
San Jose, CA, USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, RCG/Continuum
Favorite Futures: GC,CL
 
Posts: 89 since Dec 2013
Thanks: 14 given, 89 received

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!

Reply With Quote
 
  #7 (permalink)
Elite Member
Dallas Texas United States
 
Futures Experience: Advanced
Platform: NinjaTrader
Favorite Futures: ES
 
vincentmarchi's Avatar
 
Posts: 9 since Feb 2013
Thanks: 5 given, 3 received

Still need to figure out a way to calculate the UNREALIZED profit 4 multiple instrum

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.

Reply With Quote
 
  #8 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received


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?

Reply With Quote
 
  #9 (permalink)
Elite Member
Dallas Texas United States
 
Futures Experience: Advanced
Platform: NinjaTrader
Favorite Futures: ES
 
vincentmarchi's Avatar
 
Posts: 9 since Feb 2013
Thanks: 5 given, 3 received


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!

Reply With Quote
 
  #10 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

Answer
This post has been selected as an answer to the original posters question Answer



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

Reply With Quote
The following user says Thank You to gregid for this post:

Reply



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

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible to track outside account positions in TWS? swinger IB Trader Workstation 1 February 18th, 2015 01:51 AM
Only Close Open Positions? davetapson AmiBroker 0 September 23rd, 2014 04:59 AM
Open Traders Positions Athan The Elite Circle 10 March 31st, 2013 01:29 AM
Displaying net PNL patbateman NinjaTrader 0 September 30th, 2012 09:02 PM
Account position: How to differentiate positions arjfca The Elite Circle 5 February 5th, 2011 03:27 PM


All times are GMT -4. The time now is 01:41 AM.

Copyright © 2017 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
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.
no new posts
Page generated 2017-12-13 in 0.13 seconds with 38 queries on phoenix via your IP 54.92.201.232