Skip to content

Basic crypto bot that connects to exchanges and trades according to price fluctuations. UI for stats and toggles

Notifications You must be signed in to change notification settings

LucasAlvesSoares/crypto-trading-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto Trading Bot

A production-ready cryptocurrency trading bot built with Go and React, featuring paper trading and comprehensive risk management.

Features

  • Paper Trading: Test strategies with simulated execution before risking real money
  • Single Exchange Integration: Coinbase Advanced Trade API with comprehensive error handling
  • Mean Reversion Strategy: RSI + Bollinger Bands + SMA indicators
  • Risk Management: Kill switch, position limits, daily loss limits, stop-loss
  • Real-Time Dashboard: React + TypeScript UI with live monitoring
  • Event-Driven Architecture: NATS message bus for decoupled components
  • Production-Ready: Structured logging, fault tolerance, health checks

Architecture

┌──────────────┐
│   Dashboard  │ (React + TypeScript)
└──────┬───────┘
       │ HTTP/WebSocket
┌──────▼────────────────┐
│   API Gateway (Gin)   │
└──────┬────────────────┘
       │
   ┌───▼────┐
   │  NATS  │ (Message Bus)
   └───┬────┘
       │
   ┌───┴────────────────────────────────┐
   │                                    │
┌──▼──────────────┐        ┌───────────▼────────┐
│ Market Data Svc │        │  Strategy Engine   │
└─────────────────┘        └────────┬───────────┘
                                    │
                        ┌───────────▼──────────┐
                        │   Risk Manager       │
                        └───────────┬──────────┘
                                    │
                        ┌───────────▼──────────┐
                        │  Order Manager       │
                        └───────────┬──────────┘
                                    │
                        ┌───────────▼──────────┐
                        │  Exchange Connector  │
                        └───────────┬──────────┘
                                    │
                        ┌───────────▼──────────┐
                        │     PostgreSQL       │
                        └──────────────────────┘

Tech Stack

Backend:

  • Go 1.21+
  • PostgreSQL 15+
  • NATS 2.10+
  • Gin (HTTP framework)
  • sqlc (type-safe SQL)

Frontend:

  • React 18
  • TypeScript 5
  • TradingView Lightweight Charts
  • TailwindCSS

Quick Start

Prerequisites

  • Docker & Docker Compose
  • Go 1.21+
  • Node.js 18+
  • Make

1. Start Infrastructure

docker-compose up -d

2. Run Database Migrations

cd backend
go run ./cmd/migrate up

3. Start Backend Services

In separate terminals:

# Terminal 1 - Market Data Service
cd backend && go run ./cmd/market-data

# Terminal 2 - Trading Bot
cd backend && go run ./cmd/trading-bot

# Terminal 3 - API Gateway
cd backend && go run ./cmd/api-gateway

4. Start Frontend Dashboard

cd frontend
npm install
npm run dev

5. Access Dashboard

Open http://localhost:3000

Configuration

Copy .env.example to .env and configure:

# Database
DATABASE_URL=postgresql://trading:trading@localhost:5432/trading_bot?sslmode=disable

# NATS
NATS_URL=nats://localhost:4222

# Coinbase API
COINBASE_API_KEY=your_api_key
COINBASE_API_SECRET=your_api_secret
COINBASE_API_PASSPHRASE=your_passphrase
COINBASE_USE_SANDBOX=true

# Risk Management
RISK_MAX_POSITION_SIZE_USD=100
RISK_MAX_OPEN_POSITIONS=1
RISK_DAILY_LOSS_LIMIT_PERCENT=2.0
RISK_STOP_LOSS_PERCENT=2.0
RISK_MAX_HOLD_TIME_HOURS=24

# Trading Mode
TRADING_MODE=paper  # paper or live

Project Structure

.
├── backend/               # Go backend services
│   ├── cmd/
│   │   ├── api-gateway/   # HTTP API server
│   │   ├── market-data/   # Price data ingestion
│   │   ├── trading-bot/   # Main trading bot service
│   │   └── migrate/       # Database migration tool
│   ├── internal/
│   │   ├── exchange/      # Exchange connector implementations
│   │   ├── strategy/      # Trading strategies
│   │   ├── risk/          # Risk management logic
│   │   ├── order/         # Order management
│   │   ├── marketdata/    # Market data service
│   │   ├── models/        # Domain models
│   │   ├── config/        # Configuration
│   │   ├── events/        # NATS event system
│   │   └── logger/        # Logging utilities
│   ├── migrations/        # Database migrations
│   ├── go.mod            # Go dependencies
│   └── Makefile          # Backend build commands
├── frontend/              # React TypeScript dashboard
│   ├── src/
│   │   ├── components/    # React components
│   │   ├── api/          # API client
│   │   ├── hooks/        # Custom React hooks
│   │   └── types/        # TypeScript types
│   ├── package.json      # Frontend dependencies
│   └── vite.config.ts    # Vite configuration
├── docker-compose.yml     # Infrastructure setup
├── scripts/               # Utility scripts
└── README.md             # This file

Development

Generate Database Code

cd backend
sqlc generate

Build All Services

cd backend
go build -o ../bin/migrate ./cmd/migrate
go build -o ../bin/market-data ./cmd/market-data
go build -o ../bin/trading-bot ./cmd/trading-bot
go build -o ../bin/api-gateway ./cmd/api-gateway

Safety Features

Kill Switch

  • Prominent red button in UI
  • API endpoint: POST /api/v1/system/kill-switch
  • Immediately halts all trading and cancels orders
  • Requires manual re-enable

Risk Limits

  • Maximum position size (default: $100)
  • Maximum open positions (default: 1)
  • Daily loss limit (default: 2%)
  • Per-trade stop-loss (default: 2%)
  • Maximum hold time (default: 24 hours)

Paper Trading

  • Identical code path to live trading
  • Simulated execution with realistic slippage
  • No real money at risk
  • Always start here before going live

Monitoring

  • Logs: Structured JSON logs to stdout
  • Health Checks: /health endpoint on each service
  • Dashboard: Real-time monitoring at http://localhost:3000

Production Deployment

See DEPLOYMENT.md for production deployment guide.

License

MIT License - See LICENSE file for details

Disclaimer

⚠️ USE AT YOUR OWN RISK ⚠️

This software is for educational purposes. Cryptocurrency trading carries significant financial risk. Always:

  • Start with paper trading
  • Never invest more than you can afford to lose
  • Thoroughly test strategies before live trading
  • Monitor positions actively
  • Understand the code before running it

The authors are not responsible for any financial losses incurred.

About

Basic crypto bot that connects to exchanges and trades according to price fluctuations. UI for stats and toggles

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published