A year of stock and fund trading

Finance
Finance by CommScope (CC-BY-NC-ND)

A year ago, I started saving up by investing on the fund and stock market. In this post, I will share some of my learning, strategy (or lack thereof) and results from a year of fund and stock trading. I will skip a lot of small details and instead provide a high-level overview of what I have been doing.

Disclaimer: This is not financial advice. It is purely informational. I probably don’t need to write this disclaimer since I’m not a business, but now that I’ve done it, you have no excuse to blame me if you follow the same strategy as me and lose all your money :-)

Starting out

The financial market is very overwhelming at first, because there are so many terms and weird financial instruments. To me, stocks and funds are the easiest to grasp, because they are the most intuitive. Owning a stock is like owning a small part of a company. Owning a share of a fund is like owning a small part of an investment portfolio. It thus made sense to focus on stocks and funds to begin with.

Even with a limited scope, it took many hours of reading to learn about indicators such as P/E number and sharpe ratios as well as finding tips on different strategies for investment. My bank had some excellent and digestible articles (in Swedish, sorry) that were very helpful.

The first basic conclusion from this research was this: when investing, spread the risk. This is colloquially known as “don’t put all your eggs in one basket”. There are different ways of spreading the risk, and I decided to follow three common suggestions:

  • Invest at regular intervals over time instead of irregular one-time payments. This ensures that investments are made during both economic ups and downs.
  • Invest in multiple regional markets (e.g. North America, Europe, Asia). Trouble in one market might be offset by an economic boom in another.
  • Invest mostly in funds. Funds generally have at least 50+ stocks in their portfolio which is good for spreading risk over many companies.

I also wanted to get my feet wet trading stocks, so I decided to divide my monthly savings with 80% for buying funds and 20% for stocks. That has been a bad idea so far, but I will get back to that later.

Finding the right fund

Learning about fund trading is one thing, actually starting to buy them is another. The first task was to figure out the regional composition of the funds. Reading various blog posts and using my bank’s automatic fund portfolio generator, I got an overall idea of what the composition could look like. I ended up with this distribution:

Pie chart of regional composition of funds.
Regional composition of funds.

A few notes on the chosen distribution (and perhaps specific for my situation):

  • It might seem curious why I put 10% in Sweden, but this is simply because I live here :) Interestingly, the portfolio generator from the bank suggested investing a full 25% in Sweden. I decided this was too much, but 10% is still a pretty big chunk for a single country.
  • “Emerging Markets” is a blanket term that actually often covers multiple regions, for example China and Brazil.
  • Asia/Pacific funds often mostly exclude Japan which has its own category of funds.

With the regional composition figured out, the next step was deciding what specific funds to invest in. This was not as easy.

Passive vs Active Funds — a brief digression

Feel free to skip this section if you know what active and passive portfolio management is and/or if you are not interested in listening to me rant about it. I included extra information on this topic, because I think it is important and interesting. There will be a conspiracy theory as well.

It is useful to mention the two primary portfolio management styles for the funds I’m interested in: active vs passive. The distinction was very confusing to me at first, so it might be helpful to first re-iterate what they mean:

  • Passively managed funds are also known as index funds. They follow a specific market index (e.g. S&P 500, MSCI World Index), and their stock portfolio is a more-or-less direct reflection of that index.
  • Actively managed funds have investors that make active decisions (hence the name) on what to buy and sell with the goal of out-performing “the rest of the market”. This is a little vague, but it often means that they try to beat a specific index or simply “outperform the market”.

I think “passive” is a bit of a misnomer. From my perspective, “passive” means something that just “takes care of itself” and that is not the case of passive funds. Most of them are still managed by an actual human that buys and sells the stocks in the fund’s portfolio.

Besides the management style, the most important distinction between a passive and an active fund is the management fee. Passive funds are usually much cheaper than active funds. The management fee for a typical passive index fund on my trading platform ranges from 0.2%-0.5% while active funds range from 1%-2%.

This difference does not sound like a lot until it is visualized on a graph, so allow me to do that now. Assume you save $100 per month over 25 years, i.e. the total investment is $30,000. Assume also that you have a 7% rate of return each year. The graph below shows the value of the investment over time for different management fees.

