side = "sell" if order.is_sell else "buy " fill_status = f"with {order.filled_amount} filled" if order.filled_amount > Wad( 0) else "unfilled" print(f"[{index}] {order.order_id} {side} {str(order.amount)[:9]} " f"at {str(order.price)[:12]} " f"on {datetime.datetime.utcfromtimestamp(order.timestamp)} " + fill_status) #f"page {order.page}") def print_trades(trades): for trade in trades: side = "sell" if trade.is_sell else "buy " print(f"{side} {str(trade.amount)[:9]} {trade.amount_symbol} " f"at {str(trade.price)[:12]} " f"{pair.split('_')[1]} " f"on {datetime.datetime.utcfromtimestamp(trade.timestamp)}") # Gets open orders orders = okex.get_orders(pair) print_orders(orders) # Gets all orders orders = okex.get_orders_history(pair, 9) print_orders(orders) #trades = okex.get_trades(pair) #print(trades[:3]) # trades = okex.get_all_trades(pair) # print_trades(trades[:9])
class TestOKEX: def setup_method(self): self.okex = OKEXApi(api_server="localhost", api_key="00000000-0000-0000-0000-000000000000", secret_key="DEAD000000000000000000000000DEAD", password="******", timeout=15.5) def test_order(self): price = Wad.from_number(4.8765) amount = Wad.from_number(0.222) filled_amount = Wad.from_number(0.153) order = Order(order_id="153153", timestamp=int(time.time()), pair="MKR-ETH", is_sell=False, price=price, amount=amount, filled_amount=filled_amount) assert (order.price == order.sell_to_buy_price) assert (order.price == order.buy_to_sell_price) assert (order.remaining_buy_amount == amount - filled_amount) assert (order.remaining_sell_amount == (amount - filled_amount) * price) def test_ticker(self, mocker): pair = "mkr_usdt" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.ticker(pair) assert (str(response["instrument_id"]).lower().replace('-', '_') == pair) assert (float(response["best_ask"]) > 0) assert (response["instrument_id"] == response["product_id"]) def test_depth(self, mocker): pair = "mkr_usdt" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.depth(pair) assert ("bids" in response) assert ("asks" in response) assert (len(response["bids"]) > 0) assert (len(response["asks"]) > 0) def test_candles(self, mocker): pair = "mkr_usdt" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.candles(pair, "1min") assert (len(response) > 0) for item in response: assert (isinstance(item, Candle)) assert (item.timestamp > 0) assert (float(item.open) > 0) assert (float(item.high) > 0) assert (float(item.low) > 0) assert (float(item.close) > 0) def test_get_balances(self, mocker): mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.get_balances() assert (len(response) > 0) assert ("MKR" in response) assert ("ETH" in response) @staticmethod def check_orders(orders): by_oid = {} duplicate_count = 0 duplicate_first_found = -1 missorted_found = False last_timestamp = 0 for index, order in enumerate(orders): assert (isinstance(order, Order)) assert (order.order_id is not None) assert (order.timestamp > 0) # An order cannot be filled for more than the order amount assert (order.filled_amount <= order.amount) # Check for duplicates and missorted orders if order.order_id in by_oid: duplicate_count += 1 if duplicate_first_found < 0: duplicate_first_found = index else: by_oid[order.order_id] = order if not missorted_found and last_timestamp > 0: if order.timestamp > last_timestamp: print(f"missorted order found at index {index}") missorted_found = True last_timestamp = order.timestamp if duplicate_count > 0: print(f"{duplicate_count} duplicate orders were found, " f"starting at index {duplicate_first_found}") else: print("no duplicates were found") assert (duplicate_count == 0) assert (missorted_found is False) def test_get_orders(self, mocker): pair = "mkr_eth" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.get_orders(pair) assert (len(response) > 0) for order in response: # Open orders cannot be completed filled assert (order.filled_amount < order.amount) assert (isinstance(order.is_sell, bool)) assert (order.price > Wad(0)) TestOKEX.check_orders(response) def test_get_all_orders(self, mocker): pair = "mkr_eth" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.get_orders_history(pair, 99) assert (len(response) > 0) for order in response: assert (isinstance(order.is_sell, bool)) assert (order.price > Wad(0)) TestOKEX.check_orders(response) def test_order_placement_and_cancellation(self, mocker): pair = "mkr_usdt" mocker.patch("requests.post", side_effect=OkexMockServer.handle_request) order_id = self.okex.place_order(pair, True, Wad.from_number(639.3), Wad.from_number(0.15)) assert (isinstance(order_id, str)) assert (order_id is not None) cancel_result = self.okex.cancel_order(pair, order_id) assert (cancel_result) @staticmethod def check_trades(trades): by_tradeid = {} duplicate_count = 0 duplicate_first_found = -1 missorted_found = False last_timestamp = 0 for index, trade in enumerate(trades): assert (isinstance(trade, Trade)) if trade.trade_id in by_tradeid: print(f"found duplicate trade {trade.trade_id}") duplicate_count += 1 if duplicate_first_found < 0: duplicate_first_found = index else: by_tradeid[trade.trade_id] = trade if not missorted_found and last_timestamp > 0: if trade.timestamp > last_timestamp: print(f"missorted trade found at index {index}") missorted_found = True last_timestamp = trade.timestamp if duplicate_count > 0: print(f"{duplicate_count} duplicate trades were found, " f"starting at index {duplicate_first_found}") else: print("no duplicates were found") assert (duplicate_count == 0) assert (missorted_found is False) def test_get_trades(self, mocker): pair = "mkr_eth" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.get_trades(pair) assert (len(response) > 0) TestOKEX.check_trades(response) def test_get_all_trades(self, mocker): pair = "mkr_usdt" mocker.patch("requests.get", side_effect=OkexMockServer.handle_request) response = self.okex.get_all_trades(pair) assert (len(response) > 0) TestOKEX.check_trades(response)