-
Notifications
You must be signed in to change notification settings - Fork 17
Description
Somewhat monumental, this is our first lurker sponsored feature
request and task set :party:
Feat summary
As part of utilizing piker
in a real-time event driven crypto
trading strategy (using news.treeofalpha.com) we have a request to
add live trading support in for the binance
broker backend, first
for their futures market and later as well for spot markets.
There actually was work done almost 2 years ago by @guilledk in
#182 which was never brought to
full completion and testing, but a lot of the initial API
integration work was prototyped to a MVP.
Obviously a lot has changed in many subsystems since then so
there will be some adjustments needed after rebase to latest main
branch.
Required base functionality:
- live data feeds for l1 and possibly l2 books for futures:
-
https://dev.binance.vision/t/websocket-api-for-futures/14930
-
APIs:
-
capacity and concurrency desirables:
-
Stream aggTrade for all symbols with 50mil+ market cap.
-
If it's viable, stream all symbols.
-
If not, start streaming on news event
-
-
connectivity implementation requirements:
-
All interactions with the HTTP API should be done using sessions.
- luckily we already do this implicitly using
asks
- luckily we already do this implicitly using
-
All calls should have retry in case something fails
- in other words retry logic around all requests such that we
don't error on (presumably) ephemeral network outages?
- in other words retry logic around all requests such that we
-
The connection should be always open and the ping endpoint polled every
1-2 minutes to ensure that the connection is open.- ping/pong support: https://github.com/binance/binance-futures-connector-python#heartbeat
- see the
-
-
Function for getting +/-1.5% of liquidity from orderbook P3
-
Call this function every .1s and display it in a custom GUI
- IOW, something which offers a predicate for how quickly
a certain sized order will clear without causing a liquidity
gap B)- use
techtonicdb
? - just run a local book mirroring actor?
- use
-
-
Function for getting all the data for the symbols (exchangeInfo)
-
This function should be called once every 2-3hrs to update the
symbols in case there was a new symbol added to the exchange -
we already have this in our current backend data layer
but we need to do this for the futes set right?
-
- live order mgmt (futes)
-
Stream all user actions regarding orders and positions
- https://binance-docs.github.io/apidocs/futures/en/#user-data-streams
- so we can rx all trade dialog updates much like we do over
ws inkraken
backend B)
- so we can rx all trade dialog updates much like we do over
- depending on market set (for eg. futes vs. spot) we must
use a http API in anything that is not spot for
requests:
- https://binance-docs.github.io/apidocs/futures/en/#user-data-streams
-
Write wrappers for opening a limit order, closing opened position(s), increasing/reducing position, etc.
- we already have this via our
piker.OrderClient
API though
we need to add aclose_all()
helper which brings the dst
asset holding / balance to net-zero.
- we already have this via our
-
Opening the positions should be in USDT
-
Increasing and decreasing the position should be in both
USDT and contract - IOW, dolla_size based slot allocation as is already supported in
our order mode pane but exposed through an API B)
-
-
Order is considered as open if it's received from the user stream P2
-
Order status is updated from the user stream P2
-
There can be a situation where SL is hit during the opening of a limit order
-
In this case, the remaining order should be canceled first
and then the position closed- IOW, no stale position should be left when a stop loss is
triggered. - If we're going to implement stops using the existing
dark orders from our EMS we're going to need to add
an algorithm for the stop's limit-price and/or offer
simply stop-market submissions.
- IOW, no stale position should be left when a stop loss is
-
- live position tracking
-
Function for getting the current balance
-
It can be either data from P2 or run in a separate thread and fetched
from the exchange after every open/close position event. - IOW, balance tracking which, again, we're more or less
already doing inpiker.accounting
and friends 🏄 -
Function to get price percentage change from P1
- IOW, realtime pnl tracking and display, again we have in
piker.accounting
and friends 🏄
- IOW, realtime pnl tracking and display, again we have in
-
This requirements section is the future nice to have / end goal
requirements and will likely require more coordination for testing
and design with the lurker sponsor 😉
- real-time ticker/symbol/market name parsing and dynamic search,
load, and cache of real-time data feeds from first section above.-
For each symbol received from news.treeofalpha.com, immediately send
the symbol so the code can prepare (i.e., get the price before the
news from P1 to calculate price percentage change).
-
binance
resources:
-
dev forum: https://dev.binance.vision/
-
high level FAQ:
-
futures:
-
APIs:
-
testnet:
- https://www.binance.com/en/support/faq/how-to-access-mock-trading-in-binance-futures-b3706b248f2b4b1caabb4bf253bf067f
- all forum search:
https://dev.binance.vision/search?q=testnet - https://dev.binance.vision/t/testnet-pairs-do-not-support-quoteorderqty/9677
- https://dev.binance.vision/t/testnet-trading-volume/16140
-
orders:
-
understanding different future type classes: COIN-margined vs. USD-margined:
-
-
spot api (which has ws ctl 💥):
-
history endpoints:
- "https://www.binance.com/en/landing/data"
- https://github.com/binance/binance-public-data/blob/master/python/download-trade.py
- "https://github.com/binance/binance-public-data/"
- "https://github.com/binance/binance-public-data/tree/master/python"
- 1w OHLC B0
https://binance-docs.github.io/apidocs/websocket_api/en/#klines
-
recent drama (for fun):
Detailed impl TODO:
-
bring Binance updates #182 up to date
- rebase to master
- Binancial secs: futes for the peepz! #520
-
reorg
.brokers.binance
into subpkg like other backends -
add futures ws live feeds
- exchange info lookup / search
- should only require additional
FutesPair
type for handling
mkt info without having to changeClient
interface?- see 3b66efc,
- probably we need a special error for when there exists no
futures contract for a pair?
- should only require additional
- exchange info lookup / search
-
finish full
emsd
based order control support for futures
and spot:-
testnet setup: https://www.binance.com/en/support/faq/api-frequently-asked-questions-360004492232
-
we can use a reqid to tie
oid
from EMS directly to binance
dialogs B)
https://github.com/binance/binance-futures-connector-python#request-id -
implement rate limiting logic to avoid bans client side:
-
much more to come.. !
testing TODO:
- tests for round trip latency of live limit submissions
- stop loss via dark orders
- stop loss via
binance
API submission - connectivity resilience under network outage(s)
- particularly dumping a pp if/when network returns
(maybe) much more to come here!