Overall Statistics
Total Trades
180
Average Win
1.42%
Average Loss
-0.97%
Compounding Annual Return
59.169%
Drawdown
11.100%
Expectancy
0.558
Net Profit
59.372%
Sharpe Ratio
2.52
Loss Rate
37%
Win Rate
63%
Profit-Loss Ratio
1.46
Alpha
0.264
Beta
1.138
Annual Standard Deviation
0.155
Annual Variance
0.024
Information Ratio
2.169
Tracking Error
0.129
Treynor Ratio
0.344
Total Fees
$863.41
//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.
//Custom algorithm development: warrencharding@yahoo.com.
//Do not remove this copyright notice.
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
            			orderby stock.DollarVolume descending  
            			select stock.Symbol).Take(totalHighDollarVolumeStocks);
            });
        }

        public void OnData(TradeBars data)
        {
            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 = RSI(stockData.Ticker, rsiPeriod, MovingAverageType.Exponential, resolution);
            	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;
            
            Trade(data);
        }

		public void Trade(TradeBars data)
		{
            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 (sortedStocks.Exists(x=>x.Ticker==security.Symbol)==false)
                {
                    Liquidate(security.Symbol);
                }
            }
            foreach (var security in sortedStocks)
            {
            	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)
        {
            this.securityChanges = changes;
        }
    }
}