Overall Statistics
namespace QuantConnect 
{ 
    public partial class QCUMartingalePositionSizing : QCAlgorithm 
    {
    	int iPeriod = 15;
        //decimal iTP = 0.02m;
        //decimal iSL = 0.02m;
        decimal iLeverage = 4m;
        decimal iVolume = 1m;
        string iSymbol = "MSFT";
        
        RelativeStrengthIndex iRsi = null;
        Dictionary<int, Order> iOrders = new Dictionary<int, Order>();
        
        public override void Initialize()
        {
        	var resolution = Resolution.Minute;
        	
        	SetCash(25000);
            SetStartDate(2017, 1, 1);
            SetEndDate(2017, 5, 1); 
            SetBenchmark(iSymbol);
            SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage);
            AddSecurity(SecurityType.Equity, iSymbol, resolution, true, iLeverage, false);
            
            iRsi = RSI(iSymbol, iPeriod, MovingAverageType.Simple, resolution);
        }
        
        public void OnData(TradeBars data) 
        {
            var price = data[iSymbol].Close;
            
            if (CanOpen() == 1) 
            {
            	MarketOrder(iSymbol, iVolume);
            	LimitOrder(iSymbol, -iVolume, price + 1);
            	StopMarketOrder(iSymbol, -iVolume, price - 1);
                return;
            }
            
            if (CanClose() == 1) 
            {
                Liquidate();
                return;
            }
        }
        
        public override void OnOrderEvent(OrderEvent orderEvent)
        {
        	iOrders[orderEvent.OrderId] = Transactions.GetOrderById(orderEvent.OrderId);
        }
        
        public int CanOpen()
        {
            if (iRsi.IsReady && Portfolio.Invested == false) 
            {
                if (iRsi < 70 && iRsi > 50) 
                {
                	return -1;
                }
                
                if (iRsi > 30 && iRsi < 50)
                {
                	return 1;
                }
            }
            
            return 0;
        }
        
        public int CanClose() 
        {
            return 0;
        }
    }
}