Hi,
Thanks a lot for the vertical spread tutorial in the option strategy section. Although the tutorial shows how to do a vertical spread order, it doesn't show how to stop loss or take profits and exit the open orders.
- Is there a straight way to calculate the vertical spread proft?
- Is there a straight way to calculate a bull put profit?
Link Liang
Hi Nick,
Here is my attempt based on our bull call spread tutorial. self.CheckProfit is called if we have invested in options, and the profit is calculated there. You could calculate profit for all vertical spreads in the same way. Here is more information about usage of option data, and here is an introduction to our Securities and Portfolio objects.
Hope it helps to implement your own strategy.
Coder
Hi Link,
Thanks for your code! It is very helpful. I have a few questions regarding the profit in the code above:
profit = (-buy.AskPrice + max(buy.Strike - self.Securities[buy.UnderlyingSymbol].Price, 0)) * 100 * self.Portfolio[buy.Symbol].Quantity +(-sell.BidPrice + max(sell.Strike - self.Securities[sell.UnderlyingSymbol].Price, 0)) * 100 * self.Portfolio[sell.Symbol].Quantity
1) I used the formula below to calculate the profits and the results are very different from the above. I can't exlain why?
profit2 = self.Portfolio[sell.Symbol].UnrealizedProfit + self.Portfolio[buy.Symbol].UnrealizedProfit
2) I assume this is the profits before exercise date (manually exercising). Will it be more accurate to use buy.FillPrice and sell.FillPrice?
3) Is the formula for theoretical profit or real profits?
Thanks!!!
Coder
Hi Link,
I have attached the backtest. I see something that I can't explain. It is obvious that profit2 is the Unralized profit = $258 (can be verified in the backtest). But as it can be seen equity is $600,348.00. We only had one vertical trade which is not exercised. How has the equity increased above original cash = $600,000? (Is that becasue of implied volatility?)
Thanks for your time
Link Liang
Hi Nick,
1) It is very different because it is calculated in a completely different way. When we calculate UnrealizedProfit, the algorithm actually simulates a self.Liquidate() instead of a manual exercising. For example, here is the order information of our initial purchases:
2017-05-01 09:31:00 Time: 5/1/2017 1:31:00 PM OrderID: 1 Symbol: GOOG 170616P00890000 Status: Filled Quantity: 1 FillPrice: 13.3 USD OrderFee: 1 USD 2017-05-01 09:31:00 Time: 5/1/2017 1:31:00 PM OrderID: 2 Symbol: GOOG 170616P00920000 Status: Filled Quantity: -1 FillPrice: 23.7 USD OrderFee: 1 USD
And if you try to Liquidate immediately after that, here are the log:
2017-05-01 09:32:00 Time: 5/1/2017 1:32:00 PM OrderID: 3 Symbol: GOOG 170616P00890000 Status: Filled Quantity: -1 FillPrice: 9.1 USD OrderFee: 1 USD 2017-05-01 09:32:00 Time: 5/1/2017 1:32:00 PM OrderID: 4 Symbol: GOOG 170616P00920000 Status: Filled Quantity: 1 FillPrice: 27.4 USD OrderFee: 1 USD
With some calculation (with consideration of order fee), you would find the actual profit after these transactions is the "profit2" - self.Portfolio.TotalUnrealizedProfit.
In fact, if we run this algorithm longer, those two calculations gets closer until reach the same value at the moment when options expire.
2) Yes you are right, we should use buy.FillPrice and sell.FillPrice. You could access them in OnOrderEvent(){https://www.quantconnect.com/docs/algorithm-reference/trading-and-orders#Trading-and-Orders-Order-Events}. However, by self.Buy() and self.Sell() we actually make MarketOrder which fills immediately, the fill price is buy.AskPrice and sell.BidPrice for buy and sell repectively.
3) It's theoretical profit. It calculates the profit if the options expire immediately and exersice.
4) The "total equity" you see is actually self.Portfolio.TotalPortfolioValue . It stands for the current market price of all of your holdings. As the price goes up, the total value of the two options we are holding also goes up and that's why you see the total holding value is not exactly the original cash in hand.
Coder
Hi Link, Thanks a lot for the detailed answers!!
Regarding item 4) Correct, but I expect the total portfolio value to be self.Portfolio.TotalPortfolioValue + self.Portfolio.UnrealizedProfit but in this case it doesn't add up to $600,258
Link Liang
Hi Nick,
The headers are from streaming packets sent from LEAN, they are not precise, and for display purposes only. If you're concerned about the specific profit and loss please use the self.Portfolio.TotalPortfolioValue and self.Portfolio.TotalProfit. More usage of portfolio properties can be found here. Thanks for your support!
Coder
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!