Package: digitex-engine-client
Digitex engine client libraryThis library implements a client interface to the Digitex futures trading engine.InstallationFirst things first, you're going to need to install this library.If you have the source code of this library in a local checkout, you may runpip3 install .(where.is the path to the root directory of the library
checkout). Otherwise you may have luck runningpip3 install digitex-engine-clientif you have the right repositories configured.Whatever the source, Pip will automatically fetch the dependencies and build the
Protobuf definition. For this to work, you do needprotoc, the Protobuf
compiler, to be installed on your machine (try using your distribution's
appropriate package). The build script (setup.py) will look forprotocin
thePATH; you can also specify theprotoclocation explicitly with thePROTOCenvironment variable. If the script fails to findprotoc, it will
raise an error right away.ConnectingThe main API of the library is the abstractClientclass, which represents a
connection to the engine backend, which serves as a proxy for the engine. The
concreteWsClientserves as an implementation of a client.To connect to the engine, instantiateWsClient:client=WsClient(host='ws.mainnet.digitexfutures.com',token='your-api-key-here')The host to use for testnet isws.testnet.digitexfutures.com; for mainnet usews.mainnet.digitexfutures.com. For authentication, you can also pass abeareror asession_idinstead of atoken, depending on the type of key that you
have.You can get a token over athttps://exchange.digitexfutures.com/profile/apiTo check if the connection is successful, ping the engine:client.ping()MessagesTo interact with the exchange, a client can send and receivemessages. The
exact messages that can be sent and received are defined in themessages.protofile, which is compiled usingprotocinto thedigitex_engine_client.messages_pb2submodule.It is thus possible to construct messages directly. For example, here's how one
can construct and send the ping message:fromdigitex_engine_client.messages_pb2import*message=Message(ping_msg=PingMessage())client.send_message(message)The library provides some shortcuts to make this more convenient. First, there's
a semi-publicmessage_proxythat can be used to create messages in a more
natural way:message=client.message_proxy.ping()client.send_message(message)Any arguments that would have been passed to either the genericMessageconstructor or a constructor of a specific message should be passed as arguments
into themessage_proxymethod.Finally, it's possible to call the methods on a client object directly:client.ping()This will do the following:Create the message using the specified arguments,Fill in generic fileds such asclient_idandtimestampautomatically,
unless they are set explicitly,Send the message by callingsend_message(),Return the sent message (this is useful to e.g. note the generatedclient_id).Seemessages.protofor a complete description of supported messages, and readengine/API.mdfor an in-depth explanation of their meanings.ListeningThere are two streams of events you can listen on, thetradingevents and
themarket dataevents, also known ascontrolanddatastreams.In order to listen for messages, use:formessageinclient.subscribe_to_trading_events():do_something_with(message)And the same formarket_datainstead oftrading.It's necessary to calldigitex_engine_client.WsClient.subscribe()before
trying to listen for market data events. The library will print a warning if you
forget to do this.The object returned by thesubscribe_to_..._events()methods is alistener. You can use it directly as an iterator over received messages.Please note that waiting for a message is a blocking operation. You should spawn
multiple threads in order to listen to multiple streams and send messages at the
same time.Sync or asyncYou can use the library in either sync or async manner. Using sync is
straightforward; using async means insertingasyncandawaitin a lot of
places, and relying onasynciotasks instead of threads. Internally, the
WebSocket code always uses async code, and tries to transparently run it on a
background event loop if it detects you want to use it as sync.ExamplesPlease see theexamplesdirectory for a few examples, including a
simple bot that monitors the spot price and the last trade price and tries to
place orders.