Trading Bots — Why money should not be your main goal

An argument for the second “hello word”

Kelvin Kramp
7 min readJun 30, 2021
Rituals provide structure to our social environment. Photo of Chale Wote, the annual street art festival by Enoch Appiah

Python can extend the power of your brain exponentially through mass data analysis, but getting started with coding can be just as chaotic as entering any community within normal life. There are different versions of the language, different modules to achieve the same thing, different habits for testing and debugging code, different software programs you can use as a coding interface, etc. In everyday culture, there are rituals that give structure to the chaotic environment that we live in. We say “Hi” to our neighbours, we go to school to get a diploma, we comb our hair in the morning. These activities can be considered rituals that give structure to the interaction with our social environment. The activities have a positive effect by facilitating confidence and a sense of belonging among those who practice the ritual. However, they can also be very painful. Men who have undergone a traditional circumcision during manhood, have a high complication rate and, of course, pain. How senseless as this might seem to an outsider, the symbolic meaning of belonging to a group and achieving in-group status dominates the fear and pain of these individuals during the painful act. In this post, I will propose a similar ritualistic process for your coding career. Don’t worry, the one I suggest will not harm your precious sensitive overcivilized flesh, but it will involve a big friendly green snake that will hurt your bank account if you mishandle it…

Using Python to build a trading bot entails writing code that uses historical financial data to create useful predictions about future stock prices. If you want to achieve an effective algorithmic trading application you have to bring many different skills to the table: 1) create your strategy, 2) understand the necessary tools, 3) store and retrieve data in a reliable way, 4) tweak the subunits of a complex system and 5) know how to debug if something goes wrong. These are all necessary skills that you will use in the future if you continue with coding.

Emotionally, the testing of your trading strategy (also called backtrading or paper trading) is not such a big deal, but real trading is different. There are time delays in your order with price changes in the between time, there are orders that are deleted because a time limit has been exceeded, and there are sudden value changes due to news reports. Coding a trading bot will probably have a similar mental effect as losing some foreskin. You get scared to lose money and worried about the possible consequences it will have on your real financial situation.

Unfortunately, I can’t promise you that this won’t happen when you start making a trading bot. But you have to be courageous. If you complete the project correctly and you’ve made a live trading bot, you have officially made one of your coding projects a very extremely almost non-existent tiny but real part of the economy… We can say to ourselves and others at the end of the journey that we can make something that works…

THE WORKFLOW

To make it easier for you to get started, here is an adjusted workflow from the book Machine Trading by Ernest Chan. You don’t per se have to use a graphical user interface, but it does make it easy to turn switches on and off or adjust variables during the fine-tuning process.

Trading bot workflow. Ideally, you would of course also have a feedback loop from the GUI back to the trading bot.

STARTING UP

How much time does it cost to complete the project?

The time for coding the trading bot itself is of course dependent on your level. Here is a raw estimate based on my two-year experience in coding:

  • Basic level: variables, loops, functions, loops — 4–8 weeks full time.
  • Intermediate level: object-oriented programming, inheritance, decorators — 4–6 weekends.
  • Advanced level: professional coder — a couple of hours (?).

Which Broker Server should I use?

There are many servers you can use in the U.S. to trade with Python, but if you are living outside the united state, there are fewer options (Robinhood, Alpaca for instance don’t allow non-US residents). I live in the Netherlands so it took me a long time to find a broker with a suitable API. Here is a list of websites where you can open a brokers account from outside the U.S. I found none in that list that allows you to communicate with a python API. I am using one that is not in the list called DeGiro which has a python API along with clear documentation on the basic commands you’ll be using 99% of the time in the bot. I’m happy with it, their Python API is very easy to work with so I recommend using that one if you live outside the U.S.

Which feedback mechanism should I use?

If you want to get feedback out of your trading bot you have two options for running your code 24x7:

  • Use a cloud service like Google cloud platform, Amazon Webservices or Heroku.
  • Let an old laptop run 24x7 to do the trading.

You can send yourself a feedback signal through:

  • Sending yourself an e-mail or SMS, or another kind of notification for every specific number of transactions or for a specific time interval or something like that.
  • Visualize the data with Matlplotlib, Seaborn, Plotly, Dash or another package on a cloud server so you’ll be able to check the performance of your bot from anywhere with your phone.

The cloud service of the Heroku platform is the easiest one to use. If you choose that one I would recommend carefully following this tutorial from Charming Data to prevent yourself from making mistakes in the setup. Don’t forget to adjust the WEB-CONCURRENCY option to 1 as described in this post so you won’t have your buy and sell orders being performed in double.

Dash is probably the easiest way to make a graphical user interface. It’s rising in popularity and the documentation is very good. This is a medium post on how to get started with Dash, but you can also find a lot of good tutorials on YouTube.

The advantage of using Dash is that it doesn’t take that much effort to create a graph that gives you updates every x minutes/hours/days with the live update callback. This tutorial and this StackOverflow post show you how to implement this live update into your application.

How much money and stock to experiment with?

Depending on your daredevil level you can use whatever parameters you want in this equation so this is a difficult one to advise you in. You need to think of at least the following parameters:

  • The frequency of trading attempts of your bot.
  • The maximum number of stocks you will buy per specific time interval (e.g. 10 stocks/24 hours).
  • The maximum number of stocks you want to have in your possession.
  • The maximum price for a stock. Keep in mind that the lower the maximum price you’re willing to pay for a stock, the longer you will have to keep it to achieve a break-even price if the stock price is rising. Or if you choose for a low maximum price you will have to buy a higher number of stocks.
  • The minimum price of a stock to make a profit in regard to the transaction costs (between 1–3 euros) of your online trading account provider.
  • The maximum number of days you want to keep on trying to buy a stock after you have placed the order.

How to store your data within the workflow?

You can use JSON files, pickle files, csv files, SQL databases, non-SQL like mongoDB or other options to store your data. They all have their advantages and drawbacks. This is an informative post on StackOverflow. After messing around with some of the options I personally prefer MongoDB.

Should I use machine learning?

There are some very cool machine learning tutorials online:

However, this video gives convincing reasons that the contributory value to price prediction is limited. It gives the answer to the question of whether ML can really help in trading. In short:

  • Finding patterns in the past with machine learning doesn’t per se say anything about future prices.
  • If you train your machine learning algorithm to more data or more different data sources it will be prone to overfitting (seeing patterns that are not there).
  • If you do decide to try it, having fun and learning new things should be your main objective.

While analyzing patterns with machine learning seems to be not beneficial, at least not to the individual trader, there have been many publications that shed light on the predictive power of sentiment analysis. The most convincing one I came across was the one by Bollen et al. published in 2011. The graph beneath comes from this open-source article. In red, you see the calm sentiment analysis score during the election of President Obama and thanksgiving. In blue, you see the average positivity in opinion.

When you correlate the calm sentiment with stock prices 3 days later you get the graph beneath. The calm score is shown in red at the bottom. The stock price of the American Dow Jones is shown in blue in the middle. The graph at the top shows the areas of congruency between stock price and sentiment accentuated in grey.

CONCLUSION

Creating a trading bot is a positive thing as it can function as a second “hello world” ritual. Building one from scratch will be hard and painful but you will gain a range of engineering skills and go through an adventurous journey. I would motivate anybody who wants to learn to code to try building one. Not for the money but for the experience of creating a real-life application.

--

--