Simple Brazilian Market Index Analysis using Python

Guilherme Braga
6 min readSep 25, 2021

I have always been fascinated by economic related concepts and how society “reached this point”. The idea that Western society basically works with private companies competing with each other to satisfy a demand is pretty neat. I have never been able to fully pursue some of the topics that were interesting to me because I was busy attending computer engineering classes. Most of those classes were great because It made me look at things differently, and a lot of times, It made me think about how a problem could be solved with the least amount of effort. Not because being lazy is a desirable trait in an engineer, but because being efficient is.

So, I started reading some of the basic books on investments precisely with efficiency in mind (also because I kinda had to start to plan out financially for the long term).

One of the first things you learn when It comes to investing is that nothing beats good ‘n old patience and of course, diversification. I mean, that’s what John C. Bogle says in his book called “The Little Book of Common Sense Investing” from 2007. And honestly, It’s quite convenient to think that one is able to beat the average investor just by waiting and buying an index fund. When I think about the stock market, I can’t help but think about how things were back in the 1970’s: people with funny looking moustaches screaming apparently random numbers at each other, with a level of confidence that is not something you can just pretend to have.

The thing that gets me thinking a lot is that John C. Bogle’s book analyses the S&P 500, but I currently live in Brazil. Brazil’s Stock Market Index is called IBOVESPA, and It’s graphic does not look that much like the S&P 500 (that’s an understatement to be honest). But really, the problem is, the IBOVESPA is kind of going nowhere for like, 10 years now, once you start analysing values converted to american dollars. This conversion from BRL to USD might not only be helpful to see the bigger picture for a Brazilian investor trying to figure out what actually fits in their pocket, but also for foreigner investors looking at Brazil.

So yeah, what’s left for me? I’m unable to be a “Common Sense Investor ‘’ If I’m not in the USA buying Black Rock’s VOO? I mean, there is a S&P500 ETF in the Brazilian Stock Market, but what about an actual IBOVESPA ETF? There are like, more than 5 IBOVESPA ETFs here, are they all inherently garbage?

But hey, I like numbers and I like Python, so let’s see how things go for John C. Bogle’s book. By the end of this little experiment, I should be able to point out how dollar-cost averaging looks like for the Brazilian Market, but ultimately, what should I learn in order to become a better Python programmer.

So, first things first, I need info on the Brazilian Stock Market Index itself. Luckly, I was able to get what I needed pretty fast, on B3’s website (just the basics, I was not able to get the variation month by month). Now I have everything looking nice on a Google Spreadsheet that can be exported.

Spreadsheets are cool, specially when I don’t have to download proprietary software just for a small task.

Great, so, what do I do with this? First, I need to get some basic information from this with Python, like putting columns on simple arrays and printing that on my Linux terminal. That was possible because of two magic words: “import xlrd”.

Cool picture made with Carbon.

So all I have to do now is do some math. So, let’s say that given the variation for each year, I need to check out the accumulated variation over a set of years in order to compute the gain or loss of money, assuming that a theoretical ETF is going to variate accordingly over the years. I kind of have to state this because of dividends. For my basic study, I will focus only on the given data, and the contribution of every single time a to the final ammount of money accumulated. The accumulated variation may be calculated using the following operation.

So, now I have everything I need for a basic test. I’m going to evaluate two scenarios, from 1968 to 2018: “Investor A” is going to “buy the dip” every 5 years ($5000 investment each time the market drops significantly, for every 5 year period) and “Investor B” other is going to invest every year no matter what ($1000 dollar-cost averaging). Yes, the ideia is that “Investor A” has a crystal ball sitting in the closet, this person just knows when the market is considered “cheaper” every 5 years. By 2018, both investors should have invested initially the same amount of money. Some of the most important Python functions that I coded were:

Function that applies the accumulated variation from “start_year” to “finish_year”. At the end, I will add up the current value of each year’s investment contribution, for each Investor.
Given a year with negative variation, how long does Is It going to take to recover If you decide to Invest just before “a dip”.

And the results are kind of what I expected. “Investor A” ended up with $698236.25, “Investor B” ended up with $2048499.24 (almost 3 times more). And visually, that makes a lot of sense. The following graphics were made using matplotlib, and it worked surprisingly well.

Every single dot on the line represents the year an Investor buys the theoretical Ibovespa ETF. The Investor waiting to “buy the dip” is wasting a lot of time outside of the market, so apparently this person is kind of missing out on the smaller positive variations.

Another interesting information that one is able to get from this is the optimal duration of the investments. Apparently things have worked better so far on the long run, for periods above 5–10 years. I have computed the necessary time to recover from a bad year for every year previously analysed, and the results are also very interesting.

Not unusual to see your initial investment sitting at a loss on a five years period.

So hey, I got my answer right? Well, yes and no. Yes, DCA worked but I analysed way less data when compared to studies previously made. And at the moment, IBOVESPA is highly dependent on a small number of companies when compared to the S&P 500 (65 stocks in total), so that may defeat the porpouse behind diversification using ETFs to begin with. There is also a problem with Brazilian ETFs regarding taxation and administration fees (often higher than what is considered normal in the USA) when compared to american ETFs. Given the ammount of companies listed on Brazil’s Stock Market and exemption from income tax (If you sell low values each month), stock picking is often justified. And there is also the dynamic behind Brazil’s inflation and the value of the Brazilian Real (right now in 2021, things are not looking great), that adds another layer of complexity to this problem. Maybe the Brazilian “Common Sense Investor” should use a mixed strategy, or just stick to an ETF that has more variety.

Bottom line is, dollar-cost averaging looks promising and keeping stocks for the long run should be safer. That’s all I am confortable with given the simplicity of my experiment, but It was a nice Python exercise for me. Also, I think I should learn how to make graphics prettier and data “cleaner”, but this is something I could only visualise given this “hands on” experiment. I will be learning R programming language in the near future to see how that compares to Python.

--

--

Guilherme Braga

A Computer Engineer from Brazil. I write about my nerdy stuff, mostly about my experiences as an IT professional and Magic: The Gathering/boardgames.