Overall Statistics
Total Trades
24
Average Win
0.15%
Average Loss
0%
Compounding Annual Return
5.347%
Drawdown
0.900%
Expectancy
0
Net Profit
1.780%
Sharpe Ratio
1.349
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.114
Beta
-3.567
Annual Standard Deviation
0.036
Annual Variance
0.001
Information Ratio
0.84
Tracking Error
0.036
Treynor Ratio
-0.014
Total Fees
$3.00
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;

namespace QuantConnect 
{
    public partial class QCUMartingalePositionSizing : QCAlgorithm 
    {
		string iSymbol = "MSFT";
		string iChart = "Deals";
		
		DateTime iTime;

        public override void Initialize()
        {
        	SetCash(10000);
            SetStartDate(2018, 1, 1);
            SetEndDate(DateTime.Now.Date); 
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);

			AddEquity(iSymbol, Resolution.Minute);

            var chart = new Chart(iChart);
			var seriesStock = new Series("Stock", SeriesType.Line, 0);

			chart.AddSeries(seriesStock);

            AddChart(chart);
        }
        
        public void OnData(TradeBars data) 
        {
        	if (IsMarketOpen(iSymbol) == false)
        	{
        		return;
        	}
        	
        	if (IsNewBar(TimeSpan.FromHours(1)) == false)
        	{
        		return;
        	}
        	
        	var price = Securities[iSymbol].Price;
        	
        	Plot(iChart, "Stock", price);

			if (Portfolio[iSymbol].Invested)
			{
				MarketOrder(iSymbol, -100);
			}
			
			if (Portfolio.Invested == false)
			{
				var contracts = OptionChainProvider.GetOptionContractList(iSymbol, Time);
				
				var otmCalls = 
					from c in contracts
                    where c.ID.OptionRight == OptionRight.Call
                    where c.ID.StrikePrice - price < 5 && c.ID.StrikePrice - price > 1
                    where (c.ID.Date - Time).TotalDays < 35 && (c.ID.Date - Time).TotalDays > 0
                    select c;
				
				var otmPuts = 
					from c in contracts
                    where c.ID.OptionRight == OptionRight.Put
                    where price - c.ID.StrikePrice < 5 && price - c.ID.StrikePrice > 1
                    where (c.ID.Date - Time).TotalDays < 35 && (c.ID.Date - Time).TotalDays > 0
                    select c;

                var contractCall = otmCalls
                	.OrderBy(o => o.ID.Date)
                	.ThenByDescending(o => o.ID.StrikePrice - price)
					.FirstOrDefault();
//Debug(Time + " : " + otmCalls.Count().ToString() + " : " + otmPuts.Count().ToString());
				var contractPut = otmPuts
                	.OrderBy(o => o.ID.Date)
                	.ThenByDescending(o => price - o.ID.StrikePrice)
					.FirstOrDefault();

				if (contractCall != null)
				{
	                AddOptionContract(contractCall, Resolution.Minute);
	                MarketOrder(contractCall, -1);
				}

				if (contractPut != null)
				{
	                AddOptionContract(contractPut, Resolution.Minute);
	                MarketOrder(contractPut, -1);
				}
			}
        }

        protected decimal GetBalance(bool equity = false) 
        {
        	var balance = 0m;

        	if (equity)
        	{
        		balance += Portfolio.TotalUnrealizedProfit;
        	}

        	return
        		balance + 
        		Portfolio.TotalProfit - 
        		Portfolio.TotalFees;
        }
        
		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;
		}
    }
}