def test_verify_market_order_payload():
    """Verify market order structure"""
    raw = CreateOrder(MarketOrder("BTC_EUR", Side.sell, 0.05))
    as_json_string = raw.to_json()
    LOG.debug("market_order: %s", as_json_string)
    market_order = json.loads(as_json_string)
    assert market_order["type"] == "CREATE_ORDER"
    result = market_order["order"]
    assert result["instrument_code"] == "BTC_EUR"
    assert result["type"] == "MARKET"
    assert result["side"] == "SELL"
    assert Decimal(result["amount"]) == Decimal(0.05)
def test_verify_stop_order_payload():
    """Verify stop order structure"""
    raw = CreateOrder(StopOrder("BTC_EUR", Side.sell, 1.23, 5000.50, 5001))
    as_json_string = raw.to_json()
    LOG.debug("stop_order: %s", as_json_string)
    stop_order = json.loads(as_json_string)
    assert stop_order["type"] == "CREATE_ORDER"
    result = stop_order["order"]
    assert result["instrument_code"] == "BTC_EUR"
    assert result["type"] == "STOP"
    assert result["side"] == "SELL"
    assert Decimal(result["amount"]) == Decimal(1.23)
    assert Decimal(result["price"]) == Decimal(5000.50)
    assert Decimal(result["trigger_price"]) == Decimal(5001)
def test_verify_limit_order_payload():
    """Verify limit order structure"""
    raw = CreateOrder(LimitOrder("BTC_EUR", Side.buy, 1.2, 5000.50))
    as_json_string = raw.to_json()
    LOG.debug("limit_order: %s", as_json_string)
    limit_order = json.loads(as_json_string)
    assert limit_order["type"] == "CREATE_ORDER"
    result = limit_order["order"]
    assert result["time_in_force"] == "GOOD_TILL_CANCELLED"
    assert result["instrument_code"] == "BTC_EUR"
    assert result["type"] == "LIMIT"
    assert result["side"] == "BUY"
    assert Decimal(result["amount"]) == Decimal(1.2)
    assert Decimal(result["price"]) == Decimal(5000.50)
def test_verify_client_id():
    """Verify that client_id is accepted"""
    my_client_id = str(uuid.uuid4())
    as_json_string = CreateOrder(
        LimitOrder("BTC_EUR", Side.buy, 1.2, 5000.50, my_client_id)).to_json()
    LOG.debug("order: %s", as_json_string)
    limit_order = json.loads(as_json_string)
    assert str(my_client_id) == limit_order["order"]["client_id"]

    as_json_string = CreateOrder(
        StopOrder("BTC_EUR", Side.buy, 1.2, 5000.50, 5000,
                  my_client_id)).to_json()
    LOG.debug("order: %s", as_json_string)
    stop_order = json.loads(as_json_string)
    assert str(my_client_id) == stop_order["order"]["client_id"]
示例#5
0
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()
def test_verify_limit_order_supports_time_in_force():
    """Verify limit order with time_in_force structure"""
    raw = CreateOrder(
        LimitOrder("BTC_EUR", Side.sell, 0.12, 123.0, None,
                   TimeInForce.fill_or_kill))
    as_json_string = raw.to_json()
    LOG.debug("limit_order: %s", as_json_string)
    limit_order = json.loads(as_json_string)
    assert limit_order["type"] == "CREATE_ORDER"
    result = limit_order["order"]
    assert result["time_in_force"] == "FILL_OR_KILL"
    assert result["instrument_code"] == "BTC_EUR"
    assert result["type"] == "LIMIT"
    assert result["side"] == "SELL"
    assert Decimal(result["amount"]) == Decimal(0.12)
    assert Decimal(result["price"]) == Decimal(123.0)
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()
async def main():
    when_order_created = asyncio.get_event_loop().create_future()
    when_order_cancelled = asyncio.get_event_loop().create_future()
    when_order_book_snapshot_received = asyncio.get_event_loop().create_future(
    )

    async def handle_message(event: json):
        LOG.info("%s", event)
        if event["type"] == "ORDER_BOOK_SNAPSHOT":
            when_order_book_snapshot_received.set_result(
                "snapshot received...")
        elif 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 = AdvancedBitpandaProWebsocketClient(
        api_token=my_api_token,
        wss_host="wss://streams.exchange.bitpanda.com",
        callback=handle_message)

    account_history_subscription = AccountHistorySubscription()
    trading_subscription = TradingSubscription()
    orders_subscription = OrdersSubscription()
    order_book_subscription = OrderBookSubscription(["BTC_EUR"])
    await bp_client.start(
        Subscriptions([
            account_history_subscription, orders_subscription,
            trading_subscription, order_book_subscription
        ]))

    await when_order_book_snapshot_received
    LOG.info("asks book BTC_EUR: %s", bp_client.get_order_book("BTC_EUR").asks)
    LOG.info("bids BTC_EUR: %s", bp_client.get_order_book("BTC_EUR").bids)

    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("Balances: %s", bp_client.get_state().balances)
    LOG.info("Open orders: %s", bp_client.get_state().open_orders_by_order_id)

    LOG.info("Cancel Order with client_id: %s", client_id)
    await bp_client.cancel_order(CancelOrderByClientId(client_id))
    await when_order_cancelled
    LOG.info("Open orders: %s", bp_client.get_state().open_orders_by_order_id)

    await bp_client.close()
示例#9
0
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 = AdvancedBitpandaProWebsocketClient(
        api_token=my_api_token,
        wss_host="wss://streams.exchange.bitpanda.com",
        callback=handle_message)

    # Activates ACCOUNT_HISTORY, TRADING & ORDERS channel
    await bp_client.start_with(None, True)

    client_id = str(uuid.uuid4())
    new_order_with_client_id = CreateOrder(
        LimitOrder("BTC_EUR", Side.buy, Decimal('0.01'), Decimal('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("Balances: %s", bp_client.get_state().balances)
    LOG.info("Open orders: %s", bp_client.get_state().open_orders_by_order_id)

    LOG.info("Cancel Order with client_id: %s", client_id)
    await bp_client.cancel_order(CancelOrderByClientId(client_id))
    await when_order_cancelled
    LOG.info("Open orders: %s", bp_client.get_state().open_orders_by_order_id)

    await bp_client.close()
 async def create_order(self, order: CreateOrder):
     """Creates an order via orders channel"""
     await self.send_message(order.to_json())