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

Binance syminfo and mintick #184

Merged
merged 3 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions piker/brokers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
asks.init('trio')

__brokers__ = [
'binance',
'questrade',
'robinhood',
'ib',
Expand Down
122 changes: 101 additions & 21 deletions piker/brokers/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

import arrow
import asks
from fuzzywuzzy import process as fuzzy
import numpy as np
import trio
import tractor
Expand Down Expand Up @@ -81,6 +82,7 @@
ohlc_dtype = np.dtype(_ohlc_dtype)

_show_wap_in_history = False
_search_conf = {'pause_period': 0.375}


# https://binance-docs.github.io/apidocs/spot/en/#exchange-information
Expand Down Expand Up @@ -148,38 +150,66 @@ class Client:
def __init__(self) -> None:
self._sesh = asks.Session(connections=4)
self._sesh.base_location = _url
self._pairs: dict[str, Any] = {}

async def _api(
self,
method: str,
data: dict,
params: dict,
) -> Dict[str, Any]:
resp = await self._sesh.get(
path=f'/api/v3/{method}',
params=data,
params=params,
timeout=float('inf')
)
return resproc(resp, log)

async def symbol_info(

self,
sym: Optional[str] = None
sym: Optional[str] = None,

) -> dict:
) -> dict[str, Any]:
'''Get symbol info for the exchange.

'''
# TODO: we can load from our self._pairs cache
# on repeat calls...

# will retrieve all symbols by default
params = {}

resp = await self._api('exchangeInfo', {})
if sym is not None:
for sym_info in resp['symbols']:
if sym_info['symbol'] == sym:
return sym_info
else:
raise SymbolNotFound(f'{sym} not found')
sym = sym.upper()
params = {'symbol': sym}

resp = await self._api(
'exchangeInfo',
params=params,
)

entries = resp['symbols']
if not entries:
raise SymbolNotFound(f'{sym} not found')

syms = {item['symbol']: item for item in entries}

if sym is not None:
return syms[sym]
else:
return resp['symbols']
return syms

async def cache_symbols(
self,
) -> dict:
if not self._pairs:
self._pairs = await self.symbol_info()

return self._pairs

async def bars(
self,
symbol: str = 'BTCUSDT',
symbol: str,
start_time: int = None,
end_time: int = None,
limit: int = 1000, # <- max allowed per query
Expand All @@ -198,8 +228,8 @@ async def bars(
# https://binance-docs.github.io/apidocs/spot/en/#kline-candlestick-data
bars = await self._api(
'klines',
{
'symbol': symbol,
params={
'symbol': symbol.upper(),
'interval': '1m',
'startTime': start_time,
'endTime': end_time,
Expand Down Expand Up @@ -237,7 +267,9 @@ async def bars(

@asynccontextmanager
async def get_client() -> Client:
yield Client()
client = Client()
await client.cache_symbols()
yield client


# validation type
Expand All @@ -256,11 +288,21 @@ class AggTrade(BaseModel):


async def stream_messages(ws):

timeouts = 0
while True:

with trio.move_on_after(5):
with trio.move_on_after(5) as cs:
msg = await ws.recv_msg()

if cs.cancelled_caught:

timeouts += 1
if timeouts > 2:
raise trio.TooSlowError("binance feed seems down?")

continue

# for l1 streams binance doesn't add an event type field so
# identify those messages by matching keys
# https://binance-docs.github.io/apidocs/spot/en/#individual-symbol-book-ticker-streams
Expand Down Expand Up @@ -436,13 +478,24 @@ async def stream_quotes(
sym_infos = {}
uid = 0

async with open_cached_client('binance') as client, send_chan as send_chan:
async with (
open_cached_client('binance') as client,
send_chan as send_chan,
):

# keep client cached for real-time section
cache = await client.cache_symbols()

for sym in symbols:
d = await client.symbol_info(sym)
d = cache[sym.upper()]
syminfo = Pair(**d) # validation
sym_infos[sym] = syminfo.dict()

si = sym_infos[sym] = syminfo.dict()

# XXX: after manually inspecting the response format we
# just directly pick out the info we need
si['price_tick_size'] = syminfo.filters[0]['tickSize']
si['lot_tick_size'] = syminfo.filters[2]['stepSize']

symbol = symbols[0]

Expand Down Expand Up @@ -483,7 +536,7 @@ async def stream_quotes(
# TODO: use ``anext()`` when it lands in 3.10!
typ, quote = await msg_gen.__anext__()

first_quote = {quote['symbol']: quote}
first_quote = {quote['symbol'].lower(): quote}
task_status.started((init_msgs, first_quote))

# signal to caller feed is ready for consumption
Expand All @@ -492,5 +545,32 @@ async def stream_quotes(
# start streaming
async for typ, msg in msg_gen:

topic = msg['symbol']
topic = msg['symbol'].lower()
await send_chan.send({topic: msg})


@tractor.context
async def open_symbol_search(
ctx: tractor.Context,
) -> Client:
async with open_cached_client('binance') as client:

# load all symbols locally for fast search
cache = await client.cache_symbols()
await ctx.started()

async with ctx.open_stream() as stream:

async for pattern in stream:
# results = await client.symbol_info(sym=pattern.upper())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah turns out they don't have a "search" in the API they offer, you have to have the exact symbol.

Collecting the whole cache at client startup is reasonably fast anyway (and we do it async with the other backends), so all gud i think.


matches = fuzzy.extractBests(
pattern,
cache,
score_cutoff=50,
)
# repack in dict form
await stream.send(
{item[0]['symbol']: item[0]
for item in matches}
)
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
# tsdbs
'pymarketstore',
#'kivy', see requirement.txt; using a custom branch atm

# fuzzy search
'fuzzywuzzy[speedup]',

],
tests_require=['pytest'],
python_requires=">=3.9", # literally for ``datetime.datetime.fromisoformat``...
Expand Down