kelp
kelp copied to clipboard
[13] Add support for web-sockets on binance
Desired Behavior
I want to support web-sockets for the following endpoints on binance:
- fetch trade history
- get orderbook
- create order
- cancel order
- get open orders
- get balance
- ... and a few more (see detailed specification below)
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:
- Add the following to your
plugins/binanceExchange_ws.go
file:GetTickerPrice()
function implemented from theapi/exchange.go#TickerAPI
interface. Use the Individual Symbol Book Ticker Stream functionality in binance. Add supporting test in the same PR inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:GetOrderBook()
function implemented from theapi/exchange.go#OrderbookFetcher
interface. Use the Partial Book Depth Stream functionality in binance. Add supporting test in the same PR inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:GetTradeHistory()
andGetLatestTradeCursor()
functions implemented from theapi/exchange.go#TradeFetcher
andapi/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 inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:GetOpenOrders()
function implemented from theapi/exchange.go#TradeAPI
interface. Use the Payload: Order Update Stream functionality in binance. Add supporting test in the same PR inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:AddOrder()
andCancelOrder()
functions implemented from theapi/exchange.go#TradeAPI
interface. Use the Payload: Order Update Stream functionality in binance. Add supporting tests in the same PR inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:GetAccountBalances()
function implemented from theapi/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 inplugins/binanceExchange_ws_test.go
. - Add the following to your
plugins/binanceExchange_ws.go
file:GetTrades()
functions implemented from theapi/exchange.go#TradeAPI
interface. Use the Trade Stream functionality in binance. Add supporting test in the same PR inplugins/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.