I'm converting a manual option strategy that trades spreads, and liquidates via take profit%, stop loss%, or at 'X' days before expiration.
To do so, I need to track the unrealized profit of the combined position (all legs in the spread), and have an easy way to look at the expiry of the spread (the leg expiring soonest).
I'm about to implement this logic, and write an execution/position manager, extend the OptionStrategy class, to track unrealized profit, among other things. The truth is, I'd rather not do all that if I don't have to :-)
Is there currently any easy way / convenience mechanism to do this in QC? Or is one in the works? Has anyone done this before?
Derek Melchin
Hi .ekz.,
To gather the unrealized profit of an OptionStrategy, we can use
unrealized_profit = sum([self.Portfolio[ticket.Symbol].UnrealizedProfit for ticket in self.order_tickets])
To determine the closest expiry of the contracts composing the OptionStrategy, we can use
expiry = min([self.Securities[ticket.Symbol].Expiry for ticket in self.order_tickets])
See the attached backtest for reference.
Best,
Derek Melchin
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.
.ekz.
This is great. Thank as always... will add to my Options spread management logic and share with the community soon!
.ekz.
Quick followup question...
In order to calculate the percentage (UnrealizedProfitPercent) for the spread, I believe I have to use the the initial cost for the spread to do the math (i can't simply sum the 'UnrealizedProfitPercent' for each leg ).
To do so, I am keeping a running total as each leg gets filled; doing this in an orderEvent handler.
cost_of_leg = orderEvent.FillPrice * ( -1 if (orderEvent.Direction == OrderDirection.Buy) else 1) net_cost = net_cost + cost_of_leg
Note: I flip the sign of the fill price (if i am selling a leg, it's minus).
Is there a better way to do this?
.ekz.
Scratch that --revisiting my calculations. Since it's a credit spread, I should be looking at percent of max profit earned so far, rather than unrealized profit percent.
All good here.
.ekz.
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!