Chart of fund value over time at various fund values
Fund value over time with 7% return rate and various management fees.1

The result is quite interesting. Without any fees, the $30,000 investment ends up at about $81,007 (a 170% increase). That’s pretty good, but probably unrealistic since most funds have at least a small fee. With a cheap fund (0.2% fee), the money would have reached $78,490. Still pretty good! With a more expensive fund (1% fee), the money would have reached $69,299.

Think about this for a second. With just 1% in fees, the total outcome is 12% lower ($9,191) than if the fee had been just 0.2%. For a fund with 2% in fees, the outcome is a whooping 24% lower. The reason for this is that fees are double-bad. Not only do they dig into the investment each year, but that lost capital is also lost potential for future investment. So the effect is much worse over time as seen on the graph.

At this point, it is relevant to ask the big question: do active funds have higher rate of return than passive funds?. This is literally a million-dollar question, and I don’t know the answer. I can appeal to your intuition and share some articles though.

First, how well should an active fund perform for it to be a better choice than an index fund? In rough numbers, a fund with a 1.5% fee needs to be 1.3 percent-points better than an index fund with a 0.2% fee. Let’s say the index fund has a rate of return of 7% like in the example above. The active fund thus needs to have a rate of return of 8.3% just to break even with the index fund.

Here is a follow-up question: do you think it is possible for an active fund to outperform the market for 25 years straight? Of course not, it is just not possible. Or at least: it is not mathematically possible for all active funds to do this.

Don’t take my word for it though. Here are a few articles that I found: Almost no one can beat the market, Mission Impossible: Beating the Market Forever, How Many Mutual Funds Routinely Rout the Market: Zero.

The only people that say it is possible to beat the market are the fund managers. And for good reason. If a billion-dollar fund takes out a 2% fee, that is a very hefty salary for potentially only a handful of managers. In fact, it is a salary so overwhelmingly high that it becomes very difficult to trust that they always have their investors’ best interest in mind.

This is actually my biggest issue with active funds, and I admit it is ideological and not necessarily rational. I hate the thought of some rich manager investing my money with absolutely no risk for themselves. If a manager under-performs, they might lose their job, but I lose all my money. If I invest in an index fund and the market crashes, I cannot really blame the market.

This section has already gotten a bit out of hand, but I think I promised a conspiracy theory at the beginning of this section so let me end with that: could the term “passive fund” be cooked up by the financial elite to make it sound less attractive? We are often told to have active lifestyles, make active choices in life, participate actively in our community etc. It is very rare that “passive” is a good thing. So when choosing a fund to invest in, we might subconsciously be turned off by “passive” funds. Think about it. It all makes sense! :-)

Low cost funds for the win

It should be clear by now that I have a preference towards passive index funds. However, that is not to say that active and passive funds cannot coexist. There are a few fairly cheap active funds out there, so when I finally had to actually choose funds for my portfolio, I decided to take a somewhat mixed approach based on the following rules of thumb:

  • Keep fees as low as possible, preferably below 1%.
  • Invest in about two funds per region. At least one passive and possibly one active.
  • Only consider funds with a sustainability profile/statement.
  • Look at past performance, sharpe-ratio and Morningstar rating but do not get hung up on it.

I ended up with 12 funds initially last year, and have changed out two funds and added another, so I now invest in 11 funds monthly, but I still have 13 funds in the portfolio. I could share the entire portfolio at this point, but I could not find any good tools for sharing a Swedish fund portfolio (besides typing them out in Excel), and the information will not really be helpful for anyone living outside Sweden anyway.

Here are some numbers though…

  • The average management fee for my portfolio is 0.47%. The lowest fees is 0% and highest is 1.8% (I recently changed this for my monthly transfer). Without the most expensive fund, the average fee is 0.35%.
  • The investment has grown between 6% and 10%. The number varies quite a lot during any given month.
  • The highest growth for a single fund is 14.4% and is a fund for the Emerging Markets region.
  • The lowest growth for a single fund is 6.3% and is a fund for the North America region.

A note on growth: The numbers I quote above are the actual growth in terms of how much extra money I would get if I sold all funds right now. The growth of the portfolio in terms of the market itself is calculated by my bank to be 25.9% in one year. Honestly, I don’t understand how this number is calculated, but it looks pretty on a graph when compared to the Dow Jones World Index:

