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.
basic_buy_bot
Tyler Goodlet 2023-06-09 14:35:08 -04:00
parent bc4ded2662
commit f99e8fe7eb
1 changed files with 35 additions and 30 deletions

View File

@ -21,6 +21,7 @@
Binance backend Binance backend
""" """
from __future__ import annotations
from collections import OrderedDict from collections import OrderedDict
from contextlib import ( from contextlib import (
asynccontextmanager as acm, asynccontextmanager as acm,
@ -40,6 +41,8 @@ import hmac
import time import time
import decimal import decimal
import hashlib import hashlib
from pathlib import Path
import trio import trio
from trio_typing import TaskStatus from trio_typing import TaskStatus
import pendulum import pendulum
@ -87,13 +90,16 @@ log = get_logger('piker.brokers.binance')
def get_config() -> dict: def get_config() -> dict:
conf: dict
path: Path
conf, path = config.load() conf, path = config.load()
section = conf.get('binance') section = conf.get('binance')
if not section: if not section:
log.warning(f'No config section found for binance in {path}') log.warning(f'No config section found for binance in {path}')
return dict() return {}
return section return section
@ -225,12 +231,12 @@ class Client:
def __init__(self) -> None: def __init__(self) -> None:
self._sesh = asks.Session(connections=4) self._sesh = asks.Session(connections=4)
self._sesh.base_location = _url self._sesh.base_location: str = _url
self._pairs: dict[str, Pair] = {} self._pairs: dict[str, Pair] = {} # mkt info table
conf = get_config() conf = get_config()
self.api_key = conf.get('api', {}).get('key') self.api_key: str = conf.get('api_key', '')
self.api_secret = conf.get('api', {}).get('secret') self.api_secret: str = conf.get('api_secret', '')
if self.api_key: if self.api_key:
self._sesh.headers.update({'X-MBX-APIKEY': self.api_key}) self._sesh.headers.update({'X-MBX-APIKEY': self.api_key})
@ -255,7 +261,7 @@ class Client:
async def _api( async def _api(
self, self,
method: str, method: str,
params: Union[dict, OrderedDict], params: dict | OrderedDict,
signed: bool = False, signed: bool = False,
action: str = 'get' action: str = 'get'
) -> dict[str, Any]: ) -> dict[str, Any]:
@ -263,19 +269,11 @@ class Client:
if signed: if signed:
params['signature'] = self._get_signature(params) params['signature'] = self._get_signature(params)
if action == 'get': resp = await getattr(self._sesh, action)(
resp = await self._sesh.get( path=f'/api/v3/{method}',
path=f'/api/v3/{method}', params=params,
params=params, timeout=float('inf')
timeout=float('inf') )
)
elif action == 'post':
resp = await self._sesh.post(
path=f'/api/v3/{method}',
params=params,
timeout=float('inf')
)
return resproc(resp, log) return resproc(resp, log)
@ -830,21 +828,28 @@ async def handle_order_requests(
async def trades_dialogue( async def trades_dialogue(
ctx: tractor.Context, ctx: tractor.Context,
loglevel: str = None loglevel: str = None
) -> AsyncIterator[dict[str, Any]]: ) -> AsyncIterator[dict[str, Any]]:
# XXX: required to propagate ``tractor`` loglevel to piker logging async with open_cached_client('binance') as client:
get_console_log(loglevel or tractor.current_actor().loglevel) if not client.api_key:
await ctx.started('paper')
return
positions = {} # TODO: get already open pos # table: PpTable
# ledger: TransactionLedger
await ctx.started(positions, {}) # TODO: load pps and accounts using accounting apis!
# positions: dict = {}
# accounts: set[str] = set()
# await ctx.started((positions, {}))
async with ( # async with (
ctx.open_stream() as ems_stream, # ctx.open_stream() as ems_stream,
trio.open_nursery() as n # trio.open_nursery() as n
): # ):
n.start_soon(handle_order_requests, ems_stream) # n.start_soon(handle_order_requests, ems_stream)
await trio.sleep_forever() # await trio.sleep_forever()
@tractor.context @tractor.context