• Stars
    star
    256
  • Rank 159,219 (Top 4 %)
  • Language
    Go
  • License
    GNU General Publi...
  • Created almost 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ready to deploy, distributed cryptocurrency trading bot

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

More Repositories

1

system-design

Learn how to design systems at scale and prepare for system design interviews
31,424
star
2

fullstack-starterkit

GraphQL first full-stack starter kit with Node, React. Powered by TypeScript
TypeScript
1,168
star
3

learn-go

Master the fundamentals and advanced features of the Go programming language
Go
877
star
4

Proximity

An Open Source social media that does not use your data against you
TypeScript
358
star
5

portfolio

My portfolio website with built-in blogs and courses support
MDX
285
star
6

react-native-boilerplate

A Robust React Native boilerplate to kickstart your new app ⚡
JavaScript
161
star
7

tutorials

This repository contains all the code snippets from articles and videos
Go
107
star
8

roadmaps

Roadmaps for AWS, DevOps, and Frontend
95
star
9

delhi-metro-app

Delhi metro app with React Native TypeScript
TypeScript
18
star
10

preview-environments

Quickly create temporary preview environments
TypeScript
14
star
11

uno

Classic UNO card game implemented with React and Python using Socket.IO for realtime multiplayer functionality.
TypeScript
14
star
12

KCards

An app that helps you leave your paper based business cards behind
13
star
13

s3-explorer

AWS S3 bucket explorer
Go
9
star
14

webdrop

Share files with people nearby instantly and securely. Open Source and Peer2Peer.
TypeScript
9
star
15

scale-etl

Partition, Transform, Load, and Search large CSV files
Go
8
star
16

digital-card

My personal digital card in the terminal!
JavaScript
7
star
17

cloudflare-workers-typescript

Cloudflare Workers TypeScript setup with esbuild
TypeScript
6
star
18

flask-postgres

Python
2
star
19

okteto-demo

Demo for okteto
Go
2
star