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
{
    /// <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.001m;
        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 SelectionData(QCAlgorithm algorithm, string symbol)
            {
                Fast = new ExponentialMovingAverage(10);
                
            	algorithm.Log(String.Format("Initializing: {0}", symbol));
                
	            var history = algorithm.History(symbol, 11);
	            foreach (var tradeBar in history)
	            {
	            	algorithm.Log(String.Format("Updating: {0}", symbol));
	            	Fast.Update(tradeBar.EndTime, tradeBar.Close);
	            }
            }

            // updates the fast and slow indicators, returning true when they're both ready
            public bool Update(DateTime time, decimal value)
            {
                return Fast.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(2010, 04, 01);
            SetCash(1000*1000);
            
        	SetBrokerageModel(BrokerageName.TradierBrokerage);
            SetSecurityInitializer(new CustomSecurityInitializer(BrokerageModel, DataNormalizationMode.Raw));

            AddUniverse(coarse =>
            {
                return (from cf in coarse
                        // grab th SelectionData instance for this symbol
                        let avg = _averages.GetOrAdd(cf.Symbol, sym => new SelectionData(this, cf.Symbol))
                        // 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 fast ema over their slow ema
                        where avg.Fast > 0.0m
                        // prefer symbols with a larger delta by percentage between the two averages
                        orderby avg.Fast ascending 
                        // we only need to return the symbol and return 'Count' symbols
                        select cf.Symbol).Take(Count);
            });
        }

        /// <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;
        }
    }
}
using QuantConnect.Orders.Slippage;

namespace QuantConnect
{
    class CustomSecurityInitializer : BrokerageModelSecurityInitializer
    {
        private readonly DataNormalizationMode _dataNormalizationMode;

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomSecurityInitializer"/> class
        /// with the specified normalization mode
        /// </summary>
        /// <param name="brokerageModel">The brokerage model used to get fill/fee/slippage/settlement models</param>
        /// <param name="dataNormalizationMode">The desired data normalization mode</param>
        public CustomSecurityInitializer(IBrokerageModel brokerageModel, DataNormalizationMode dataNormalizationMode)
            : base(brokerageModel)
        {
            _dataNormalizationMode = dataNormalizationMode;
        }

        /// <summary>
        /// Initializes the specified security by setting up the models
        /// </summary>
        /// <param name="security">The security to be initialized</param>
        public override void Initialize(Security security)
        {
            // first call the default implementation
            base.Initialize(security);

            // now apply our data normalization mode
            security.SetDataNormalizationMode(_dataNormalizationMode);
            security.SlippageModel = new ConstantSlippageModel(0.001m);
        }
    }
}