Overall Statistics
Total Trades
9
Average Win
28.59%
Average Loss
0%
Compounding Annual Return
879.220%
Drawdown
16.000%
Expectancy
0
Net Profit
169.964%
Sharpe Ratio
3.905
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
1.802
Beta
1.302
Annual Standard Deviation
0.516
Annual Variance
0.266
Information Ratio
3.747
Tracking Error
0.494
Treynor Ratio
1.548
Total Fees
$9.00
namespace QuantConnect.Algorithm.CSharp
{
    /// <summary>
    /// This example demonstrates how to add options for a given underlying equity security.
    /// It also shows how you can prefilter contracts easily based on strikes and expirations, and how you
    /// can inspect the option chain to pick a specific option contract to trade.
    /// </summary>
    public class BasicTemplateOptionsAlgorithm : QCAlgorithm
    {
        private const string UnderlyingTicker = "SPY";
        public readonly Symbol Underlying = QuantConnect.Symbol.Create(UnderlyingTicker, SecurityType.Equity, Market.USA);
        public readonly Symbol OptionSymbol = QuantConnect.Symbol.Create(UnderlyingTicker, SecurityType.Option, Market.USA);
        public DateTime Expiry;
        public string sym;
        

        public override void Initialize()
        {
            SetStartDate(2017, 10, 1);
            SetEndDate(DateTime.Now);
            SetCash(2800);

            var equity = AddEquity(UnderlyingTicker);
            var option = AddOption(UnderlyingTicker);

            // set our strike/expiry filter for this option chain
            option.SetFilter(u => u.Strikes(-2, +2)
                                   .Expiration(TimeSpan.FromDays(20), TimeSpan.FromDays(40)));

            // use the underlying equity as the benchmark
            SetBenchmark(equity.Symbol);
        }

        /// <summary>
        /// Event - v3.0 DATA EVENT HANDLER: (Pattern) Basic template for user to override for receiving all subscription data in a single event
        /// </summary>
        /// <param name="slice">The current slice of data keyed by symbol string</param>
        public override void OnData(Slice slice)
        {
            if (!Portfolio.Invested && IsMarketOpen(OptionSymbol))
            {
                OptionChain chain;
                if (slice.OptionChains.TryGetValue(OptionSymbol, out chain))
                {
                    // we find at the money (ATM) put contract with farthest expiration
                    var atmContract = chain
                        .OrderByDescending(x => x.Expiry)
                        .ThenBy(x => Math.Abs(chain.Underlying.Price - x.Strike))
                        .ThenByDescending(x => x.Right)
                        .FirstOrDefault();

                    if (atmContract != null)
                    {
                        // if found, trade it
                        SetHoldings(atmContract.Symbol, -.4m);
                        Expiry = atmContract.Expiry;
                        sym = atmContract.Symbol;
                        Log("Expiry " + Expiry);
                    }
                }
            }
            if (Portfolio.Invested && Expiry-Time <= TimeSpan.FromDays(2))
            {
            	SetHoldings(sym, 0);
            }
        }
    }
}