Exemple #1
0
def private_order_service_cancel_order(channel):
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    # Create a request for an order cancellation with a tonce that is the nanos since unix epoch
    tonce = round(time.time() * 1E9)
    request = order_pb2.CancelOrderRequest(
        order_id=1,
        market=orders_pb2.BTC_EUR,
        tonce=int(tonce),
    )

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.CancelOrder(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PrivateOrderService.CancelOrder error: " + str(e),
              file=sys.stderr)
    # CODEINCLUDE-END-MARKER: ref-code-example-request

    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-response
    print("{}: {} for {}".format(
        type(response).__name__,
        response.order_id,
        orders_pb2.Market.Name(response.market),
    ))
def private_order_service_create_order(channel):
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    # CODEINCLUDE-BEGIN-MARKER: authentication-request
    # Create a request for a new order with an orderId that is the nanos since unix epoch
    order_id = round(time.time() * 1E9)
    request = order_pb2.CreateOrderRequest(
        market=orders_pb2.BTC_EUR,
        limit_order=order_pb2.LimitOrderRequest(
            side=orders_pb2.BUY,
            base_amount=1.0,
            price=3000.0,
        ),
        tonce=int(order_id),
    )
    # CODEINCLUDE-END-MARKER: authentication-request

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.CreateOrder(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PrivateOrderService.CreateOrder error: " + str(e),
              file=sys.stderr)
    # CODEINCLUDE-END-MARKER: ref-code-example-request

    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-response
    order_type_detail = ""
    if response.WhichOneof("order_type") == "limit_order":
        order_type_detail = "{} {}@{} ".format(
            orders_pb2.Side.Name(response.limit_order.side),
            response.limit_order.base_amount,
            response.limit_order.price,
        )
    elif response.WhichOneof("order_type") == "market_order":
        order_type_detail = "{} {} ".format(
            orders_pb2.Side.Name(response.market_order.side),
            response.market_order.base_amount,
        )
    if response.deadline_ns:
        deadline = "deadline @ {}".format(response.deadline_ns)
    else:
        deadline = "(no deadline)"
    print("{}: {} for {} {} {}".format(
        type(response).__name__,
        response.order_id,
        orders_pb2.Market.Name(response.market),
        order_type_detail,
        deadline,
    ))
def private_order_service_cancel_order(channel):
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    # Create a request for an order cancellation with a tonce that is the nanos since unix epoch
    tonce = round(time.time() * 1E9)
    request = order_pb2.CancelOrderRequest(
        order_id=1,
        market=orders_pb2.BTC_EUR,
        tonce=tonce,
    )

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.CancelOrder(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PrivateOrderService.CancelOrder error: " + str(e),
              file=sys.stderr)
Exemple #4
0
def private_order_service_stream_order_events(channel):
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    start = long(time.time() * 1E9)
    # Create a request for streaming all your order events in the BTC_EUR market that occur after start
    request = order_pb2.StreamOrderEventsRequest(
        search_direction=orders_pb2.FORWARD,
        timestamp_ns=start,
        markets=[orders_pb2.BTC_EUR])

    try:
        # Make the request synchronously and iterate over the received orderbook entries
        for response in stub.StreamOrderEvents(request):
            print(response)
            # CODEINCLUDE-END-MARKER: ref-code-example-request
            parse_and_print(response)
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    except grpc.RpcError as e:
        print("PrivateOrderService.StreamOrderEvents error: " + str(e),
              file=sys.stderr)
