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
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 |
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. |
@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. |
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