book
Checkout our new book! Hands on AI Trading with Python, QuantConnect, and AWS Learn More arrow

Brain

Brain ML Stock Ranking

Introduction

The Brain ML Stock Ranking dataset by Brain generates a daily ranking for US Equities based on their predicted ranking of future returns relative to the universe median across four-time horizons: next 2, 3, 5, 10, and 21 days (one trading month). The data covers 1,000 US Equities (universe updated yearly by including the largest 1,000 US companies of the previous year), starts in January 2010, and is delivered on a daily frequency. This dataset is created by a voting scheme of machine learning classifiers that non-linearly combine a variety of features with a series of techniques aimed at mitigating the well-known overfitting problem for financial data with a low signal-to-noise ratio. Examples of features are time-varying stock-specific features like price and volume-related metrics or fundamentals; time-fixed stock-specific features like the sector and other database information; market regime features such as volatility and other financial stress indicators; calendar features representing possible anomalies, for example, the month of the year.

More precisely the ML Stock Ranking score is related to the confidence of a Machine Learning classifier in predicting top or bottom quintile returns for the next N trading days (e.g. next 21 trading days) for a stock with the respect to the median of the universe and ranges from -1 to +1.

A negative score means that the system is more confident that the stock belongs to the lower returns quintile, a positive score means that the system is more confident that the stock belongs to the higher returns quintile. It is important to note that the score has a meaning only if used to compare different stocks to perform a ranking.

Typical use is to download the score for a large stock universe for a given day, e.g. 500 stocks or the full universe of 1000 stocks, order the stocks by mlAlpha score and go long the top K stocks, or build a long-short strategy going long the top K and short the bottom K stocks.

For more information, refer to Brain's summary paper.

This dataset depends on the US Equity Security Master dataset because the US Equity Security Master dataset contains information on splits, dividends, and symbol changes.

For more information about the Brain ML Stock Ranking dataset, including CLI commands and pricing, see the dataset listing.

About the Provider

Brain is a Research Company that creates proprietary datasets and algorithms for investment strategies, combining experience in financial markets with strong competencies in Statistics, Machine Learning, and Natural Language Processing. The founders share a common academic background of research in Physics as well as extensive experience in Financial markets.

Getting Started

The following snippet demonstrates how to request data from the Brain ML Stock Ranking dataset:

self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol
self.dataset_symbol = self.add_data(BrainStockRanking2Day, self.aapl).symbol

self._universe = self.add_universe(BrainStockRankingUniverse, self.universe_selection)
_symbol = AddEquity("AAPL", Resolution.Daily).Symbol;
_datasetSymbol = AddData<BrainStockRanking2Day>(_symbol).Symbol;

_universe = AddUniverse<BrainStockRankingUniverse>(UniverseSelection);

Data Summary

The following table describes the dataset properties:

PropertyValue
Start DateJanuary 2010
Asset Coverage*1,000 US Equities
Data DensitySparse
ResolutionDaily
TimezoneUTC
The coverage includes all assets since the start date. It increases over time.

Requesting Data

To add Brain ML Stock Ranking data to your algorithm, call the AddDataadd_data method. Save a reference to the dataset Symbol so you can access the data later in your algorithm.

class BrainMLRankingDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2021, 1, 1)
        self.set_end_date(2021, 7, 8)
        self.set_cash(100000)
        
        self.aapl = self.add_equity("AAPL", Resolution.DAILY).symbol
        self.two_day_symbol = self.add_data(BrainStockRanking2Day, self.aapl).symbol
        self.three_day_symbol = self.add_data(BrainStockRanking3Day, self.aapl).symbol
        self.five_day_symbol = self.add_data(BrainStockRanking5Day, self.aapl).symbol
        self.ten_day_symbol = self.add_data(BrainStockRanking10Day, self.aapl).symbol
        self.month_symbol = self.add_data(BrainStockRanking21Day, self.aapl).symbol
public class BrainMLRankingDataAlgorithm : QCAlgorithm
{
    private Symbol _symbol, _2DaySymbol, _3DaySymbol, _5DaySymbol, _10DaySymbol, _monthSymbol;
    
