Backtests : Separating luck from skill

Introduction

In our previous post, we say that, when running a screen it shows the stocks that pass our current filters. But how do we distinguish between whether using that same approach with the same criteria would have really worked in the past, or was just a result of luck and repetition from trying many rules till we found a winner?

First let’s see what role luck even has here. Imagine the Texas Sharpshooter fallacy: A sharpshooter fires multiple shots at a barn, before walking up, painting a target ring around the densest cluster of holes and calling himself a master. This is our first mistake: peeking at results and then choosing rules does not give accurate results.

Another mistake occurs when we only study the companies that still exist, also called survivorship bias. This same scenario occurred during World War II, when military analysts of the Allied forces studied returning bomber planes to identify which areas had to be reinforced with armour. Naturally, they settled on the spots with the most bullet holes, namely the wings, fuselage and tails. However, they made a crucial mistake, as pointed out by statistician Abraham Wald: They were only considering the planes that had returned, meaning planes could take damage in these spots and still fly. On the contrary, planes that had been hit in other areas (such as the engines, fuel tanks and cockpits) didn’t survive and make it back, making those the areas that actually needed reinforcing.

Similar mistakes are prone to occur when we run screeners, making backtests a verification requirement. Let’s see how they work.

A backtest works by replaying the filtering criteria we have using past data, in order to model what stocks would have passed if the same screen had been run at that date using data only available at the time. This allows us to see how a screener would have performed in the past without actually risking capital. It also follows those stocks through the years to see what they earned compared to the market.

A backtest can be broken into 3 steps: Collecting past data, running the screen on the data and analysing the results. Each step is deceptively hard, which I experienced when I tried to build my own backtest model.

For the first step, this involved collecting past data. I used Trendlyne, for which I already had a paid subscription, to rewind to the end of September of each year from 2012 to 2025. The data for each year comprises a list of passers, which I downloaded as a csv. However, the limits imposed by the rewind tool in Trendlyne capped my export data, allowing me to collect only the data of stocks that passed and not all stocks in the stock universe at a given date. Data collection mistakes such as mislabelling a September 2016 data file as 2026 and having mismatching columns did occur, but they were luckily fixed during the data check.

The principal risk we face here when collecting data is survivorship bias. This comes mainly in the form of not counting firms that had been delisted, merged or become insolvent. However, since whether or not Trendlyne drops delisted firms hasn’t been established, this will have to be checked by counting how many firms known to be delisted have appeared in earlier lists. This will be hard to do completely due to the lack of complete data on delisted firms.

Just like Abraham Wald and his discovery, the Texas sharpshooter fallacy and luck are traps for our other steps in similar ways, which we will cover next.

Glossary

Backtest : A model that tests rules on past data to see how it would have performed.
Benchmark : A stock index or or standard you use to compare your stocks against (eg: Nifty 50, S&P 500).
Excess return : Portfolio’s return – benchmark’s return ( the excess amount you earn from your screen with respect to how much you would have earned had you just invested the same amount in a stock index.)
CSV : Comma separated values, a plain text file format where tabular data is stored with comma separators.
Delisting : Permanently removing stocks of a listed company from a stock exchange.

Note: This is a learning project, not investment advice.

The Stock Market : The largest Trading Behemoth the world has ever seen

Part 1

In the busy and fast-paced lives we lead, we rarely notice how everything around us is constantly improving, updating and changing. From your local grocery store to the S&P 500, prices keep fluctuating everywhere. All these actions seem cluttered, distorted, and too fast to comprehend. However, a relatively simple system runs behind everything. It is the flowing river that connects all the streams: The economic law of Demand and Supply.

Most people generically coin demand as the wish to buy a product. Yet, the real meaning is much deeper. In economics, demand is termed as the willingness and ability to buy something. Once someone fills both these criteria, his wish can be called demand. Supply is similar, and refers to the willingness and ability to sell when. When people who can both demand and supply come together – Volià! – we have a market!

Now, the interaction between the people who demand and the people who supply is what determines the price of nearly everything, from fruits to stocks. This occurs in a relatively simple fashion ; people who supply (let’s call them person A for simplicity) wish to sell their product to people who demand (person B). However, person A and person B now have to come to a reasonable price : too low and person A wouldn’t be willing to supply, too high and person B wouldn’t have to demand. The price both parties agree upon is called the equilibrium price, and is the price the product will sell for till this negotiation happens again.

While my example used two people, the real world generally involves all those wanting to demand and supply in this equation ; this could be as little as two people to as much as the whole world! In stock markets, these negotiations happen continually, and between myriads of people, resulting in continually shifting prices.

Now, these shifting prices can go two ways : up or down. What determines its direction has to do with how many people are there on the supply side and how many on the demand side. Think of it as a scale ; when more people are there to demand than those to supply, the item becomes scarce, pushing its price up. On the contrary, if there are more people to supply, more of the item is available to circulate, pushing its price down.

The general aim of most investors in the stock market is to buy low and sell high. This unwritten law is the backbone to investing, and is the key to profiting from the market. However, this is much easier said than done.

