Overall Statistics
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.UniverseSelection;
using System;

namespace QuantConnect.Algorithm.CSharp
{
    public class StocksOnTheMove : QCAlgorithm
    {
        private Symbol spy = QuantConnect.Symbol.Create("SPY", SecurityType.Equity, Market.USA);
        private const int NumberOfSymbolsCoarse = 700;
        private const int NumberOfSymbolsFine = 2;
        private List<Symbol> stocks = new List<QuantConnect.Symbol>();

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

            SetStartDate(2012, 01, 01);
            SetEndDate(2012, 12, 31);
            SetCash(20000);

            AddEquity(spy.ID.Symbol, Resolution.Minute);

            AddUniverse(CoarseSelectionFunction, FineSelectionFunction);

            Schedule.On(DateRules.Every(DayOfWeek.Monday, DayOfWeek.Friday), TimeRules.At(9, 31), () =>
            {
                Log(stocks.Count.ToString());
            });
        }

        #region Universe Stuff
        // sort the data by daily dollar volume and take the top 'NumberOfSymbolsCoarse'
        public IEnumerable<Symbol> CoarseSelectionFunction(IEnumerable<CoarseFundamental> coarse)
        {
            // select only symbols with fundamental data and sort descending by daily dollar volume
            var sortedByDollarVolume = coarse
                .Where(x => x.HasFundamentalData)
                .OrderByDescending(x => x.DollarVolume);

            // take the top entries from our sorted collection
            var top = sortedByDollarVolume.Take(NumberOfSymbolsCoarse);

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

        // sort the data by P/E ratio and take the top 'NumberOfSymbolsFine'
        public IEnumerable<Symbol> FineSelectionFunction(IEnumerable<FineFundamental> fine)
        {
            return fine.Select(x => x.Symbol);
        } 
        #endregion

        public void OnData(TradeBars data)
        {

        }

        public override void OnSecuritiesChanged(SecurityChanges changes)
        {
            if(changes.AddedSecurities.Count > 0)
            {
                stocks.AddRange(changes.AddedSecurities.Select(x => x.Symbol));
            }

            if (changes.RemovedSecurities.Count > 0)
            {
                foreach(Symbol symbol in changes.RemovedSecurities.Select(x => x.Symbol))
                {
                    stocks.Remove(symbol);
                }
            }
        }
    }
}