    public override void Initialize()
    {
        SetStartDate(2021, 1, 1);
        SetEndDate(2021, 7, 8);
        SetCash(100000);
        
        _symbol = AddEquity("AAPL", Resolution.Daily).Symbol;
        _2DaySymbol = AddData<BrainStockRanking2Day>(_symbol).Symbol;
        _3DaySymbol = AddData<BrainStockRanking3Day>(_symbol).Symbol;
        _5DaySymbol = AddData<BrainStockRanking5Day>(_symbol).Symbol;
        _10DaySymbol = AddData<BrainStockRanking10Day>(_symbol).Symbol;
        _monthSymbol = AddData<BrainStockRanking21Day>(_symbol).Symbol;
    }
}

Accessing Data

To get the current Brain ML Stock Ranking data, index the current Slice with the dataset Symbol. Slice objects deliver unique events to your algorithm as they happen, but the Slice may not contain data for your dataset at every time step. To avoid issues, check if the Slice contains the data you want before you index it.

def on_data(self, slice: Slice) -> None:
    if slice.contains_key(self.two_day_symbol):
        data_point = slice[self.two_day_symbol]
        self.log(f"{self.two_day_symbol} rank at {slice.time}: {data_point.rank}")

    if slice.contains_key(self.three_day_symbol):
        data_point = slice[self.three_day_symbol]
        self.log(f"{self.three_day_symbol} rank at {slice.time}: {data_point.rank}")

    if slice.contains_key(self.five_day_symbol):
        data_point = slice[self.five_day_symbol]
        self.log(f"{self.five_day_symbol} rank at {slice.time}: {data_point.rank}")

    if slice.contains_key(self.ten_day_symbol):
        data_point = slice[self.ten_day_symbol]
        self.log(f"{self.ten_day_symbol} rank at {slice.time}: {data_point.rank}")

    if slice.contains_key(self.month_symbol):
        data_point = slice[self.month_symbol]
        self.log(f"{self.month_symbol} rank at {slice.time}: {data_point.rank}")
