Overall Statistics
//Copyright Warren Harding 2016, granted to the public domain.
//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.9m;
    	decimal upperBandRatio = 1.1m;
    	decimal rsiBuyCutoff=50;
    	decimal maximumTrade=25000;
    	decimal minimumTrade = 500m;
    	int maPeriod=60;
    	int rsiPeriod=3;
    	//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(2011, 6, 30);
			SetEndDate(2016, 9, 30);
            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,HES,NAVI,AAL,SIG,ALXN,STX,SWKS,MPC,NOV,URI,NBL,QRVO,WMB,HOG,JWN,DAL,OI,PXD,OKE,MYL,NVDA,RCL,CXO,TGNA,COG,XEC,HPE,ETFC,EOG,KSS,GPS,REGN,HRB,HAL,AVGO,PRGO,BHI,UA,M,ADS,CSC,URBN,KMI,AA,COP,PYPL,KORS,LUV,EQT,VLO,FTI,FMC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,CVX,FDX,TWX,CHRW,XLNX,CA,RHI,PBCT,BAX,PNW,ES,T,EMR,CI,AMGN,DNB,TMO,DRI,BBT,CAT,ROK,MHK,WBA,TXT,ED,PSX,CME,ROST,PH,EIX,TSN,A,TGT,ZBH,HOT,PCG,AEP,LOW,TROW,PPG,PCLN,D,SHW,STZ,XL,CAG,DVA,XOM,MTB,HSIC,ABBV,BA,ADBE,ROP,ITW,HRS,PX,VZ,MRK,LH,PG,VAR,PM,BCR,L,SYY,LLY,HSY,SJM,TEL,DFS,MSFT,BDX,WU,FB,CSCO,UTX,AIZ,APD,NDAQ,DE,SO,JPM,NLSN,AET,OMC,FLIR,ORCL,UNH,FISV,IBM,EXPD,HD,AMZN,DHR,CMCSA,DOW,EFX,ISRG,LLL,FIS,MO,HON,HUM,COST,INTU,PAYX,SBUX,MKC,COL,CHD,CTAS,ECL,ZTS,AIG,PFE,AVY,PGR,USB,AME,BLL,APH,K,CVS,SNA,CINF,YUM,GD,TJX,EL,MDT,ORLY,WMT,DPS,ADP,MA,GE,GLW,ICE,GOOGL,CLX,SWK,KMB,GOOG,AFL,TRV,AON,RSG,VRSK,BRK-B,IFF,MSI,RTN,DIS,MMC,LMT,KO,STJ,TMK,V,CL,NOC,PEP,MCD,MMM,WM,ALL,UPS,JNJ,LLTC";
        	
			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 = .25m / 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);
        			stockData.Price=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.RSI<rsiBuyCutoff
            				  orderby stockData.EmaMinusPriceRatio() 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,MovingAverageType.Exponential);
        	}
        	
        	public string Ticker;
            public ExponentialMovingAverage EMA;
			public RelativeStrengthIndex RSI;
			public decimal Price;
			
            public bool Update(DateTime time, decimal value)
            {
                return EMA.Update(time, value) && RSI.Update(time, value);
            }
            
            public decimal EmaMinusPriceRatio()
            {
            	if (Price!=0)
            	{
            		return (EMA-Price)/Price;
            	}
            	else
            	{
            		return 0;
            	}
            }
        }
        
        public class CustomFeeModel : IFeeModel
	    {
	        public decimal GetOrderFee(Security security, Order order)
	        {
	            decimal fee = order.AbsoluteQuantity*0.01m;
	            if (fee<5)
	            {
	            	fee=5;
	            }
	            if (fee>10)
	            {
	            	fee=10;
	            }
	            return fee;
	        }
	    }
    }
}