Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
using QuantConnect.Securities.Option;

namespace QuantConnect 
{   
    /*
    *   Basic Template Algorithm
    *
    *   The underlying QCAlgorithm class has many methods which enable you to use QuantConnect.
    *   We have explained some of these here, but the full base class can be found at:
    *   https://github.com/QuantConnect/Lean/tree/master/Algorithm
    */
    public class BasicTemplateAlgorithm : QCAlgorithm
    {
    	public static String UnderlyingTicker = "AAPL"; //"MSFT"; //"SPY"; // "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);
    

        // Manual add symbols required in your initialize method:
		public override void Initialize() {
            SetCash(10000); 
            SetStartDate(2017, 1, 1);            SetEndDate(2017,1, 05); 

   			var equity = AddEquity(UnderlyingTicker, Resolution.Minute);
            var option = AddOption(UnderlyingTicker, Resolution.Minute);

		    //option.SetFilter(-2, +2, TimeSpan.Zero, TimeSpan.FromDays(10));
		}

		public override void OnData(Slice data) {
			// enough to do the below once a day
			if (this.Time.Hour != 9) return;
			if (this.Time.Minute != 55) return;
			
			
		    OptionChain chain;
		    if (data.OptionChains.TryGetValue(OptionSymbol, out chain))  {
		        // find the second call strike under market price expiring today
		        var contract = (
		            from optionContract in chain.OrderByDescending(x => x.Strike)
		            where optionContract.Right == OptionRight.Call
		            where optionContract.Expiry == Time.Date
		            where optionContract.Strike < chain.Underlying.Price
		            select optionContract
		            ).Skip(2).FirstOrDefault();
		 
		        if (contract != null) {
		            Log("Contract: " + contract.Symbol + "/" + contract.Expiry);
		        }
		        else	
		        	Log("No contract found at: " + this.Time);
		    }
		    else	
		    	Log("TryGetValue did not return any chain at: " + this.Time);
		    	Log("OptionChains: " + data.OptionChains.Count);
		}
    }
}