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);
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
Disclaimer: The dataset is provided by the data provider for informational purposes only and do not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor do they constitute an offer to provide investment advisory or other services by the data provider.
For more example algorithms, see Examples.
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:
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
namespace QuantConnect { 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 altCoarsee.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}"); } }
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}"); } }
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
Disclaimer: The dataset is provided by the data provider for informational purposes only and do not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor do they constitute an offer to provide investment advisory or other services by the data provider.
For more example algorithms, see Examples.