We thought it would be a good time to create a simple template for using fundamental data to rank and select securities. Our intention is that someone who is focused on value investing and fundamental analysis can clone this algorithm and modify it to suit their interests without a lot of coding skill, as an exercise to help get started. Seong Lee did the bulk of the work writing this algo.
Here’s how it works:
The algorithm selects metrics from our fundamentals database. It uses these metrics to rank stocks for purchase. The algorithm reevaluates the rankings once per month and rebalances the portfolio. So if you query for 5 metrics from the fundamentals database, it will rank each security by each of those factors. In turn, a single ranking of securities is created from the separate rankings. The top stocks from this cumulative ranking are then purchased.
It is flexible in that you can switch the metrics you want to use or the number of metrics used overall. You can also easily set the number of stocks purchased with a single simple variable change. You don't need a lot of coding skill to make these kinds of changes.
Want to try yourself? Hit the “Clone Algorithm” button and follow the steps below to modify the algorithm with your own ranking criteria. My sample doesn't perform so well. Maybe you can build one that does.
Instructions for Modifying the Stock Selection Logic
Step 1: Select your metrics for ranking
The selected metrics are defined in the query() function:
fundamental_df = get_fundamentals( query( # To add a metric. Start by typing "fundamentals." fundamentals.operation_ratios.roic, fundamentals.valuation_ratios.pe_ratio, fundamentals.operation_ratios.ebit_margin ) .filter(fundamentals.valuation.market_cap > 1000e6) .filter(fundamentals.asset_classification.morningstar_sector_code != 103) .order_by(fundamentals.operation_ratios.roic.desc()) .limit(num_stocks) )
You can change the metrics selected and used for the ranking algorithm by updating the comma separated list inside the query() function. Our fundamentals DB has 600+ metrics to choose from. Type "fundamentals.” and a search box will pop up to help you search for the metric you have in mind.
Each stock in the algorithm will be ranked using each of these metrics. In this example case, each stock will be ranked by the three metrics listed. Each metric ranking will contribute equally.
Step 2. Select the size of your ranking pool
By default, 1000 companies are selected. This number is controlled by:
num_stocks = 1000
set on line 29. This sets the maximum number of stocks selected for ranking.
Step 3. Filtering the selection of stocks
Which stocks are ranked? The stocks selected and used for ranking are controlled by two factors: how they are filtered and how they are ordered. The filter clause in this algo template is on lines 45-6. Multiple clauses can be strung together like in the sample where we filter based on market cap and filter out financial sector stocks:
Furthermore, you can order the stocks selected based on a metric, as demonstrated in the sample query on line 47. Ordering by a different metric will sort the universe of stocks by that metric. So if there are more than 1000 stocks that fit your criteria, it will select the top 1000 stocks as determined by the metric used in your order_by clause.
Step 4. Check your sorting order
By default, the each metric contributes to the ranking, ordered from highest value to lowest (descending).
Sometimes, you want to rank using a metric going from lowest to highest.
Line 55 lets you set which metrics you’d like to rank lowest value to highest, using their fundamentals database name:
lower_the_better = ['pe_ratio']
So go clone this algorithm and experiment with your own variations of a multi-factor ranking model.
|Returns||1 Month||3 Month||6 Month||12 Month|
|Alpha||1 Month||3 Month||6 Month||12 Month|
|Beta||1 Month||3 Month||6 Month||12 Month|
|Sharpe||1 Month||3 Month||6 Month||12 Month|
|Sortino||1 Month||3 Month||6 Month||12 Month|
|Volatility||1 Month||3 Month||6 Month||12 Month|
|Max Drawdown||1 Month||3 Month||6 Month||12 Month|