Overall Statistics
Total Orders
6
Average Win
0.59%
Average Loss
-2.87%
Compounding Annual Return
-24.373%
Drawdown
6.900%
Expectancy
-0.397
Net Profit
-4.537%
Sharpe Ratio
-2.13
Sortino Ratio
-1.796
Probabilistic Sharpe Ratio
5.538%
Loss Rate
50%
Win Rate
50%
Profit-Loss Ratio
0.21
Alpha
-0.196
Beta
0.051
Annual Standard Deviation
0.083
Annual Variance
0.007
Information Ratio
-2.758
Tracking Error
0.197
Treynor Ratio
-3.48
Total Fees
$4.00
Estimated Strategy Capacity
$6000.00
Lowest Capacity Asset
GOOCV XG8PSNPNECFA|GOOCV VP83T1ZUHROL
Portfolio Turnover
10.07%
#region imports
    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.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Algorithm.Selection;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Equity;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;   
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Storage;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion


using QuantConnect.DataSource;

namespace QuantConnect
{
    public class USEquityOptionsDataAlgorithm : QCAlgorithm
    {
        private Symbol _underlying;
        private Symbol _optionSymbol;
        private OptionContract? _contract = null;
        
        public override void Initialize()
        {
            SetStartDate(2020, 6, 1);
            SetEndDate(2020, 8, 1);
            SetCash(100000);
            UniverseSettings.Asynchronous = true;
            // Requesting data
            _underlying = AddEquity("GOOG").Symbol;
            var option = AddOption("GOOG");
            _optionSymbol = option.Symbol;
            // Set our strike/expiry filter for this option chain
            option.SetFilter(-2, +2, 0, 7);
        }

        public override void OnData(Slice slice)
        {
            if (Portfolio[_underlying].Invested)
            {
                Liquidate(_underlying);
            }
            
            if (_contract != null && Portfolio[_contract.Symbol].Invested)
            {
                return;
            }
                
                
            var chain = slice.OptionChains.get(_optionSymbol);
            if (chain != null)
            {
                // Select call contracts
                var calls = chain.Where(x => x.Right == OptionRight.Call).ToList();
                if (calls.Count() == 0)
                {
                    return;
                }
                
                // Select the call contracts with the furthest expiration
                var furthestExpiry = calls.OrderByDescending(x => x.Expiry).First().Expiry;
                var furthestExpiryCalls = calls.Where(x => x.Expiry == furthestExpiry).ToList();
                
                // From the remaining contracts, select the one with its strike closest to the underlying price
                var contract = furthestExpiryCalls.OrderByDescending(x => Math.Abs(chain.Underlying.Price - x.Strike)).Last();
                
                _contract = contract;
                MarketOrder(contract.Symbol, 1);
            }
        }
        
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            foreach (var security in changes.AddedSecurities)
            {
                // Historical data
                var history = History(security.Symbol, 100, Resolution.Minute);
                Debug($"We got {history.Count()} from our history request for {security.Symbol}");
            }
        }
    }
}