Overall Statistics
using QuantConnect.Securities.Option;

namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// Options history for a given underlying at a specified date, time.
    /// </summary>
    public class BasicTemplateOptionsHistoryAlgorithm : QCAlgorithm
    {
        private const string UnderlyingTicker = "GLD";
        public readonly Symbol Underlying = QuantConnect.Symbol.Create(UnderlyingTicker, SecurityType.Equity, Market.USA);
        public readonly Symbol OptionSymbol = QuantConnect.Symbol.Create(UnderlyingTicker, SecurityType.Option, Market.USA);
        
        private DateTime selDateTime = DateTime.ParseExact("2011-09-21 15:35", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
        private bool printedInfo = false;
        
        public override void Initialize()
        {
            SetStartDate(2011, 9, 21);
            SetEndDate(2011, 9, 21);
            
            SetCash(10000);
            var equity = AddEquity(UnderlyingTicker);
            var option = AddOption(UnderlyingTicker);
            equity.SetDataNormalizationMode(DataNormalizationMode.Raw);
            
            option.PriceModel = OptionPriceModels.CrankNicolsonFD();
            option.EnableGreekApproximation = true;
            
            option.SetFilter(-10, +1, TimeSpan.FromDays(7), TimeSpan.FromDays(180));
        }
        
        
        public override void OnData(Slice slice)
        {
            if (!printedInfo && Time >= selDateTime)
            {
                foreach (var chain in slice.OptionChains)
                {
                    var underlying = Securities[chain.Key.Underlying];
                    foreach (var contract in chain.Value)
                    {
                        Log(String.Format(@"{0},Bid={1} Ask={2} Last={3} OI={4} σ={5:0.000} NPV={6:0.000} Δ={7:0.000} Γ={8:0.000} ν={9:0.000} ρ={10:0.00} Θ={11:0.00} IV={12:0.000}",
                             contract.Symbol.Value,
                             contract.BidPrice,
                             contract.AskPrice,
                             contract.LastPrice,
                             contract.OpenInterest,
                             underlying.VolatilityModel.Volatility,
                             contract.TheoreticalPrice,
                             contract.Greeks.Delta,
                             contract.Greeks.Gamma,
                             contract.Greeks.Vega,
                             contract.Greeks.Rho,
                             contract.Greeks.Theta / 365.0m,
                             contract.ImpliedVolatility));
                    }
                }
                
                printedInfo = true;
            }
        }
    }
}