Liquibook Documentation

repository·master·Indexed 23 days ago

https://github.com/enewhuis/liquibook

A high-performance, open-source C++ header-only order matching engine for financial exchanges and trading test-beds. It supports various order properties including side, quantity, price, stop loss, All or None (AON), and Immediate or Cancel (IOC). The engine provides notifications for order status (accepted, rejected, filled, canceled) and market data (trades, security changes, depth book, and BBO). Includes the mt_order_entry example program for manual order entry and testing.

Tokens
8.4K
Snippets
14
Records
52
Agent score
81%

What's inside Liquibook

  1. Define symbols and order book types

    master

    Liquibook supports arbitrary UTF-8 strings as symbols. However, the mt_order_entry program has specific parsing rules and uses prefixes to determine the type of order book to create for a new symbol:

    • Simple Order Book: Prefix the symbol with a plus sign (+).
    • Depth Order Book: Prefix the symbol with an exclamation mark (!).
    • Prompt for Type: If no prefix is used, the program will prompt the user to choose between [S]imple, [D]epth, or N to cancel.

    Parsing Restrictions for mt_order_entry:

    • Symbols must not contain spaces.
    • Symbols must not begin with + or ! (unless used for the purpose of defining the book type).
    • Avoid using ALL or * as symbols to prevent conflicts with the DISPLAY command.

    Note: Using a prefix on a symbol that already has an existing book will simply use the existing book and ignore the prefix.

  2. Identify and reference orders by ID

    master

    The mt_order_entry program assigns a unique integer ID to every order, displayed after a hash sign (e.g., #1).

    Referencing Orders:

    • Direct ID: Use the number directly (e.g., CANCEL 1).
    • With Hash: You can optionally prefix the ID with a hash (e.g., CANCEL #1).
    • Relative ID: Use a leading minus sign to reference orders relative to the next ID to be assigned:
      • -1: The most recent order.
      • -2: The order before the most recent one, and so on.
  3. Specify prices using atomic units

    master

    Liquibook uses integers for prices to avoid floating-point issues. Prices must be expressed in terms of the "atomic currency unit".

    Example: If the currency is US Dollars, the atomic unit is typically a penny. To represent $15.00, use 1500.

    Market Orders: You can use the keyword MARKET or the abbreviation MKT to indicate that no limit price is specified. This allows trades to be generated at the counter-order price or the current market price.

  4. Supported Order Properties in Liquibook

    master

    Liquibook manages orders based on several properties. While only side, quantity, and price are strictly required, the engine supports the following:

    • Side: Buy or Sell.
    • Quantity: The amount of the asset.
    • Symbol: A character string representing the asset (no restrictions imposed).
    • Price: Desired price or "Market" (to accept current market price). Trades are generated at the specified price or any better price.
    • Stop loss price: Also known as a stop price; holds the order until the market price reaches this value.
    • All or None flag: Specifies the entire order must be filled or no trades occur.
    • Immediate or Cancel flag: Specifies that any remainder of the order after matching against existing orders should be canceled. (Combining 'All or None' and 'Immediate or Cancel' results in a 'Fill or Kill' order).

    Users can define additional custom properties on the order object; these will not affect Liquibook's internal matching logic.

  5. Run the mt_order_entry example program

    master

    The mt_order_entry command is an example program used to manually enter orders into Liquibook order books via the console or a script file. It displays callbacks generated by Liquibook in response to requests.

    Command Line Options:

    • script_file_name: (Optional) The name of a script file containing a series of requests (one per line). Use a single hyphen (-) to read commands directly from the console instead of a file.
    • log_file_name: (Optional) The name of a file where output should be written. Prompts will still appear on the console.
  6. Syntax for manual order entry requests

    master

    Requests are read from the console or a script file. Lines starting with # or empty lines are ignored as comments.

    Format: REQUEST [parameters]* [;]

    • A request consists of a command followed by necessary parameters.
    • Requests are terminated by a semicolon (;) or the keyword END.
    • The semicolon is optional for some commands but recommended for script files to avoid interactive prompting.
    • If parameters are missing, the program will prompt the user for them (this occurs even when reading from a file).
    BUY 100 GOOG 850 AON;
  7. Build Liquibook tests and examples on Linux

    master

    To build the included tests and example programs on Linux, you must have MPC (a Perl-based cross-platform build tool) installed.

    1. Set up the environment:

      cd liquibook
      . ./env.sh

      Note: If readlink is missing, set the $LIQUIBOOK_ROOT environment variable manually to the directory containing liquibook before running env.sh.

    2. Generate makefiles and build:

      $MPC_ROOT/mwc.pl -type make liquibook.mwc
      make depend
      make all

    Build Output Locations

    • Libraries: $LIQUIBOOK_ROOT/lib
    • Example programs: $LIQUIBOOK_ROOT/bin
    • Test programs: $LIQUIBOOK_ROOT/bin/test
    # Linux Build Sequence
    cd liquibook
    . ./env.sh
    $MPC_ROOT/mwc.pl -type make liquibook.mwc
    make depend
    make all
  8. Integrate Liquibook into your C++ project

    master

    Liquibook is a header-only library. To use it in your application, you do not need to compile the library itself. Simply add the Liquibook/src directory to your project's include path and include the main header:

    #include <book/order_book.h>

    Liquibook is compatible with existing order models as long as they provide a trivial interface that can be wrapped or added to. It also supports the use of smart or regular pointers for orders and is compatible with existing identifiers for securities, accounts, exchanges, and orders.

    #include <book/order_book.h>
  9. Build Liquibook examples and tests with Visual Studio (Windows)

    master

    To generate Visual Studio project and solution files on Windows, use the following steps:

    1. Navigate to the directory and prepare the environment script:

      cd liquibook
      copy winenv.bat w.bat
      edit w.bat

      (Edit w.bat to configure your specific environment variables.)

    2. Run the environment setup and MPC:

      w.bat
      mpc.bat
    3. Open the generated solution:

      • Run liquibook.sln from the command line, or use File | Open | Project or Solution within Visual Studio.
    # Windows Build Sequence
    cd liquibook
    copy winenv.bat w.bat
    edit w.bat
    w.bat
    mpc.bat
  10. Configure Liquibook build features (Boost and QuickFAST)

    master

    The build behavior of the tests and examples is controlled via the liquibook.features file.

    Boost Test (for Unit Tests)

    To build unit tests, you must have the Boost test library installed.

    • Set the $BOOST_ROOT environment variable.
    • MPC expects include files at $BOOST_ROOT/include/boost and libraries at $BOOST_ROOT/lib.
    • To disable unit tests, edit liquibook.features and set boost=0.

    QuickFAST (for Depth Feed Example)

    To run the depth feed publisher/subscriber example:

    • Install QuickFAST and set $QUICKFAST_ROOT to its location.
    • In liquibook.features, set QuickFAST=1.
    • If you do not want to run this example, set $QUICKFAST_ROOT to liquibook/noQuickFAST.
  11. Use the OrderBook class to manage limit orders

    master

    The OrderBook<OrderPtr> class is the central component for managing a security's limit order book. It is a template class where OrderPtr can be a common or smart pointer to an order object, provided the object adheres to the required interface.

    Key capabilities include:

    • Adding orders (add)
    • Canceling orders (cancel)
    • Replacing orders (replace)
    • Managing market price and stop orders
    • Notifying the application of events via listeners.
  12. Manage market depth with the Depth class

    master

    The liquibook::book::Depth<SIZE> class is a container for limit order data aggregated by price. It maintains a fixed-size view of the market (bids and asks) and handles overflow by storing excess levels in internal maps.

    Key characteristics:

    • Fixed Size: The template parameter SIZE determines the number of levels maintained for each side (bids and asks). Total internal storage is SIZE * 2 levels.
    • Efficient Copying: Designed so that DepthLevel objects can be easily copied (e.g., via memcpy) for use in separate callback threads.
    • Change Tracking: Uses ChangeId to track updates. You can check if the depth has changed since your last update using .changed() and mark the current state as published using .published().