teacherstrange / HyperTrade

Ready to deploy, distributed cryptocurrency trading bot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HyperTrade

Ready to deploy, distributed cryptocurrency trading bot.

Buy Me A Coffee Twitter: karan_6864 DigitalOcean Referral Badge

USE THIS SOFTWARE AT YOUR OWN RISK. THE AUTHOR ASSUMES NO LIABILITY FOR YOUR TRADING OUTCOMES.

charts dataframe portfolio config config-strategy charts-indicators telegram

πŸ“– Contents

πŸ” Overview

Here's a short overview of the project.

overview

πŸ’‘ 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 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 is supported but contributions are welcome!

Please leave a ⭐ as motivation or support by donating 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 bot to receive real-time notifications and much more.

  • Ready to deploy: Comes with Infrastructure as Code which is ready to deploy to Digital Ocean.

  • Easy to upgrade: Modify the system easily to your needs.

⚑️ Technologies

stack

❓ 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 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 lots more events for the Web and DB services.

working

🏭 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.

architecture k8s

Note: Kubernetes diagram was generated using Lens - The Kubernetes IDE

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. Inter-service communications are mainly via publish-subscribe and request-reply patterns. Essentially, NATS acts as our service mesh!

We also use JetStream for real-time, persisted data streams.

publish-subscribe request-reply

Read more about Distributed communication patterns with NATS

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

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.
  • 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.

config-strategy

πŸ“š 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:

telegram-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 Kubernetes cluster. But first, we will need to provision our infrastructure.

Important: Infrastructure we're about to provision has its own cost!

Tools

Steps

  • Create and export a GITHUB_TOKEN to access our Github container registry.
$ export TF_VAR_GHCR_TOKEN=value-of-your-token
  • Get an API token from DigitalOcean dashboard 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 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

About

Ready to deploy, distributed cryptocurrency trading bot

License:GNU General Public License v3.0


Languages

Language:Go 49.0%Language:TypeScript 41.6%Language:Python 4.6%Language:Dockerfile 1.4%Language:Shell 1.2%Language:HCL 1.0%Language:CSS 0.6%Language:Makefile 0.3%Language:HTML 0.2%