DIY: Your (Thai) Mutual Fund Portfolio Summary with Python (เขียนไว้อ่านเอง)


Those of us well-versed in the capital market will know that there are plenty of tools for accessing data of Thai common equities or even derivatives (Yahoo Finance or Morningstar) but the choices aren’t at all that great for Thai mutual funds. Since not everyone is “lucky” enough to make it rain through equity arbitrage, it is often wiser to build your retirement nest eggs through mutual funds. Loosely borrowing from the perpetual lessons left behind by Benjamin Graham, mutual funds are great due to the following reasons:

1.       Lower risk due to diversification (imagine a bucket of assorted sweets, some you will love and others not so much, but at the end of the day you end up a happy kid!)
2.       Cheaper transaction and management fees (especially index funds)
3.       You can focus on your professional job/growth (and not be that guy at the water cooler who tells you that he just made 200% return day trading at his desk at the expense of your employer)

So there you have it, equity trading is not ideal for us busy working bees who are ok with the long term market returns (believed to be 6-7% annually). Most of us therefore end up buying mutual funds (yes, those LTFs, SSFs and RMFs are mutual funds with funny names) for the purpose of retirement savings as well as tax deduction.

I realized that there are PAID tools and apps out there to help you monitor your mutual fund portfolio. However, not everyone can afford those fees (at least not I)! On top of that, analysis and visualization on those tools and apps are often extremely limited.
Note: Some of those paid services still show your portfolio breakdown as a pie chart. If someone thinks a pie chart is a good way to visualize data, you probably shouldn’t trust them (don’t get me started on those “donut” charts). People have difficult time gauging the differences in angles so a stacked bar chart is always preferred over a pie chart.

Thus I am writing this post today to demonstrate how Python can help you perform all the analysis you need – FOR FREE! Why Python and not Java or R? Python is like Tiger Balm: it is quick, ubiquitous and solves most problems. You can achieve most things with Python which can be scripted up on any contemporary personal computer.

First, I need to state that the sample data here does not represent anyone’s portfolio nor is it supposed to represent the funds’ performance in any way.

Let’s get going! The prerequisites of this little task: NOTHING! You don’t need to know anything to carry out what follows, but knowledge of some Python and its libraries can be helpful. Make sure you have these libraries installed:

1.       pandas
2.       numpy
3.       investpy
4.       matplotlib

If you are new to Python, you can use pip install to get these libraries – StackOverflow will solve any of your problems you may encounter!

Investpy is a great API in pulling mutual fund data from which actually has Thai mutual fund data!
Note: if you happen to know any other API that can retrieve Thai mutual fund data, help a brother out and let me know!

First, import our needed packages:

Then we will initialize some dates for the purpose of pulling data through investpy API:

Here, “startDate” can just be the day you started your portfolio. For me, it is just the day that I started my first job right out of college and began earning.

In this little DIY, we will import data from two files, one spreadsheet and one text file. The spreadsheet is the record of your transaction of each fund, it looks like:

The text file just contains a list of all the funds in your portfolio:

There is a caveat here, the names on these two files must match, and they must also be the names that these funds are titled on

Now we are ready to import the files:

Next, we will use the investpy API to pull the mutual fund data from the web. I use two lists that I append to for each mutual fund in my portfolio. The list “fundCost” will contain the total cost of each fund in your portfolio while the list “fundVal” will contain the total value of each fund in your portfolio as of the latest close price.

Having done that, we can start visualizing! Matplotlib is our best friend here. I haven’t really formatted it to look quite appealing yet but it will do the job for now.

The output is as you would expect:

This is an easy way to visualize how much you have invested in each fund as well as the current value.
We can also compute the return by combining the lists into a new dataframe.

So plotting the returns in a similar manner can be done:

Voila, we now have the return plotted:

That’s all I have for demonstration today. Sorry for the lack of formatting of the figures. There is plenty of room for analysis that I did not include here for simplicity. I encourage you to continue to work with the data and develop further analyses as suited.

I hope you will benefit from what I have shared here. Remember - there is one thing we, whether aristocrats or proletariats, have in common: inflation as a lifelong enemy. At the end of the day, no level of data analysis will bring us financial security if we lack the discipline to save, invest smartly and continue to learn as we grow older and wiser by the day. It is okay to be speculative at times (go ahead and gamble it a bit in Bitcoin or those so-called "small-caps" but don't get carried away!

Until next time!

อ่านกระทู้อื่นที่พูดคุยเกี่ยวกับ  Python การลงทุน นักลงทุน กองทุนรวม มนุษย์เงินเดือน
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่