Overall Statistics
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;

namespace QuantConnect 
{
    public partial class ShortStrangle : QCAlgorithm 
    {
		string iSymbol = "SPY";

		DateTime iTime;

        public override void Initialize()
        {
        	SetCash(100000);
            SetStartDate(2018, 1, 1);
            SetEndDate(DateTime.Now.Date); 
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);
			AddEquity(iSymbol, Resolution.Minute);
       }
        
        public void OnData(TradeBars data) 
        {
        	if (IsMarketOpen(iSymbol) == false)
        	{
        		return;
        	}
        	
        	if (IsNewBar(TimeSpan.FromHours(1)) == false)
        	{
        		return;
        	}
        	
        	var price = Securities[iSymbol].Price;
        	
			if (Portfolio[iSymbol].Invested)
			{
				Liquidate();
			}
			
			if (Portfolio.Invested == false)
			{
				var contracts = OptionChainProvider.GetOptionContractList(iSymbol, Time);
				
				var longPut = contracts
					.Where(c => c.ID.OptionRight == OptionRight.Put)
					.Where(c => price - c.ID.StrikePrice >= 0 && price - c.ID.StrikePrice <= 5)
                	.OrderBy(c => c.ID.Date)
                	.ThenBy(c => c.ID.StrikePrice)
                	.FirstOrDefault();
				
				var longCall = contracts
					.Where(c => c.ID.OptionRight == OptionRight.Call)
					.Where(c => c.ID.StrikePrice - price >= 0 && c.ID.StrikePrice - price <= 5)
                	.OrderBy(c => c.ID.Date)
					.ThenByDescending(c => c.ID.StrikePrice)
					.FirstOrDefault();
				
				var shortPut = contracts
					.Where(c => c.ID.OptionRight == OptionRight.Put)
					.Where(c => price - c.ID.StrikePrice > 5 && price - c.ID.StrikePrice <= 10)
                	.OrderBy(c => c.ID.Date)
					.ThenBy(c => c.ID.StrikePrice)
                	.FirstOrDefault();

				var shortCall = contracts
					.Where(c => c.ID.OptionRight == OptionRight.Call)
					.Where(c => c.ID.StrikePrice - price > 5 && c.ID.StrikePrice - price <= 10)
                	.OrderBy(c => c.ID.Date)
					.ThenByDescending(c => c.ID.StrikePrice)
                	.FirstOrDefault();
				
				if (longCall != null && longPut != null && shortCall != null && shortPut != null)
				{
	                AddOptionContract(longPut, Resolution.Minute);
	                AddOptionContract(longCall, Resolution.Minute);
	                AddOptionContract(shortPut, Resolution.Minute);
	                AddOptionContract(shortCall, Resolution.Minute);
	                MarketOrder(longPut, -1);
	                MarketOrder(longCall, -1);
	                MarketOrder(shortPut, -1);
	                MarketOrder(shortCall, -1);
				}
			}
        }

		public bool IsNewBar(TimeSpan interval, int points = 1)
		{
			var date = Securities[iSymbol].LocalTime;

			if ((date - iTime).TotalSeconds > interval.TotalSeconds * points)
			{
				iTime = new DateTime(date.Ticks - date.Ticks % interval.Ticks, date.Kind);
				return true;
			}

			return false;
		}
    }
}