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 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.
    	//int atrPeriod = 15;
    	//decimal buyLowRatio = 0.97m;
    	//decimal sellHighRatio = 1.03m;
    	//decimal minimumAtrPercentage=0.015m;
    	
    	int atrPeriod = 15;
    	decimal buyLowRatio = 1m;
    	decimal sellHighRatio = 1.01m;
    	decimal minimumAtrPercentage=0.005m;
    	
    	//Be careful adjusting this next one, too high of a setting will result in unrealistically large
    	//purchases being made with no regards for slippage.
    	decimal ratioOfLastMinuteForMaxTrade =0.05m;
    	
    	//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 minutesToHoldBuyOrdersFor=10;
    	int minutesToHoldSellOrdersFor=10;
        
        Dictionary<string,AverageTrueRange> atrs=new Dictionary<string,AverageTrueRange>();
        Dictionary<string,OrderTicket> buyOrders=new Dictionary<string,OrderTicket>();
        Dictionary<string,int> buyOrderCounts = new Dictionary<string,int>();
        Dictionary<string,decimal> buyOrderPrices = new Dictionary<string,decimal>();
        Dictionary<string,OrderTicket> sellOrders=new Dictionary<string,OrderTicket>();
        Dictionary<string,int> sellOrderCounts = new Dictionary<string,int>();
        public override void Initialize()
        {
            SetStartDate(2016, 3, 1);
			SetEndDate(2016, 4, 1);
            SetCash(1000000);

			//volatile etf's
			//string tickersString="UVXY,XIV,NUGT,DUST,JNUG,JDUST,LABU,LABD,GUSH,DRIP,TVIX,GASL,GASX,DWTI,UWTI,DGAZ,UGAZ,UBIO,ZBIO,BRZU,RUSS,SCO,UCO,RUSL,ERY,ERX,BIOL,SVXY,VXX,SILJ,BIB,BIS,VIXY,SOXL,VIIX,SOXS,BZQ,USLV,SLVP,DSLV,GDXJ,GLDX";

			//volatile stocks
			//string tickersString="LGCY,CDRB,RPRX,BCEI,SXE,UNXL,LEI,ARGS,ALIM,MCEP,AREX,CWEI,DRYS,GBSN,ORIG,SRPT,DNR,CBAY,DRWI,ECR,RLYP,NADL,EVEP,SKYS,VRNG,PTCT,ERN,CLF,RESN,WG,EPE,BAS,GLF,SDRL,PLG,VYGR,HCLP,ANFI,GST,LEU,GSL,RXII,MEP,FCSC,CHMA,DSX,EMES,TDW,GNCA,CCXI,VSLR,JONE,TTPH,CNXR,CRMD,CARA,SALT,GRAM,SN,CBMX,MEMP,FMSA,CPXX,TROV,ETE,CLVS,OVAS,ENPH,OMEX,NGL,TMST,CHK,CLMT,ORPN,WLL,SM,HBM,GOL,YRD,BBG,BPT,SGYP,UNT,CNAT,RXDX,CLD,VRX,ATW,RBPAA,GLBL,PES,CIE,TKAI,AMID,AVGR,HLX,SKY,SDLP,INAP,AEGR";
			
			//68 biggest companies ranked by market cap.
			string tickersString="AAPL,GOOGL,GOOG,MSFT,XOM,BRK.A,BRK.B,FB,AMZN,JNJ,GE,WFC,T,NSRGY,CHL,JPM,RHHBY,PG,RDS.B,RDS.A,WMT,VZ,PFE,BUD,KO,BABA,CVX,TCEHY,SPY,NVS,V,DIS,HD,ORCL,TM,SSNLF,PM,MRK,BAC,PEP,CMCSA,NVO,INTC,IBM,CSCO,C,PTR,HSBC,UNH,MO,TSM,BMY,GILD,AMGN,TOT,SLB,RLNIY,MCD,MDT,CVS,MA,SNY,GSK,BTI,BP,LRLCY,MMM,IDEXY";
			
			//848 biggest, ranked by market cap
			//string tickersString="AAPL,GOOGL,GOOG,MSFT,XOM,BRK.A,BRK.B,FB,AMZN,JNJ,GE,WFC,T,NSRGY,CHL,JPM,RHHBY,PG,RDS.B,RDS.A,WMT,VZ,PFE,BUD,KO,BABA,CVX,TCEHY,SPY,NVS,V,DIS,HD,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ORCL,OI,PCAR,PH,PDCO,PAYX,PYPL,PNR,PBCT,PEP,PKI,PRGO,PFE,PCG,PM,PSX,PNW,PXD,PBI,PNC,RL,PPG,PPL,PX,CFG,PCLN,PFG,PG,PGR,PLD,PRU,PEG,PSA,PHM,PVH,QRVO,PWR,QCOM,DGX,RRC,RTN,O,RHT,REGN,RF,RSG,RAI,RHI,ROK,COL,ROP,ROST,RCL,R,CRM,SNDK,SCG,SLB,SNI,STX,SEE,SRE,SHW,SIG,SPG,SWKS,SLG,SJM,SNA,SO,LUV,SWN,SE,SPGI,STJ,SWK,SPLS,SBUX,HOT,STT,SRCL,SYK,STI,SYMC,SYF,SYY,TROW,TGT,TEL,TE,TGNA,TDC,TSO,TXN,TXT,HSY,TRV,TMO,TIF,TWX,TWC,TJX,TMK,TSS,TSCO,RIG,TRIP,FOXA,FOX,TSN,TYC,UDR,ULTA,USB,UA,UNP,UAL,UNH,UPS,URI,UTX,UHS,UNM,URBN,VFC,VLO,VAR,VTR,VRSN,VRSK,VZ,VRTX,VIAB,V,VNO,VMC,WMT,WBA,DIS,WM,WAT,ANTM,WFC,HCN,WDC,WU,WY,WHR,WFM,WMB,WLTW,WEC,WYN,WYNN,XEL,XRX,XLNX,XL,XYL,YHOO,YUM,ZBH,ZION,ZTS";
			
			//string tickersString=tickersString1+","+tickersString2;
			string[] tickers = tickersString.Split(new string[1] { "," }, StringSplitOptions.RemoveEmptyEntries);
			foreach (string ticker in tickers)
			{
				AddSecurity(SecurityType.Equity,ticker,Resolution.Minute);
			}
            foreach (Security s in Securities.Values)
            {
            	s.FeeModel=new CustomFeeModel();
            	atrs.Add(s.Symbol,ATR(s.Symbol,atrPeriod,MovingAverageType.Simple,Resolution.Minute));
            }
        }
		
        public void OnData(TradeBars data)
        {
			Buy(data);
			Sell(data);
        }
        
        public void Buy(TradeBars data)
        {
        	CancelBuyOrders();
        	int quantity = 0;
        	decimal minimumPurchase = 500m;
        	decimal maxTrade;
            OrderTicket orderTicket;
            decimal buyPrice;
    	    foreach (TradeBar bar in data.Values)
            {
            	if (Portfolio.Cash - SumBuyOrders(buyOrders,buyOrderPrices) < minimumPurchase)
                {
                    break;
                }
                if (!Portfolio[bar.Symbol].HoldStock)
                {
                    maxTrade = bar.Close * bar.Volume / ratioOfLastMinuteForMaxTrade;
                    quantity =(int)Math.Floor(Math.Min(Portfolio.Cash-SumBuyOrders(buyOrders,buyOrderPrices), maxTrade) / bar.Close);
                    if (quantity * bar.Close > minimumPurchase & quantity > 0)
                    {
                        if (atrs[bar.Symbol] / bar.Close > minimumAtrPercentage)
                        {
                        	if (buyOrders.ContainsKey(bar.Symbol) == false)
                        	{
                            	buyPrice=bar.Low * buyLowRatio;
                                orderTicket = LimitOrder(bar.Symbol, quantity,buyPrice);
                                buyOrders.Add(bar.Symbol,orderTicket);
                                buyOrderCounts.Add(bar.Symbol,0);
       							buyOrderPrices.Add(bar.Symbol,buyPrice);
                        	}
                        }
                    }
                }
            }
            foreach (string key in buyOrderCounts.Keys.ToList())
            {
                buyOrderCounts[key] = buyOrderCounts[key] + 1;
            }
        }
        
        public void Sell(TradeBars data)
        {
        	CancelSellOrders();
        	decimal sellPrice;
    	    TradeBar bar;
    	    OrderTicket orderTicket;
            foreach (SecurityHolding stock in Portfolio.Values)
            {
                if (Portfolio[stock.Symbol].Quantity > 0 & data.ContainsKey(stock.Symbol))
                {
                    bar = data[stock.Symbol];
                    if (sellOrders.ContainsKey(stock.Symbol)==false)
                    {
                        sellPrice = bar.High * sellHighRatio;
                        orderTicket = LimitOrder(stock.Symbol, -Portfolio[stock.Symbol].Quantity, sellPrice);
                        sellOrders.Add(stock.Symbol,orderTicket);
                        sellOrderCounts.Add(stock.Symbol,0);
                    }
                }
            }
            foreach (string key in sellOrderCounts.Keys.ToList())
            {
                sellOrderCounts[key] = sellOrderCounts[key] + 1;
            }
        }
        
        public void CancelBuyOrders()
        {
        	string[] symbols=buyOrders.Keys.ToArray();
        	foreach (string symbol in symbols)
        	{
			    if (buyOrderCounts[symbol]>minutesToHoldBuyOrdersFor)
	        	{
	        		buyOrders[symbol].Cancel();
	        		buyOrders.Remove(symbol);
	        		buyOrderCounts.Remove(symbol);
	        		buyOrderPrices.Remove(symbol);
	        	}
        	}
        	symbols=buyOrders.Keys.ToArray();
        	foreach (string symbol in symbols)
        	{
    		    if (buyOrders[symbol].Status == OrderStatus.Filled)
	            {
            		buyOrders.Remove(symbol);
            		buyOrderCounts.Remove(symbol);
            		buyOrderPrices.Remove(symbol);
	            }
        	}
        }
        
        public void CancelSellOrders()
        {
        	string[] symbols=sellOrders.Keys.ToArray();
        	foreach (string symbol in symbols)
        	{
			    if (sellOrderCounts[symbol]>minutesToHoldSellOrdersFor)
	        	{
	        		sellOrders[symbol].Cancel();
	        		sellOrders.Remove(symbol);
	        		sellOrderCounts.Remove(symbol);
	        	}
        	}
        	symbols=sellOrders.Keys.ToArray();
        	foreach (string symbol in symbols)
        	{
    		    if (sellOrders[symbol].Status == OrderStatus.Filled)
	            {
            		sellOrders.Remove(symbol);
            		sellOrderCounts.Remove(symbol);
	            }
        	}
        }
        
        public static decimal SumBuyOrders(Dictionary<string,OrderTicket> buyOrders, Dictionary<string,decimal> buyOrderPrices)
        {
        	decimal sum=0;
            foreach (string key in buyOrders.Keys.ToList())
            {
                sum += buyOrders[key].Quantity * buyOrderPrices[key];
            }
            return sum;
        }
       
        
    }
    
    public class CustomFeeModel : IFeeModel
    {
        public decimal GetOrderFee(Security security, Order order)
        {
            var fee = order.AbsoluteQuantity*0.01m;
            if (fee<5)
            {
            	fee=5;
            }
            if (fee>10)
            {
            	fee=10;
            }
            return fee;
        }
    }
}