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;
}