-
Notifications
You must be signed in to change notification settings - Fork 704
Initial support for options trading #4825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Initial support for options trading #4825
Conversation
db2c68f
to
a9de7de
Compare
Sure!
How option selling is different from option buying, from futures selling, from futures buying, from XYZ selling or XYZ buying, etc, etc.?
And that's the right approach, because an option (future, XYZ, etc.) is a security (or "securitized" asset, feel free to track your real estate portfolio with PP).
That's because PP doesn't support short trades, which is a well-known limitation, with many people making attempts towards resolving it.
Each option is distinct security in PP because it's a distinct security in reality. And no, it doesn't make "aggregation and performance tracking difficult", it just needs to be done separately, at a different level.
For comparison, #1736 also tried to implement short trades support with adhoc transaction types. That didn't fly. Again, an attempt to implement much more generic and direly needed feature by introducing adhoc transaction types didn't fly, countered by a question "What do you do if you buy 1 security, sell 2, and buy 1 again?" Back to OPTION_BUY/OPTION_SELL, do you anticipate that someone wanting to add futures support will need to add FUTURES_BUY/FUTURES_SELL, someone wanting to support XYZ would need to add XYZ_BUY/XYZ_SELL, and given that number of derivatives/funky instruments is potentially unlimited, do you anticipate the potential need to add dozens of adhoc
Oh no, please don't remove anything ;-). Feel free to add a new visualization/analysis/whatever layer for options, but removing something behind people's back doesn't sound right.
Well, that only means that first change which would be added to PP related to options would be a simple and easy to review "Shown only options" / "Show only non-options" filter.
What do you mean "preventing"? It's enabling it. Anyone can take what's real - individual transactions and analyze them in any way they want. It's opposite, your change of "removing" something and enforcing rigid adhoc structure will prevent/complicate any strategy-level analysis beyond that adhoc one.
In the UI - sounds stellar. Shaking up (removing!) things from the data model for that sound questionable.
Please don't lose us anything. That's not acceptable ;-).
An option is security and tracking unrealized P&L for a security is simple: opening amount - current amount. That works for any security (options included), so again, please don't lose that for us.
I would suggest that users benefit from ability to track their transactions in general and generic way. On top of which any analysis can be implemented. Consequently, any change which limits them in recording and tracking what actually happens and enforcing limited and adhoc view is not for users' benefit.
Sorry, but again, the opposite - users requiring more specialized tracking for options beyond the "generic security" level, can implement a more specialized tool on top of existing PP data model. If such a tool implemented within PP - again, great/stellar. (If not, many people certainly implemented it outside, again interfacing with a generic "it's a security" data model.) |
Thanks @pfalcon for the feedback on the PR — I appreciate the thoughtful consideration. I’d like to open a broader discussion around how derivatives in general might be better supported in PortfolioPerformance. I fully understand and respect the architectural decisions in PP so far, and I hope this can be a collaborative exploration of the right path forward. Current Limitations with Derivative InstrumentsMost derivatives — including options, futures, and future options — introduce characteristics that differ fundamentally from standard securities:
These properties are difficult to model within PP's current structure, where:
Selling a Stock vs Selling a DerivativeThis distinction is critical for both risk modeling and portfolio visualization:
From a modeling standpoint:
This matters especially for premium-based strategies (e.g., selling puts for income), which behave more like income generation than capital gains — even though they rely on buy/sell mechanics. Challenges with Current PP ModelingIf I import a year’s worth of option or future trades into PP today:
Proposal for DiscussionIf derivatives are to be supported more fully in PP, a few potential directions could help:
Final ThoughtsI’m not advocating for a full redesign, but rather raising a modeling gap that is increasingly relevant for users who trade derivatives for income or hedging purposes. I’d love to hear your thoughts on whether derivatives support fits within the scope of PP, and if so, how you think it might be structured to balance flexibility with PP’s core model. Happy to rework my approach and contribute in whatever direction aligns best with the project. FYI |
Glad that you find it useful. And I'd like to note that I don't steer PP direction, everything here is just my personal opinion (but backed by contribution to PP over some time, and even more experimenting with it).
I would disagree with such formulation. I'd say, they differ somewhat, mostly adding more properties. (There's definitely more behavior behind them, but arguably, PP's task is capture results of that behavior == state of properties, not model the behavior).
They disappear from "platform", but not from broker statements, and arguably, PP's task is exactly to capture these statements and make available to user or further analytics in many ways.
I again would disagree. For most people, source of PP transaction is brokerage statements, and expiration is always mirrored in these statement, so for as long as they are properly imported into PP, this aspect of the behavior is not something to worry on the PP side.
Each instrument which had transactions is persistent (cannot be deleted). That's of course how it should be. But PP has concept of "inactive" instruments, which is perfect fit for expiring instruments.
As was mentioned previously, this is (largely) not underlying/derivative difference, but short vs long trade difference, with lack of short trades support being major limitation of PP, requiring addressing with priority.
As mentioned above, the most obvious way to deal with that is to set them "inactive". Such are not shown by default, but can be easily shown still (checking which previous option you sold before, at what strike, for how much premium would be very natural and regular tasks). How this is done (setting inactive) - is a separate matter, worth discussion.
But they are absolutely capital gains - you buy security, you sell it, or sell, then buy (expiration is nothing than selling/buying for 0). It's unrealistic to expect them treated otherwise - either by majority of participants or by tax codes. Here's important point - it's unrealistic to make PP do what each individual may want. And the approach of the contributors should be "let's implement something generic, aspects of which may suit many people, then hopefully one aspect of this generic solution may work for us, or if still not, then maybe it will be small change in personal fork on top of generic solution, without enforcing it on everyone". So again, it's unrealistic to treat capital gains from options as "income" (e.g. as dividends from stocks), because it's not. But if you instead formulate user story as "I want capital gains (fees, etc.) from options calculated separately from stocks", you get my full understanding. Actually, that's what I implemented (well, prorotyped) in my fork.
As discussed, there's no-nonsense archiving mechanism. PP has not bad filtering mechanisms, but they can be elaborated 5x times more for good. There's no "auto" in zeroing, and we'd need separate discussion on whether it's "better" (in some definition of "better") be done inside or outside PP (again, you get "auto" if you just properly import brokerage statements).
If you say "let's add "expirationDate" property to Security", you get my immediate +0.5. I'm sure we'll get there, eventually. However, I don't think adding that is any kind of first step of priority. To remind, you can add arbitrary custom fields to security in PP. So, you can add expiration date. What's next?
This is absolutely the highest priority issue in PP. I said "enough" to perfectionism ("it's not complete!") brushed up my 2-years old patches and posted #4832. All effort was made to make it a breeze for @buchen to merge. We'll see how it goes.
PP currently (to the best of my knowledge) supports 2 types of instruments: exchange [currency] pair and well, "everything else". Being an exchange pair is "calculated property". #4827 adds another calculated property for "being an option". An explicit type field is possible, but probably again not a priority, because a custom field can be added and seen where it goes from there.
It's definitely useful to consider what lies on a path for better derivative support in PP, and specifically, what lies on critical path (can't be maneuvered around). Short sale support is definitely such a thing. Pulling that together definitely would be helpful. An obvious problem area is test coverage. Last time I intended to post #4832 was winter holidays, I then faced #4481, got feedback, and there it waited another half a year. |
A few weeks ago I created a pull request to reintroduce the previously available support for options trading in Portfolio Performance versions prior to 0.63. With these changes it is possible to import option buy transactions with a price of 0$ again. They are handled as regular buy and sell transactions. You can find the details here:
My approach aligns more with the generic idea suggested by @pfalcon, rather than the direction described by you, @itshukychen.
I truly appreciate any discussion and effort aimed at improving or reinstating support for options and other derivative instruments. Some thoughts about our different approaches:
Some thoughts on your other ideas and requirements
In general I like the idea of adding additional meta data to a generic solution. This allows the usage of generic widgets and on top of it the creation of custom analysis widets or filters that allow more detailed analysis as you described. In conclusion I see the following things that should be adressed in PP:
@buchen and @Nirus2000 |
@starze :
I left some comments in that PR.
Right.
It's not a specialty in any way. Any security can be bought or sold for 0 amount. Lack of support of that in PP is an obvious issue. (But completely orthogonal to other issues, like lack of short trade support.)
It's pretty sad that you're not sure. Without different parties being sure of it, a lot of functionality in PP will remain locked out. You can project that to @buchen - just imagine that if he was interested in options, they would have been supported. But he's not interested in them and then "not sure". For the rest, you can project it on yourself - imagine that you maintain a complex open-source project with which you largely satisfied and somebody sent you a wall of code. I have some experience with maintaining open-source projects, and for me, the situation is all familiar - walls of code tend to wait a long time. That's why I'm trying to make simple patches which would have the greatest effect. Then, ideally, there would be positive feedback loop, where a maintainer would be able to easily process simple patches quickly, and contributors would be motivated to make next simple steps to build real complex functionality. Sadly, that approach doesn't seem to work with PP, where simple patches, complex patches - all wait long. I still don't think that bombarding maintainers with walls of code would help. Actually, assuming that model of what goes into PP and what isn't, is "random selection", it's only worse - because again, walls of code are hard to review, and that likely means that they full of adhoc assumptions and limitation. As an example, comment like #4647 (comment) shows that the case of long options was underthought, probably untested, and possibly doesn't work in reasonable way. |
Margin Requirements. What hasn't been mentioned by now is: When opening a short position the capital at risk isn't just the opened trade value, but in addition an intial margin set by the broker. In my cash account (not sure how this behaves in a margin account) that required margin cannot be used for other trades until you close that short position again. This aspect is different, when going short instead of long. I'd assume this aspect is crucial for calculating the total position size and RoR of trades. Margin requirements may change over time. Besides that there are some specifics to options - different forms of collateral depending on short side:
That being said, I think what collateral type is being used when the option gets exercised can be manually modelled in Portfolio Performance somehow with a combination of sell/buy/in-/outbound-delivery transactions. To start with I would be happy to see the short trades in the trades view and also under calculation, without getting an error (or being silently ignored, like it is in the calculation view). Would be cool, if others could add other securities and financial products into Portfolio Performance vía some sort of interface/plugin-mechanism. Would that be possible? And just because it was asked what further differences there are:
I would assume every asset class has it's own specifics in that matter. |
💡 Overview
This PR introduces initial support for option selling/buying tracking in PortfolioPerformance.
Previously, options imported from IB were treated as standard securities. While this accurately reflected the instrument itself, it had several limitations:
This PR addresses these issues by:
✅ Changes
OPTION_BUY
andOPTION_SELL
across the application (transactions, earnings, accounts, etc.).🔍 Background & Rationale
Before this change, each unique option (strike/expiry/type) was treated as its own security, cluttering the portfolio view and preventing strategy-level analysis.
This implementation instead groups transactions under the underlying instrument, making it possible to track P&L across strategies like SPX weekly puts or similar.
By doing so, we lose per-option price tracking, which is tied to the specific option contract. I consider this tradeoff acceptable, as:
❓ Open Questions
Option price tracking:
Currently, the underlying security’s price is used (if available), which adds helpful context.
→ Should we consider adding price tracking at the option level in the future?
Option metadata:
Right now, the full option ticker (e.g.
SPXW 250613P05605000
) is stored in the transaction comment.→ Would it make sense to model a dedicated
OptionTransaction
class with structured fields for metadata and future pricing support?Looking forward to feedback! Let me know if this direction aligns with project goals, or if any part of the implementation needs refinement.