Overall Statistics
Total Orders
9
Average Win
0%
Average Loss
-0.01%
Compounding Annual Return
-1.125%
Drawdown
0.200%
Expectancy
-1
Start Equity
10000000
End Equity
9990076
Net Profit
-0.099%
Sharpe Ratio
-2.204
Sortino Ratio
-1.988
Probabilistic Sharpe Ratio
16.122%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.012
Beta
0.03
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-0.514
Tracking Error
0.132
Treynor Ratio
-0.347
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
SPX 31
Portfolio Turnover
0.42%
#region imports
    using Newtonsoft.Json;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using QuantConnect.Algorithm.Framework;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Algorithm.Framework.Alphas;
    using QuantConnect.Algorithm.Framework.Portfolio;
    using QuantConnect.Algorithm.Framework.Portfolio.SignalExports;
    using QuantConnect.Algorithm.Framework.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Algorithm.Selection;
    using QuantConnect.Api;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Configuration;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Auxiliary;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.Data.Custom.IconicTypes;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.Shortable;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.OptionExercise;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Orders.TimeInForces;
    using QuantConnect.Python;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Index;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Positions;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;
    using QuantConnect.Securities.CryptoFuture;
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Securities.Volatility;
    using QuantConnect.Storage;
    using QuantConnect.Statistics;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion

namespace QuantConnect
{
    public class IndexOptionsUniverseAlgorithm : QCAlgorithm
    {
        private Symbol _index, _optionSymbol, _hedge;
        
        public override void Initialize()
        {
            SetStartDate(2021, 1, 1);
            SetEndDate(2021, 2, 1);
            SetCash(10000000);
            // Asynchronous can use computational resources efficiently
            UniverseSettings.Asynchronous = true;

            // Subscribe to the underlying for the underlying position
            // Set the data normalization mode to raw for strike price comparability
            _index = AddIndex("SPX").Symbol;
            // Requesting option data and filter for the hedge candidates
            var option = AddIndexOption(_index);
            _optionSymbol = option.Symbol;
            option.SetFilter(OptionFilter);

            // Set scheduled event to buy a hedge option contract at market open to eliminate the intra-day movement
            Schedule.On(
                DateRules.EveryDay(_index),
                TimeRules.AfterMarketOpen(_index, 1),
                BuyHedgeContract
            );

            // Set a scheduled event to sell the hedge contract before market close, since we want to earn from inter-day movement
            // Leave 2 minutes contingency to fill
            Schedule.On(
                DateRules.EveryDay(_index),
                TimeRules.BeforeMarketClose(_index, 2),
                SellHedgeContract
            );
        }

        private OptionFilterUniverse OptionFilter(OptionFilterUniverse universe)
        {
            // Select the contracts with delta very close to -1 and high open interest
            // This can effectively hedge most of the price change of the underlying and ensure the liquidity
            // Make sure the contract is expiring close for its tradbility
            return universe
                .IncludeWeeklys()
                .PutsOnly()
                .Expiration(2, 7)
                .Delta(-1m, -0.95m)
                .OpenInterest(10, 1000);
        }

        private void BuyHedgeContract()
        {
            if (CurrentSlice.OptionChains.TryGetValue(_optionSymbol, out var chain))
            {
                // Order the underlying if not hold, the order size should match the option contract
                // Order only if option chain data ready for hedging
                if (!Portfolio[_index].Invested)
                {
                    MarketOrder(_index, Securities[_optionSymbol].SymbolProperties.ContractMultiplier);
                }

                // Get the contract with delta closest to -1 (lowest possible delta)
                var contract = chain.MinBy(x => x.Greeks.Delta);
                _hedge = contract.Symbol;
                // Buy 1 deep ITM put with delta close to -1 to eliminate the intraday movement
                MarketOrder(_hedge, 1);
            }
        }

        private void SellHedgeContract()
        {
            // Check if any hedge contract position, if so, liquidate before market close to expose to underlying overnight movement
            if (_hedge != null)
            {
                Liquidate(_hedge);
                _hedge = null;
            }
        }
    }
}