Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binancial secs: futes for the peepz! #520

Open
wants to merge 73 commits into
base: storage_middleware_layer
Choose a base branch
from

Commits on Jun 27, 2023

  1. binance: start drafting live order ctl endpoints

    First draft originally by @guilledk but update by myself 2 years later
    xD. Will crash at runtime but at least has the machinery to setup signed
    requests for auth-ed endpoints B)
    
    Also adds a generic `NoSignature` error for when credentials are not
    present in `brokers.toml` but user is trying to access auth-ed eps with
    the client.
    Guillermo Rodriguez authored and goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    bc4ded2 View commit details
    Browse the repository at this point in the history
  2. binance: dynamically choose the rest method

    Instead of having a buncha logic branches for 'get', 'post', etc. just
    pass the `method: str` and do a attr lookup on the `asks` sesh.
    
    Also, adjust the `trades_dialogue()` ep to switch to paper mode when no
    client API key is detected/loaded.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    f99e8fe View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    d3a5048 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    1d9c195 View commit details
    Browse the repository at this point in the history
  5. binance: add submit_cancel() & listen key mgmt

    Patch again originally from @guilledk and adds a sesh for futures
    testnet as well as a order canceller method B)
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    f8af13d View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    e03da40 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    e85e031 View commit details
    Browse the repository at this point in the history
  8. Add order status tracking

    Guillermo Rodriguez authored and goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    ef544ba View commit details
    Browse the repository at this point in the history
  9. Configuration menu
    Copy the full SHA
    eaaf6e4 View commit details
    Browse the repository at this point in the history
  10. binance: add deposits/withdrawals API support

    From @guilledk,
    - Drop Decimal quantize for now
    - Minor tweaks to trades_dialogue proto
    Guillermo Rodriguez authored and goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    7c00ca0 View commit details
    Browse the repository at this point in the history
  11. Configuration menu
    Copy the full SHA
    06026ec View commit details
    Browse the repository at this point in the history
  12. Configuration menu
    Copy the full SHA
    2dc8ee2 View commit details
    Browse the repository at this point in the history
  13. Configuration menu
    Copy the full SHA
    e035af2 View commit details
    Browse the repository at this point in the history
  14. Configuration menu
    Copy the full SHA
    26a8638 View commit details
    Browse the repository at this point in the history
  15. Configuration menu
    Copy the full SHA
    ed0c255 View commit details
    Browse the repository at this point in the history
  16. Configuration menu
    Copy the full SHA
    ae1c5a0 View commit details
    Browse the repository at this point in the history
  17. Support USD-M futes live feeds and exchange info

    Add the usd-futes "Pair" type and thus ability to load all exchange
    (info for) contracts settled in USDT. Luckily we don't seem to have to
    modify anything in the `Client` interface (yet) other then a new
    `.mkt_mode: str` which determines which endpoint set to make requests.
    Obviously data received from endpoints will likely need diff handling as
    per below.
    
    Deats:
    - add a bunch more API and WSS top level domains to `.api` with comments
    - start a `.binance.schemas` module to house the structs for loading
      different `Pair` subtypes depending on target market: `SpotPair`,
      `FutesPair`, .. etc. and implement required `MktPair` fields on the
      new futes type for compatibility with the clearing layer.
    - add `Client.mkt_mode: str` and a method lookup for endpoint parent
      paths depending on market via `.mkt_req: dict`
    
    Also related to live feeds,
    - drop `Struct` typecasting instead opting for specific fields both for
      speed and simplicity atm.
    - breakout `subscribe()` into module level acm from being embedded
      closure.
    - for now swap over the ws feed to be strictly the futes ep (while
      testing) and set the `.mkt_mode = 'usd_futes'`.
    - hack in `Client._pairs` to only load `FutesPair`s until we figure out
      whether we want separate `Client` instances per market or not..
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    dac93dd View commit details
    Browse the repository at this point in the history
  18. Configuration menu
    Copy the full SHA
    aa49c38 View commit details
    Browse the repository at this point in the history
  19. Configuration menu
    Copy the full SHA
    8220bd1 View commit details
    Browse the repository at this point in the history
  20. Configuration menu
    Copy the full SHA
    d173d37 View commit details
    Browse the repository at this point in the history
  21. binance: fix FutesPair to have .filters

    Not sure why it seemed like futures pairs didn't have this field but add
    it to the parent `Pair` type as well as drop the overridden
    `.price/size_tick` fields instead doing the same as in spot as well.
    
    Also moves the `MarketType: Literal` (for the `Client.mkt_mode: str`)
    and adds a pair type lookup table for exchange info loading.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    dc23f1c View commit details
    Browse the repository at this point in the history
  22. Switch Client.mkt_mode to 'usd_futes' if 'perp' in fqme

    The beginning of supporting multi-markets through a common API client.
    Change to futes market mode in the client if `.perp.` is matched in the
    fqme. Currently the exchange info and live feed ws impl will swap out
    for their usd-margin futures market equivalent (endpoints).
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    e68c55e View commit details
    Browse the repository at this point in the history
  23. Configuration menu
    Copy the full SHA
    4c4787c View commit details
    Browse the repository at this point in the history
  24. Always expand FQMEs with .venue and .expiry values

    Since there are indeed multiple futures (perp swaps) contracts including
    a set with expiry, we need a way to distinguish through search and
    `FutesPair` lookup which contract we're requesting. To solve this extend
    the `FutesPair` and `SpotPair` to include a `.bs_fqme` field similar to
    `MktPair` and key the `Client._pairs: ChainMap`'s backing tables with
    these expanded fqmes. For example the perp swap now expands to
    `btcusdt.usdtm.perp` which fills in the venue as `'usdtm'` (the
    usd-margined fututes market) and the expiry as `'perp'` (as before).
    This allows distinguishing explicitly from, for ex., coin-margined
    contracts which could instead (since we haven't added the support yet)
    fqmes of the sort `btcusdt.<coin>m.perp.binance` thus making it explicit
    and obvious which contract is which B)
    
    Further we interpolate the venue token to `spot` for spot markets going
    forward, which again makes cex spot markets explicit in symbology; we'll
    need to add this as well to other cex backends ;)
    
    Other misc detalles:
    
    - change USD-M futes `MarketType` key to `'usdtm_futes'`.
    
    - add `Pair.bs_fqme: str` for all pair subtypes with particular
      special contract handling for futes including quarterlies, perps and
      the weird "DEFI" ones..
    
    - drop `OHLC.bar_wap` since it's no longer in the default time-series
      schema and we weren't filling it in here anyway..
    
    - `Client._pairs: ChainMap` is now a read-only fqme-re-keyed view into
      the underlying pairs tables (which themselves are ideally keyed
      identically cross-venue) which we populate inside `Client.exch_info()`
      which itself now does concurrent pairs info fetching via a new
      `._cache_pairs()` using a `trio` task per API-venue.
    
    - support klines history query across all venues using same
      `Client.mkt_mode_req[Client.mkt_mode]` style as we're doing for
      `.exch_info()` B)
      - use the venue specific klines history query limits where documented.
    
    - handle new FQME venue / expiry fields inside `get_mkt_info()` ep such
      that again the correct `Client.mkt_mode` is selected based on parsing
      the desired spot vs. derivative contract.
    
    - do venue-specific-WSS-addr lookup based on output from
      `get_mkt_info()`; use usdtm venue WSS addr if a `FutesPair` is loaded.
    
    - set `topic: str` to the `.bs_fqme` value in live feed quotes!
    
    - use `Pair.bs_fqme: str` values for fuzzy-search input set.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    8e03212 View commit details
    Browse the repository at this point in the history
  25. kraken: add <pair>.spot.kraken fqme interpolation

    As just added for binance move to using an explicit `.<venue>.kraken`
    style for spot markets which makes the current spot symbology expand to
    `<PAIR>.SPOT` from the new `Pair.bs_fqme: str`. Reasons for why are
    laid out in the equivalent patch for binance. Obviously this also primes
    for supporting kraken's futures venue APIs as well 🏄
    https://docs.futures.kraken.com/#introduction
    
    Detalles:
    - add `.spot.kraken` parsing to `get_mkt_info()` so that if the venue
      token is not passed by caller we implicitly expand it in.
    - change `normalize()` to only return the `quote: dict` not the topic
      key.
    - rewrite live feed msg loop to use `match:` syntax B)
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    9ff03ba View commit details
    Browse the repository at this point in the history
  26. Configuration menu
    Copy the full SHA
    3c89295 View commit details
    Browse the repository at this point in the history
  27. Configuration menu
    Copy the full SHA
    f792ecf View commit details
    Browse the repository at this point in the history
  28. Configuration menu
    Copy the full SHA
    9972bd3 View commit details
    Browse the repository at this point in the history
  29. Move API urls to .venues

    Also add a lookup helper for getting addrs by venue:
    `get_api_eps()` which returns the rest and wss values.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    0c74a67 View commit details
    Browse the repository at this point in the history
  30. Configuration menu
    Copy the full SHA
    2ee11f6 View commit details
    Browse the repository at this point in the history
  31. Configuration menu
    Copy the full SHA
    1bb7c9a View commit details
    Browse the repository at this point in the history
  32. Load Assets during echange info queries

    Since we need them for accounting and since we can get them directly
    from the usdtm futes `exchangeInfo` ep, just preload all asset info that
    we can during initial `Pair` caching. Cache the asset infos inside a new per venue
    `Client._venues2assets: dict[str, dict[str, Asset | None]]` and mostly
    be pedantic with the spot asset list for now since futes seems much
    smaller and doesn't include transaction precision info.
    
    Further:
    - load a testnet http session if `binance.use_testnet.futes = true`.
    - add testnet support for all non-data endpoints.
    - hardcode user stream methods to work for usdtm futes for the moment.
    - add logging around order request calls.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    c6d1007 View commit details
    Browse the repository at this point in the history
  33. Configuration menu
    Copy the full SHA
    43494e4 View commit details
    Browse the repository at this point in the history
  34. binance: first draft live order ctl support B)

    Untested fully but has ostensibly working position and balance loading
    (by delegating entirely to binance's internals for that) and an MVP ems
    order request handler; still need to fill out the order status update
    task implementation..
    
    Notes:
    - uses user data stream for all per account balance and position tracking.
    - no support yet for `piker.accounting` position tracking.
    - no support yet for full order / position real-time update via user
      stream.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    f36061a View commit details
    Browse the repository at this point in the history
  35. Configuration menu
    Copy the full SHA
    98f6d85 View commit details
    Browse the repository at this point in the history
  36. Use Client._pairs cross-venue table for orders

    Since the request handler task will work concurrently across venues
    (spot, futes, margin) we need to be sure that we look up the correct
    venue to update the order dialog and this is naturally determined by the
    FQME-style symbol in the `BrokerdOrder` msg; the best way to map that
    symbol-key to the correct venue/`Pair` is by using said `._pairs:
    ChainMap`.
    
    Further, handle limit order errors by catching and relaying back an
    error response to the EMS. Fix the "account name" to be `binance.usdtm`
    so that we can eventually and explicitly support all venues by name.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    a9c016b View commit details
    Browse the repository at this point in the history
  37. Configuration menu
    Copy the full SHA
    249d358 View commit details
    Browse the repository at this point in the history
  38. Split out crypto$ derivs into separate type set

    For crypto derivatives (at least futes), yes they are margined, but
    generally not around a single unit of vlm (like equities or commodities
    futes) so don't pre-set the order mode allocator to use a #unit limit,
    $limit is fine.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    60b0b72 View commit details
    Browse the repository at this point in the history
  39. Configuration menu
    Copy the full SHA
    45ded4f View commit details
    Browse the repository at this point in the history
  40. Configuration menu
    Copy the full SHA
    8a06e4d View commit details
    Browse the repository at this point in the history
  41. Configuration menu
    Copy the full SHA
    09007cb View commit details
    Browse the repository at this point in the history
  42. Fix user event matching

    Was using the wrong key before from our old code (not sure how that
    slipped back in.. prolly doing too many git stashes XD), so fix that to
    properly match against order update events with 'ORDER_TRADE_UPDATE'.
    
    Also, don't match on the types we want to *cast to*, that's not how
    match syntax works (facepalm), so we have to typecast prior to EMS msg
    creation / downstream logic.
    
    Further,
    - try not bothering with binance's own internal `'orderId'` field
      tracking since they seem to support just using your own user version
      for all ctl endpoints? (thus we only need to track the EMS `.oid`s B)
    - log all event update msgs for now.
    - pop order dialogs on 'closed' statuses.
    - wrap cancel requests in an error handler block since it seems the EMS
      is double sending requests from the client?
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    3f555b2 View commit details
    Browse the repository at this point in the history
  43. Configuration menu
    Copy the full SHA
    4eeb232 View commit details
    Browse the repository at this point in the history
  44. Add full real-time position update support B)

    There was one trick which was that it seems that binance will often send
    the account/position update event over the user stream *before* the
    actual clearing (aka FILLED) order update event, so make sure we put an
    entry in the `dialogs: OrderDialogs` as soon as an order request comes
    in such that even if the account update arrives first the
    `BrokerdPosition` msg can be relayed without delay / order event order
    considerations.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    572badb View commit details
    Browse the repository at this point in the history
  45. Configuration menu
    Copy the full SHA
    6eee6ea View commit details
    Browse the repository at this point in the history
  46. binance: support order "modifies" B)

    Only a couple tweaks to make this work according to the docs:
    https://binance-docs.github.io/apidocs/futures/en/#modify-order-trade
    
    - use a PUT request.
    - provide the original user id in a `'origClientOrderId'` msg field.
    - don't expect the same oid in the PUT response.
    
    Other broker-mode related details:
    - don't call `OrderDialogs.add_msg()` until after the existing check
      since we want to check against the *last* msgs contents not the new
      request.
    - ensure we pass the `modify=True` flag in the edit case.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    dc3ac8d View commit details
    Browse the repository at this point in the history
  47. Support live order loading (with caveats)

    As you'd expect query and sync the EMS with existing live orders
    reported by the market venue by packing them in `Status` msgs and
    sending over the order dialog stream before starting the handler tasks.
    
    XXX CAVEAT:
    - there appears to be no way (at least on the usdtm market/venue) to
      distinguish between different contracts such as perps vs. the
      quarterlies?
    - for now we just assume that the perp is being used since
      there's no indicator otherwise in the 'symbol' field?
    - we should maybe open an issue with the futures-connector project to
      see how they'd recommend solving this discrepancy?
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    5c315ba View commit details
    Browse the repository at this point in the history
  48. Factor OrderDialogs into .clearing._util

    It's finally a decent little design / interface and definitely can be
    used in other backends like `kraken` which rolled something lower level
    but more or less the same without a wrapper class.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    7f39de5 View commit details
    Browse the repository at this point in the history
  49. Support loading quarterly futes existing lives

    Do parsing of the `'symbol'` and check for an `_<expiry>` suffix, in
    which case we re-format in capitalized FQME style, do the
    `Client._pairs[str, Pair]` lookup and then send the `Pair.bs_fqme` in
    the `Order.fqme: str` field.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    77db2fa View commit details
    Browse the repository at this point in the history
  50. Configuration menu
    Copy the full SHA
    fe902c0 View commit details
    Browse the repository at this point in the history
  51. Drop per-venue request methods from Client

    Use dynamic lookups instead by mapping to the correct http session and
    endpoints path using the venue routing/mode key. This let's us simplify
    from 3 methods down to a single `Client._api()` which either can be
    passed the `venue: str` explicitly by the caller (as is needed in the
    `._cache_pairs()` case) or falls back to the client's current
    `.mkt_mode: str` setting B)
    
    Deatz:
    - add couple more tables to suffice all authed-endpoint use cases:
      - `.venue2configkey: dict[str, str]` which maps the venue key to the
        `brokers.toml` subsection which should be used for auth creds and
        testnet config.
      - `.confkey2venuekeys: dict[str, list[str]]` which maps each config
        subsection key to the list of venue name keys for doing config to
        venues lookup.
    - always build out testnet sessions for spot and futes venues (though if
      not set the sessions obviously won't ever be used).
    - add and use new `config.ConfigurationError` custom exceptions when api
      creds are missing.
    - rename `action: str` to `method: str` in `._api()` since it's the
      proper ReST term and switch what was "method" to be `endpoint: str`.
    - mask out `.get_positions()` since we can get that from a user stream
      wss request (and are doing that).
    - (in theory) import and use spot testnet url as necessary.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    9970fa8 View commit details
    Browse the repository at this point in the history
  52. Configuration menu
    Copy the full SHA
    676b005 View commit details
    Browse the repository at this point in the history
  53. Configuration menu
    Copy the full SHA
    e4c1003 View commit details
    Browse the repository at this point in the history
  54. kraken: use new OrderDialogs type, handle .spot

    Drop the older `dict[str, ChainMap]` prototype we had since the new
    `OrderDialogs` built-out while adding `binance` order support is more
    refined and general. Also, handle new and now expect `.spot` venue token
    in FQMEs since kraken too has futes markets that we'll likely want to
    support eventually.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    5d93017 View commit details
    Browse the repository at this point in the history
  55. Always use fully expanded FQME throughout .clearing

    Since crypto backends now also may expand an FQME like `xbteur.kraken`
    -> `xbteur.spot.kraken` (by filling in the venue token), we need to use
    this identifier when looking up per-market order dialogs or submitting
    new requests. The simple fix is to simply look up that expanded from
    from the `Feed.flumes` table which is always keyed by the `MktPair.fqme:
    str` - the expanded form.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    d82173d View commit details
    Browse the repository at this point in the history
  56. Configuration menu
    Copy the full SHA
    a4d16ec View commit details
    Browse the repository at this point in the history
  57. Configuration menu
    Copy the full SHA
    65f2549 View commit details
    Browse the repository at this point in the history
  58. Configuration menu
    Copy the full SHA
    d9708e2 View commit details
    Browse the repository at this point in the history
  59. Configuration menu
    Copy the full SHA
    3fcf44a View commit details
    Browse the repository at this point in the history
  60. kraken: handle .spot.kraken new-style FQMEs

    After #520 we've moved to better supporting explicit venues for cex
    backends which is important where a provider offers both spot and
    derivatives markets (kraken, binance, kucoin) and we need to distinguish
    which is being traded given a common asset pair (eg. BTC/USDT). So, make
    this work for `kraken`'s brokerd such that requests and pre-existing
    live order are (un)packed to/from EMS messaging form.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    f7f7613 View commit details
    Browse the repository at this point in the history
  61. Move broker_init() into brokers._daemon

    We might as well start standardizing on `brokerd` init such that it can
    be used more generally in client code (such as the `.accounting.cli`
    stuff).
    
    Deats of `broker_init()` impl:
    - loads appropriate py pkg module,
    - reads any declared `__enable_modules__: listr[str]` which will be
      passed to `tractor.ActorNursery.start_actor(enabled_modules=<this>)`
    - loads the `.brokers._daemon._setup_persistent_brokerd
    
    As expected the `accounting.cli` tools can now import directly from this
    new location and use the common daemon fixture definition.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    b1ef549 View commit details
    Browse the repository at this point in the history
  62. ib: expose trade EP as open_trade_dialog()

    Should be the final production backend to switch this over B)
    
    Also tidy up the `update_and_audit_msgs()` validator to log vs. raise
    when `validate: bool` is set; turn it off by default to avoid raises
    until we figure out wtf is up with ib ledger processing or wtv..
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    3be1d61 View commit details
    Browse the repository at this point in the history
  63. Configuration menu
    Copy the full SHA
    efd52e8 View commit details
    Browse the repository at this point in the history
  64. Configuration menu
    Copy the full SHA
    cbe364c View commit details
    Browse the repository at this point in the history
  65. Configuration menu
    Copy the full SHA
    032976b View commit details
    Browse the repository at this point in the history
  66. Move .accounting related config loaders to subpkg

    Like you'd think:
    - `load_ledger()` -> ._ledger
    - `load_accounrt()` -> ._pos
    
    Also fixup the old `load_pps_from_ledger()` and expose it from a new
    `.accounting.cli.disect` cli cmd for trying to figure out why pp calcs
    are totally mucked on stupid ib..
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    cf1f4be View commit details
    Browse the repository at this point in the history
  67. Configuration menu
    Copy the full SHA
    2d291bd View commit details
    Browse the repository at this point in the history
  68. Configuration menu
    Copy the full SHA
    249b091 View commit details
    Browse the repository at this point in the history
  69. Configuration menu
    Copy the full SHA
    85fa87f View commit details
    Browse the repository at this point in the history
  70. Always add a paper (account) entry to order mode init

    Allows for tracking paper engine orders despite the ems not necessarily
    being opened by the current order mode instance (UI) in "paper"
    execution mode; useful for tracking bots/strats running against the same
    EMS daemon.
    goodboy committed Jun 27, 2023
    Configuration menu
    Copy the full SHA
    49e67d5 View commit details
    Browse the repository at this point in the history
  71. Configuration menu
    Copy the full SHA
    cdf9105 View commit details
    Browse the repository at this point in the history
  72. Configuration menu
    Copy the full SHA
    e7e7919 View commit details
    Browse the repository at this point in the history
  73. Configuration menu
    Copy the full SHA
    4a8eafa View commit details
    Browse the repository at this point in the history