Overall Statistics
Total Trades
359
Average Win
1.47%
Average Loss
-1.51%
Compounding Annual Return
12.739%
Drawdown
51.700%
Expectancy
0.276
Net Profit
81.704%
Sharpe Ratio
0.496
Loss Rate
35%
Win Rate
65%
Profit-Loss Ratio
0.97
Alpha
0.031
Beta
1.009
Annual Standard Deviation
0.273
Annual Variance
0.074
Information Ratio
0.131
Tracking Error
0.245
Treynor Ratio
0.134
Total Fees
$359.00
//Copyright Warren Harding 2016
//Use entirely at your own risk.
//Custom algorithm development: warrencharding@yahoo.com.
//Do not remove this copyright notice.

using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using QuantConnect.Data.Market;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.Slippage;
using QuantConnect.Securities;
namespace QuantConnect
{
    public class Algo4 : QCAlgorithm
    {
    	//You can adjust this first set to optimize.
    	decimal lowerBandRatio = 0.975m;
    	decimal upperBandRatio = 1.025m;
    	decimal rsiBuyCutoff=50;
    	decimal maximumTrade=1000000;
    	decimal minimumTrade = 500m;
    	int maPeriod=20;
    	int rsiPeriod=5;
    	//Your broker is going to hate you if you set these too low as it will result in large amounts of unfilled
    	//order cancellations. It bogs down LEAN as well.
    	int barsToHoldBuyOrdersFor=0;
    	int barsToHoldSellOrdersFor=0;
    	
    	Resolution resolution = Resolution.Daily;
    	
    	decimal ratioOfDollarVolumeForMaxTrade;

		List<StockData> stockDatas = new List<StockData>();
		List<OrderTicketWrapper> buyOrders=new List<OrderTicketWrapper>();
		List<OrderTicketWrapper> sellOrders=new List<OrderTicketWrapper>();
        
        public override void Initialize()
        {
            SetStartDate(2012, 1, 1);
			SetEndDate(2017, 1, 1);
            SetCash(100000);
            
			//s&p 500.
        	string tickersString ="CHK,DO,ENDP,MUR,SWN,FCX,RIG,NRG,MRO,NEM,RRC,CF,MU,APA,VRTX,FOSL,APC,WDC,DVN,MOS,FTR,MNK,VIAB,WYNN,UAL,FSLR,TSO,NFX,HP,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UX,I,NLST,WYY,FSAM,EVOK,SNMX,LUNA,DGAZ,PXLW,GEN,XRA,HEAR,NEON,PTLA,BIOD,TPB,ISNS,LEJU,FNBC,VSAR,ARLZ,SCON,LPCN,TBRA,NEOS,IMMU,CLD,AFMD,UGAZ,VYGR,XXII,AST,NOG,STRP,NVTA,ARGS,TRXC,OREX,HIIQ,SYMX,ORPN,PRKR,BRZU,ASM,ARWR,WLB,NURO,MGT,DNR,NK,GORO,BLDP,RETA,EXK,RTK,UEC,JONE,XBIT,CGIX,EGLT,EVRI,VNDA,UVXY,VUZI,GST,OVAS,TVIX,LODE,KGJI,CLNT,SQNS,NERV,CXRX,VTAE,HUSA,DGLY,HOS,CBR,EDIT,RYI,CVEO,EPZM,GALE,BREW,FSM,ALIM";
			
        	
			string[] tickers = tickersString.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string ticker in tickers)
			{
				AddSecurity(SecurityType.Equity,ticker,resolution);
				StockData stockData=new StockData(ticker,maPeriod,rsiPeriod);
				stockDatas.Add(stockData);
			}
            foreach (Security s in Securities.Values)
            {
            	s.FeeModel=new CustomFeeModel();
            }
        
           	//Be careful adjusting this next one, too high of a setting will result in unrealistically large
    		//purchases being made with no regards for slippage.    
        	if (resolution == Resolution.Daily)
	    	{
	    		ratioOfDollarVolumeForMaxTrade = 1m / 6.5m / 60m;
	    	}
	    	else if (resolution==Resolution.Minute)
	    	{
	    		ratioOfDollarVolumeForMaxTrade = .25m;
	    	}
	    	
        }
		
        public void OnData(TradeBars data)
        {
        	UpdateIndicators(data);
			Buy(data);
			Sell(data);
        }
        
        public void UpdateIndicators(TradeBars data)
        {
        	foreach (StockData stockData in stockDatas)	
        	{
        		if (data.ContainsKey(stockData.Ticker))
        		{
        			stockData.Update(data[stockData.Ticker].Time,data[stockData.Ticker].Close);
        		}
        	}
        }
        
        public void Buy(TradeBars data)
        {
            CancelExpiredOrders(buyOrders,barsToHoldBuyOrdersFor);
        	int quantity = 0;
            decimal buyPrice;
            TradeBar bar;
            string ticker;
            
            var stockDatas2 = from stockData in stockDatas
            				  where stockData.Change > 0.0m
            				  where (1 - stockData.AverageMinusCloseRatio) < lowerBandRatio
            				  where stockData.RSI>rsiBuyCutoff
            				  //orderby stockData.Change descending
            				  orderby stockData.Change descending
            				  select stockData;
            
    	    foreach (StockData stockData in stockDatas2)
            {
            	ticker=stockData.Ticker;
            	if (!Portfolio[ticker].HoldStock && data.ContainsKey(ticker) && stockData.EMA.IsReady)
            	{
            		bar=data[ticker];
            		buyPrice=stockData.EMA*lowerBandRatio;
            		if (bar.Close<buyPrice)
            		{
		            	quantity=SizePosition(bar);
		            	if (quantity > 0)
		            	{
		            		buyPrice=bar.Close;
		            		OrderTicketWrapper orderTicketWrapper=new OrderTicketWrapper();
		                    orderTicketWrapper.orderTicket = LimitOrder(bar.Symbol, quantity,buyPrice);
		                    orderTicketWrapper.price=buyPrice;
		                    buyOrders.Add(orderTicketWrapper);
		            	}
            		}
            	}
            }
            foreach (OrderTicketWrapper orderTickerWrapper in buyOrders)
            {
                orderTickerWrapper.count++;
            }
        }
        
