There are many reasons in finance to look for risk free rates. This is a very common task, usually the expected returns on risky investments are compared to the returns from a risk free asset to help us evaluate if the investment is worth our time and money.
Some practical examples are when choosing the stocks and bonds for our investment portfolio or deciding if is a good idea to buy a specific real estate property.
By definition a risk free asset is the one we know the expected returns, have no default risk and there can be no reinvestment risk. It will depend on the investment location but most likely the free risk rate will came from a government security.
For this exercise I build a simple model, with less than 50 lines of code, that allows us to automate the query of two security rates in the Mexican financial market, from a specific date bracket.
To explain better this model, I draw a process diagram illustrating the main steps that will be performed:
For the first part, the model will ask if the asset to be valuated is comparable within the Mexican market. If not, it will tell us to choose an asset that better fit the model and end the process.
This could be easily expanded to other markets, like in the US financial market, but to keep it simple I limit the model to only one country, Mexico.
In finance time is the most important variable, so it is always a good idea to differentiate when our investment is going long-term or short-term. For the next step in the model it will require us to specify if we are looking to perform short or long-term, depending the answer we set, it will discriminate the instruments that will be using.
If we are looking for a short-term analysis, like for example when buying a stock, it will chose the most common security in Mexico, the CETE 28, or by it’s name in English the 28 day Treasury Certificate.
If instead, the investment we are looking is in a longer time frame, like in the acquisition of a fixed asset, the model will be choosing the 10 year Mexico Bond as reference.
Now the model knows which instrument to look for, the next step is to specify if we will need the rates in a daily [D] or a monthly [M] frequency. For most valuation tasks it will be sufficient with this two, so choosing other frequency will return a message indicating there is no available data and end the process.
The last input data the model will require is the date brackets, indicating the start and end we need to query. For this input data request format is crucial so after asking the question it will remind us the date format “YYYY-MM-DD”.
With all the information gathered we could consult in the Bank of Mexico website manually, but if we are doing this task frequently it might be a good idea to automate it. Fortunately for us, this institution, as most government sites in Mexico, has a public API at the SIE, or in english the Economic Information System, where we can consult data series from different indicators/instruments like the Cete and Bond.
To request the information I build a function named “consultaBMEX”, which will recall the series ID, the start and end dates, as well as a token.
With this information, the function will request the data from the IPA, save it into a JSON file and then convert this file into a pandas dataframe.
Now with our data in a dataframe, the next part is just to prepare the data series, by looking for inconsistencies, converting all entries into a float and returning out data clean.
For example if we look for a Cete28 > Monthly > 2010–01–01 / 2021–01–01 will result with a list like the following:
Below I share the code for a complete view of how the process diagram translate into python:
Although this was a very simple exercise, it could be easily scalable or incorporated into other programs. For example, if you only want to use the program to gather information, you may want to detail a little more some of the manual tasks and automate them so you don’t have to answer too many questions.
But also, you may want to incorporate this model as part of another more complex program, like in a CAPM valuation model and use it to evaluate investment opportunities in order to have a better decision where to allocate your resources.
Hope you find it interesting but mostly useful for your projects.