Hopefully, my fund portfolio will continue to grow steadily. Anything above 5% is a win for me, so I am currently very happy with how it is going.

Not a stock trader

While my funds have done well, my stocks have not. I currently have a few shares in 19 different companies. My buying strategy has been a bit random, and I have tried out different approaches to trading stocks. Here are a few highlights:

  • The Cash Dividends. Many big companies pay dividends. This sounded fun, so I bought some stock in a few boring, stable companies like Nordea (a bank), Telia (a phone/telecommunication company) and Knowit (an IT consultancy). I got my first payouts a few months ago, and actually, it was fun to get extra pizza money during the early months of the year! Besides the dividends, the stock prices of these companies are also the most stable with the exception of Knowit that has increased 124% in one year and is pretty much the only reason why my portfolio is not showing red numbers (yet).
  • The Stock Emissions. When companies want extra money for whatever reason (e.g. more research, new investments or paying off loans), they will often emit new stock. I participated in one of these emissions for a company called BrainCool that produces and sells equipment for medicinal cooling of the brain. So far, the stock has not been very cool though. The value of my stock is down 48% as of this writing.
  • The IPO. I participated in the initial public offering (IPO) for a company called Isofol Medical that produces a drug used during colorectal cancer treatment that potentially works better than existing drugs. The value of the stock fell immediately after the IPO and my stock is now down 20% so it was also not a good investment.
  • The company in trouble. I purchased stock in Eltel without realizing the company was in an obviously bad shape. I basically just looked at their (previously) fairly generous dividend payouts, and failed to pay attention to the numerous scandals and trouble the company was going through. Because of their problems, they decided to not pay any dividends to investors this year. But not only that, the stock price itself has dropped 51% since I bought it.

Overall, my stock portfolio has increased 0.21% in value and that includes cash dividends. Let’s round that to 0%, and the only redeeming factor is that at least I did not lose any money (yet).

The last story in the list above is the best example of my incompetence as a stock trader, and why I should probably just stick to funds. After all, I will always be an amateur trader, and I just do not have the time and skills to really analyze the market and make informed decisions about my stock purchases.

The path forward

A year ago, I started at $0 and grew my portfolios slowly using a monthly money transfer. As you can imagine, this means my total investment is still quite small. This also explains why I have been willing to take some risks and experiment a bit with stock trading. Losses suck, but they are not catastrophic when the investment is small. However, as my savings keep growing, there is more at stake. My investments are probably going to be a significant portion of my future pension, so having a fairly stable growth using funds is probably better than trying my luck with stock trading, although the yields are potentially much higher.

I have not fully decided whether to give up on stock trading completely though, because there is a certain allure to the idea of owning a part of many companies directly as well as the dream of the 5,000% stock increase.

How to shape the path forward is still an open question for me. Writing this post has helped gather my thoughts on the subject, but I have not concluded anything yet so I will probably keep doing what I am doing. Money decisions are always difficult :-)


  1. I generated the graphs in this sheet

Generating music with deep learning

Automatic, machine-generated music has been a small interest of mine for some time now. A few days ago, I tried out a deep learning approach for generating music… and failed miserably. Here’s the story about my efforts so far, and how computational complexity killed the post-rock.

The spark of an idea

When Photo Amaze was created in 2014, I thought it would be fun to have some kind of ambient music playing while navigating through the 3D maze. But I did not want to play pre-recorded music. I wanted it to be automatically generated on-the-fly, based on the contents of the pictures in the maze.

That was the spark. A picture is worth a thousand words, so why can’t it be worth a few seconds of music as well? For example, take a look at this picture:

Mountain with running water in the foregroundmotional impact, like the ambient sound of a running water stream or the whistle of the wind picking up speed over the mountain.

Since I can make a connection between photo and music, perhaps a machine could do this automatically as well. This is not a novel idea, but it is a nut that has yet to be cracked, and it was an intriguing idea to start exploring.

Hard-coded music mappings

Modern browsers have all the ingredients necessary for doing both image analysis and sound generation. There are numerous JavaScript libraries for analyzing and manipulating pictures, and the Web Audio API makes it possible to create synthesized sound in a fairly straightforward way. Thus, it made sense to start here.

