This is a post on how to use QuantShare to rank stocks based on fundamental ratios then use these ranks in a trading screen. First, we must download fundamental data then rank some or all our stocks using the composite function. Finally, we will show you advanced usage tips to get the most out of this composite function.
How to get fundamental stock data
In the post, I will show you how to get fundamental data for U.S. stocks. However, you can get fundamental data for other stock markets by adding the data manually using the "Data -> ASCII Import" feature or by creating your own fundamental data downloader.
Go to the following page (Fundamental Valuation Ratios for US Stocks) then click on "Download" button to get this valuation downloader. Install the item then use the download manager (Download -> Download Manager) to run it and thus retrieve the last valuation ratios for more than 6000 U.S. stocks. This item gets the following fundamental ratios: price to earnings ratio, forward price to earnings ratio, peg, price per share, price per book, price per cash, price per free cash flow, earning per share or EPS.
Other fundamental data downloaders:
Ownership data for US companies
Financial fundamental data for US companies
Historical Earnings Surprise, EPS and Consensus Data
Market cap, ROE, ROA and Forward PE for U.S. Stocks - Fundamental Data from Yahoo
There are several ways to visualize the data:
Data Editor: Select "Data -> Edit Database". Select "Custom" as database type then choose a custom database. Select a symbol to display its data.
Data Viewer: Select "Tools -> Database Data". Choose a database from the list. You can directly download recent data for this database by clicking on the "Download" button.
Chart: Create a new pane in a chart, click on "Add Indicator" icon, select "Databases/Fields" tab, choose a database/field then click on "OK" to plot a time-series on the chart.
What is the Price to Book Ratio?
The price to book ratio, also called P/B ratio, is a financial measure used to compare the value of a company (market price) to its book value (value of assets according to the firm's balance sheet).
The price to book ratio is calculated as follows: market capitalization divided by the book value. The book value is the total assets minus intangible assets and liabilities.
Another way (similar) to calculate the P/B ratio is by dividing the stock share price by the book value per share.
This ratio is mainly used by value investors. If a stock is trading below its book value (The ratio is lower than 1) then this could be interpreted by one of the following facts: Either the market (investors) thinks the asset value is overstated or the company is having low or negative return on its assets.
For the rest of the article, we will use the price to book ratio to perform fundamental stock analysis.
Access the price to book ratio
To access the price to book ratio of a stock, you should use the following function:
a = GetData("fund_valuation", "pb"); // "pb" is the field name of the price to book ratio
The above instruction will read the "fund_valuation" database and retrieve data for the analyzed stock from the "PB" field. It returns a time-series that contains historical data for the price to book ratio. Note however that in order to gather historical data you will need to run the downloader (described above) several times (once a day, week or month). Another solution would be to add historical fundamental stock data directly into a custom database (either using a downloader or manually via ASCII Import). If you have historical data of some fundamental items, you can use them to create fundamental-based trading systems and test their robustness by backtesting them.
Ranking stocks based on a valuation ratio
QuantShare trading software has a very easy to use function to rank stocks based on any given metric or measure. This function gets a time-series measure for each analyzed stock, groups the data by date/time, then calculates the rank of each stock for each trading date. The result can be used to perform other calculations or ranking.
You can for example rank you stocks based on two financial or valuation ratios then calculate the average ratio.
The function is called "comp" and it can be called as follows:
a = comp(close, "rank");
// Rank each stock based on its share price. The higher the share price the lower the rank. The stock that is ranked "1" is it has the highest share price.
Fundamental Stock Analysis: Grouping
The group option of the composite function is very useful.
Let us imagine you want to rank your stocks based on the price to book ratio. However, you want to rank only stocks that belong to the same group, industry for instance. This means that you want to compare stocks that are part of the same industry and thus you will get several stocks with the first rank (one for each industry).
This can be done very easily. Please check the next line:
a = comp(close, "rank", Industry()); // Industry function returns the industry name of the analyzed stock.
Make sure industry field is filled for each stock symbol (Symbol -> Update Symbol).
You can fill industry fields automatically by using one of the following downloaders: Update the Industry Name of your U.S. Stocks, Industry and Sector Information for the US Stock Market.
Fundamental Stock Analysis: Filtering
Here is another option that makes the composite function even more powerful. The filter option allows you to select which stocks or securities to use in the ranking system.
Image, you want to purchase top five stocks with the highest monthly return. You can implement this trading rule simply by typing:
buy = comp(roc(25), "rank") <= 5;
Now, let us say you want to consider only stocks whose average daily volume is above one million dollars. How to implement this? Any idea?
The optional fifth parameter of the "comp" function is the key. It allows you to define a filter formula so that you can instruct the ranking function to consider only the securities that passes this filter.
In our example, our formula is:
volFilter = sma(volume, 30) > 1000000;
buy = comp(roc(25), "rank", 1, volFilter) <= 5; // Disable grouping by setting its value to "1". You can also disable filtering by also setting its value to "1".
Let us create a screen that displays stocks whose monthly return is higher than 20%. The screen will also display two columns:
- The rank of each stock based on its price to book ratio
- The rank of each stock based on the 5-bar rate of return
filter = roc(25) > 20;
a = GetData("fund_valuation", "pb"); // Get fundamental stock data (Price/Book)
pb = comp(a, "rank"); // Rank based on variable "a"
rank5 = comp(roc(5), "rank");
AddColumn("Rank 5", rank5);