def private_order_service_create_order(channel):
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    # Create a request for a new order with an orderId that is the nanos since unix epoch
    order_id = round(time.time() * 1E9)
    request = order_pb2.CreateOrderRequest(
        market=orders_pb2.BTC_EUR,
        limit_order=order_pb2.LimitOrderRequest(
            side=orders_pb2.BUY,
            base_amount=1.0,
            price=3000.0,
        ),
        tonce=order_id,
    )

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.CreateOrder(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PrivateOrderService.CreateOrder error: " + str(e), file=sys.stderr)
Exemple #6
0
# Subscribe to a public orderbook stream and set a new order
if __name__ == '__main__':
    creds = grpc.ssl_channel_credentials()

    with grpc.secure_channel('mockgrpc.test.tulipsolutions.nl:443',
                             creds) as channel:
        # Create a SHA256 HMAC with the base64 decoded 'secret' string as its key
        dummy_secret = base64.standard_b64decode("secret==")
        dummy_jwt = "eyJraWQiOiI2YzY4OTIzMi03YTcxLTQ3NGItYjBlMi1lMmI1MzMyNDQzOWUiLCJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMjM0In0.IL9QJQl55qn3oPsT7sFa7iwd5g1GsEQVr0IO7gCe1UmQdjT7jCIc-pUfjyYUgptPR8HBQl5ncXuBnxwjdXqOMwW1WhPmi_B3BRHQh3Sfu0zNXqKhkuz2-6DffXK1ek3DmK1NpaSikXtg2ruSQ4Uk5xHcnxmXY_SwEij0yot_JRKYEs-0RbyD5Z4jOFKcsbEW46WQmiWdgG3PUKiJT5TfdFd55JM55BwzSOdPIP1S_3dQ4VTDo30mWqAs1KaVbcPqCQmjT1PL0QScTp4w8-YPDcajcafIj98ve9LUoLBLraCIAX34D-hOxu643h9DoG2kIPFfZyXbkDTiUKOl7t-Ykg"

        # Create an interceptor that signs messages with the provided secret.
        # Only messages to the private API that have a 'signed' field will be signed.
        message_auth_interceptor = message_authentication_interceptor.create(
            dummy_secret)
        # Create an interceptor that adds a JWT token when a request to a private service is made.
        jwt_interceptor = jwt_interceptor.create(dummy_jwt)
        # Add interceptors to all requests over the channel
        channel = grpc.intercept_channel(channel, jwt_interceptor,
                                         message_auth_interceptor)

        # Construct clients for accessing PublicOrderbookService and PrivateOrderService using the existing connection.
        # Add a deadline to all requests to the PrivateOrderService
        public_orderbook_service_stub = orderbook_pb2_grpc.PublicOrderbookServiceStub(
            channel)
        private_order_service_stub = order_pb2_grpc.PrivateOrderServiceStub(
            channel)

        with ThreadPoolExecutor(max_workers=2) as executor:
            executor.submit(stream_orderbook, public_orderbook_service_stub),
            executor.submit(create_order, private_order_service_stub),
Exemple #7
0
def private_order_service_get_order_events(channel):
    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-request
    stub = order_pb2_grpc.PrivateOrderServiceStub(channel)

    # Create a request for 10 most recent order events on the BTC_EUR and BTC_USD markets
    request = order_pb2.GetOrderEventsRequest(
        search_direction=orders_pb2.BACKWARD,
        limit=10,
        markets=[orders_pb2.BTC_EUR, orders_pb2.BTC_USD])

    try:
        # Make the request synchronously with a 1s deadline
        response = stub.GetOrderEvents(request, timeout=1)
        print(response)
    except grpc.RpcError as e:
        print("PrivateOrderService.GetOrderEvents error: " + str(e),
              file=sys.stderr)
    # CODEINCLUDE-END-MARKER: ref-code-example-request

    # CODEINCLUDE-BEGIN-MARKER: ref-code-example-response
    result = "{}\n".format(type(response).__name__)
    for event in response.events:
        if event.WhichOneof("event") == "create_order_event":
            create_order_event = event.create_order_event
            if create_order_event.deadline_ns:
                deadline = "deadline @ {}".format(
                    create_order_event.deadline_ns)
            else:
                deadline = "(no deadline)"
            if create_order_event.WhichOneof(
                    "order_type") == "create_limit_order":
                limit_order_event = create_order_event.create_limit_order
                result += "\t{}: Event {} order {} on market {} {} {}@{} {}\n".format(
                    type(limit_order_event).__name__,
                    event.event_id,
                    event.order_id,
                    orders_pb2.Market.Name(event.market),
                    orders_pb2.Side.Name(limit_order_event.side),
                    limit_order_event.base_amount,
                    limit_order_event.price,
                    deadline,
                )
            elif create_order_event.WhichOneof(
                    "order_type") == "create_market_order":
                market_order_event = create_order_event.create_market_order
                result += "\t{}: Event {} order {} on market {} {} {} {}\n".format(
                    type(market_order_event).__name__,
                    event.event_id,
                    event.order_id,
                    orders_pb2.Market.Name(event.market),
                    orders_pb2.Side.Name(market_order_event.side),
                    market_order_event.base_amount,
                    deadline,
                )
        elif event.WhichOneof("event") == "fill_order_event":
            fill_order_event = event.fill_order_event
            result += "\t{}: Event {} order {} on market {} {} {}@{}\n".format(
                type(fill_order_event).__name__,
                event.event_id,
                event.order_id,
                orders_pb2.Market.Name(event.market),
                orders_pb2.Side.Name(fill_order_event.side),
                fill_order_event.base_amount,
                fill_order_event.price,
            )
        elif event.WhichOneof("event") == "cancel_order_event":
            cancel_order_event = event.cancel_order_event
            result += "\t{}: Event {} order {} on market {}\n".format(
                type(cancel_order_event).__name__,
                event.event_id,
                event.order_id,
                orders_pb2.Market.Name(event.market),
            )
    print(result)