The first experiment I did was to create a more-or-less fixed mapping between an image’s content and some kind of sound output. The high-level idea of the implementation was to simply map brighter colors to brighter sound notes. The steps to produce the output sound were something like this:

  • Find at most 200 “feature pixels” in the input image using trackingjs.
  • For each found “feature pixel”:
    • Calculate the average pixel value between the three RGB color values. This produces a single number per pixel between 0 and 255.
    • Normalize the pixel value from 0-255 to 20-500. This produces a base frequency for the output sound.
    • Create a sine wave oscillator using the Web Audio API for the pixel value.
    • Combine the oscillators to a single sound output.
    • While playing the sound, randomize the frequency of each oscillator slightly over time.

Using this approach, an image would be turned into a randomly changing output sound consisting of about 200 sine waves, each with a frequency between 20 and 500 Hz.

Here is an example output using the mountain from above as the input image (the red dots mark the found “features” of the image).

That might not sound terrible, until you realize that the sound is basically the same for any input image:

Mila might be a monster dog, but that output is just too dark :-)

There were a ton of problems with this implementation, to the point that it was actually outright silly. For example, the “feature pixel” selection mostly found edges/corners of the image, and using just 100 pixels as input corresponded to just 0.01% of all available pixels in the test images. Another problem was how the final pixel value was calculated from the average between the red, green and blue value of the pixel. Some colors arguably have more impact to the viewer than others, but this fact is not captured when taking the average.

Even with all its problems, the first experiment was a good first step, considering I did not know where to start before. It is possible that with a lot of tweaks, lots of new ideas and lots of time, this approach could start producing more interesting soundscapes. However, the downside of the approach was also that the music creation would always be guided by the experimenters: the humans. And I wanted to remove them from the equation.

Machine learning to the rescue

The second experiment ended before it even really started. It was clear that some kind of machine learning was needed to move forward, and it seemed that an artificial neural network might be the solution.

This was the idea:

  • Use every pixel of the input image as a single input node of the neural network.
  • Treat every output node as a single sound sample.

For the purposes of this blog post, everything that happens between input and output nodes of the network is largely hidden magic. With that in mind, here is how the network would look (P1 – Pm are the input pixels and S1 – Sn are the output samples):

Photo to sound neural network
Mapping a photo’s input pixels (P1-Pm) to a soundwave output (S1-Sn).

To get an idea of the size of the network, consider this: the mountain test image from above is 1024 by 683 pixels, so the network would have 699,392 input nodes when using images of that size. Digital sound is just a collection of amplitudes in very tiny pieces called samples. The most commonly used sampling rate for music is 44.1KHz, which means that every second of digital music consists of 44,100 individual samples. For a neural network of this design to produce a five-second sound, it would thus require 220,500 output nodes.

The intentions were good but the implementation never happened. After having the initial idea, I started Python and tried to simply read and write soundfiles, but it didn’t go so well, and the weekend was nearly over, and, “oh a squirrel!”… and the code was never touched again.

Machine learning is great, but the motivation was suddenly lacking, and the project was put on ice. This was about two years ago, and the project was not revived until quite recently.

AI art

Deep learning has been steadily on the rise in recent years, often outperforming other machine learning techniques in specific areas such as voice recognition, language translation and image analysis. But deep learning is not limited to “practical” use cases. It has also been used to create art.

A well-known example of “AI Art” is Google’s Deep Dream Generator. The software is based on an initial project called DeepDream which produced images based on how it perceived the world internally. Some of its images were even shown and sold at an art exhibition.

The A.I. Duet project shows another interesting use case for deep learning: the creator of the project, Yotam Mann, trained a model that can produce short sequences of piano notes based on the note input of a human. So if I played C-D-E, the software might respond with F-G-A although the result would most likely be slightly more interesting than that.1

A.I. Duet is impressive, but it still has a big limitation: it only works for specific notes for a specific instrument. So while the result is amazing, what I really want is more complex arrangements and raw audio output. Even so, the above examples show that deep learning is a powerful and versatile machine learning technique, and it is now finally becoming more feasible than ever to achieve the goal of creating music using AI.

