mock orders validated from kraken

kraken_orders
Konstantine Tsafatinos 2022-02-07 20:13:24 -05:00
parent b55debbe95
commit 66da58525d
1 changed files with 73 additions and 47 deletions

View File

@ -234,7 +234,7 @@ class Client:
) )
return resproc(resp, log) return resproc(resp, log)
async def get_user_data( async def kraken_endpoint(
self, self,
method: str, method: str,
data: Dict[str, Any] data: Dict[str, Any]
@ -251,9 +251,9 @@ class Client:
self, self,
data: Dict[str, Any] = {} data: Dict[str, Any] = {}
) -> Dict[str, Any]: ) -> Dict[str, Any]:
balances = await self.get_user_data('Balance', data) balances = await self.kraken_endpoint('Balance', data)
## TODO: grab all entries, not just first 50 ## TODO: grab all entries, not just first 50
traders = await self.get_user_data('TradesHistory', data) traders = await self.kraken_endpoint('TradesHistory', data)
positions = {} positions = {}
vols = {} vols = {}
@ -277,6 +277,33 @@ class Client:
return positions, vols return positions, vols
async def submit_limit(
self,
oid: str,
symbol: str,
price: float,
action: str,
size: str,
# account: str,
reqid: int = None,
) -> int:
"""Place an order and return integer request id provided by client.
"""
# Build order data from kraken
data = {
"userref": 1,
"ordertype": "limit",
"type": action,
"volume": size,
"pair": symbol,
"price": price,
"validate": True
}
resp = await self.kraken_endpoint('AddOrder', data)
print(resp)
return reqid
async def symbol_info( async def symbol_info(
self, self,
pair: Optional[str] = None, pair: Optional[str] = None,
@ -447,61 +474,60 @@ async def handle_order_requests(
async for request_msg in ems_order_stream: async for request_msg in ems_order_stream:
log.info(f'Received order request {request_msg}') log.info(f'Received order request {request_msg}')
# action = request_msg['action'] action = request_msg['action']
# if action in {'buy', 'sell'}: if action in {'buy', 'sell'}:
# account = request_msg['account'] account = request_msg['account']
# if account != 'kraken.spot': if account != 'kraken.spot':
# log.error( log.error(
# 'This is a kraken account, \ 'This is a kraken account, \
# only a `kraken.spot` selection is valid' only a `kraken.spot` selection is valid'
# ) )
# await ems_order_stream.send(BrokerError( await ems_order_stream.send(BrokerError(
# oid=request_msg['oid'], oid=request_msg['oid'],
# symbol=request_msg['symbol'], symbol=request_msg['symbol'],
# reason=f'Kraken only, No account found: `{account}` ?', reason=f'Kraken only, No account found: `{account}` ?',
# ).dict()) ).dict())
# continue continue
# # validate # validate
# order = BrokerdOrder(**request_msg) order = BrokerdOrder(**request_msg)
# # call our client api to submit the order # call our client api to submit the order
# ## TODO: look into the submit_limit method, do it write my own? reqid = await client.submit_limit(
# reqid = await client.submit_limit(
# oid=order.oid, oid=order.oid,
# symbol=order.symbol, symbol=order.symbol,
# price=order.price, price=order.price,
# action=order.action, action=order.action,
# size=order.size, size=order.size,
# ## XXX: how do I handle new orders ## XXX: how do I handle new orders
# reqid=order.reqid, reqid=order.reqid,
# ) )
# # deliver ack that order has been submitted to broker routing # deliver ack that order has been submitted to broker routing
# await ems_order_stream.send( await ems_order_stream.send(
# BrokerdOrderAck( BrokerdOrderAck(
# # ems order request id # ems order request id
# oid=order.oid, oid=order.oid,
# # broker specific request id # broker specific request id
# reqid=reqid, reqid=reqid,
# ).dict() ).dict()
# ) )
# elif action == 'cancel': elif action == 'cancel':
# msg = BrokerdCancel(**request_msg) msg = BrokerdCancel(**request_msg)
# await client.submit_cancel( await client.submit_cancel(
# reqid=msg.reqid reqid=msg.reqid
# ) )
# else: else:
# log.error(f'Unknown order command: {request_msg}') log.error(f'Unknown order command: {request_msg}')
@tractor.context @tractor.context
@ -526,7 +552,7 @@ async def trades_dialogue(
msg = pack_position(acc_name, norm_sym, pos, vols[ticker]) msg = pack_position(acc_name, norm_sym, pos, vols[ticker])
all_positions.append(msg.dict()) all_positions.append(msg.dict())
open_orders = await client.get_user_data('OpenOrders', {}) open_orders = await client.kraken_endpoint('OpenOrders', {})
#await tractor.breakpoint() #await tractor.breakpoint()
await ctx.started((all_positions, (acc_name,))) await ctx.started((all_positions, (acc_name,)))