Overall Statistics
Total Trades
209
Average Win
1.53%
Average Loss
-0.90%
Compounding Annual Return
45.198%
Drawdown
10.700%
Expectancy
0.423
Net Profit
45.346%
Sharpe Ratio
1.956
Loss Rate
47%
Win Rate
53%
Profit-Loss Ratio
1.69
Alpha
0.183
Beta
1.151
Annual Standard Deviation
0.162
Annual Variance
0.026
Information Ratio
1.471
Tracking Error
0.136
Treynor Ratio
0.276
Total Fees
$1025.63
//Copyright HardingSoftware.com, granted to the public domain.
//Use entirely at your own risk.
//This algorithm contains open source code from other sources,
//no claim is being made to such code.
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;

namespace QuantConnect.Algorithm.CSharp
{
    public class UniverseTemplate : QCAlgorithm
    {
        SecurityChanges securityChanges = SecurityChanges.None;
		List<StockData> highDollarVolumeStocks=new List<StockData>();
		int totalHighDollarVolumeStocks=5;
		int totalSortedStocks=1;
		int rsiPeriod=5;
		Resolution resolution=Resolution.Daily;

        public override void Initialize()
        {
            UniverseSettings.Resolution = resolution;

            SetStartDate(2016, 8, 30);
            SetEndDate(2017, 8, 30);
            SetCash(100000);

            AddUniverse(coarse =>
            {
            	return (from stock in coarse
            			//where stock.HasFundamentalData == false
            			orderby stock.DollarVolume descending  
            			select stock.Symbol).Take(totalHighDollarVolumeStocks);
            });
            
        }



        public void OnData(TradeBars data)
        {
        	foreach (StockData stockData in highDollarVolumeStocks)
        	{
        		if (data.ContainsKey(stockData.Ticker))
        		{
        			stockData.RsiIndicator.Update(Time, data[stockData.Ticker].Close);
        		}
        	}
        	
            var sortedStocksEnumerable = from x in highDollarVolumeStocks
            					where x.RsiIndicator.IsReady
            					orderby x.RsiIndicator descending
            					select x;
            					
            List<StockData> sortedStocks=sortedStocksEnumerable.Take(totalSortedStocks).ToList();
            foreach (var security in Portfolio.Values)
            {
            	if (Securities.ContainsKey(security.Symbol))
            	{
	                if (sortedStocks.Exists(x=>x.Ticker==security.Symbol)==false)
	                {
	                    Liquidate(security.Symbol);
	                }
            	}
            }
            foreach (var security in sortedStocks)
            {
            	if (Securities.ContainsKey(security.Ticker))
            	{
	            	if (Portfolio[security.Ticker].Invested==false)
	            	{
						SetHoldings(security.Ticker,1m/(decimal)sortedStocks.Count);
	            	}
            	}
            }
        }

		public class StockData
		{
			public string Ticker;
			public RelativeStrengthIndex RsiIndicator;
		}

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            securityChanges = changes;
            foreach (var security in securityChanges.RemovedSecurities)
            {
            	List<StockData> stockDatas=highDollarVolumeStocks.Where(x=>x.Ticker==security.Symbol).ToList();
            	if (stockDatas.Count>=1)
            	{
                	highDollarVolumeStocks.Remove(stockDatas.First());
            	}
            }
            foreach (var security in securityChanges.AddedSecurities)
            {
            	StockData stockData=new StockData();
            	stockData.Ticker=security.Symbol;
            	stockData.RsiIndicator = new RelativeStrengthIndex(rsiPeriod, MovingAverageType.Exponential);
            	var history = History(stockData.Ticker, rsiPeriod, resolution);
	            foreach (var tradeBar in history)
	            {
	            	stockData.RsiIndicator.Update(tradeBar.EndTime, tradeBar.Close);
	            }
                highDollarVolumeStocks.Add(stockData);
            }
            securityChanges = SecurityChanges.None;
        }
    }
}