        public void Sell(TradeBars data)
        {
        	CancelExpiredOrders(sellOrders,barsToHoldSellOrdersFor);
        	decimal sellPrice;
    	    TradeBar bar;
            string ticker;
            foreach (StockData stockData in stockDatas)
            {
            	ticker=stockData.Ticker;
                if (Portfolio[ticker].Quantity > 0 && data.ContainsKey(ticker) && stockData.EMA.IsReady)
                {
                    bar = data[ticker];
                	sellPrice = stockData.EMA * upperBandRatio;
                	if (bar.Close > sellPrice)
                	{
                    	sellPrice = bar.Close;
                    	OrderTicketWrapper orderTicketWrapper = new OrderTicketWrapper();
                        orderTicketWrapper.orderTicket = LimitOrder(ticker, -Portfolio[ticker].Quantity, sellPrice);
                        orderTicketWrapper.price=sellPrice;
                        sellOrders.Add(orderTicketWrapper);
                	}

                }
            }
            foreach (OrderTicketWrapper orderTickerWrapper in sellOrders)
            {
                orderTickerWrapper.count++;
            }
        }
        
        static void CancelExpiredOrders(List<OrderTicketWrapper> orderTickerWrappers,int barsToHoldOrdersFor)
        {
        	foreach (OrderTicketWrapper orderTickerWrapper in orderTickerWrappers)
        	{
			    if (orderTickerWrapper.count>barsToHoldOrdersFor)
	        	{
	        		orderTickerWrapper.orderTicket.Cancel();
	        	}
        	}
        	orderTickerWrappers.RemoveAll(x=>x.orderTicket.Status==OrderStatus.Filled | x.orderTicket.Status==OrderStatus.Canceled);
        }
        
        
        static decimal SumBuyOrders(List<OrderTicketWrapper> buyOrders)
        {
        	decimal sum=0;
            foreach (OrderTicketWrapper orderTickerWrapper in buyOrders)
            {
                sum += orderTickerWrapper.orderTicket.Quantity * orderTickerWrapper.price;
            }
            return sum;
        }
        
        int SizePosition(TradeBar bar)
        {
            decimal maxTrade=bar.Close*bar.Volume*ratioOfDollarVolumeForMaxTrade;
            if (maxTrade>maximumTrade)
            {
            	maxTrade=maximumTrade;
            }
            int quantity =(int)Math.Floor(Math.Min(Portfolio.Cash-SumBuyOrders(buyOrders), maxTrade) / bar.Close);
            quantity = RoundLot(quantity);
            if (quantity * bar.Close < minimumTrade)
        	{
        		return 0;
        	}
        	return quantity;
        }
        
        static int RoundLot(int inOddLotQuantity)
        {
            decimal inQuantity = (decimal)inOddLotQuantity;
            if (inQuantity > 2000000)
            {
                decimal small = inQuantity / 1000000;
                small = Math.Floor(small);
                return (int)(small * 1000000);
            }
            if (inQuantity > 200000)
            {
                decimal small = inQuantity / 100000;
                small = Math.Floor(small);
                return (int)(small * 100000);
            }
            if (inQuantity > 20000)
            {
                decimal small = inQuantity / 10000;
                small = Math.Floor(small);
                return (int)(small * 10000);
            }
            if (inQuantity > 2000)
            {
                decimal small = inQuantity / 1000;
                small = Math.Floor(small);
                return (int)(small * 1000);
            }
            if (inQuantity > 200)
            {
                decimal small = inQuantity / 100;
                small = Math.Floor(small);
                return (int)(small * 100);
            }
            if (inQuantity > 20)
            {
                decimal small = inQuantity / 10;
                small = Math.Floor(small);
                return (int)(small * 10);
            }
            return inOddLotQuantity;
        }
       
       	class OrderTicketWrapper
		{
			public OrderTicket orderTicket;
			public int count=0;
			public decimal price;
		}
       
        class StockData
        {
        	public StockData(string ticker,int maPeriod,int rsiPeriod)
        	{
	      		Ticker=ticker;
	            EMA = new ExponentialMovingAverage(maPeriod);
	            RSI = new RelativeStrengthIndex(rsiPeriod);
        	}
        	
        	public string Ticker;
            public ExponentialMovingAverage EMA;
            public RelativeStrengthIndex RSI;
			public decimal Price;
			public decimal AverageMinusCloseRatio;
			public decimal Change;
			public decimal PreviousValue=0;
			
            public bool Update(DateTime time, decimal value)
            {
            	bool ready=EMA.Update(time, value)  && RSI.Update(time, value);
            	if (ready)
            	{
            		AverageMinusCloseRatio=(EMA - value) / value;
           			Price=value;
           			if (PreviousValue!=0)
           			{
           				Change=(value-PreviousValue)/PreviousValue;
           			}
           			PreviousValue=value;
            	}
                return ready;
            }
        }
        
        public class CustomFeeModel : IFeeModel
	    {
	        public decimal GetOrderFee(Security security, Order order)
	        {
	        	return 1;
	        }
	    }
    }
}