public override void OnData(Slice slice)
{
    if (slice.ContainsKey(_2DaySymbol))
    {
        var dataPoint = slice[_2DaySymbol];
        Log($"{_2DaySymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    if (slice.ContainsKey(_3DaySymbol))
    {
        var dataPoint = slice[_3DaySymbol];
        Log($"{_3DaySymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    if (slice.ContainsKey(_5DaySymbol))
    {
        var dataPoint = slice[_5DaySymbol];
        Log($"{_5DaySymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    if (slice.ContainsKey(_10DaySymbol))
    {
        var dataPoint = slice[_10DaySymbol];
        Log($"{_10DaySymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    if (slice.ContainsKey(_monthSymbol))
    {
        var dataPoint = slice[_monthSymbol];
        Log($"{_monthSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }
}

To iterate through all of the dataset objects in the current Slice, call the Getget method.

def on_data(self, slice: Slice) -> None:
    for dataset_symbol, data_point in slice.get(BrainStockRanking2Day).items():
        self.log(f"{dataset_symbol} rank at {slice.time}: {data_point.rank}")

    for dataset_symbol, data_point in slice.get(BrainStockRanking3Day).items():
        self.log(f"{dataset_symbol} rank at {slice.time}: {data_point.rank}")

    for dataset_symbol, data_point in slice.get(BrainStockRanking5Day).items():
        self.log(f"{dataset_symbol} rank at {slice.time}: {data_point.rank}")

    for dataset_symbol, data_point in slice.get(BrainStockRanking10Day).items():
        self.log(f"{dataset_symbol} rank at {slice.time}: {data_point.rank}")

    for dataset_symbol, data_point in slice.get(BrainStockRanking21Day).items():
        self.log(f"{dataset_symbol} rank at {slice.time}: {data_point.rank}")
public override void OnData(Slice slice)
{
    foreach (var kvp in slice.Get<BrainStockRanking2Day>())
    {
        var datasetSymbol = kvp.Key;
        var dataPoint = kvp.Value;
        Log($"{datasetSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    foreach (var kvp in slice.Get<BrainStockRanking3Day>())
    {
        var datasetSymbol = kvp.Key;
        var dataPoint = kvp.Value;
        Log($"{datasetSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    foreach (var kvp in slice.Get<BrainStockRanking5Day>())
    {
        var datasetSymbol = kvp.Key;
        var dataPoint = kvp.Value;
        Log($"{datasetSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    foreach (var kvp in slice.Get<BrainStockRanking10Day>())
    {
        var datasetSymbol = kvp.Key;
        var dataPoint = kvp.Value;
        Log($"{datasetSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }

    foreach (var kvp in slice.Get<BrainStockRanking21Day>())
    {
        var datasetSymbol = kvp.Key;
        var dataPoint = kvp.Value;
        Log($"{datasetSymbol} rank at {slice.Time}: {dataPoint.Rank}");
    }
}

Historical Data

To get historical Brain ML Stock Ranking data, call the Historyhistory method with the dataset Symbol. If there is no data in the period you request, the history result is empty.

# DataFrames
two_day_history_df = self.history(self.two_day_symbol, 100, Resolution.DAILY)
three_day_history_df = self.history(self.three_day_symbol, 100, Resolution.DAILY)
five_day_history_df = self.history(self.five_day_symbol, 100, Resolution.DAILY)
ten_day_history_df = self.history(self.ten_day_symbol, 100, Resolution.DAILY)
thirty_day_history_df = self.history(self.month_symbol, 100, Resolution.DAILY)
history_df = self.history([self.two_day_symbol, 
                           self.three_day_symbol, 
                           self.five_day_symbol, 
                           self.ten_day_symbol,
                           self.month_symbol], 100, Resolution.DAILY)

# Dataset objects
two_day_history_bars = self.history[BrainStockRanking2Day](self.two_day_symbol, 100, Resolution.DAILY)
three_day_history_bars = self.history[BrainStockRanking3Day](self.three_day_symbol, 100, Resolution.DAILY)
five_day_history_bars = self.history[BrainStockRanking5Day](self.five_day_symbol, 100, Resolution.DAILY)
ten_day_history_bars = self.history[BrainStockRanking10Day](self.ten_day_symbol, 100, Resolution.DAILY)
month_history_bars = self.history[BrainStockRanking21Day](self.month_symbol, 100, Resolution.DAILY)
// Dataset objects
var twoDayHistory = History<BrainStockRanking2Day>(_2DaySymbol, 100, Resolution.Daily);
var threeDayHistory = History<BrainStockRanking3Day>(_3DaySymbol, 100, Resolution.Daily);
var fiveDayHistory = History<BrainStockRanking5Day>(_5DaySymbol, 100, Resolution.Daily);
var tenDayHistory = History<BrainStockRanking10Day>(_10DaySymbol, 100, Resolution.Daily);
var monthHistory = History<BrainStockRanking21Day>(_monthSymbol, 100, Resolution.Daily);

// Slice objects
var history = History(new[] {_2DaySymbol, _3DaySymbol, _5DaySymbol, 
                             _10DaySymbol, _monthSymbol}, 100, Resolution.Daily);

For more information about historical data, see History Requests.

Universe Selection

To select a dynamic universe of US Equities based on Brain ML Stock Ranking data, call the AddUniverseadd_universe method with the BrainStockRankingUniverse class and a selection function.

def initialize(self) -> None:
    self._universe = self.add_universe(BrainStockRankingUniverse, self.universe_selection)

def universe_selection(self, alt_coarse: List[BrainStockRankingUniverse]) -> List[Symbol]:
    return [d.symbol for d in alt_coarse \
                if d.rank2_days > 0 \
                and d.rank3_days > 0 \
                and d.rank5_days > 0]
private Universe _universe;
public override void Initialize()
{
    _universe = AddUniverse<BrainStockRankingUniverse>(altCoarse =>
    {
        return from d in altCoarse.OfType<BrainStockRankingUniverse>() 
            where d.Rank2Days > 0m && d.Rank3Days > 0m  && d.Rank5Days > 0m
            select d.Symbol;
    })
};

For more information about dynamic universes, see Universes.

Universe History

You can get historical universe data in an algorithm and in the Research Environment.

Historical Universe Data in Algorithms

To get historical universe data in an algorithm, call the Historyhistory method with the Universe object and the lookback period. If there is no data in the period you request, the history result is empty.

var universeHistory = History(_universe, 30, Resolution.Daily);
foreach (var ranks in universeHistory)
{
    foreach (BrainStockRankingUniverse rank in ranks)
    {
        Log($"{rank.Symbol} 2-day rank at {rank.EndTime}: {rank.Rank2Days}");
    }
}
# DataFrame example where the columns are the BrainStockRankingUniverse attributes: 
history_df = self.history(self._universe, 30, Resolution.DAILY, flatten=True)

# Series example where the values are lists of BrainStockRankingUniverse objects: 
universe_history = self.history(self._universe, 30, Resolution.DAILY)
for (_, time), ranks in universe_history.items():
    for rank in ranks:
        self.log(f"{rank.symbol} 2-day rank at {rank.end_time}: {rank.rank2_days}")

Historical Universe Data in Research

To get historical universe data in research, call the UniverseHistoryuniverse_history method with the Universe object, a start date, and an end date. This method returns the filtered universe. If there is no data in the period you request, the history result is empty.

var universeHistory = qb.UniverseHistory(universe, qb.Time.AddDays(-30), qb.Time);
foreach (var ranks in universeHistory)
{
    foreach (BrainStockRankingUniverse rank in ranks)
    {
        Console.WriteLine($"{rank.Symbol} 2-day rank at {rank.EndTime}: {rank.Rank2Days}");
    }
}
# DataFrame example where the columns are the BrainStockRankingUniverse attributes: 
history_df = qb.universe_history(universe, qb.time-timedelta(30), qb.time, flatten=True)

# Series example where the values are lists of BrainStockRankingUniverse objects: 
universe_history = qb.universe_history(universe, qb.time-timedelta(30), qb.time)
for (_, time), ranks in universe_history.items():
    for rank in ranks:
        print(f"{rank.symbol} 2-day rank at {rank.end_time}: {rank.rank2_days}")

You can call the Historyhistory method in Research.

Remove Subscriptions

To remove a subscription, call the RemoveSecurityremove_security method.

self.remove_security(self.two_day_symbol)
self.remove_security(self.three_day_symbol)
self.remove_security(self.five_day_symbol)
self.remove_security(self.ten_day_symbol)
self.remove_security(self.month_symbol)
RemoveSecurity(_2DaySymbol);
RemoveSecurity(_3DaySymbol);
RemoveSecurity(_5DaySymbol);
RemoveSecurity(_10DaySymbol);
RemoveSecurity(_monthSymbol);

If you subscribe to Brain ML Stock Ranking data for assets in a dynamic universe, remove the dataset subscription when the asset leaves your universe. To view a common design pattern, see Track Security Changes.

Example Applications

The Brain ML Stock Ranking dataset enables you to test strategies using the machine learning ranking provided by Brain. Examples include the following strategies:

  • Constructing a portfolio of securities with each security's weight in the portfolio reflecting its Brain ML Stock Ranking
  • Buying stocks with the largest Brain ML Stock Ranking
  • Building a market-neutral strategy based on the top N and bottom N stocks in the Brain ML Stock Ranking

Classic Algorithm Example

The following example algorithm constructs a portfolio where the weight of each security in the portfolio is scaled based on its Brain ML Ranking. It gives a larger allocation to securities that have a higher Brain ML Ranking.

from AlgorithmImports import *
from QuantConnect.DataSource import *

class BrainMLRankingDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2021, 1, 1)
        self.set_end_date(2021, 7, 8)
        self.set_cash(100000) 
        
        # We cherry picked 5 largest stocks, high trading volume provides better information and credibility for ML ranking
        tickers = ["AAPL", "TSLA", "MSFT", "F", "KO"]
        self.symbol_by_dataset_symbol = {}
        for ticker in tickers:
            # Requesting data to get 2 days estimated relative ranking
            symbol = self.add_equity(ticker, Resolution.DAILY).symbol
            dataset_symbol = self.add_data(BrainStockRanking2Day, symbol).symbol
            self.symbol_by_dataset_symbol[dataset_symbol] = symbol
            
            # Historical data
            history = self.history(dataset_symbol, 365, Resolution.DAILY)
            self.debug(f"We got {len(history)} items from our history request for {symbol}")
        
    def on_data(self, slice: Slice) -> None:
        # Collect rankings for all selected symbols for ranking them
        points = slice.Get(BrainStockRanking2Day)
        if points is None:
            return
        symbols = []
        ranks = []
        for point in points.Values:
            symbols.append(self.symbol_by_dataset_symbol[point.symbol])
            ranks.append(point.rank)
        
        # Rank each symbol's Brain ML ranking relative to each other for positional sizing
        if len(ranks) == 0:
            return
        ranks = [sorted(ranks).index(rank) + 1 for rank in ranks]
        
        # Place orders according to the ML ranking, the better the rank, the higher the estimated return and hence weight
        for i, rank in enumerate(ranks):
            weight = rank / sum(ranks)
            self.set_holdings(symbols[i], weight)
public class BrainMLRankingDataAlgorithm : QCAlgorithm
{
    private Dictionary<Symbol, Symbol> _symbolByDatasetSymbol = new Dictionary<Symbol, Symbol>();
    
    public override void Initialize()
    {
        SetStartDate(2021, 1, 1);
        SetEndDate(2021, 7, 8);
        SetCash(100000);
        
        // We cherry picked 5 largest stocks, high trading volume provides better information and credibility for ML ranking
        var tickers = new List<string>() {"AAPL", "TSLA", "MSFT", "F", "KO"};
        foreach (var ticker in tickers)
        {
            // Requesting data to get 2 days estimated relative ranking
            var symbol = AddEquity(ticker, Resolution.Daily).Symbol;
            var datasetSymbol = AddData<BrainStockRanking2Day>(symbol).Symbol;
            _symbolByDatasetSymbol.Add(datasetSymbol, symbol);
            
            // Historical data
            var history = History<BrainStockRanking2Day>(datasetSymbol, 365, Resolution.Daily);
            Debug($"We got {history.Count()} items from our history request for {symbol}");
        }
    }

    public override void OnData(Slice slice)
    {
        // Collect rankings for all symbols for ranking them
        var points = slice.Get<BrainStockRanking2Day>();
        if (points == None)
        {
            return;
        }
        var symbols = new List<Symbol>();
        var ranks = new List<decimal>();
        foreach (var point in points.Values)
        {
            symbols.Add(_symbolByDatasetSymbol[point.Symbol]);
            ranks.Add(point.Rank);
        }
        
        // Rank each symbol's Brain ML ranking relative to the other symbols for positional sizing
        if (ranks.Count() == 0) return;
        var sortedRanksTemp = new List<decimal>(ranks); 
        sortedRanksTemp.Sort();
        var sortedRanks = new List<decimal>();
        for (var i = 0; i < symbols.Count(); i++)
        {
            sortedRanks.Add(sortedRanksTemp.IndexOf(ranks[i]) + 1);
        }
        
        // Place orders, the better the rank, the higher the estimated return and hence weight
        for (var i = 0; i < symbols.Count(); i++)
        {
            var rank = sortedRanks[i];
            var weight = rank / sortedRanks.Sum();
            SetHoldings(symbols[i], weight);
        }
    }
}

Framework Algorithm Example

The following example algorithm constructs a portfolio where the weight of each security in the portfolio is scaled based on its Brain ML Ranking. It gives a larger allocation to securities that have a higher Brain ML Ranking.

from AlgorithmImports import *
from QuantConnect.DataSource import *

class BrainMLRankingDataAlgorithm(QCAlgorithm):
    def initialize(self) -> None:
        self.set_start_date(2021, 1, 1)
        self.set_end_date(2021, 7, 8)
        self.set_cash(100000) 
        
        self.universe_settings.resolution = Resolution.DAILY
        # Select based on ML ranking data
        self.add_universe(BrainStockRankingUniverse, self.universe_selection)
        
        self.add_alpha(BrainMLRankingAlphaModel())
        
        self.set_portfolio_construction(InsightWeightingPortfolioConstructionModel())
         
        self.add_risk_management(NullRiskManagementModel())
        
        self.set_execution(ImmediateExecutionModel())

    def universe_selection(self, alt_coarse: List[BrainStockRankingUniverse]) -> List[Symbol]:
        # Select the ones that expected to out-perform the median of the whole market in 2-3 days
        return [d.symbol for d in alt_coarse \
                if d.Rank2Days > 0.2  \
                and d.Rank3Days > 0.2]

class BrainMLRankingAlphaModel(AlphaModel):
    
    symbol_data_by_symbol = {}
    symbol_by_dataset_symbol = {}
    
    def update(self, algorithm: QCAlgorithm, slice: Slice) -> List[Insight]:
        insights = []
        
        # Collect rankings for all selected symbols for ranking them
        points = slice.Get(BrainStockRanking2Day)
        if points is None:
            return []
        symbols = []
        ranks = []
        for point in points.Values:
            symbols.append(self.symbol_by_dataset_symbol[point.symbol])
            ranks.append(point.rank)
        
        # Rank each symbol's Brain ML ranking relative to each other for positional sizing
        if len(ranks) == 0:
            return []
        ranks = [sorted(ranks).index(rank) + 1 for rank in ranks]
        
        # Place orders according to the ML ranking, the better the rank, the higher the estimated return and hence weight
        for i, rank in enumerate(ranks):
            weight = rank / sum(ranks)
            insights.append(Insight.price(symbols[i], timedelta(days=7), InsightDirection.UP, None, None, None, weight))
        
        return insights
        
    def on_securities_changed(self, algorithm: QCAlgorithm, changes: SecurityChanges) -> None:
        for security in changes.added_securities:
            symbol = security.symbol
            symbol_data = SymbolData(algorithm, symbol)
            self.symbol_data_by_symbol[symbol] = symbol_data
            self.symbol_by_dataset_symbol[symbol_data.dataset_symbol] = symbol
        
        for security in changes.removed_securities:
            symbol_data = self.symbol_data_by_symbol.pop(security.symbol, None)
            if symbol_data:
                symbol_data.dispose()
                
            for dataset_symbol, symbol in self.symbol_by_dataset_symbol.items():
                if symbol == security.symbol:
                    self.symbol_by_dataset_symbol.pop(dataset_symbol)
                    break


class SymbolData:
    def __init__(self, algorithm, symbol):
        self.algorithm = algorithm
        
        # Requesting data to get 2 days estimated relative ranking
        self.dataset_symbol = algorithm.add_data(BrainStockRanking2Day, symbol).symbol
        
        # Historical data
        history = algorithm.history(self.dataset_symbol, 365, Resolution.DAILY)
        algorithm.debug(f"We got {len(history)} items from our history request for {symbol}")
        
    def dispose(self):
        # Unsubscribe from the Brain ML Ranking feed for this security to release computation resources
        self.algorithm.remove_security(self.dataset_symbol)
public class BrainMLRankingDataAlgorithm : QCAlgorithm
{
    public override void Initialize()
    {
        SetStartDate(2021, 1, 1);
        SetEndDate(2021, 7, 8);
        SetCash(100000);
        
        // Select based on ML ranking data
        AddUniverse<BrainStockRankingUniverse>(altCoarse =>
        {
            // Select the ones that expected to out-perform the median of the whole market in 2-3 days
            return from d in altCoarse.OfType<BrainStockRankingUniverse>() 
                where d.Rank2Days > 0.2m && d.Rank3Days > 0.2m
                select d.Symbol;
        });
        
        AddAlpha(new BrainMLRankingAlphaModel());
        
        SetPortfolioConstruction(new InsightWeightingPortfolioConstructionModel());
        
        AddRiskManagement(new NullRiskManagementModel());
        
        SetExecution(new ImmediateExecutionModel());
    }
}
    
    
public class BrainMLRankingAlphaModel : AlphaModel
{
    private Dictionary<Symbol, SymbolData> _symbolDataBySymbol = new Dictionary<Symbol, SymbolData>();
    private Dictionary<Symbol, Symbol> _symbolByDatasetSymbol = new Dictionary<Symbol, Symbol>();

    public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice slice)
    {
        var insights = new List<Insight>();
        
        // Collect rankings for all selected symbols for ranking them
        var points = slice.Get<BrainStockRanking2Day>();
        if (points == None)
        {
            return insights;
        }
        var symbols = new List<Symbol>();
        var ranks = new List<decimal>();
        foreach (var point in points.Values)
        {
            symbols.Add(_symbolByDatasetSymbol[point.Symbol]);
            ranks.Add(point.Rank);
        }
        
        // Rank each symbol's Brain ML ranking relative to the other symbols for positional sizing
        if (ranks.Count() == 0) 
        {
            return insights; 
        }
        var sortedRanksTemp = new List<decimal>(ranks); 
        sortedRanksTemp.Sort();
        var sortedRanks = new List<decimal>();
        for (var i = 0; i < symbols.Count(); i++)
        {
            sortedRanks.Add(sortedRanksTemp.IndexOf(ranks[i]) + 1);
        }
        
        // Place orders according to the ML ranking, the better the rank, the higher the estimated return and hence weight
        for (var i = 0; i < symbols.Count(); i++)
        {
            var rank = sortedRanks[i];
            var weight = rank / sortedRanks.Sum();
            insights.Add(Insight.Price(symbols[i], TimeSpan.FromDays(7), InsightDirection.Up, None, None, None, (double)weight));
        }
        
        return insights;
    }

    public override void OnSecuritiesChanged(QCAlgorithm algorithm, SecurityChanges changes)
    {
        foreach (var security in changes.AddedSecurities)
        {
            var symbol = security.Symbol;
            var symbolData = new SymbolData(algorithm, symbol);
            _symbolDataBySymbol.Add(symbol, symbolData);
            _symbolByDatasetSymbol.Add(symbolData.datasetSymbol, symbol);
        }

        foreach (var security in changes.RemovedSecurities)
        {
            var symbol = security.Symbol;
            if (_symbolDataBySymbol.ContainsKey(symbol))
            {
                _symbolDataBySymbol[symbol].dispose();
                _symbolDataBySymbol.Remove(symbol);
            }
            
            foreach (var entry in _symbolByDatasetSymbol)
            {
                if (entry.Value == symbol)
                {
                    _symbolByDatasetSymbol.Remove(entry.Key);
                }
            }
        }
    }
}


public class SymbolData
{
    private QCAlgorithm _algorithm;
    public Symbol datasetSymbol;

    public SymbolData(QCAlgorithm algorithm, Symbol symbol)
    {
        _algorithm = algorithm;
        
        // Requesting data to get 2 days estimated relative ranking
        datasetSymbol = algorithm.AddData<BrainStockRanking2Day>(symbol).Symbol;
        
        // Historical data
        var history = algorithm.History<BrainStockRanking2Day>(datasetSymbol, 365, Resolution.Daily);
        algorithm.Debug($"We got {history.Count()} items from our history request for {symbol}");
    }
    
    public void dispose()
    {
        // Unsubscribe from the Brain ML Ranking feed for this security to release computation resources
        _algorithm.RemoveSecurity(datasetSymbol);
    }
}

Research Example

The following example lists US Equities having the highest 2-day rank.

#r "../QuantConnect.DataSource.BrainSentiment.dll"
using QuantConnect.DataSource;

var qb = new QuantBook();

// Requesting data
var aapl = qb.AddEquity("AAPL", Resolution.Daily).Symbol;
var symbol = qb.AddData<BrainStockRanking2Day>(aapl).Symbol;

// Historical data
var history = qb.History<BrainStockRanking2Day>(symbol, 30, Resolution.Daily);
foreach (BrainStockRanking2Day rank in history)
{
    Console.WriteLine($"{rank} at {rank.EndTime}");
}

// Add Universe Selection
IEnumerable<Symbol> UniverseSelection(IEnumerable<BaseData> altCoarse)
{
    return (from d in altCoarse.OfType<BrainStockRankingUniverse>()
        orderby d.Rank2Days descending select d.Symbol).Take(10);
}
var universe = qb.AddUniverse<BrainStockRankingUniverse>(UniverseSelection);

// Historical Universe data
var universeHistory = qb.UniverseHistory(universe, qb.Time.AddDays(-5), qb.Time);
foreach (var ranks in universeHistory)
{
    foreach (BrainStockRankingUniverse rank in ranks)
    {
        Console.WriteLine($"{rank.Symbol} 2-day rank at {rank.EndTime}: {rank.Rank2Days}");
    }
}
qb = QuantBook()

# Requesting Data
aapl = qb.add_equity("AAPL", Resolution.DAILY).symbol
symbol = qb.add_data(BrainStockRanking2Day, aapl).symbol

# Historical data
history = qb.history(BrainStockRanking2Day, symbol, 30, Resolution.DAILY)
for (symbol, time), row in history.iterrows():
    print(f"{symbol} rank at {time}: {row['rank']}")

# Add Universe Selection
def universe_selection(alt_coarse: List[BrainStockRankingUniverse]) -> List[Symbol]:
    return [d.symbol for d in sorted(alt_coarse, key=lambda x: x.Rank2Days, reverse=True)[:10]]

universe = qb.add_universe(BrainStockRankingUniverse, universe_selection)

# Historical Universe data
universe_history = qb.universe_history(universe, qb.time-timedelta(5), qb.time)
for (_, time), ranks in universe_history.items():
    for rank in ranks:
        print(f"{rank.symbol} 2-day rank at {rank.end_time}: {rank.Rank2Days}")

Data Point Attributes

The Brain ML Stock Ranking dataset provides BrainStockRankingBase and BrainStockRankingUniverse objects.

BrainStockRankingBase Attributes

BrainStockRankingBase objects have the following attributes:

BrainStockRankingUniverse Attributes

BrainStockRankingUniverse objects have the following attributes:

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: