Valuator - A Ruby Script To Pick A Trending Value Stock Portfolio

11/05/2014, A couple of minutes read.

I’ve always been curious about investing on the stock market. However, until recently i’ve neither had the knowledge, money or confidence to do so. Some of these things haven’t changed, but I have been doing a lot more reading around the subject.

Disclaimer: I am not and do not claim to be an expert in investing. You should always do your own, thorough and detailed research, before investing in stocks. To this date (12/05/2014), I have not used this script to pick stocks I have then followed through and invested money into.

A few months back I stumbled across this Reddit thread. I found it fascinating reading about the ‘program’ that SwellsInMoisture had developed and found myself reading this thread a few times over.

Seeing a few people asking if this would be of any use as a Python script got me thinking. Then I saw the comment by nicolamr who had turned the screener into a web app. However, on inspection the site didn’t seem to work. Well, it worked but I couldn’t see any data from one of the listed snapshots.

With that being the case, I found nicolamr’s repository on Github and proceeded to convert it to Ruby. Not having really read any Python before I wasn’t sure what to expect. It turned out to be pretty straight forward though. Quite Ruby like actually. A few missing end’s and a couple of extra ;’s.

Anyway, I now have my own functioning Trending Value Stock script/screener. The script pulls data from various sources (finviz and Yahoo finance), before calculating the necessary rankings indicated by O’Shaughnessy in his book: ‘What Works on Wall Street’.

On the Forbes website, O’Shaughnessy explains:

“if you want the best results over time, go with small cap stocks trading at lower-than-average multiples of earnings, sales and book value–and buy them when they’re trending higher. Momentum does matter as history shows that winners tend to keep on winning.”

Our initial aim when it comes to picking our trending value stocks is to calculate a value composite. The value composite will measure undervaluation of a stock and replaces the common approach of simply using the price-to-sales ratio. It also provides us with another route towards portfolio diversitification; highly recommended when investing.

In a video, unfortunately no-longer available on the Forbes website] (you’ll have to take my word for it), O’Shaughnessy talks about the problem with single-factor valuation ratios. He explains that they move ‘in and out of favor’ and can significantly underperform the overall market over any given 10-year period, despite their long-term outperformance.

The Value Composite

Our value composite is composed of six value measures:

Firstly, we shortlist all stocks with a market cap > $200M.

We then assign each stock in our universe a score of 1 to 100 for each of the aforementioned value measures, in comparison to one another. Then calculate an average score from each of these values to create an overall value composite.

A “perfect” value stock will have a rating of 600 (or 100%)(a 100 score for each measure). However, a stock with a composite score > 420 is normally a financially sound, well rounded company and undervalued too.

The script itself does this and generates a CSV file of our entire universe at the time of running it.

Our Trending Value Portfolio is then narrowed down to the 10% of stocks with the best average score. We sort the 10% by their trailing six-month performance (the trending part of this screen) and select the top 25 from the list.

At present I am doing this manually so I can clearly see the results of all the calculations and be sure that no silly errors have occured. I will then manually filter the 10% in the previously mentioned manner before selecting a final shortlist of 25.

Turnover

O’Shaughnessey rebalances his portfolio annually. The cost (commission) paid on investing in a portfolio of this size will cost you a fair whack hence only rebalancing annually. With that in mind, this script should only need to be run once a year - in theory.

Testing

I am testing the results of my script using one of the many stocks apps available on iOS: Stocks+. I picked an amount I wanted to ‘virtually’ invest and split it evenly amongst the 25 shortlisted companies.

It should also be possible to back test this script/screen using historical data. However, i’ve not gotten round to doing this yet.

Download

The repository for this script is on Github here. Feel free to report issues or submit pull requests.

Or you can download the repository as a zip, right now.