stellar-deprecated / kelp

Kelp is a free and open-source trading bot for the Stellar DEX and 100+ centralized exchanges
https://kelpbot.io
Other
1.09k stars 262 forks source link

[13] Add support for web-sockets on binance #715

Open nikhilsaraf opened 3 years ago

nikhilsaraf commented 3 years ago

Desired Behavior

I want to support web-sockets for the following endpoints on binance:

Impact

The desired behavior will allow me to run Kelp at a sub-second latency on Binance

Feature Suggestion

We can achieve the desired behavior by creating a new exchange integration by implementing the exchange.go interface and calling it binanceExchange_ws.go in the plugins directory.

We want to implement the api.Exchange interface in exchange.go (and therefore all the interfaces that are composed into the api.Exchange interface).

See Specification section below for details

Specification

Desired Design Pattern

You can wrap the ccxtExchange.go struct in your new struct (binanceExchange_ws.go) by making use of the delegation software design pattern. A good example of this pattern in our repo is batchedExchange.go

Consumers of the exchange.go interface invoke the GetOrderBook() endpoint as a polling request, so the web-socket exchange will need to maintain a cache of the latest orderbook state and return that when the consumer calls GetOrderBook() on exchange.go (i.e. your implementation for binance called binanceExchange_ws.go). The same applies for other endpoints such as GetTradeHistory() and GetAccountBalances etc.

Effectively, binanceExchange_ws.go is converting a web-sockets API (provided by Binance) to a REST API (api.Exchange).

Examples to follow

A good example to follow for this task is the kraken.go implementation which is based on Kraken's REST API. We want to build a similar integration but for Binance using web-sockets. Note that binance is already supported via our ccxt-rest integration as ccxt-binance via the ccxtExchange.go file.

Useful Links

Here is the link to Binance's API docs

Golang SDKs you can use: https://github.com/adshao/go-binance

Note: You will need to use Golang 1.13 to build and compile Kelp along with Glide. Please do not try and update the dependency pattern to use go mod when adding support for the Binance's Golang SDK since that is not the task. Only add your new SDK to the existing list.

Testing

Need a test similar to ccxtExchange_test.go. It is ok to copy-paste this test and then modify it.

Deliverables

Please submit this in multiple separate pull requests, broken up in the following way, in the following order. This will greatly help to speed up review:

  1. Add the following to your plugins/binanceExchange_ws.go file: GetTickerPrice() function implemented from the api/exchange.go#TickerAPI interface. Use the Individual Symbol Book Ticker Stream functionality in binance. Add supporting test in the same PR in plugins/binanceExchange_ws_test.go.
  2. Add the following to your plugins/binanceExchange_ws.go file: GetOrderBook() function implemented from the api/exchange.go#OrderbookFetcher interface. Use the Partial Book Depth Stream functionality in binance. Add supporting test in the same PR in plugins/binanceExchange_ws_test.go.
  3. Add the following to your plugins/binanceExchange_ws.go file: GetTradeHistory() and GetLatestTradeCursor() functions implemented from the api/exchange.go#TradeFetcher and api/exchange.go#FillTrackable interfaces. Use the Payload: Order Update Stream functionality in binance. GetLatestTradeCursor is a value saved in memory. Add supporting tests in the same PR in plugins/binanceExchange_ws_test.go.
  4. Add the following to your plugins/binanceExchange_ws.go file: GetOpenOrders() function implemented from the api/exchange.go#TradeAPI interface. Use the Payload: Order Update Stream functionality in binance. Add supporting test in the same PR in plugins/binanceExchange_ws_test.go.
  5. Add the following to your plugins/binanceExchange_ws.go file: AddOrder() and CancelOrder() functions implemented from the api/exchange.go#TradeAPI interface. Use the Payload: Order Update Stream functionality in binance. Add supporting tests in the same PR in plugins/binanceExchange_ws_test.go.
  6. Add the following to your plugins/binanceExchange_ws.go file: GetAccountBalances() function implemented from the api/exchange.go#Account interface. Use the Payload: Account Update Stream and the Payload: Balance Update Stream functionality in binance. Add supporting tests in the same PR in plugins/binanceExchange_ws_test.go.
  7. Add the following to your plugins/binanceExchange_ws.go file: GetTrades() functions implemented from the api/exchange.go#TradeAPI interface. Use the Trade Stream functionality in binance. Add supporting test in the same PR in plugins/binanceExchange_ws_test.go.

Note: you can leave the deposit and withdraw API unfinished.

Pull Request Process

When submitting Pull Requests, please add comments in the PR to help guide the code reviewer to better understand the code. If a call is necessary, please suggest that in the PR and we can get on a call.