Ready to deploy, distributed cryptocurrency trading bot.
USE THIS SOFTWARE AT YOUR OWN RISK. THE AUTHOR ASSUMES NO LIABILITY FOR YOUR TRADING OUTCOMES.
## š Contents
- [Overview](#overview)
- [Motivation](#motivation)
- [Features](#features)
- [Technologies](#technologies)
- [Working](#working)
- [Architecture](#architecture)
- [Getting Started](#getting-started)
- [Customization](#customization)
- [Usage](#usage)
- [Telegram](#telegram)
- [Web](#web)
- [Deployment](#deployment)
- [Contribute](#contribute)
- [License](#license)
## š Overview
Here's a short overview of the project.
[![overview](https://user-images.githubusercontent.com/29705703/160863938-a8350ddd-5bf7-442e-beb9-fe590b28a725.png)](https://youtu.be/jHBlUlIY4AQ)
## š” Motivation
A few months ago I got into the crypto market. It was all new and fascinating to me. The idea of this project originally came from a script I used to automate buying and selling of fiat assets.
I was curious and wanted to scale it into a real system that can execute trades for me. I had a lot of fun building this. I got to play with lots of different technologies while growing my financial knowledge.
Feel free to [reach out](https://karanpratapsingh.com?action=contact) to me if you have any additional questions. There are lots of fixes and features to be done!
_It is important to note that this project is under active development and was developed as an experiment. Currently, only [Binance](https://binance.com/) is supported but contributions are welcome!_
**Please leave a ā as motivation or support by [donating](https://www.buymeacoffee.com/karanps) if you liked the idea š**
## āØ Features
This system has lots of features such as:
- **Web UI**: Real-time access to charts, technical indicators, and portfolio.
- **Configurable**: Supports assets and strategies customization easily without touching any code!
- **Portable**: Export your trades and real-time data frame to CSV.
- **Multiple Assets**: Trade multiple crypto assets simultaneously!
- **Telegram Support**: On the move? Use [Telegram](#telegram) bot to receive real-time notifications and much more.
- **Ready to deploy**: Comes with [Infrastructure as Code](https://dev.to/karanpratapsingh/introduction-to-infrastructure-as-code-with-terraform-4f29) which is ready to deploy to [Digital Ocean](https://www.digitalocean.com/).
- **Easy to upgrade**: Modify the system easily to your needs.
## ā”ļø Technologies
## ā Working
Here's a simple diagram illustrating how streams and events are published/subscribed in the system. We use NATS for messaging and streams, more about that in the [architecture](#architecture) section.
Basically, The Exchange service publishes Kline/Candlestick data based on our configuration. Strategy service then subscribes to it and publishes a stream of dataframe which contains the indicators, buy/sell signals, and other metadata. The buy/sell signals are determined using the configuration as well, where we can customize our strategies given that they are supported.
Now, The Exchange service subscribes to the dataframe stream and executes trades on the signal using the Binance API. Any event such as order, trade, error, etc is published and then the Notification service can send a message to the user using the Telegram API.
*_Note: This only represents core events, there are more events for the Web and DB services._*
## š Architecture
It's a microservices based architecture with event driven approach for decoupling. It uses distributed streams and messaging which keeps the system simple yet robust to make sure it can grow in the future.
_Note: Kubernetes diagram was generated using [Lens - The Kubernetes IDE](https://github.com/nevalla/lens-resource-map-extension/)_
**Why so many technologies?**
This started as an all Go project, but then I decided to add Python to the mix as it has a really good ecosystem for technical indicators and mathematical use cases.
For the web, I used React as it's easy and TypeScript provides some sanity to JavaScript projects!
Nginx and Postgres fit right in as the use case grew. As always, use what's right for the project!
**Why event driven?**
Originally I made this using HTTP REST, but systems like this are event driven by nature. Plus this approach helps to decouple services even more.
**How do services communicate?**
All the messaging infrastructure use cases are handled by [NATS](https://nats.io). Inter-service communications are mainly via publish-subscribe and request-reply patterns. Essentially, NATS acts as our service mesh!
We also use [JetStream](https://docs.nats.io/nats-concepts/jetstream) for real-time, persisted data streams.
_Read more about [Distributed communication patterns with NATS](https://dev.to/karanpratapsingh/distributed-communication-patterns-with-nats-g17)_
**Why Kubernetes? Isn't it overkill?**
I agree! Kubernetes can be bit overkill, especially for this project. But my goal here was to keep it cloud agnostic, even though it was very tempting to just spin up AWS lambdas with event bridge.
## š Getting Started
Here we will setup our development environment. But first, we will need to install the following tools.
**Tools**
- [Minikube](https://minikube.sigs.k8s.io/docs/start/)
- [Skaffold](https://skaffold.dev/docs/install/)
- [Helm](https://helm.sh/docs/intro/install/)
- [Go >= 1.18](https://go.dev/doc/install)
- [Node >= 16.14](https://nodejs.org/en/download/)
- [Python >= 3.10](https://www.python.org/downloads/)
- [yq](https://github.com/mikefarah/yq)
- [volta](https://volta.sh/)
- [doctl](https://github.com/digitalocean/doctl)
**Steps**
- Once all the tools are installed, execute `make prepare` to prepare the local environment.
- Create a `infrastructure/k8s/env.yaml` to similar structure as `infrastructure/k8s/env.example.yaml` and [follow this guide](./docs/secrets-setup.md).
- Review the configuration in `services/exchange/config.json`
- Start development with `make dev` command.
- Use `make stop` to stop the local minikube cluster.
## š Customization
We can customize trading strategies either via web app or `services/exchange/config.json` directly.
Currently, only few trading strategies like RSI and MACD are supported and I plan to add more soon.
## š Usage
### š¬ Telegram
Telegram bot helps us to interact with the system easily and receive real-time notifications.
**Commands**
The telegram bot supports the following commands:
- **`/configs`**: Get asset configurations.
- **`/balance`**: Get current account balance.
- **`/positions`**: Get actively held positions.
- **`/stats`**: Get portfolio statistics.
- **`/enable`**: Enable trading for a symbol.
- **`/disable`**: Disable trading for a symbol.
- **`/dump`**: Dump all the positions for a symbol.
_Note: `enable`, `disable` and `dump` commands are symbol specific, and are executed as `/cmd symbol`. Example, `/enable ETHUSDT`_
### š» Web
Since this application deals with sensitive financial data, it is **not recommended** to expose it via ingress unless we have proper RBAC authorization in place. Hence, it is recommended to connect to it via port-forwarding on your local machine.
**Steps**
- Connect to the application via port-forwarding using `make connect` command.
_Note: Make sure `doctl` is authenticated, and we're using the correct k8s cluster name._
- Open `localhost:8080` to see the web interface.
- Once done, use the `make disconnect` command to remove the kubectl context from your machine.
_Note: It is recommended to use the telegram bot over the user interface on non-secure systems._
## š Deployment
Deployments can be done via `deploy.yml` Github actions to our [Digital Ocean](https://www.digitalocean.com/) Kubernetes cluster. But first, we will need to provision our infrastructure.
_Important: Infrastructure we're about to provision has its own cost!_
**Tools**
- [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli)
**Steps**
- Create and export a [GITHUB_TOKEN](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry#authenticating-to-the-container-registry) to access our Github container registry.
```
$ export TF_VAR_GHCR_TOKEN=value-of-your-token
```
- Get an API token from DigitalOcean [dashboard](https://cloud.digitalocean.com/account/api/tokens) and export it temporarily for terraform.
```
$ export DIGITALOCEAN_TOKEN=value-of-your-token
```
- Apply the Terraform configuration.
```
$ cd infrastructure
$ terraform init
$ terraform apply
```
- Go to Github and add `GHCR_TOKEN` and `DIGITALOCEAN_TOKEN` to your repository secrets for Github actions.
- Generate `base64` string of your secrets, and add it as `APP_SECRETS` to your repository secrets for Github actions.
```
$ cat infrastructure/k8s/env.yaml | base64
```
- Go to the actions tab and run the `Build and Deploy` action.
- Once the deploy is complete, our application will be deployed in the `hypertrade` namespace on Kubernetes.
_Note: If you want to change the name of the project, make sure to update all the associated Kubernetes manifest files, skaffold config, nginx config etc._
## š Contribute
Contributions are welcome as always, before submitting a new PR please make sure to open a new issue so community members can discuss it.
Additionally, you might find existing open issues which can help with improvements.
This project follows a standard [code of conduct](./CODE_OF_CONDUCT.md) so that you can understand what actions will and will not be tolerated.
## š License
This project is GPL-3.0 licensed, as found in the [LICENSE](./LICENSE)