API Reference

C++ client example

Introduction

The RapidMarket C++ client allows users to subscribe to 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 "l2_book.pb.h"

std::string cr_line = "\r" + std::string(64, ' ') + "\r";

void run(std::string ipcpath) {
  // 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(ipcpath);
  // subscribe all messages
  subscriber.set(zmq::sockopt::subscribe, "");

  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;
    }

    // parse message
    book.ParseFromArray(msg.data(), msg.size());
    auto ask = book.levels_asks(0);
    auto bid = book.levels_bids(0);
    askPrice = ask.price();
    askQty = ask.qty();
    bidPrice = bid.price();
    bidQty = bid.qty();

    // print message
    std::cout << cr_line << "bid: " << bidPrice << ", " << bidQty
              << "\task: " << askPrice << ", " << askQty << std::flush;

    msg.rebuild();
    book.Clear();
  }
}

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 ipcpath(argv[1]);
  run(ipcpath);
  return EXIT_SUCCESS;
}

Requirements

To use the RapidMarket C++ client 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.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: rapidmarket_client.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

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 "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.