Monitoring Your Portfolio with Python — One Year Later

Kelvin Tan
Python in Plain English
3 min readApr 19, 2021

--

Not as complicated as Schrödinger’s cat though. Photo by Jackie Zhao on Unsplash

This is a follow-up post on my original article on how we could use Python to monitor our investment portfolios (link).

A little more than a year ago, my portfolio was in a sea of red, reeling in the throes / throughs (sorry that was too punny to ignore) of the worst stock market crash since the global financial crisis a mere Chinese zodiac cycle ago.

In those days, if you believed in long-term market value, almost any well-diversified index fund or ETF would have been a buy.

Fast-forward to the present day, the opportunity landscape is now as barren as ever. The timing-the-market and time-in-the-market camps are no longer aligned. While it is far more pleasant refreshing your portfolio view nowadays, it is far less so when it comes to investing your excess liquidity.

Intuitively, I feel like a five-percent correction could happen at any time. At the same time, I would also not be surprised if equity markets decide to roar ahead for a few years to come without retracing significantly.

A part of me wants to just dollar cost average, while the other part insists on building a discretionary execution strategy.

As a long-term investor, I am actually very sensitive to transaction costs so I try not to behave like a trader — buying in and out unnecessarily. In fact, I am still feeling sore over the fact that my government-supported Supplementary Retirement Scheme (SRS) and Central Provident Fund (CPF) monies do not have access to a tax- and expense-efficient ETF, such as VWRA.

VWRA, or the accumulating or automatically dividend-reinvesting Vanguard FTSE All-World ETF has a world-leading expense ratio of only 0.22% per annum. The local feeder fund in Singapore, the Infinity Global ETF, has a shocking expense ratio of 0.72%! The 0.5% delta might not look like much now, but over a period of decades, the lost savings would have amounted 10%, 20% or even 30% or more of impacted wealth depending on the level of long-term returns.

Nevertheless, I could only focus on things that I have control over. One of which is determining my investment strategy. In terms of execution, I enlist the help of Python once again to build a signal generator for me to ‘buy’ or ‘accumulate’, or do nothing. Since my key invested assets are essentially a broadly-diversified market, with VWRA being the prime example, I would be adopting a buy-and-hold attitude for this exercise — i.e. no ‘sell’ signal required.

As a refresher, I would recommend quickly skimming the first part of the story to get caught up before proceeding.

The seven lines of code above would be inserted between ‘demo_tracker_1.py’ and ‘demo_tracker_2.py’. In these few lines we establish the price threshold, below which we would have a buy signal.

I have experimented with a mean less x times standard deviation of return, in which case the mean and std methods from pandas come to mind.

In the end, I decided to settle on the quantile method of taking a certain percentile — in this case 0.25 or 25% — of annual returns, calculated daily, as the threshold.

For the purpose of calculating annual returns, I use the pct_change method, assuming 252 days to a trading year. This is particularly useful for time series analysis with pandas.

The Python script would be helpful in quickly informing me when would be a good time to buy on a historical returns basis, but I would still need to make the decision on the split between how much of the cash flow would be allotted to the time-in-the-market (dollar cost averaging) pool, while the rest would be funnelled to the timing-the-market (accumulate on buy signal) pool.

Ah, such is the joy of investing.

More content at plainenglish.io

--

--