def test_taker_order(self): qty = Decimal('1.234') price = Decimal('0.0234') order_type = 'buy' taker_order = TakerOrder( exchange='binance', status='open', order_type=order_type, currency='BTC/ETH', order_body={ 'quantity': str(qty), 'price': price }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) is_buy = order_type == 'buy' assert taker_order.quantity() == qty assert taker_order.price() == price assert taker_order.is_sell() != is_buy assert taker_order.is_buy() == is_buy
async def test_find_update_or_create_orders(repository_with_schema): taker_order = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) taker_order = await repository_with_schema.create_order(taker_order) updated_order_body = {'foo': 'bar bar'} new_taker_order = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 3 }, order_id='fe55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=2, created_at=None, updated_at=None, ) taker_order.order_body = updated_order_body updated_orders = await repository_with_schema.find_update_or_create_orders( [taker_order, new_taker_order]) assert len(updated_orders) == 2 for order in updated_orders: if order.order_id == new_taker_order.order_id: assert order.id == 2 else: assert order.id == taker_order.id assert order.order_body == updated_order_body assert order == taker_order
async def test_get_taker_orders_by_maker_id(repository_with_schema): taker_order_1 = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) taker_order_2 = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) taker_order_1 = await repository_with_schema.create_order(taker_order_1) taker_order_2 = await repository_with_schema.create_order(taker_order_2) by_maker_order_id = await repository_with_schema.get_taker_orders_by_maker_id( {1}) assert by_maker_order_id[0] == taker_order_1 assert by_maker_order_id[1] == taker_order_2
async def get_taker_orders_by_maker_id( self, maker_ids: Set[int]) -> List[TakerOrder]: await self._ensure_connected() query = TakerOrdersTable.select().where( TakerOrdersTable.c.maker_order_id.in_(maker_ids)) res = await self._db.fetch_all(query=query) # TODO this mapping to record class should be pulled up to function outside of repo class results = [] res = await self._db.fetch_all(query=query) for row_id, *fields in res: record = TakerOrder(*fields) record.id = row_id results.append(record) return results
async def test_update_status(repository_with_schema): taker_order = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) new_status = Status.CANCELED taker_order = await repository_with_schema.create_order(taker_order) await repository_with_schema.update_status([taker_order], new_status) cancelled_order, = await repository_with_schema._get_orders( 'taker', new_status) assert cancelled_order.order_id == taker_order.order_id assert cancelled_order.exchange == taker_order.exchange
async def test_update_order(repository_with_schema): taker_order = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=1, created_at=None, updated_at=None, ) new_order_id = 'aaa' taker_order = await repository_with_schema.create_order(taker_order) taker_order.order_id = new_order_id await repository_with_schema.update_order(taker_order) fetched_order, = await repository_with_schema.get_open_orders('taker') assert fetched_order == taker_order assert fetched_order.order_id == new_order_id
async def get_filled_orders_in_range(self, start_date, end_date): query = TakerOrdersTable.select().where( TakerOrdersTable.c.status == Status.FILLED) filled_taker_orders = [] res = await self._db.fetch_all(query=query) for row_id, *fields in res: record = TakerOrder(*fields) record.id = row_id filled_taker_orders.append(record) if len(filled_taker_orders) == 0: return [] maker_order_ids = list( map(lambda o: o.maker_order_id, filled_taker_orders)) query_maker = MakerOrdersTable.select().where( MakerOrdersTable.c.id.in_(maker_order_ids)) filled_maker_orders_res = await self._db.fetch_all(query=query_maker) id_to_maker_order_map = {} for row_id, *fields in filled_maker_orders_res: record = MakerOrder(*fields) record.id = row_id id_to_maker_order_map[str(row_id)] = record maker_taker_pairs = [] for taker_order in filled_taker_orders: maker_order_id = str(taker_order.maker_order_id) maker_order = id_to_maker_order_map[maker_order_id] if maker_order.order_type == OrderType.BUY: profit = -maker_order.price() * maker_order.quantity( ) + taker_order.price() * taker_order.quantity() else: profit = maker_order.price() * maker_order.quantity( ) - taker_order.price() * taker_order.quantity() profit = decimal_to_str(profit) maker_taker_pairs.append({ 'currency': taker_order.currency, 'created_at': taker_order.created_at, 'profit': profit, 'taker': { 'id': taker_order.id, 'side': taker_order.order_type, 'order_id': taker_order.order_id, 'maker_order_id': taker_order.maker_order_id, 'price': decimal_to_str(taker_order.price()), 'quantity': decimal_to_str(taker_order.quantity()), 'total': decimal_to_str(taker_order.price() * taker_order.quantity()), }, 'maker': { 'id': maker_order.id, 'side': maker_order.order_type, 'order_id': { 'tx_hash': maker_order.tx_hash, 'tx_output_index': maker_order.tx_output_index, }, 'price': decimal_to_str(maker_order.price()), 'quantity': decimal_to_str(maker_order.quantity()), 'total': decimal_to_str(maker_order.price() * maker_order.quantity()), } }) maker_taker_pairs = sorted(maker_taker_pairs, key=lambda o: o['created_at']) for o in maker_taker_pairs: o['created_at'] = arrow.get(o['created_at']).isoformat() return maker_taker_pairs
async def main(): currency = 'LSK/BTC' for i in range(10): print(f'Creating order pairs for testing:{i}') now = arrow.utcnow() tx_hash = '7059fc2763fba359d30b248b243107b7eb7a39909eb5dfff216f157ea53b04c8' tx_output_index = 0 maker_order_body = { 'tradeHeight': 51, 'deposit': 600, 'settlement': 300, 'shiftMaker': 5, 'shiftTaker': 20, 'sendsFromChain': 'btc', 'receivesToChain': 'lsk', 'sendsFromAddress': '1AJP6ck7XkhhTT7QTrn7U81UczmxgX3Azn', 'receivesToAddress': '2570416870016743267L', 'sendsUnit': '0.01493', 'receivesUnit': '100', 'doubleHashedBcAddress': '0x74a9ab94273274e627bb17ec9b18af81f48646a2b62ab58afde3bab11bda9676', 'collateralizedNrg': '1', 'nrgUnit': '1', 'txHash': tx_hash, 'txOutputIndex': tx_output_index, 'isSettled': False, 'fixedUnitFee': '0', 'base': 2, 'sendsUnitDenomination': 'btc', 'receivesUnitDenomination': 'lsk' } maker_order = MakerOrder( exchange='borderless', status='filled', order_type='buy', # buy LSK/BTC as i receiveToChain is lsk currency=currency, order_body=maker_order_body, tx_hash=tx_hash, tx_output_index=tx_output_index, block_height='1', taker_order_body={}, created_at=now.datetime, updated_at=now.datetime, id=None, ) maker_order = await order_repository.create_order(maker_order) maker_order_id = maker_order.id taker_order_time = now.shift(minutes=10) if maker_order.order_type == OrderType.BUY: taker_order_type = OrderType.SELL taker_price = decimal_to_str(maker_order.price() * Decimal('1.01')) else: taker_order_type = OrderType.BUY taker_price = decimal_to_str(maker_order.price() * Decimal('0.99')) taker_order_body = { 'price': taker_price, 'quantity': decimal_to_str(maker_order.quantity()), } taker_order = TakerOrder( exchange='binance', status='filled', order_type=taker_order_type, currency=currency, order_body=taker_order_body, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=maker_order.id, created_at=taker_order_time.datetime, updated_at=taker_order_time.datetime, ) taker_order = await order_repository.create_order(taker_order) await asyncio.sleep(1)
async def test_get_open_orders(repository_with_schema): open_orders = await repository_with_schema.get_open_orders('taker') assert len(open_orders) == 0 open_orders = await repository_with_schema.get_open_orders('maker') assert len(open_orders) == 0 maker_order = MakerOrder( exchange='borderless', status='open', order_type='sell', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 1 }, tx_hash='a1b2b3c4d5e6faa2321', tx_output_index=1, block_height='1', taker_order_body={}, created_at=None, updated_at=None, id=None, ) maker_order = await repository_with_schema.create_order(maker_order) assert maker_order.id == 1 taker_order = TakerOrder( exchange='binance', status='open', order_type='buy', currency='BTC/ETH', order_body={ 'foo': 'bar', 'baz': 2 }, order_id='ee55df98-95d7-409f-a8f0-d8a5bf047acd', maker_order_id=maker_order.id, created_at=None, updated_at=None, ) taker_order = await repository_with_schema.create_order(taker_order) assert taker_order.id == 1 open_taker_orders_count = await repository_with_schema.count_open_orders( 'taker') open_orders = await repository_with_schema.get_open_orders('taker') assert len(open_orders) == open_taker_orders_count assert isinstance(open_orders[0], TakerOrder) assert open_orders[0].id == taker_order.id open_maker_orders_count = await repository_with_schema.count_open_orders( 'maker') open_orders = await repository_with_schema.get_open_orders('maker') assert len(open_orders) == open_maker_orders_count assert isinstance(open_orders[0], MakerOrder) assert open_orders[0].id == maker_order.id await repository_with_schema.delete_order(maker_order) await repository_with_schema.delete_order(taker_order) open_orders = await repository_with_schema.get_open_orders('maker') assert len(open_orders) == 0 open_orders = await repository_with_schema.get_open_orders('taker') assert len(open_orders) == 0 await repository_with_schema.close()