API Documentation

Language: C++ Java Python
Basic: Intro/Trade Quote Category Status Symbol Options
Detailed: Trade Quote

Introduction to Options

This example should use the demo.DO.nx2 tape instead of demo.XU.nx2. Messages for option contracts are very similar to messages for equities. The primary difference is the addition of pnxOptionHdr to the coreHeader object. The major challenge for options processing is the number of messages. In addition to parsing options messages, this guide will also cover some recommended performance improvements.

Code

#include "stdio.h"
#include <thread>
#include "NxCoreAPI_Wrapper_C++.h"
NxCoreClass NxCore;
thread_local bool g_successfulExclude = false;

int OnNxCoreCallback(const NxCoreSystem* pNxCoreSys, const NxCoreMessage* pNxCoreMsg) {
    if (pNxCoreMsg->MessageType == NxMSG_STATUS && !g_successfulExclude) {
        int optionPermissions[4] = { 2, 20, 21, 22 };
        for (int permission : optionPermissions) {
            if (pNxCoreSys->UserData != permission && NxCore.ExcludeOpraExch(NxPERMID_REMOVE | permission) != 0)
                return NxCALLBACKRETURN_CONTINUE;
        }
        g_successfulExclude = true;
    }

    if (pNxCoreMsg->MessageType == NxMSG_TRADE) {
        const NxCoreHeader& header = pNxCoreMsg->coreHeader;
        NxOptionHdr* pnxOptionHdr = header.pnxOptionHdr;
        if (pnxOptionHdr != nullptr) {
            char* symbol = header.pnxStringSymbol->String;
            const NxTime& timestamp = header.nxExgTimestamp;
            const NxDate& expireDate = pnxOptionHdr->nxExpirationDate;
            double strikePrice = NxCore.PriceToDouble(pnxOptionHdr->strikePrice, 7);
            char* contractType;
            if (pnxOptionHdr->PutCall == 1)
                contractType = "Put";
            else
                contractType = "Call";

            const NxCoreTrade& trade = pNxCoreMsg->coreData.Trade;
            double price = NxCore.PriceToDouble(trade.Price, trade.PriceType);
            int size = trade.Size;

            printf("Trade for %s %s expiring %d%02d%02d with strike $%.2f at %02d:%02d:%02d for %d shares at $%.2f\n",
                symbol, contractType, expireDate.Year, expireDate.Month, expireDate.Day, strikePrice,
                timestamp.Hour, timestamp.Minute, timestamp.Second, size,price);
        }
    }
    return NxCALLBACKRETURN_CONTINUE;
}
void thProcess(char* pszFilename,int permission) {
    int returnValue = NxCore.ProcessTape(pszFilename, 0, NxCF_EXCLUDE_CRC_CHECK, permission, OnNxCoreCallback);
    NxCore.ProcessReturnValue(returnValue);
}
int main(int argc, char* argv[]) {
    if (argc < 3)
        return 1;
    if (!NxCore.LoadNxCore(argv[1])) {
        printf("loading library failed\n");
        return 2;
    }
    std::thread threads[4];
    threads[0] = std::thread(thProcess, argv[2], 2);
    threads[1] = std::thread(thProcess, argv[2], 20);
    threads[2] = std::thread(thProcess, argv[2], 21);
    threads[3] = std::thread(thProcess, argv[2], 22);

    for (std::thread& thread : threads)
        thread.join();

    return 0;
}

Splitting data

One way of improving performance is limiting what part of the options feed is processed on a thread. Nanex splits the option feed into 4 permissions 2, 20, 21, and 22 each corresponding to different ranges of option roots. The function ExcludeOpraExch is used to remove a permission from processing. By removing all option permissions except for the one assigned to threadPermission, each thread will only process a quarter of the data.

if (pNxCoreMsg->MessageType == NxMSG_STATUS && !g_successfulExclude) {
    int optionPermissions[4] = { 2, 20, 21, 22 };
    for (int permission : optionPermissions) {
        if (pNxCoreSys->UserData != permission
            && NxCore.ExcludeOpraExch(NxPERMID_REMOVE | permission) != 0)
            return NxCALLBACKRETURN_CONTINUE;
    }
    g_successfulExclude = true;
}

Option Header

Option contract information is contained within pnxOptionHdr in the coreHeader object. For this example we will get the expiration date, strike price, and put/call. The strike price always has a price type of 7.

const NxCoreHeader& header = pNxCoreMsg->coreHeader;
NxOptionHdr* pnxOptionHdr = header.pnxOptionHdr;
if (pnxOptionHdr != nullptr) {
    char* symbol = header.pnxStringSymbol->String;
    const NxTime& timestamp = header.nxExgTimestamp;
    const NxDate& expireDate = pnxOptionHdr->nxExpirationDate;
    double strikePrice = NxCore.PriceToDouble(
        pnxOptionHdr->strikePrice, 7);
    char* contractType;
    if (pnxOptionHdr->PutCall == 1)
        contractType = "Put";
    else
        contractType = "Call";
    ...
}

Process Tape Flags

To improve performance we are going to use a control flag in the 3rd argument of ProcessTape. NxCF_EXCLUDE_CRC_CHECK disables frequent CRC calculations. CRC checks will still be performed, but not as frequently.

int returnValue = NxCore.ProcessTape(
    pszFilename,
    NULL,
    NxCF_EXCLUDE_CRC_CHECK,
    permission,
    OnNxCoreCallback);

Launching Threads

On the main thread we create an array of thread objects with the 4 different option permission value. Then, we wait for every thread to finish.

std::thread threads[4];
threads[0] = std::thread(thProcess, argv[2], 2);
threads[1] = std::thread(thProcess, argv[2], 20);
threads[2] = std::thread(thProcess, argv[2], 21);
threads[3] = std::thread(thProcess, argv[2], 22);

for (std::thread& thread : threads)
    thread.join();