This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
BBGO is a Go-based cryptocurrency trading framework supporting 8 exchanges (Binance, OKEx, KuCoin, MAX, Bitget, Bybit, Coinbase, Bitfinex) with 40+ built-in strategies, backtesting, and a web dashboard.
# Fast local build (no web dashboard — preferred for development)
make bbgo-slim # → build/bbgo/bbgo-slim
# Full build with embedded web dashboard (requires frontend assets)
make bbgo # → build/bbgo/bbgo
# Build with high-precision decimal math
make bbgo-slim-dnum
# Rebuild embedded frontend assets (only needed for full build)
make staticBuild tags: web (embed dashboard), release (version stamp), dnum (decimal math).
# Run all tests
go test ./pkg/...
# Run a specific package/test (preferred during iteration)
go test ./pkg/bbgo -v -run TestLoadConfig
# With race detector and coverage (what CI runs)
go test -count 3 -race -coverprofile coverage.txt -covermode atomic ./pkg/...
# Run with dnum tag (separate test set)
go test -race -tags dnum ./pkg/...Integration tests require exchange API credentials via env vars (e.g., BINANCE_API_KEY). Most unit tests run without credentials.
Two sub-packages provide reusable test utilities:
testhelper — Domain-specific builders and assertions for trading types:
testhelper.Market("BTCUSDT")/testhelper.Ticker("ETHUSDT")— pre-defined market and ticker fixtures (BTCUSDT, ETHUSDT, USDCUSDT, USDTTWD, BTCTWD)testhelper.Number(99.5)— flexible conversion (string/int/float) tofixedpoint.Valuetesthelper.Balance("BTC", Number(10))— create atypes.Balancewith zeroed metadata fieldstesthelper.BalancesFromText("BTC, 10.5\nETH, 100")— parse text intotypes.BalanceMaptesthelper.PriceVolumeSliceFromText("100, 10\n105, 20")— parse price/volume pairs (supports//comments)testhelper.AssertOrdersPriceSideQuantityFromText(t, "BUY, 100, 10\nSELL, 105, 20", orders)— assert order slice against text spectesthelper.MatchOrder(order)/testhelper.Catch(func(x any){...})— testify matchers for use with mocks
httptesting — HTTP client mocking and record/replay:
httptesting.HttpClientWithJson(data)— client that returns JSON for any requesthttptesting.HttpClientSaver(&req, content)— client that captures the request for inspectionMockTransport— register handlers per method/path:transport.GET("/api/v1/orders", handlerFunc)Recorder— records live HTTP interactions, saves to JSON, replays later viaMockTransport.LoadFromRecorder(). Automatically strips credential headers. Control withTEST_HTTP_RECORD=1env var.BuildResponseJson(code, payload)/SetHeader(resp, k, v)— chainable response builders
CI uses revive for linting and golangci-lint with: staticcheck, bodyclose, contextcheck, dupword, decorder, goconst, govet, gosec, misspell. Format with gofmt -s -w.
Several generators produce committed files — re-run and commit when touching their inputs:
# SQL migrations (after changing migrations/*.sql)
make migrations # uses rockhopper → pkg/migrations/{mysql,sqlite3}/
# gRPC protobuf (after changing pkg/pb/*.proto)
make grpc-go # requires protoc; install deps: make install-grpc-tools
# Go generate (requestgen, callbackgen) — run in the relevant package
go generate ./pkg/exchange/max/...- requestgen: generates HTTP API request builders from
//go:generate requestgendirectives - callbackgen: generates
OnXxx()callback registrations from//go:generate callbackgendirectives
CLI (cmd/bbgo → pkg/cmd)
→ Environment (pkg/bbgo/environment.go) — manages exchange sessions & services
→ Trader (pkg/bbgo/trader.go) — loads strategies, manages lifecycle
→ Strategies implement SingleExchangeStrategy.Run() or CrossExchangeStrategy.CrossRun()
pkg/bbgo/— Core engine: Environment, Trader, Config, strategy registry (bbgo.RegisterStrategy())pkg/types/— Shared types: Exchange interface, Order, Trade, KLine, Balancepkg/exchange/— Exchange adapters (REST + WebSocket); factory infactory.gopkg/strategy/— Built-in strategies (grid2, xmaker, bollmaker, supertrend, etc.)pkg/indicator/— Technical indicators (SMA, EMA, MACD, RSI, Bollinger, etc.)pkg/service/— Persistence and business logic (database, backtest, orders, trades)pkg/core/— TradeCollector, order store, KLine driverpkg/backtest/— Backtesting enginepkg/server/— HTTP API and web dashboard server
types.Exchange(pkg/types/exchange.go) — unified exchange interface for querying and tradingbbgo.SingleExchangeStrategy/bbgo.CrossExchangeStrategy— strategy contracts- Strategies are registered via
bbgo.RegisterStrategy()and configured through YAML
YAML config files (e.g., bbgo.yaml) define exchange sessions and strategy parameters. API credentials come from .env.local environment files. Config parsing is in pkg/bbgo/config.go.
Supports MySQL and SQLite via rockhopper migrations. Migration SQL files are in migrations/ and compiled Go packages in pkg/migrations/.
- Create package under
pkg/strategy/<name>/ - Implement
SingleExchangeStrategyorCrossExchangeStrategyinterface - Register with
bbgo.RegisterStrategy("<name>", &Strategy{})in aninit()function - Add config tests similar to
pkg/bbgo/config_test.go - Use
testifyfor assertions (already in go.mod)
Some code and tests use //go:build dnum or //go:build !dnum to conditionally compile. When adding dnum-specific code paths, mirror the build constraints in tests.