Mila drawn with Deep Dream
Although it is not really relevant to this blog post, I could not help myself: here is the Mila image from above processed with default settings of Deep Dream. It is slightly disturbing to see that… chicken?… coming out of her left paw. Thank you for ruining my sleep, robot!

The bleeding edge, where the story ends

While doing some research on the latest state of the art for machine-generated sound, I stumbled upon yet another Google project called WaveNet. In an interesting blog post, the authors of WaveNet discuss how their research can be used to improve text-to-speech quality, but what is really exciting to me is that they also managed to produce short piano sequences that sound natural (there are some examples at the bottom of their blog post).

The big surprise here is that the piano samples are not just based on specific notes. They are raw audio samples generated from a model trained with actual piano music.2

Finally! A tried and tested machine learning technique that produced raw audio. Reading about WaveNet marked the beginning of my final experiment with music generation, and is the entire reason this blog post exists.

I found an open source implementation of WaveNet, and to test the implementation, I wanted to start simple by using just one sound clip. For this purpose, I extracted an eight-second guitar intro from the post-rock track Ledge by Seas of Years3:

My hope was that by training the model with a single sound clip, I would be able to reproduce the same or a very similar clip to the original to validate that the model produced at least some sound. If this was possible, I would be able to train the model with more sound clips and see what happens.

Unfortunately, even with various tweaks to the network parameters, I could not manage to produce anything other than noise. Sharing an example of the output here is not even appropriate, because it would hurt your ears. The experiment ended with an early failure.

So what was the problem? I soon realized that even with this fairly simple example, I had been overly optimistic about the speed at which I would be able to train the model. I thought that I could train the network in just a few minutes, but the reality was very different.

The first warning sign showed itself pretty quickly: every single step of the training process took more than 30 seconds to complete. In the beginning, I did not think much about this. Some machine learning models actually start producing decent results within the first few steps of training so I was hoping it would be the same here. However, after doing more research on WaveNet, it became clear that training a WaveNet model did not just require a few learning steps, it required several thousand. Clearly, training WaveNet on my machine was completely unfeasible, unless I was willing to wait more than a month for any kind of result.

Where do we go from here?

Machine learning has been rapidly evolving in recent years, propelled by software libraries like TensorFlow, and the technology is more accessible than ever for all kinds of developers. But there is also another side of the coin: in order to use the state of the art, we are often required to have massive amounts of computing power at our disposal. This is probably why a lot of high-profile AI research and projects are produced by companies like Google, Microsoft and IBM, because they have the capacity to run machine learning at a massive scale. For lone developers like me that just want to test the waters, it can be difficult to get very far because of the complexities of scale.

As a final example to illustrate this point, consider NSynth, an open source TensorFlow model for raw audio synthesis. It is based on WaveNet and on NSynth’s project page, it says:

The WaveNet model takes around 10 days on 32 K40 gpus (synchronous) to converge at ~200k iterations.

Training a model like that would cost more than $5,000 using Google Cloud resources4. Of course, it is possible that a simpler model could be trained faster and cheaper, but the example still shows that some technologies are most definitely not available for everyone. We live in a time where there is great access to many technological advances, but the availability is often limited in practice, because of the scale at which the technologies need to operate.

So where do we go from here? Well, computational complexity killed my AI post-rock for now, but I doubt that it will take long before significant progress is made in this field. For now, I will enjoy listening to human-generated music. In a way, it is re-assuring that machines cannot outperform us in everything yet.


  1. The video explaining how A.I. Duet works is quite good. 

  2. Describing how WaveNet works is beyond the scope of this blog post, but the original paper for WaveNet is not terribly difficult to read (unlike most other AI research). 

  3. Seas of Years’s album The Ever Shifting Fields was one of my favorite post-rock albums of 2016. I recommend a listen. 

  4. I used Google’s pricing calculator with 4 machines, each with 8 GPU cores. 

Apache Beam MongoDB reader for Python

The Apache Beam SDK for Python is currently lacking some of the transforms found in the Java SDK. I created a very minimal example of an Apache Beam MongoDB read transform for Python that might be useful for someone else looking for an answer.

I will update this post in the future if/when the Apache maintainers include support for MongoDB in the SDK. I know I could contribute to the project directly, but I don’t have time for it right now unfortunately :-)