Open Source Project – C++ Implementation of Streaming Binary Protocol for Shanghai and Shenzhen Stock Exchanges

📌 Introduction

<span>fin-proto-cpp</span> is an open-source C++ protocol parsing library specifically designed for the financial sector, implementing a low-latency streaming binary protocol codec for the Shanghai Stock Exchange (SSE) and Shenzhen Stock Exchange (SZSE). The project features the following characteristics:

  • ⚡ High-performance encoding and decoding: Processes exchange binary data streams in milliseconds, utilizing a zero-copy design
  • 🔄 Protocol support: Supports the latest Binary protocol versions for both Shanghai and Shenzhen markets
  • ðŸŠķ Lightweight design: Core codec with zero external dependencies
  • 🛠 Testability: Built-in CI/CD and unit testing

GitHub address: https://github.com/xinchentechnote/fin-proto-cpp

⚙ïļ Build Process

1. Environment Dependencies

This project depends on the fin-protoc protocol compiler (implemented in Go and ANTLR4). In a Linux environment, you can directly download the precompiled binary and configure it to your <span>PATH</span>:

wget https://github.com/xinchentechnote/fin-protoc/releases/download/v0.1.6/fin-protoc-v0.1.6-linux-amd64.tar.gz
tar -xvf fin-protoc-v0.1.6-linux-amd64.tar.gz
export PATH=$PWD:$PATH

2. One-click Compilation

git clone https://github.com/xinchentechnote/fin-proto-cpp
cd fin-proto-cpp
./build.sh  # Automatically triggers CMake build

📂 Directory Structure

├── build.sh
├── include
│   ├── bytebuf.hpp       # ByteBuf wrapper, similar to Netty ByteBuf
│   ├── checksum.hpp      # Checksum interface definition
│   ├── codec.hpp         # Binary protocol codec interface definition
│   ├── sse_binary.hpp    # SSE Binary codec
│   └── szse_binary.hpp   # SZSE Binary codec
└── test
    ├── bytebuf_test.cpp
    ├── checksum_test.cpp
    ├── codec_test.cpp
    ├── sse_binary_test.cpp
    └── szse_binary_test.cpp

🏗 Core Interface Design

BinaryCodec Interface

struct BinaryCodec {
    virtual ~BinaryCodec() = default;
    virtual void encode(ByteBuf & buf) const = 0;
    virtual void decode(ByteBuf & buf) = 0;
    virtual std::string toString() const = 0;
    virtual bool equals(const BinaryCodec & other) const = 0;

    bool operator==(const BinaryCodec & other) const { return equals(other); }
    bool operator!=(const BinaryCodec & other) const { return !(*this == other); }
};

Checksum Interface

class IChecksumService {
  public:
    virtual ~IChecksumService() = default;
    virtual std::string algorithm() const = 0;
};

template <typename Input, typename Output>
class ChecksumService : public IChecksumService {
  public:
    using input_type = Input;
    using output_type = Output;
    virtual Output calc(const Input & data) const = 0;
};

🖇 UML Design Diagrams

1. Protocol Parsing Architecture

Open Source Project - C++ Implementation of Streaming Binary Protocol for Shanghai and Shenzhen Stock Exchanges

2. Encoding Process

Open Source Project - C++ Implementation of Streaming Binary Protocol for Shanghai and Shenzhen Stock Exchanges

📊 SSE and SZSE Protocol Definitions

SSE Binary Protocol

root packet SseBinary {
    uint32 MsgType `Message Type`,
    uint64 MsgSeqNum `Message Sequence Number`,
    uint32 MsgBodyLen @lengthOf(Body) `Message Body Length`,
    match MsgType as Body {
        33 : Heartbeat,
        40 : Logon,
        41 : Logout,
        58 : NewOrderSingle
    },
    uint32 Checksum @calculatedFrom("SSE_BIN") `Checksum`,
}

Details of SSE protocol definition https://github.com/xinchentechnote/fin-proto/blob/main/sse/binary/sse_bin_v0.57.pdsl

SZSE Binary Protocol

root packet SzseBinary {
    MsgType,
    BodyLength @lengthOf(Body),
    match MsgType as Body {
        1 : Logon,
        2 : Logout,
        3 : Heartbeat,
        10 : TradingSessionStatus,
        [100101] : NewOrder
    },
    int32 Checksum @calculatedFrom("SZSE_BIN"),
}

Details of SZSE protocol definition https://github.com/xinchentechnote/fin-proto/blob/main/szse/binary/szse_bin_v1.29.pdsl

ðŸ’Ą Application Scenarios

  • Matching Engine: Rapid parsing of exchange binary data
  • Trading/Risk Control Systems: Low-latency order processing
  • Protocol Parsing Education: Includes complete test cases, can serve as a learning sample

ðŸŽŊ Target Audience

Quantitative trading developers | High-frequency trading system engineers | Fintech enthusiasts

📌 Related Tags

<span>#c++</span> <span>#fintech</span> <span>#open-source</span> <span>#quantitative-trading</span> <span>#high-frequency-trading</span>

Leave a Comment