Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
using System;
using System.Collections.Concurrent;
using System.Linq;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Indicators;

namespace QuantConnect
{
    public class EmaCrossUniverseSelectionAlgorithm : QCAlgorithm
    {
        const decimal Tolerance = 0.001m;
        private const int Count = 10;
        private const decimal TargetPercent = 0.1m;
        private SecurityChanges _changes = SecurityChanges.None;
        private readonly ConcurrentDictionary<Symbol, SelectionData> _averages = new ConcurrentDictionary<Symbol, SelectionData>();

        private class SelectionData
        {
            public readonly ExponentialMovingAverage Fast;

        	public SelectionData(QCAlgorithm algorithm, Symbol symbol)
            {
                Fast = new ExponentialMovingAverage(10);
                
            	algorithm.Log(String.Format("Initializing: {0}", symbol.Value));
                
	            IEnumerable<TradeBar> history = algorithm.History(symbol, 11, Resolution.Daily);
	            
            	algorithm.Log(String.Format("History Count: {0}", history.Count()));
	            
	            foreach (var tradeBar in history)
	            {
	            	//algorithm.Log(String.Format("Updating: {0}", symbol.Value));
	            	Fast.Update(tradeBar.EndTime, tradeBar.Close);
	            }
            }
            
            public bool Update(DateTime time, decimal value)
            {
                return Fast.Update(time, value);
            }
        }

        public override void Initialize()
        {
            UniverseSettings.Leverage = 2.0m;
            UniverseSettings.Resolution = Resolution.Daily;

            SetStartDate(2010, 01, 01);
            SetEndDate(2010, 04, 01);
            SetCash(1000*1000);
            
        	SetBrokerageModel(BrokerageName.TradierBrokerage);

            AddUniverse(coarse =>
            {
                return (from cf in coarse
                        let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData(this, cf.Symbol))
                        where avg.Update(cf.EndTime, cf.Price)
                        where avg.Fast > 0.0m
                        orderby avg.Fast ascending 
                        select cf.Symbol).Take(Count);
            });
        }

        public void OnData(TradeBars data)
        {
            if (_changes == SecurityChanges.None) return;

            foreach (var security in _changes.RemovedSecurities)
            {
                if (security.Invested)
                {
                    Liquidate(security.Symbol);
                }
            }

            foreach (var security in _changes.AddedSecurities)
            {
                SetHoldings(security.Symbol, TargetPercent);
            }
            
            _changes = SecurityChanges.None;
        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            _changes = changes;
        }
    }
}