Quick Start

Introduction

Welcome! This guide will take you through how to start streaming data from the L3 Atom MVP v2. Included is a step-by-step guide on how to start collecting live data and how to interact with our feed.

Endpoint

Websocket Endpoint: wss://ws.shared.projectx.network/

Getting Started

We can use any websocket connection library to establish a connection to our service. This guide will use aaugstin’s Python 3.0 websockets library. You will need to have Python installed on your computer to follow along - so please refer to the official documentation if it is not installed.

Connecting

Once Python is installed, install the websockets library by running the following command on the terminal.

pip install websockets

Afterwards we can write a short Python script to establish a connection to our Websocket server.

import asyncio
import websockets

endpoint = "ws://194.233.73.248:30205/"

async def connect():
	async with websockets.connect(endpoint) as websocket:
		print("Connected")

if __name__ == "__main__":
	asyncio.run(connect())

Subscribing

Once we’re connected, we can subscribe to any available topic by sending the server the json string {'op': 'subscribe', 'exchange': <exchange>, 'feed': <feed>}. For example, to subscribe to the Okex raw feed, we can add the following lines to our code:

import asyncio
import websockets
import json # For the json.dumps function

endpoint = "ws://194.233.73.248:30205/"

async def connect():
    async with websockets.connect(endpoint) as websocket:
        print("Connected")

        # Subscribing to Okex raw feed
        request = {"op": "subscribe", "exchange": "okex", "feed": "raw"}
        request_json = json.dumps(request).encode('utf-8')
        await websocket.send(request_json)
        print("Subscribed")

if __name__ == "__main__":
    asyncio.run(connect())

We then asynchronously iterate through the websocket to print out the data coming in.

import asyncio
import websockets
import json # For the json.dumps function

endpoint = "ws://194.233.73.248:30205/"

async def connect():
    async with websockets.connect(endpoint) as websocket:
        print("Connected")

        # Subscribing to Okex raw feed
        request = {"op": "subscribe", "exchange": "okex", "feed": "raw"}
        request_json = json.dumps(request).encode('utf-8')
        await websocket.send(request_json)
        print("Subscribed")

        # Printing out data
        async for message in websocket:
            print(json.loads(message))

if __name__ == "__main__":
    asyncio.run(connect())

Running the above code should start printing the data received from our service.

{
    "arg": {
        "channel": "books",
        "instId": "BTC-USDT"
    },
    "action": "update",
    "data": [
        {
            "asks": [],
            "bids": [
                [
                    "29793.9",
                    "0.59453605",
                    "0",
                    "1"
                ],
                [
                    "29725",
                    "0",
                    "0",
                    "0"
                ]
            ],
            "ts": "1652854296988",
            "checksum": -1483490433
        }
    ]
}
{
    "arg": {
        "channel": "books",
        "instId": "BTC-USDT"
    },
    "action": "update",
    "data": [
        {
            "asks": [
                [
                    "30056.2",
                    "0.34339139",
                    "0",
                    "7"
                ]
            ],
            "bids": [
                [
                    "30026.3",
                    "0",
                    "0",
                    "0"
                ],
                [
                    "29725.2",
                    "0.00017224",
                    "0",
                    "1"
                ]
            ],
            "ts": "1652854297100",
            "checksum": -2048322385
        }
    ]
}
...

If you wanted the Okex normalised or trades feed instead, replace 'raw' in the 'feed' field of the request variable with 'normalised' or 'trades' instead. Explanations of the data schemas and request formats are explored in the API reference.

Unsubscribing

To unsubscribe, send {"op": "unsubscribe", "exchange": "okex", "feed", "raw"} to the service in the same way as shown above.

Maintaining an Orderbook

We have written Orderbook data structures for both L2 and L3 data which can be freely used. Our feed currently doesn’t provide a snapshot, so we have to rely on the asymptotic correctness of continual updates to maintain a correct book instead. Orderbook snapshots will be implemented in the near future.

Last updated