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 System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using QuantConnect.Securities.Option;

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

		DateTime iTime;
        Option iOptionSource;
        Symbol iOptionSymbol;

        public override void Initialize()
        {
        	SetCash(10000);
            SetStartDate(DateTime.Now.Date.AddDays(-15));
            SetEndDate(DateTime.Now.Date); 
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);

			iOptionSource = AddOption(iSymbol, Resolution.Minute, Market.USA, true);
			iOptionSymbol = iOptionSource.Symbol;

			iOptionSource.SetFilter(u => u
				.IncludeWeeklys()
               	//.Strikes(-2, +2)
               	//.Expiration(TimeSpan.Zero, TimeSpan.FromDays(7))
               	//.Where(o => Math.Abs(u.Underlying.Price - o.ID.StrikePrice) < 50)
            );
        }
        
        public override void OnData(Slice slice) 
        {
        	if (IsNewBar(TimeSpan.FromHours(1)) == false)
        	{
        		return;
        	}

        	if (IsMarketOpen(iSymbol) == false)
        	{
        		return;
        	}

			if (Portfolio[iSymbol].Invested)
			{
				MarketOrder(iSymbol, -100);
			}
			
			if (Portfolio.Invested == false)
			{
				OptionChain chain;

	            if (slice.OptionChains.TryGetValue(iOptionSymbol, out chain))
	            {
	            	var date = iOptionSource.Underlying.LocalTime;
	            	var price = iOptionSource.Underlying.Price;
Debug(
chain.FirstOrDefault().Symbol + " : " + 
chain.LastOrDefault().Symbol
);
					var otmCalls = chain
	                    .Where(o => o.Right == OptionRight.Call)
	                    .Where(o => o.Strike - price < 5 && o.Strike - price > 2)
	                    .OrderBy(o => o.Expiry)
	                	.ThenByDescending(o => o.Strike - price);

					var otmPuts = chain
	                    .Where(o => o.Right == OptionRight.Put)
	                    .Where(o => price - o.Strike < 5 && price - o.Strike > 2)
	                    .OrderBy(o => o.Expiry)
	                	.ThenByDescending(o => price - o.Strike);
	
	                var contractCall = otmCalls.FirstOrDefault();
					var contractPut = otmPuts.FirstOrDefault();

					if (contractCall != null)
					{
		                MarketOrder(contractCall.Symbol, -1);
					}
	
					if (contractPut != null)
					{
		                MarketOrder(contractPut.Symbol, -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;
		}
    }
}