How I Built My First Trading Bot For Hashnode's Hackathon
Tags: python, christmashackathon, todayilearned
Published: 2020-12-29 02:24:41

---
title: How I Built My First Trading Bot For Hashnode's Hackathon
published: true
date: 2020-12-28 14:07:07 UTC
tags: Python,ChristmasHackathon,TIL
canonical_url: https://lo-victoria.com/how-i-built-my-first-trading-bot-for-hashnodes-hackathon
---

Hello everyone! I'm excited to showcase a mini project that I've built for Hashnode's Christmas Hackathon. I will be explaining the inspiration behind this project, how I built it, some challenges I faced along the way and what I've learnt from partaking in this spontaneous hackathon. Let's begin!

### Inspiration
Financial Technology aka **FinTech** is a broad term for financial services or products that integrates technology to automate and optimize its uses. As a developer, I've always been interested in stocks and investments; in particular, the use of robo-advisors and trading bots today have piqued my curiosity for quite a while.

Hence, by participating in Hashnode's Hackathon, I thought it will be a good opportunity for me to try something new as a fun challenge.

### How I Built It
The algorithm is created and build on [QuantConnect (QC)](https://www.quantconnect.com/) -  a browser-based algorithmic trading platform. It is a simple yet powerful platform that empowers anyone to build, backtest and execute their own trading algorithms.

QC supports both Python and C#. I signed up for a free account. Then I spend the next few hours learning on how to navigate and use the platform via its bootcamp courses. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608706224322/-c8Fajfoj.png)

At first glance, it seems overwhelming to use. Just understanding all the tools, different modules and strategies available for use is difficult, maybe even more so for someone without financial knowledge. I recommend reading up on some finance 101 books to know what is a close, open, high, low, RSI, MACD, Bollinger Bands, etc. or else it will be too confusing to learn both the technical and financial aspects at the same time.

After familiarizing myself with the platform, I got down to business (kind of). 

### Challenges
The biggest challenge I faced when doing this hackathon is definitely **time management**. Time was a precious resource in my case. I have a full-time job, as well as other commitments, and so I had to allocate my time wisely and progress patiently on this project. When I have decided to build a trading bot as the project for this hackathon, I researched whenever I am free for 2 days before finally discovering and deciding to go with QC.

Another unforgettable challenge was **researching and deep-diving** into a not-so-familiar territory for me: the QC platform and trading strategies. The QC platform was incredibly overwhelming, I had to spend hours just reading the documentation to understand their modules and know how to apply them. I have zero clue on how to design profitable algorithms and testing them.

I looked up various trading strategies like pairs trading, momentum-based, short-term reversals, mean-reverting and so on. Risk and return are trade-offs to be carefully managed in every strategy. That is something that I have to bear in mind when implementing my trading bot, and is quite a huge challenge for a beginner like me.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608730602750/Z1nffuJ_5.png)

> Note: some articles/papers I found helpful are listed below in the Resources section.

### Results & Next Steps
After testing my algorithm for several rounds of backtesting, it is clear that the algorithm is unstable. The results show that it is unreliable for live trading and definitely needs more improvement to work on (see Exhibits below).

#### Exhibit A: Worst profits
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608777287889/1j5PdKKna.png)

#### Exhibit B: Best profits
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1608805534180/3IltqI2d2.png)

The next steps in this project would be to **get more financial expertise** (from a friend perhaps) to be able to build a better bot. I noticed that on test runs where the returns are high, it is most likely due to overfitting and not because the bot was doing well. Therefore, learning the various modules (i.e. machine learning) and trading strategies on QC will be a good idea to progress and test the algorithm more smoothly. 

Overall, this hackathon sure teaches me that **there is no limit to learning** as a developer. Technology can be found integrated in various sectors, such as Finance. Building a product that synergizes in both the tech and finance industry was thrilling yet challenging. I sure hope to continue learning as much as I can now that this hackathon has given me a head-start. Plus, I like this hackathon allows me to spend some time with my old friend: **Python**, after coding in JavaScript for a long time.

Thanks for reading! I hope everyone has built something awesome and cool for Hashnode's Hackathon. I shall be reading up on the tag #christmashackathon and I look forward to more interesting reads soon! Happy holidays, stay safe and cheers!

### Resources
- Bland, G. (2020, November 12). QuantConnect - A Complete Guide - AlgoTrading101 Blog. Retrieved December 23, 2020, from https://algotrading101.com/learn/quantconnect-guide/
- Faber, M. (2010, April 20). Relative Strength Strategies for Investing. Retrieved December 23, 2020, from https://papers.ssrn.com/sol3/papers.cfm?abstract_id=1585517
- Faber, Meb, A Quantitative Approach to Tactical Asset Allocation (February 1, 2013). The Journal of Wealth Management, Spring 2007, Available at SSRN: https://ssrn.com/abstract=962461
- Volta, V. (n.d.). Profitable Algorithmic Trading Strategies in Mean-Reverting Markets. Retrieved December 23, 2020, from https://www.academia.edu/42655318/Profitable_Algorithmic_Trading_Strategies_in_Mean_Reverting_Markets

- *Cover image background from freepik.com*