API Documentation

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

Detailed Trades

This guide assumes you have already read the "Getting Started and Introduction to Trades" guide. This guide will cover more detailed processing of trade messages.

Code

#include "stdio.h"
#include "NxCoreAPI_Wrapper_C++.h"
#include <string>
NxCoreClass NxCore;

int OnNxCoreCallback(const NxCoreSystem* pNxCoreSys, const NxCoreMessage* pNxCoreMsg) {
    if(pNxCoreMsg->MessageType == NxMSG_TRADE) {

        const NxCoreHeader& header = pNxCoreMsg->coreHeader;
        char* symbol = header.pnxStringSymbol->String;
        const NxTime& ts = header.nxExgTimestamp;
        std::string exchange = std::string(NxCore.GetDefinedString(NxST_EXCHANGE, header.ReportingExg));
        size_t pipePos = exchange.find_first_of('|');
        if (pipePos != std::string::npos)
            exchange = exchange.substr(0,pipePos);

        const NxCoreTrade& trade = pNxCoreMsg->coreData.Trade;
        double price = NxCore.PriceToDouble(trade.Price, trade.PriceType);
        int size = trade.Size;
        unsigned int sequence = trade.ExgSequence;
        
        std::string conditions = "";
        for (unsigned char condition : trade.ExtTradeConditions) {
            const char* conditionName = NxCore.GetDefinedString(NxST_TRADECONDITION, condition);
            conditions += (conditionName ? std::string(conditionName) : std::string("")) + std::string("|");
        }

        std::string eligibility = "";
        if (trade.ConditionFlags) {
            eligibility += std::string("(Doesn't set");
            if (trade.ConditionFlags & NxTCF_NOHIGH)
                eligibility += std::string(" high");
            if (trade.ConditionFlags & NxTCF_NOLOW)
                eligibility += std::string(" low");
            if (trade.ConditionFlags & NxTCF_NOLAST)
                eligibility += std::string(" last");
            eligibility += std::string(")");
        }

        if(trade.PriceFlags & NxTPF_EXGCANCEL)
            printf("Cancel for %s at sequence %u on %s %d shares at $%.02f\n",
                symbol, sequence, exchange.c_str(), size, price);
        else
            printf("Trade for %s at %02d:%02d:%02d sequence %u on %s for %d shares at $%.02f %s%s\n",
                symbol, ts.Hour, ts.Minute, ts.Second, sequence, exchange.c_str(),
                size, price, conditions.c_str(), eligibility.c_str());
    }
    return NxCALLBACKRETURN_CONTINUE;
}

int main(int argc, char* argv[]) {
    if (argc < 3)
        return 1;

    if (NxCore.LoadNxCore(argv[1])){
        int returnValue = NxCore.ProcessTape(argv[2], 0, 0, 0, OnNxCoreCallback);
        NxCore.ProcessReturnValue(returnValue);
    }
    else
        printf("loading library failed\n");

    return 0;
}

Trade Message

Trades are identified by combination of sequence and reporting exchange. This can be used in conjunction with cancels to determine which trade was canceled. Note that some feeds use non-sequential 64-bit sequences which may cause sequence number to not be unique. GetDefinedString returns the both a short and full name of the exchange split by a pipe. We get the short exchange name by taking the part of the string before the pipe.

std::string exchange = std::string(
    NxCore.GetDefinedString(NxST_EXCHANGE, header.ReportingExg));
size_t pipePos = exchange.find_first_of('|');
if (pipePos != std::string::npos)
    exchange = exchange.substr(0,pipePos);

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

Trade Conditions

The type of trade is indicated by up to 4 conditions. The full list of possible trade conditions can be found in the Trade Condition Table. For example, trades with less than 100 shares will have the OddLot(115) condition. GetDefinedString can be used to find the string name of the condition using NxST_TRADECONDITION as ixTable. GetDefinedString returns a nullptr if there is no valid condition.

std::string conditions = "";
for (unsigned char condition : trade.ExtTradeConditions) {
    const char* conditionName = NxCore.GetDefinedString(
        NxST_TRADECONDITION, condition);
    conditions += 
        (conditionName ? std::string(conditionName) : std::string("")) + 
        std::string("|");
}

Trade Eligiblity

Not all trades are permitted to set the daily high, low, and last. Whether or not a trade can set daily prices is determined by the aforementioned trade conditions. ConditionFlags is a bitmap of which daily prices the trade can not set. A value of zero means the trade can set all Daily prices.

std::string eligibility = "";
if (trade.ConditionFlags) {
    eligibility += std::string("(Doesn't set");
    if (trade.ConditionFlags & NxTCF_NOHIGH)
        eligibility += std::string(" high");
    if (trade.ConditionFlags & NxTCF_NOLOW)
        eligibility += std::string(" low");
    if (trade.ConditionFlags & NxTCF_NOLAST)
        eligibility += std::string(" last");
    eligibility += std::string(")");
}

Cancels

Sometimes past trades are canceled or modified. In this case, PriceFlags will have the NxTPF_EXGCANCEL(0x20) bit set. When avaiable the sequence and reporting exchange will match the trade being canceled.

if(trade.PriceFlags & NxTPF_EXGCANCEL)
    ...

Next:
Quote