Overall Statistics
Total Trades
821
Average Win
0.86%
Average Loss
-0.55%
Compounding Annual Return
16.385%
Drawdown
13.700%
Expectancy
0.302
Net Profit
83.556%
Sharpe Ratio
0.876
Loss Rate
49%
Win Rate
51%
Profit-Loss Ratio
1.54
Alpha
0.126
Beta
0.09
Annual Standard Deviation
0.155
Annual Variance
0.024
Information Ratio
0.101
Tracking Error
0.208
Treynor Ratio
1.52
Total Fees
$11144.73
using System.Collections.Concurrent;

namespace QuantConnect
{
    /// <summary>
    /// In this algorithm we demonstrate how to perform some technical analysis as
    /// part of your coarse fundamental universe selection
    /// </summary>
    public class EmaCrossUniverseSelectionAlgorithm : QCAlgorithm
    {
        // tolerance to prevent bouncing
        const decimal Tolerance = 0.01m;
        private const int Count = 10;
        // use Buffer+Count to leave a little in cash
        private const decimal TargetPercent = 0.1m;
        private SecurityChanges _changes = SecurityChanges.None;
        // holds our coarse fundamental indicators by symbol
        private readonly ConcurrentDictionary<Symbol, SelectionData> _averages = new ConcurrentDictionary<Symbol, SelectionData>();


        // class used to improve readability of the coarse selection function
        private class SelectionData
        {
            public readonly ExponentialMovingAverage Fast;
            public readonly ExponentialMovingAverage Slow;
			public readonly Maximum Max;
			private decimal _value;
			
            public SelectionData()
            {
                Fast = new ExponentialMovingAverage(100);
                Slow = new ExponentialMovingAverage(300);
                Max = new Maximum(200);
            }

            // computes an object score of how much large the fast is than the slow
            // and current price is closer to the 200 day Maximum
            public decimal ScaledDelta
            {
                get 
                { 
                	var smaScore = (Fast - Slow)/((Fast + Slow)/2m);
                	var maxScore = (Max - _value)/((Max + _value)/2m);
                	return smaScore + maxScore; 
                }
            }

            // updates the EMA50 and EMA100 indicators, returning true when they're both ready
            public bool Update(DateTime time, decimal value)
            {
            	_value = value;
            	Max.Update(time, value);
                return Fast.Update(time, value) && Slow.Update(time, value);
            }
        }

        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            UniverseSettings.Leverage = 2.0m;
            UniverseSettings.Resolution = Resolution.Daily;

            SetStartDate(2010, 01, 01);
            SetEndDate(2014, 01, 01);
            SetCash(100*1000);

            AddUniverse(coarse =>
            {
                return (from cf in coarse
                        // grab th SelectionData instance for this symbol
                        let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData())
                        // Update returns true when the indicators are ready, so don't accept until they are
                        where avg.Update(cf.EndTime, cf.Price)
                        // only pick symbols who have their 50 day ema over their 100 day ema
                        where avg.Fast > avg.Slow*(1 + Tolerance)
                        // prefer symbols with a larger delta by percentage between the two averages
                        orderby avg.ScaledDelta descending 
                        // we only need to return the symbol and return 'Count' symbols
                        select cf.Symbol).Take(Count);
            }, FineSelectionFunction);
        }

		public IEnumerable<Symbol> FineSelectionFunction(
    		IEnumerable<FineFundamental> fine)
		{
    		// sort descending by P/E ratio
    		var sortedByPeRatio = fine
        		.OrderByDescending(x => x.ValuationRatios.PERatio);

		    // take the top entries from our sorted collection
    		var topFine = sortedByPeRatio.Take(2);

		    // we need to return only the symbol objects
    		return topFine.Select(x => x.Symbol);
		}

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars dictionary object keyed by symbol containing the stock data</param>
        public void OnData(TradeBars data)
        {
            if (_changes == SecurityChanges.None) return;

            // liquidate securities removed from our universe
            foreach (var security in _changes.RemovedSecurities)
            {
                if (security.Invested)
                {
                    Liquidate(security.Symbol);
                }
            }

            // we'll simply go long each security we added to the universe
            foreach (var security in _changes.AddedSecurities)
            {
                SetHoldings(security.Symbol, TargetPercent);
            }
        }

        /// <summary>
        /// Event fired each time the we add/remove securities from the data feed
        /// </summary>
        /// <param name="changes">Object containing AddedSecurities and RemovedSecurities</param>
        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            _changes = changes;
        }
    }
}