basic order submission and cancelling with kraken

kraken_orders
Konstantine Tsafatinos 2022-02-09 13:50:42 -05:00
parent b21bbf5031
commit 96dd5c632f
1 changed files with 53 additions and 22 deletions

View File

@ -243,18 +243,17 @@ class Client:
uri_path = f'/0/private/{method}' uri_path = f'/0/private/{method}'
data['nonce'] = str(int(1000*time.time())) data['nonce'] = str(int(1000*time.time()))
resp = await self._private(method, data, uri_path) resp = await self._private(method, data, uri_path)
err = resp['error'] return resp
if err:
print(err)
return resp['result']
async def get_positions( async def get_positions(
self, self,
data: Dict[str, Any] = {} data: Dict[str, Any] = {}
) -> Dict[str, Any]: ) -> Dict[str, Any]:
balances = await self.kraken_endpoint('Balance', data) resp = await self.kraken_endpoint('Balance', data)
balances = resp['result']
## TODO: grab all entries, not just first 50 ## TODO: grab all entries, not just first 50
traders = await self.kraken_endpoint('TradesHistory', data) resp = await self.kraken_endpoint('TradesHistory', data)
traders = resp['result']
positions = {} positions = {}
vols = {} vols = {}
@ -284,7 +283,7 @@ class Client:
symbol: str, symbol: str,
price: float, price: float,
action: str, action: str,
size: str, size: float,
# account: str, # account: str,
reqid: int = None, reqid: int = None,
) -> int: ) -> int:
@ -296,14 +295,26 @@ class Client:
"userref": reqid, "userref": reqid,
"ordertype": "limit", "ordertype": "limit",
"type": action, "type": action,
"volume": size, "volume": str(size),
"pair": symbol, "pair": symbol,
"price": price, "price": str(price),
"validate": True # set to True test AddOrder call without a real submission
"validate": False
} }
resp = await self.kraken_endpoint('AddOrder', data) resp = await self.kraken_endpoint('AddOrder', data)
return resp
async def submit_cancel(
self,
reqid: str,
) -> None:
"""Send cancel request for order id ``reqid``.
"""
# txid is a transaction id given by kraken
data = {"txid": reqid}
resp = await self.kraken_endpoint('CancelOrder', data)
print(resp) print(resp)
return reqid
async def symbol_info( async def symbol_info(
self, self,
@ -495,10 +506,11 @@ async def handle_order_requests(
continue continue
# validate # validate
temp_id = next(userref_counter)
order = BrokerdOrder(**request_msg) order = BrokerdOrder(**request_msg)
# call our client api to submit the order # call our client api to submit the order
reqid = await client.submit_limit( resp = await client.submit_limit(
oid=order.oid, oid=order.oid,
symbol=order.symbol, symbol=order.symbol,
@ -506,21 +518,40 @@ async def handle_order_requests(
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=next(userref_counter), reqid=temp_id,
) )
# deliver ack that order has been submitted to broker routing err = resp['error']
await ems_order_stream.send( if err:
BrokerdOrderAck( log.error(f'Failed to submit order')
await ems_order_stream.send(
BrokerdError(
oid=order.oid,
reqid=temp_id,
symbol=order.symbol,
reason="Failed order submission",
broker_details=resp
).dict()
)
else:
## TODO: handle multiple cancels
## txid is an array of strings
reqid = resp['result']['txid'][0]
# deliver ack that order has been submitted to broker routing
await ems_order_stream.send(
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() # account the made the order
) account=order.account
).dict()
)
elif action == 'cancel': elif action == 'cancel':
msg = BrokerdCancel(**request_msg) msg = BrokerdCancel(**request_msg)