Introduction
The RapidMarket C++ 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 C++ client.
#include <cppzmq/zmq.hpp>
#include <cppzmq/zmq_addon.hpp>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <thread>
#include "l2_book.pb.h"
std::string cr_line = "\r" + std::string(64, ' ') + "\r";
void run(std::string zmqpath) {
// create zmq context
zmq::context_t context;
// create zmq subscriber socket
zmq::socket_t subscriber(context, zmq::socket_type::sub);
// connect to zmq publisher
subscriber.connect(zmqpath);
// If the second parameter is not specified, subscribe to all market data.
// subscriber.set(zmq::sockopt::subscribe, "");
subscriber.set(zmq::sockopt::subscribe, "BTCUSDT");
zmq::message_t msg;
liquidityTech::md::L2BookProto book;
double askPrice, askQty, bidPrice, bidQty;
std::cout << "Receiving market data..." << std::setprecision(5) << std::fixed << std::endl;
while (true) {
// receive message
auto result = subscriber.recv(msg, zmq::recv_flags::none);
if (!result.has_value()) {
break;
}
std::string message(static_cast<char*>(msg.data()), msg.size());
std::string actual_message = message.substr(20);
book.ParseFromArray(actual_message.data(), msg.size() - 20);
auto ask = book.levels_asks(0);
auto bid = book.levels_bids(0);
askPrice = ask.price();
askQty = ask.qty();
bidPrice = bid.price();
bidQty = bid.qty();
auto symbol = book.symbol();
std::cout << "symbol: " << symbol << ", askprice: " << askPrice << ", askqty: " << askQty << ", bidprice: " << bidPrice << ", bidqty: " << bidQty << std::endl;
msg.rebuild();
book.Clear();
std::this_thread::sleep_for(std::chrono::seconds(5));
// You can cancel the subscription to a topic by using zmq::sockopt::unsubscribe
subscriber.set(zmq::sockopt::unsubscribe, "BTCUSDT"); // cancel subscription to BTCUSDT
subscriber.set(zmq::sockopt::subscribe, "ETHUSDT"); // subscribe to ETHUSDT
}
}
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: rapidmarket_client <path>" << std::endl;
std::cerr << "Example: rapidmarket_client \"tcp://localhost:50000\"" << std::endl;
return EXIT_FAILURE;
}
std::string zmqpath(argv[1]);
run(zmqpath);
return EXIT_SUCCESS;
}
Requirements
To use the RapidMarket C++ client (Centralized Counter Version) to receive data, you need the following environment and tools:
sudo apt install -y make
sudo apt install -y g++
sudo apt install -y protobuf-compiler
sudo apt install -y libzmq3-dev
sudo apt install libyaml-cpp-dev
These commands will install the following dependencies:
- g++: C++ compiler.
- protobuf-compiler: Compiler for compiling Protobuf files.
- libzmq3-dev: Development package for the ZeroMQ library, used for communication with RapidMarket.
Please adjust the commands according to your system and package manager requirements.
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
.
├── README*.md Client example documentation
├── SERVER*.md Server documentation
├── cppzmq Dependency directory
│ ├── zmq.hpp
│ └── zmq_addon.hpp
├── l2_book.proto Protobuf definition
└── rapidmarket_client_centralized.cpp Example code
Compilation
Then build as usual.
BUILD_DIR=./bin
INCLUDE_DIR=-I/usr/include -I.
LIB_DIR=-I/usr/lib
CC=g++
CFLAGS=-Wall $(INCLUDE_DIR) $(LIB_DIR) -lzmq -lprotobuf -O3
CPP_FLAGS=-std=c++17
%.o: %.cpp
$(CC) $(CFLAGS) $(CPP_FLAGS) $< -c -o $@
$(BUILD_DIR)/rapidmarket_client_centralized: rapidmarket_client_centralized.o l2_book.pb.o
mkdir -p bin
$(CC) $^ $(CFLAGS) $(CPP_FLAGS) -o $@
.PHONY: clean
clean:
rm -f *.o
rm -f $(BUILD_DIR)/*
.PHONY: proto
proto:
protoc --cpp_out=. ./l2_book.proto
mv l2_book.pb.cc l2_book.pb.cpp
.PHONY: default
default: all
all: $(BUILD_DIR)/rapidmarket_client_centralized
Contributing
You can use the following command to compile:
make proto
make
Start command
IP_ADDR
and PORT
below are the IP address and port of the RapidMarket server, respectively.
cd bin
./rapidmarket_client "zmq_path"
# For example: ./rapidmarket_client_centralized "tcp://192.168.0.1:50000"
The program outputs real-time market data:
Receiving market data...
symbol: BTCUSDT, askprice: 30033.06000, askqty: 4.91876, bidprice: 30033.05000, bidqty: 6.48043
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.