bbgo_origin/doc/development/adding-new-exchange.md

230 lines
5.7 KiB
Markdown
Raw Permalink Normal View History

2021-12-09 04:13:35 +00:00
# Adding New Exchange
2021-12-09 04:17:56 +00:00
Open an issue and paste the following checklist to that issue.
You should send multiple small pull request to implement them.
2021-12-09 04:13:35 +00:00
**Please avoid sending a pull request with huge changes**
2022-05-28 08:54:44 +00:00
**Important** -- for the underlying http API please use `requestgen` <https://github.com/c9s/requestgen> to generate the
requests.
2022-05-23 10:13:57 +00:00
2021-12-09 04:13:35 +00:00
## Checklist
2022-09-23 17:50:16 +00:00
Exchange Interface - (required) the minimum requirement for spot trading
2021-12-09 04:13:35 +00:00
- [ ] QueryMarkets
- [ ] QueryTickers
2021-12-27 16:21:07 +00:00
- [ ] QueryOpenOrders
2021-12-09 04:13:35 +00:00
- [ ] SubmitOrders
2021-12-27 16:21:07 +00:00
- [ ] CancelOrders
- [ ] NewStream
2022-03-02 16:12:01 +00:00
Trading History Service Interface - (optional) used for syncing user trading data
2021-12-27 16:21:07 +00:00
- [ ] QueryClosedOrders
- [ ] QueryTrades
2022-03-02 16:12:01 +00:00
Order Query Service Interface - (optional) used for querying order status
- [ ] QueryOrder
2022-09-23 17:50:16 +00:00
Back-testing service - (optional, required by backtesting) kline data is used for back-testing
2021-12-27 16:21:07 +00:00
- [ ] QueryKLines
2021-12-09 04:13:35 +00:00
2022-09-23 17:50:16 +00:00
Convert functions (required):
2021-12-09 04:13:35 +00:00
- [ ] MarketData convert functions
2022-05-28 08:54:44 +00:00
- [ ] toGlobalMarket
- [ ] toGlobalTicker
- [ ] toGlobalKLine
2021-12-09 04:13:35 +00:00
- [ ] UserData convert functions
2022-05-28 08:54:44 +00:00
- [ ] toGlobalOrder
- [ ] toGlobalTrade
- [ ] toGlobalAccount
- [ ] toGlobalBalance
2021-12-09 04:13:35 +00:00
Stream
- [ ] UserDataStream
2022-05-28 08:54:44 +00:00
- [ ] Trade message parser
- [ ] Order message parser
- [ ] Account message parser
- [ ] Balance message parser
2021-12-09 04:13:35 +00:00
- [ ] MarketDataStream
2022-05-28 08:54:44 +00:00
- [ ] OrderBook message parser (or depth)
2022-09-23 17:50:16 +00:00
- [ ] KLine message parser (required for backtesting and strategy)
2022-05-28 08:54:44 +00:00
- [ ] Public trade message parser (optional)
- [ ] Ticker message parser (optional)
2022-09-23 17:50:16 +00:00
- [ ] ping/pong handling. (you can reuse the existing types.StandardStream)
- [ ] heart-beat handling or keep-alive handling. (already included in types.StandardStream)
2022-09-23 17:50:16 +00:00
- [ ] handling reconnect. (already included in types.StandardStream)
2021-12-09 04:13:35 +00:00
2021-12-09 04:16:47 +00:00
Database
2022-09-23 17:50:16 +00:00
- [ ] Add a new kline table for the exchange (required for back-testing)
2022-05-28 08:54:44 +00:00
- [ ] Add MySQL migration SQL
- [ ] Add SQLite migration SQL
2021-12-09 04:16:47 +00:00
2021-12-09 04:13:35 +00:00
Exchange Factory
- [ ] Add the exchange constructor to the exchange instance factory function.
- [ ] Add extended fields to the ExchangeSession struct. (optional)
2021-12-21 16:31:00 +00:00
2022-05-23 07:05:55 +00:00
# Tools
- Use a tool to convert JSON response to Go struct <https://mholt.github.io/json-to-go/>
- Use requestgen to generate request builders <https://github.com/c9s/requestgen>
- Use callbackgen to generate callbacks <https://github.com/c9s/callbackgen>
2021-12-21 16:31:00 +00:00
# Implementation
Go to `pkg/types/exchange.go` and add your exchange type:
```
const (
ExchangeMax = ExchangeName("max")
ExchangeBinance = ExchangeName("binance")
ExchangeFTX = ExchangeName("ftx")
ExchangeOKEx = ExchangeName("okex")
2022-05-23 07:05:55 +00:00
ExchangeKucoin = ExchangeName("kucoin")
ExchangeBacktest = ExchangeName("backtest")
2021-12-21 16:31:00 +00:00
)
```
Go to `pkg/cmd/cmdutil/exchange.go` and add your exchange to the factory
```
func NewExchangeStandard(n types.ExchangeName, key, secret, passphrase, subAccount string) (types.Exchange, error) {
switch n {
case types.ExchangeFTX:
return ftx.NewExchange(key, secret, subAccount), nil
case types.ExchangeBinance:
return binance.New(key, secret), nil
case types.ExchangeOKEx:
return okex.New(key, secret, passphrase), nil
// ...
}
}
```
2022-05-28 09:00:02 +00:00
## Using requestgen
### Alias
You can put the go:generate alias on the top of the file:
```
//go:generate -command GetRequest requestgen -method GET
//go:generate -command PostRequest requestgen -method POST
//go:generate -command DeleteRequest requestgen -method DELETE
```
Please note that the alias only works in the same file.
### Defining Request Type Names
Please define request type name in the following format:
```
{Verb}{Service}{Resource}Request
```
for example:
```
type GetMarginMarketsRequest struct {
client requestgen.APIClient
}
```
then you can attach the go:generate command on that type:
```
//go:generate GetRequest -url "/api/v3/wallet/m/limits" -type GetMarginBorrowingLimitsRequest -responseType .MarginBorrowingLimitMap
```
2022-05-28 08:54:44 +00:00
## Un-marshalling Timestamps
For millisecond timestamps, you can use `types.MillisecondTimestamp`, it will automatically convert the timestamp into
time.Time:
```
type MarginInterestRecord struct {
Currency string `json:"currency"`
CreatedAt types.MillisecondTimestamp `json:"created_at"`
}
```
## Un-marshalling numbers
For number fields, especially floating numbers, please use `fixedpoint.Value`, it can parse int, float64, float64 in
string:
```
type A struct {
Amount fixedpoint.Value `json:"amount"`
}
```
2022-03-02 16:06:44 +00:00
## Test Market Data Stream
### Test order book stream
2021-12-25 16:27:52 +00:00
```shell
godotenv -f .env.local -- go run ./cmd/bbgo orderbook --config config/bbgo.yaml --session kucoin --symbol BTCUSDT
```
2022-03-02 16:06:44 +00:00
## Test User Data Stream
```shell
2021-12-25 16:27:52 +00:00
godotenv -f .env.local -- go run ./cmd/bbgo --config config/bbgo.yaml userdatastream --session kucoin
```
2022-03-02 16:06:44 +00:00
## Test Restful Endpoints
You can choose the session name to set-up for testing:
```shell
export BBGO_SESSION=ftx
export BBGO_SESSION=kucoin
export BBGO_SESSION=binance
```
### Test user account balance
```shell
godotenv -f .env.local -- go run ./cmd/bbgo balances --session $BBGO_SESSION
```
### Test order submit
```shell
godotenv -f .env.local -- go run ./cmd/bbgo submit-order --session $BBGO_SESSION --symbol=BTCUSDT --side=buy --price=18000 --quantity=0.001
```
### Test open orders query
2021-12-25 16:27:52 +00:00
```shell
2022-03-02 16:06:44 +00:00
godotenv -f .env.local -- go run ./cmd/bbgo list-orders --session $BBGO_SESSION --symbol=BTCUSDT open
godotenv -f .env.local -- go run ./cmd/bbgo list-orders --session $BBGO_SESSION --symbol=BTCUSDT closed
```
2022-03-02 16:06:44 +00:00
### Test order status
```shell
2022-03-02 16:06:44 +00:00
godotenv -f .env.local -- go run ./cmd/bbgo get-order --session $BBGO_SESSION --order-id ORDER_ID
```
2022-03-02 16:06:44 +00:00
### Test order cancel
```shell
godotenv -f .env.local -- go run ./cmd/bbgo cancel-order --session $BBGO_SESSION --order-uuid 61c745c44592c200014abdcf
```