async def main(): when_order_created = asyncio.get_event_loop().create_future() when_order_cancelled = asyncio.get_event_loop().create_future() async def handle_message(event: json): LOG.info("%s", event) if event["type"] == "ORDER_CREATED": when_order_created.set_result("created...") elif event["type"] == "ORDER_SUBMITTED_FOR_CANCELLATION": when_order_cancelled.set_result("cancelled...") # add your api token my_api_token = "eyJ..." bp_client = BitpandaProWebsocketClient( api_token=my_api_token, wss_host="wss://streams.exchange.bitpanda.com", callback=handle_message ) orders_subscription = OrdersSubscription() await bp_client.start(Subscriptions([orders_subscription])) client_id = str(uuid.uuid4()) new_order_with_client_id = CreateOrder(LimitOrder("BTC_EUR", Side.buy, 0.01, 1000.50, client_id)) LOG.info("Creating new Order with client_id: %s", new_order_with_client_id) await bp_client.create_order(new_order_with_client_id) await when_order_created LOG.info("Cancel Order with client_id: %s", client_id) await bp_client.cancel_order(CancelOrderByClientId(client_id)) await when_order_cancelled await bp_client.close()
async def test_verify_successful_orders_channel_subscription(event_loop): """Tests subscribe / unsubscribe of the orders channel""" api_token = os.environ['BP_PRO_API_TOKEN'] test_host = os.environ['TEST_HOST'] when_subscribed = event_loop.create_future() when_order_created = event_loop.create_future() when_order_cancelled = event_loop.create_future() when_unsubscribed = event_loop.create_future() async def handle_message(json_message): if json_message["type"] == "SUBSCRIPTIONS": when_subscribed.set_result("subscribed") elif json_message["type"] == "UNSUBSCRIBED": when_unsubscribed.set_result("unsubscribed") elif json_message["type"] == "ORDER_CREATED": when_order_created.set_result("Order created") elif json_message["type"] == "ORDER_SUBMITTED_FOR_CANCELLATION": when_order_cancelled.set_result("Order cancelled") else: LOG.info("Ignored Message %s", json_message) client = BitpandaProWebsocketClient(api_token, test_host, handle_message) subscription = OrdersSubscription() await client.start(Subscriptions([subscription])) LOG.info(await when_subscribed) my_client_id = uuid.uuid4() await client.create_order( CreateOrder( LimitOrder("BTC_EUR", Side.buy, 0.01, 5000.50, str(my_client_id)))) LOG.info(await when_order_created) await client.cancel_order(CancelOrderByClientId(my_client_id)) LOG.info(await when_order_cancelled) await client.unsubscribe(Unsubscription([ChannelName.orders.value])) LOG.info(await when_unsubscribed) await client.close()
def __init__(self, api_token=None, wss_host=None, callback=None): LOG.debug("init advanced client...") self.callback = callback self.bp_ws_client = BitpandaProWebsocketClient(api_token, wss_host, self.handle_message) self.order_books = dict() self.state = AccountState(dict(), dict(), dict(), dict(), dict(), dict(), dict()) self.trading = TradingMessageHandler(self.state) self.orders_handler = OrdersMessageHandler(self.state) self.account_history = AccountHistoryMessageHandler(self.state) self.trading_buffer = [] self.initial_state_restore = False
async def test_emit_heartbeat_on_idle_connection(event_loop): """Expecting heartbeat message from event emitter: {"subscription":"SYSTEM","channel_name":"SYSTEM","type":"HEARTBEAT","time":"YYYY-MM-ddTHH:mm:ss.0Z"} """ future_result = event_loop.create_future() async def handle_message(json_message): LOG.info("emitted event %s", json_message) assert json_message["subscription"] == "SYSTEM" assert json_message["channel_name"] == "SYSTEM" assert json_message["type"] == "HEARTBEAT" future_result.set_result("Success") client = BitpandaProWebsocketClient(None, "wss://streams.exchange.bitpanda.com", handle_message) await client.start(None) LOG.info(await future_result) await client.close()
async def main(): when_msg_received = asyncio.get_event_loop().create_future() async def handle_message(event: json): LOG.info("%s", event) # ... add custom code here if when_msg_received.done() is False: when_msg_received.set_result("Received a message...") bp_client = BitpandaProWebsocketClient( api_token=None, wss_host="wss://streams.exchange.bitpanda.com", callback=handle_message) order_book_subscription = OrderBookSubscription(["BTC_EUR", "BEST_EUR"]) await bp_client.start(Subscriptions([order_book_subscription])) # ... await when_msg_received # ... await bp_client.close()
async def test_verify_successful_order_book_subscription(event_loop): """test order book channel subscribe and unsubscribe""" test_host = os.environ['TEST_HOST'] future_subscribe = event_loop.create_future() future_unsubscribe = event_loop.create_future() async def handle_message(json_message): LOG.debug("emitted event %s", json_message) if json_message["type"] == "SUBSCRIPTIONS": LOG.debug("Subscribed to order book channel") future_subscribe.set_result("Success") elif json_message["type"] == "UNSUBSCRIBED": LOG.debug("Unsubscribed from order book channel") future_unsubscribe.set_result("Success") else: LOG.debug("Ignored Message") client = BitpandaProWebsocketClient(None, test_host, handle_message) order_book_subscription = OrderBookSubscription(['BTC_EUR', 'BEST_EUR']) await client.start(Subscriptions([order_book_subscription])) LOG.debug(await future_subscribe) await client.unsubscribe(Unsubscription([ChannelName.order_book.value])) LOG.debug(await future_unsubscribe) await client.close()
async def main(): when_msg_received = asyncio.get_event_loop().create_future() async def handle_message(event: json): LOG.info("%s", event) # ... add custom code here if when_msg_received.done() is False: when_msg_received.set_result("Received a message...") # add your api token my_api_token = "ey..." bp_client = BitpandaProWebsocketClient( api_token=my_api_token, wss_host="wss://streams.exchange.bitpanda.com", callback=handle_message ) trading_subscription = TradingSubscription() await bp_client.start(Subscriptions([trading_subscription])) # ... await when_msg_received # ... await bp_client.close()
async def test_verify_successful_account_history_subscription(event_loop): """Test account history subscription handling""" api_token = os.environ["BP_PRO_API_TOKEN"] test_host = os.environ["TEST_HOST"] when_subscribed = event_loop.create_future() when_unsubscribed = event_loop.create_future() async def handle_message(json_message): if json_message["type"] == "SUBSCRIPTIONS": when_subscribed.set_result("subscribed") elif json_message["type"] == "UNSUBSCRIBED": when_unsubscribed.set_result("unsubscribed") else: LOG.info("Ignored Message %s", json_message) client = BitpandaProWebsocketClient(api_token, test_host, handle_message) subscription = AccountHistorySubscription() await client.start(Subscriptions([subscription])) LOG.info(await when_subscribed) await client.unsubscribe( Unsubscription([ChannelName.account_history.value])) LOG.info(await when_unsubscribed) await client.close()