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.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using System.Diagnostics;
using System.Collections.Concurrent;
using QuantConnect.Indicators;
using QuantConnect.Data.UniverseSelection;

namespace QuantConnect.Algorithm.CSharp
{
    class TopGainers : QCAlgorithm
    {
        private int _universeSize = 500, _trackerCount = 5;
        private readonly ConcurrentDictionary<Symbol, UniverseSelectionData> universeSelectionData = new ConcurrentDictionary<Symbol, UniverseSelectionData>();

        /// <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()
        {
            SetCash(100000);
            SetStartDate(2017, 08, 01);

            UniverseSettings.Resolution = Resolution.Minute;
            AddUniverse(coarse =>
            {
                // define universe of top N stocks by dollar volume
                // while doing this, update our selection data to track
                // our rate of change per security
                var topDollarVolume =
                from c in coarse
                let data = universeSelectionData.GetOrAdd(c.Symbol, sym => new UniverseSelectionData(this, sym))
                where data.Update(c)
                orderby c.DollarVolume descending
                select data;

                // now that we're ordered by dollar volume and have updated
                // our data for each security, take the top N by dollar volume
                // and then take the top M by rate of change
                return topDollarVolume.Take(_universeSize)
                .OrderByDescending(d => d.RateOfChange)
                .Take(_trackerCount)
                .Select(d => d.Symbol);
            });

            Schedule.On(DateRules.EveryDay(), TimeRules.At(9, 31), SelectStocks);
        }

        private void SelectStocks()
        {
            var members = UniverseManager.SingleOrDefault(u => u.Key.Value == "QC-UNIVERSE-COARSE-USA").Value.Members;
            Debug(Time.ToShortDateString() + ": " + String.Join(", ", members.Select(x => x.Key)));
            foreach (var s in Securities)
            {
                if (members.Count(x => x.Key == s.Key) > 0 && s.Key.Value != "SPY")
                    Securities.Remove(s.Key);
            }
        }

        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice data)
        {
            if (!Portfolio.Invested)
            {
            }
        }

        class UniverseSelectionData
        {
            public readonly Symbol Symbol;
            public readonly RateOfChange RateOfChange;
            public UniverseSelectionData(QCAlgorithm algorithm, Symbol symbol)
            {
                Symbol = symbol;
                RateOfChange = new RateOfChange("ROC-" + symbol, 2);
            }

            public bool Update(CoarseFundamental coarse)
            {
                return RateOfChange.Update(coarse.EndTime, coarse.Value);
            }
        }
    }
}