Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

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

Open
nikhilsaraf opened this issue Jul 3, 2021 · 0 comments
Open

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

nikhilsaraf opened this issue Jul 3, 2021 · 0 comments
Labels
feature request New feature or request help wanted Extra attention is needed
Milestone

Comments

@nikhilsaraf
Copy link
Contributor

nikhilsaraf commented Jul 3, 2021

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:

  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.

@nikhilsaraf nikhilsaraf added the feature request New feature or request label Jul 3, 2021
@nikhilsaraf nikhilsaraf added this to the v1.11.1 milestone Jul 3, 2021
@nikhilsaraf nikhilsaraf added the help wanted Extra attention is needed label Jul 3, 2021
nikhilsaraf pushed a commit that referenced this issue Jul 18, 2021
…s.go + GetTickerPrice() (#717) (part of #715)

* feat/add ttlMap + binanceExchangeWs + binanceExchangeWs.GetTickerPrice

* patch/ remove ttlmap + log in ms + add now variable

* bugfix/ deps

* patch/resolve comments

* patch/ revert version go-jwt-middleware

* patch/ refactor Set/Get/Del

* patch/ transform waitForFirstEvent to timeToWaitForFirstEvent + fix glide.lock

* patch/ update glide.yaml

* patch/ fix version for go-binance glide.yaml

* patch/ forgot defer
nikhilsaraf pushed a commit that referenced this issue Jul 18, 2021
* add GetOrderBook

* add more comments

* move comment on next line

* patch/resolve comments

* patch/ fixes

* patch/ increase waitTime

* add timeout instead of waiting

* patch/ remove timeout

* patch/ check for len > fetchSize
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature request New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant