Introduction
The RapidMarket Python client (Centralized Counter Version) allows users to subscribe to their focused symbols among whole-market symbol list and receive real-time market data. This client leverages ZeroMQ for communication and Protocol Buffers (Protobuf) for efficient data serialization.
Usage
Below is an example version of the RapidMarket Centralized Counter Python client.
import sys
import zmq
import l2_book_pb2
CR_LINE = "\r" + " " * 64 + "\r"
def run(ipcpath: str):
# create zmq context
context = zmq.Context()
# create zmq subscriber socket
socket = context.socket(zmq.SUB)
# connect to zmq publisher
socket.connect(ipcpath)
# subscribe specific symbols
# If the second parameter is not specified, subscribe to all symbols
socket.subscribe("BTCUSDT")
socket.subscribe("ETHUSDT")
book = l2_book_pb2.L2BookProto()
print("Receiving market data...")
while True:
msg = socket.recv()
msg = msg[20:]
book.ParseFromString(msg)
bid = book.levels_bids[0]
ask = book.levels_asks[0]
bid_price = bid.price
bid_qty = bid.qty
ask_price = ask.price
ask_qty = ask.qty
# print message
print(
"{}bid: {:.5f}, {:.5f}\task: {:.5f}, {:.5f}".format(
CR_LINE, bid_price, bid_qty, ask_price, ask_qty
),
end="",
)
book.Clear()
def main():
if len(sys.argv) != 2:
print("Usage: python3 rapidmarket_client.py <path>", file=sys.stderr)
print(
'Example: python3 rapidmarket_client.py "tcp://localhost:50000"',
file=sys.stderr,
)
exit(1)
ipcpath = sys.argv[1]
run(ipcpath)
if __name__ == "__main__":
main()
Requirements
To use the RapidMarket Python client to receive data, you need the following environment and tools:
# Install the Proto Buffer compiler
sudo apt install -y protobuf-compiler
sudo apt install python3-pip
# The following two commands install the runtime dependency that matches the version of the Proto Buffer compiler
version=$(apt show protobuf-compiler | grep -i version | awk -F'[ -]' '{print $2}')
pip install protobuf==${version}
# Install ZeroMQ dependency
pip install pyzmq
These commands will install the following dependencies:
- protobuf-compiler: Compiler for compiling Protobuf files.
- protobuf: Python runtime library for Protocol Buffers.
- pyzmq: Python bindings for ZeroMQ, used for communication with RapidMarket.
Getting Started
For more detailed information on ZeroMQ and Protobuf, including their roles in the RapidMarket application, please refer to the Getting Started page.
Directory structure
├── l2_book.proto Protobuf definition file
└── rapidmarket_client.py Client example code
Compilation
To generate the l2_book_pb2.py
file required by Proto Buffer, use the following command:
protoc --python_out=. ./l2_book.proto
Start command
IP_ADDR
and PORT
below are the IP address and port of the RapidMarket server, respectively.
python3 ./rapidmarket_client.py "tcp://${IP_ADDR}:${PORT}"
# For example: python3 ./rapidmarket_client.py "tcp://192.168.0.1:50000"
The program outputs real-time market data:
Receiving market data...
bid: 30033.05000, 6.48043 ask: 30033.06000, 4.91876
l2_book.proto File Explanation
The message format released by RapidMarket is defined in l2_book.proto. It outlines the hierarchical structure and fields of market data, aiding clients in correctly parsing received market data.