Building my first trading algorithm and how you can do that too

Ken Mok
3 min readApr 7, 2021

TL;DR — I build my first trading algorithm with Python, and how you can do this following my process

What this post is about and why should you be reading this?

This is not an article with code that you can take and implement right away (if you are looking for this, there are plenty of resources only clicks away from Google).

Instead, this post may give you something to consider, if you are interested in the process and would like to build your first algorithm

Why building a trading algorithm?

There are many advantages of using algorithm in trading and the major ones for me are

  • automatic and speed of execution
  • eliminate human error stemmed from emotional and psychological factors
  • the strategy can be backtested with historic data

One may argue a faulted algorithm may do much more damage in specific scenarios — this is a valid point and this is why vigorous backtesting is important

What was the process like?

If you are convinced and ready to build your algorithm, here’s a plan to help you kickstart the process

  • research — this is the most important phase. If your goal is to deploy the algorithm and doing actual trade for you, you have to consider which trading platform to use. Some platforms have API connection available and support multiple programming language. The other thing to consider is the algorithm to use, while there are many to choose from (or you can build an original one), a good research can help you to identify some potential candidates to start with
  • plan — another important stage to plan out what to build and the order of building. My suggestion is to start with the most simple algorithm and building MVP (minimum viable product). It will help you understand the whole picture in the shortest period of time and you can always improve in the next iteration
  • build — if the first two steps done right, this one should be easy — just building out what you have researched and planned
  • test and optimize — this is the fun part. Now you have built the algorithm, it is time to test against some historic data and see how it perform. The idea is to compare against a benchmark (usual the index) given certain period of time. My suggestion is to test using different time period (e.g. bull market vs. bear market). By looking at the result, you may find areas you want to improve the algorithm, it is now time to implement the changes and see its impact to the result. For example, below is a result of backtesting with the algorithm inspired by way of turtle.
A sample result from one of my backtesting
  • trial run — if you are satisfy with the algorithm, before trading with real money, it is best to trial run it for a period of time to see how it does in real market
  • deploy — now it is the time to let the algorithm do the magic. But don’t just sit and do nothing. Monitor the performance on regular basis and see if any intervention needed

What’s next?

If you have read until this far and are ready to build your algorithm, I would love to hear from you and know your experience in building your own algorithm.

--

--

Ken Mok

I write random things, irregularly Probably I will be writing on the road with mobile and I fail to quote any reference properly But I hope they are interesting