Overall Statistics
Total Trades
644
Average Win
5.15%
Average Loss
-5.44%
Compounding Annual Return
8.701%
Drawdown
60.900%
Expectancy
0.173
Net Profit
569.120%
Sharpe Ratio
0.364
Probabilistic Sharpe Ratio
0.004%
Loss Rate
40%
Win Rate
60%
Profit-Loss Ratio
0.95
Alpha
0.084
Beta
0.234
Annual Standard Deviation
0.264
Annual Variance
0.07
Information Ratio
0.144
Tracking Error
0.29
Treynor Ratio
0.411
Total Fees
$4598.49
Estimated Strategy Capacity
$140000000.00
Lowest Capacity Asset
NVDA RHM8UTD8DT2D
//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.
//Do not remove this copyright notice.

#region imports
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Globalization;
    using System.Drawing;
    using QuantConnect;
    using QuantConnect.Algorithm.Framework;
    using QuantConnect.Algorithm.Framework.Selection;
    using QuantConnect.Algorithm.Framework.Alphas;
    using QuantConnect.Algorithm.Framework.Portfolio;
    using QuantConnect.Algorithm.Framework.Execution;
    using QuantConnect.Algorithm.Framework.Risk;
    using QuantConnect.Parameters;
    using QuantConnect.Benchmarks;
    using QuantConnect.Brokerages;
    using QuantConnect.Util;
    using QuantConnect.Interfaces;
    using QuantConnect.Algorithm;
    using QuantConnect.Indicators;
    using QuantConnect.Data;
    using QuantConnect.Data.Consolidators;
    using QuantConnect.Data.Custom;
    using QuantConnect.DataSource;
    using QuantConnect.Data.Fundamental;
    using QuantConnect.Data.Market;
    using QuantConnect.Data.UniverseSelection;
    using QuantConnect.Notifications;
    using QuantConnect.Orders;
    using QuantConnect.Orders.Fees;
    using QuantConnect.Orders.Fills;
    using QuantConnect.Orders.Slippage;
    using QuantConnect.Scheduling;
    using QuantConnect.Securities;
    using QuantConnect.Securities.Equity;
    using QuantConnect.Securities.Future;
    using QuantConnect.Securities.Option;
    using QuantConnect.Securities.Forex;
    using QuantConnect.Securities.Crypto;
    using QuantConnect.Securities.Interfaces;
    using QuantConnect.Storage;
    using QuantConnect.Data.Custom.AlphaStreams;
    using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
    using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
#endregion
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=10;
		int totalSortedStocks=1;
		Resolution resolution=Resolution.Daily;

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

            SetStartDate(2000, 1, 1);
            SetCash(10000);

            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)
        {
        	int period=3;
            foreach (StockData stockData in highDollarVolumeStocks)
            {
            	if (Securities.ContainsKey(stockData.Ticker))
            	{
	            	TradeBar[] bars=History(stockData.Ticker,period,resolution).ToArray();
	            	decimal[] closes=bars.Select(x=>x.Close).ToArray();
	            	if (bars!=null && bars.Length==period)
	            	{
		            	stockData.Changes1=ChangesPercent(closes);
		            	stockData.Changes2=Changes(stockData.Changes1);
		            	stockData.Fitness=-stockData.Changes1.Last();
		            	if (stockData.Changes2.Last()<0)
		            	{
		            		stockData.Fitness=-1000;
		            	}
	            	}
            	}
            	else
            	{
            		stockData.Fitness=-1000;
            	}
            }
            var sortedStocksEnumerable = from x in highDollarVolumeStocks
            					orderby x.Fitness descending
            					select x;
            					
            List<StockData> sortedStocks=sortedStocksEnumerable.Where(x=>x.Fitness>0m).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,0.99m/(decimal)sortedStocks.Count);
	            	}
            	}
            }
        }

		public class StockData
		{
			public string Ticker;
			public decimal[] Changes1;
        	public decimal[] Changes2;
        	public decimal Fitness;
		}

        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)
            {
                security.SetLeverage(10);
            	StockData stockData = new StockData();
            	stockData.Ticker = security.Symbol;
                highDollarVolumeStocks.Add(stockData);
            }
            securityChanges = SecurityChanges.None;
        }
        
        public static decimal[] Changes(decimal[] values)
        {
            List<decimal> lOut = new List<decimal>();
            for (int i = 0; i < values.Length - 1; i++)
            {
                lOut.Add(values[i + 1] - values[i]);
            }
            return lOut.ToArray();
        }

        public static decimal[] ChangesPercent(decimal[] values)
        {
            List<decimal> lOut = new List<decimal>();
            for (int i = 0; i < values.Length - 1; i++)
            {
                lOut.Add((values[i + 1] - values[i]) / values[i]);
            }
            return lOut.ToArray();
        }
    }
}