I am trading vertical spreads in my algorithm. I need to calculate the profit loss of a spread but no matter what I do the spread pricing is absolutely wrong.
In the example below, there are many instances where the option.price, ask, bid etc come as ZERO for any of the legs and that destroys the profit calculation model.
Has anyone been able to calculate option pricing reliably?
public void CalculateProfitLoss(TradeInfo tradeInfo, Option longOption, Option shortOption, out decimal currentProfit, out decimal roi)
{
// Calculate mid prices with fallback to last price if mid price is invalid
decimal longMidPrice = GetValidMidPrice(longOption) ?? longOption.Price;
decimal shortMidPrice = GetValidMidPrice(shortOption) ?? shortOption.Price;
decimal spreadValue = longMidPrice - shortMidPrice;
// Adjust spreadValue calculation based on real market conditions
if (longMidPrice <= 0 || shortMidPrice <= 0 || spreadValue < 0)
{
spreadValue = Math.Max(0, longOption.Price - shortOption.Price);
}
// Calculate profit and ROI
currentProfit = (spreadValue - tradeInfo.EntryPrice) * 100 * tradeInfo.Quantity;
roi = currentProfit / (tradeInfo.EntryPrice * 100 * tradeInfo.Quantity);
// Safeguard for Put Spread: No loss if stock price is below short strike
if (tradeInfo.TradeType.Contains("Put") && longOption.Underlying.Price < tradeInfo.ShortStrike)
{
if (currentProfit < 0)
{
//LogHelper.LogWarning(_bot, $"Adjustment: Preventing loss for PUT spread when stock price {longOption.Underlying.Price} is below short strike {tradeInfo.ShortStrike}");
currentProfit = Math.Max(currentProfit, 0);
roi = Math.Max(roi, 0);
}
}
// Safeguard for Call Spread: No loss if stock price is above short strike
if (tradeInfo.TradeType.Contains("Call") && longOption.Underlying.Price > tradeInfo.ShortStrike)
{
if (currentProfit < 0)
{
//LogHelper.LogWarning(_bot, $"Adjustment: Preventing loss for CALL spread when stock price {longOption.Underlying.Price} is above short strike {tradeInfo.ShortStrike}");
currentProfit = Math.Max(currentProfit, 0);
roi = Math.Max(roi, 0);
}
}
// Log detailed information
//LogHelper.LogInfo(_bot, $"Calculated Profit and ROI for {tradeInfo.Symbol.Value}. LongStrike: {tradeInfo.LongStrike}, ShortStrike: {tradeInfo.ShortStrike}, SpreadValue: {spreadValue:C}, CurrentProfit: {currentProfit:C}, ROI: {roi:P}");
// Extra logging to help diagnose issues
//LogHelper.LogInfo(_bot, $"Stock Price: {longOption.Underlying.Price}, LongMidPrice: {longMidPrice:C}, ShortMidPrice: {shortMidPrice:C}, SpreadValue: {spreadValue:C}");
}
// Utility function to get a valid mid price
private decimal? GetValidMidPrice(Option option)
{
decimal midPrice = (option.BidPrice + option.AskPrice) / 2;
if (midPrice <= 0 || option.BidPrice == 0 || option.AskPrice == 0)
{
return null;
}
return midPrice;
}
Mia Alissi
It seems like you're having trouble with option prices coming as zero, which is affecting your profit calculation model. This could be due to the fact that the options are not liquid enough, and hence, the bid or ask prices are not available.
In QuantConnect, you can use the
self.Portfolio.TotalPortfolioValue
andself.Portfolio.TotalProfit
to get the specific profit and loss of your portfolio. More usage of portfolio properties can be found in the QuantConnect documentation.For calculating the profit of a vertical spread, you can refer to the following code snippet provided by a QuantConnect user:
This code calculates the profit for a bull call spread. You can adjust it according to your needs.
If you're trading vertical spreads and only one of the legs is getting filled, you might want to consider using the
OptionStrategies
helper class in QuantConnect. This class can help you handle vertical spreads more easily. You can refer to the QuantConnect documentation for its usage.Please note that the code examples provided are in Python, as QuantConnect supports both Python and C#.
Sources:
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.
Dee Znut
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!