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

namespace QuantConnect 
{ 
    public partial class QCUMartingalePositionSizing : QCAlgorithm 
    {
    	int iPeriod = 15;
        decimal iTP = 10.0m;
        decimal iSL = 5.0m;
        decimal iLeverage = 4m;
        decimal iVolume = 1m;
        string iSymbol = "MSFT";
        
        RelativeStrengthIndex iRsi = null;

        Dictionary<int, OrderTicket> iTickets = new Dictionary<int, OrderTicket>();
        Dictionary<int, OrderTicket> iLimits = new Dictionary<int, OrderTicket>();
        Dictionary<int, OrderTicket> iStops = new Dictionary<int, OrderTicket>();
        
        public override void Initialize()
        {
        	var resolution = Resolution.Daily;
        	
        	SetCash(25000);
        	SetBenchmark(iSymbol);
            SetStartDate(2017, 10, 1);
            SetEndDate(DateTime.Now.Date); 
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);
            AddSecurity(SecurityType.Equity, iSymbol, resolution, true, iLeverage, false);
            
            iRsi = RSI(iSymbol, iPeriod, MovingAverageType.Simple, resolution);
        }
        
        public void OnData(TradeBars data) 
        {
            if (CanOpen() == 1) 
            {
            	GoLong(iSymbol, iVolume);
                return;
            }
            
            if (CanClose() == 1) 
            {
                Liquidate();
                return;
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
        }

		protected OrderTicket GoLong(string symbol, decimal size)
		{
			var ticket = MarketOrder(symbol, size, false);

			iTickets[ticket.OrderId] = ticket;

			var process = new Thread(() => {

				Transactions.WaitForOrder(ticket.OrderId);

				if (Transactions.GetOrderById(ticket.OrderId).Status != OrderStatus.Filled) 
				{
					return;
				}

				iLimits.Select(o => Transactions.CancelOrder(o.Key));
				iStops.Select(o => Transactions.CancelOrder(o.Key));

				iLimits.Clear();
				iStops.Clear();

				var volume = Portfolio[symbol].Quantity;
				var price = Securities[symbol].Price;
				decimal priceDn = (decimal) (price - iSL);
				decimal priceUp = (decimal) (price + iTP);

Log("### Price : " + price + " vs SL : " + (priceDn) + " vs TP : " + (priceUp));
	        	var orderSL = StopMarketOrder(symbol, volume, priceDn, "SL #" + ticket.OrderId.ToString());
	        	var orderTP = StopMarketOrder(symbol, -volume, priceUp, "TP #" + ticket.OrderId.ToString());	

	        	iStops[orderSL.OrderId] = orderSL;
	        	iLimits[orderTP.OrderId] = orderTP;
			});

			process.Start();

			return ticket;
		}
        
        protected int CanOpen()
        {
            if (iRsi.IsReady) 
            {
                if (iRsi < 70 && iRsi > 50) 
                {
                	return -1;
                }
                
                if (iRsi > 30 && iRsi < 50)
                {
                	return 1;
                }
            }
            
            return 0;
        }
        
        protected int CanClose() 
        {
            return 0;
        }
    }
}