API Documentation

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

Getting Started and Introduction to Trades

This is a simple introduction to writing code using NxCore. If you are not ready to begin writing code, check out Time and Sales to extract NxCore Data using a GUI. This example will cover printing the price and size of trades.
The NxCore package is distributed as a jar files, supporting a variety of operating systems and architectures. While the web documentation is primarily for c++, the name and description of variables will usually be the same. Types of variables will also be similar, with the String object replacing char * and Int replacing short and char. Note the Java API has a decreased throughput. For high performance programs it is recomended to use C/C++/C#.

Getting Started

  • Install Java 17.0.2 or higher.
  • Download and extract the zip which includes everything you need to get started: here
  • On Linux or macOS, you must enable signal chaining by preloading libjsig.
    • For Linux: export LD_PRELOAD=libjvm.so-directory/libjsig.so
    • For macOS: DYLD_FORCE_FLAT_NAMESPACE=0 DYLD_INSERT_LIBRARIES="JAVA_HOME/lib/libjsig.dylib"
  • Compile the java code specifying the jar files as the classpath. For example: javac TradeSample.java -cp NxCoreClass.jar
  • Run the java program specifying the jar files as the classpath, the library as the first argument, and path of the demo.XU.nx2 file as the second argument.
    • On Windows: java -cp NxCoreClass.jar; TradeSample NxCoreAPI64.dll demo.XU.nx2
    • On Linux: java -cp NxCoreClass.jar: TradeSample libnx.so demo.XU.nx2
    • On macOS: java -cp NxCoreClass.jar: TradeSample libnx.dylib demo.XU.nx2

Code

import net.nanex.NxCoreClass;
class TradeSample extends NxCoreClass{

    @Override
    public int OnNxCoreCallback(nxCoreSystem NxCoreSys, NxCoreMessage nxCoreMsg) {
        if (nxCoreMsg.MessageType ==  defines.NxMSG_TRADE) {
            NxCoreHeader header = nxCoreMsg.coreHeader;
            String symbol = header.pnxStringSymbol.String;
            NxTime timestamp = header.nxExgTimestamp;

            NxCoreTrade trade = nxCoreMsg.coreData.Trade;
            double price = PriceToDouble(trade.Price,trade.PriceType);
            int size = trade.Size;

            System.out.println("Trade for " + symbol + " at "
                + String.format("%02d:%02d:%02d", timestamp.Hour, timestamp.Minute, timestamp.Second)
                + " for " + size + " shares at $" + String.format("%.2f", Price));
        }
        return defines.NxCALLBACKRETURN_CONTINUE;
    }
    
    public static void main(String args[]) {
        TradeSample nxCore = new TradeSample();

        if (args.length > 1 && nxCore.LoadNxCore(args[0]) != 0){
            int returnValue = nxCore.ProcessTape(args[1], 0, 0, 0);
            nxCore.ProcessReturnValue(returnValue);
        }
        else
            System.out.println("loading library failed");
    }
}

Import

This imports the NxCore package. If you receive an error: package net.nanex does not exist or java.lang.NoClassDefFoundError: net/nanex/NxCoreClass, ensure -cp NxCoreClass.jar is included in the command.

import net.nanex.NxCoreClass;

Creating Class

The Sample Application must extend NxCoreClass in order to use the callback, described later. The main function should create an object of the extended class, which will be used for the rest of the application.

class TradeSample extends NxCoreClass{
    public static void main(String args[]) {
        TradeSample nxCore = new TradeSample();
        ...
    }
    ...
}

Callback Function

The NxCore API uses a callback function to deliver tick data. OnNxCoreCallback must be defined with 2 parameters of class NxCoreSystem and NxCoreMessage, respectively. OnNxCoreCallback is always the name of the function used for the callback and @Override tells the compiler to use this function. The callback should return NxCALLBACKRETURN_CONTINUE to receive the next message.
The type of message is set in the MessageType field of the NxCoreMessage object. Since we only want to process trades we check that MessageType equals NxMSG_TRADE.

@Override
public int OnNxCoreCallback(NxCoreSystem nxCoreSys, NxCoreMessage nxCoreMsg) {
    if (nxCoreMsg.MessageType ==  defines.NxMSG_TRADE) {
        ...
    }
    return defines.NxCALLBACKRETURN_CONTINUE;
}

Message Header

The coreHeader object in NxCoreMessage contains general information about the message. For this example we will get the Symbol from pnxStringSymbol and exchange timestamp. For most feeds nxExgTimestamp will have millisecond resolution.

NxCoreHeader header = nxCoreMsg.coreHeader;
String symbol = header.pnxStringSymbol.String;
NxTime timestamp = header.nxExgTimestamp;

Trade Message

The Trade object in the coreData object in NxCoreMessage contains details of the trade when MessageType is NxMSG_TRADE. For this example we will get the size of the trade and the price as a double. PriceToDouble can be used to convert NxCore integer Prices into a double floating-point price.

NxCoreTrade trade = nxCoreMsg.coreData.Trade;
double price = PriceToDouble(trade.Price,trade.PriceType);
int size = trade.Size;

Printing

This simply prints the values we found earlier in the code.

System.out.println("Trade for " + symbol + " at "
    + String.format("%02d:%02d:%02d", timestamp.Hour, timestamp.Minute, timestamp.Second)
    + " for " + size + " shares at $" + String.format("%.2f", Price));

Loading Library

Processing NxCore tapes requires that the NxCore dynamic library is loaded. The argument for LoadNxCore is the path to the corresponding dynamic library. On success LoadNxCore returns non-zero.
For 32-bit Windows: "NxCoreAPI.dll"
For 64-bit Windows: "NxCoreAPI64.dll"
For Linux: "libnx.so"
For aarch64 Linux: "libnx_aarch64.so"
For macOS: "libnx.dylib"

if (args.length > 1 && nxCore.LoadNxCore(args[0]) != 0)
    ...

else
    System.out.println("loading library failed");

Start Processing

ProcessTape begins processing the file in the first argument. For this simple example all other arguments can be ignored. ProcessReturnValue prints the result of ProcessTape.

int returnValue = nxCore.ProcessTape(args[1], 0, 0, 0);
nxCore.ProcessReturnValue(returnValue)

Next:
Quote
Detailed Trade