Although I have only mentioned one, there are myriads of other factors that affect stock prices : Company financial condition, public sentiment, future forecast changes and even the climate has a hand in this! Now, although humans cannot weigh all the factors and make a probable prediction on stock prices, investors continue to confuse me by buying stocks no one would have bought otherwise, and proving the whole market wrong as they earn from skeptical stocks that could have soared or crashed.

Although at first glance the stock market may appear chaotic and cataclysmic, we now know of the peaceful system that quietly ticks underneath the noise. It governs everything, from our daily lives to our business ventures, and can be a formidable enemy to predict. Adding to this, the other innumerable influences add to the challenge.

My goal is to uncover the process behind stock selection, identify the core parameters that are taken into consideration and judge how accurate this method is. I want to do it all by computing my methods based on Mathematics, Statistics and Economics to finally make sense of this irrational clutter and to find the answer to the age-old question: What makes a stock truly worth buying?

Inflation : The double edged sword

For most people, every single day, hour and minute has something to do with money. Whether you’re in a supermarket, at the office, or with your friends buying ice-cream, these actions involve money. However, money hasn’t always been something that had value, though the items you purchase with it, have. Let’s explore a bit further.

Its original purpose

The reason why the need for money ever arose was quite simply, because of our needs and wants. People started wanting things that they didn’t have. But, way before money came into place, there was another system called the barter system.

Let’s say that in a imaginary caveman tribe of Uggs, one caveman (let’s call him Smugg) was living in his cave. Smugg became quite hungry and wanted to have breakfast, but he didn’t have a stone plate to cook on. So, he went to his neighbour Thugg and offered a portion of the deer he had caught yesterday in exchange for a stone plate. Thugg happily agreed and both cavemen went home to enjoy their lunch.

This form of exchange is an early example of the barter system. People ‘barter’, or trade, the resources they have for other resources that they don’t have but need or want. However, there was one drawback to this method. No one could accurately guess how much deer is equal to one stone plate, or how many sacks of wheat are equal to a goat. To address this issue, people started to compare their objects with something else that everyone could agree had the same value, such as gold.

All that glitters isn’t gold

People as far back as the early Egyptians started using gold and silver to act as a currency for their goods. Now, everyone could agree that one chicken is equal to half a bar of gold, and that a cow is two bars. This leads us to our first characteristic of money: it is a medium of exchange. Gold bars are also known as commodity money, since they have an intrinsic value of their own. This is why it was also generally accepted among the people of the past.

While the jingle of coins or the clanking of bars in your pocket would have been quite nice to hear, it ran headfirst into another problem which took some time to overcome: it was quite cumbersome. If you were going to buy a house, it would require quite a lot of coins or bars, which was quite difficult to carry in the olden days and made you an easy target for criminals. People tried to avoid this by boring holes in their coins and stringing them around their necks, but the real breakthrough came from the Chinese.

Tea, anyone?

The Chinese were starting to run out of the metals they needed to make their coins. As a result, they turned to a quite surprising alternative : Tea! Tea was actually preferred as a form of currency by nomads since not only was tea valuable, but it could be eaten and brewed into drinks that were thought to cure coughs and colds.

While this method did seem to be useful, it encountered yet another problem. Compressed tea couldn’t stand the test of time, as it began degrading. Since it couldn’t act as a store of value, the Chinese put their minds together and came up with yet another solution that we use to this day : paper! The first paper notes were supposedly used by the Chinese as a medium of exchange, and it ticked all the boxes. It was :

  • Portable – It was easy to carry around.
  • A store of value – It had a value that could be used when buying or selling.
  • A medium of exchange – It was used to bridge buyers and sellers.
  • Easily divisible – It had smaller denominations that could be used for smaller payments.
  • Durable – It could stand the test of time.

Modern times

The paper currency printed by the government in China actually represented silver and gold in the treasury. This was the same in America in early times, where each note printed represented an amount of precious metal in the treasury.

This however, was slowly stopped. The reason why money isn’t a commodity money anymore is because the world economy was growing rapidly, and needed enough money to keep up with it. Since precious metals were low in supply, the government instead chose to print money based on inflationary rates. This led to the introduction of fiat currency.

Fiat currency is not backed by any valuable item and is only accepted by us because the government says so. This also means that if everyone gets the notion that this money isn’t worth anything, we’ll have useless pieces of paper lying about.

Present day

Nowadays, the prices of our goods are based on inflation. For example, more people wanting biscuits leads to biscuit manufacturers increasing prices. This increases inflation, and happens to be a perpetual cycle.

The rising level of inflation also changes the value of money. For example, a dollar in 1900 can buy 14.3 gallons of gasoline, whereas the same dollar can only buy 0.3 gallons today. Inflation has both good and bad effects on the economy: It can help suppliers and sellers by increasing the prices of their products, but it can also adversely affect consumers who have to cope with rising prices.

The aim of investors is to be on the good side of inflation, in order to ride out the rising prices of stocks to much higher valuations (that often seem unheard of) in order to generate unbelievable returns.