Hi fellow quants!
Pretty basic question, but it's something I'm having a hard time with: How can I get the TotalUnrealizedProfit in percentage terms of my entire portfolio (not just of one security)? PS: I code in Python.
Thank you in advance,
Giulio
Adam W
self.Portfolio.TotalUnrealizedProfit / self.Portfolio.TotalPortfolioValue
GiulioC
Hi Adam,
thank you for your reply, but unfortunately it doesn't work with options. The reason is fairly simple: if I open a credit option spread (or, in general, any credit operation) I receive some cash, but self.Portfolio.TotalUnrealizedProfit just calculates the value that my open contracts have on the market, without considering the cash I received
Louis Szeto
Hi GiulioC
The cash received is already realized :)
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
GiulioC
Hi Loius,
yes sure, what I actually meant by “unrealized profit” is “total profit of the open trades” (the two things are always the same, except for credit trades). I actually did find a way to solve the problem, for anyone who might encounter my same issue. Note that it is not 100% precise, but it surely works.
Whenever self.Portfolio is not Invested, I set the variable “self.credit_cash” to 0. Every time I open a credit trade, “self.credit_cash” goes up by the amount of the credit received. Then when I want to calculate the profits of my open trades, I just go through every open position I have (with “[x for x in self.Portfolio.values() if self.Portfolio[x.Symbol].Invested]”) and, for every open trade I have, if I'm long, I add its current value to “self.credit_cash” (cause in order to close that trade, I receive money), if instead I'm short, I subtract it. After going through the whole list, I will have a pretty accurate measure of the profits of my open trades.
Hope to have helped someone with my same problem.
Giulio
GiulioC
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!