Overall Statistics
Total Trades
2
Average Win
17.05%
Average Loss
0%
Compounding Annual Return
-20.266%
Drawdown
3.700%
Expectancy
-1
Net Profit
-1.702%
Sharpe Ratio
0.369
Sortino Ratio
0.451
Probabilistic Sharpe Ratio
42.819%
Loss Rate
100%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.056
Beta
-0.575
Annual Standard Deviation
0.114
Annual Variance
0.013
Information Ratio
0.849
Tracking Error
0.252
Treynor Ratio
-0.074
Total Fees
$1.00
Estimated Strategy Capacity
$1300000.00
Lowest Capacity Asset
GOOCV W6HEW4GGEUZQ|GOOCV VP83T1ZUHROL
Portfolio Turnover
2.72%
#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.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 QuantConnect.Data.Custom.AlphaStreams;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion
namespace QuantConnect.Algorithm.CSharp
{
    public class CustomOptionAssignmentAlgorithm : QCAlgorithm
    {
        public override void Initialize()
        {
            SetStartDate(2015, 12, 1);
            SetEndDate(2015, 12, 28);
            SetCash(100000);
            SetSecurityInitializer(new MySecurityInitializer(BrokerageModel, new FuncSecuritySeeder(GetLastKnownPrices)));
            var option = AddOption("GOOG");
            option.SetFilter(universe => universe.CallsOnly().Strikes(-65, 0).Expiration(0, 30));
        }

        public override void OnData(Slice data)
        {
            if (Portfolio.Invested) return;

            foreach (var kvp in data.OptionChains)
            {
                var chain = kvp.Value;
                var minExpiry = chain.Select(x => x.Expiry).Min();
                var contracts = chain.Where(x => x.Expiry == minExpiry).OrderBy(x => x.Strike).ToList();

                if (contracts.Any())
                {
                    MarketOrder(contracts[0].Symbol, -1);
                }
            }
        }
    }

    public class MySecurityInitializer : BrokerageModelSecurityInitializer
    {
        public MySecurityInitializer(IBrokerageModel brokerageModel, ISecuritySeeder securitySeeder)
            : base(brokerageModel, securitySeeder)
        {
        }

        public override void Initialize(Security security)
        {
            base.Initialize(security);

            if (security.Type == SecurityType.Option)
            {
                (security as Option).SetOptionAssignmentModel(new MyOptionAssignmentModel());
            }
        }
    }

    public class MyOptionAssignmentModel : IOptionAssignmentModel
    {
        public OptionAssignmentResult GetAssignment(OptionAssignmentParameters parameters)
        {
            var option = parameters.Option;
            // Check if the contract is ITM
            if ((option.Right == OptionRight.Call && option.Underlying.Price > option.StrikePrice) ||
                (option.Right == OptionRight.Put && option.Underlying.Price < option.StrikePrice))
            {
                return new OptionAssignmentResult(option.Holdings.AbsoluteQuantity, "MyTag");
            }

            return OptionAssignmentResult.Null;
        }
    }
}