Basic: | Intro/Trade | Quote | Category | Status | Symbol | Options |
Detailed: | Trade | Quote |
Introduction to Categories
Categories messages cover a wide variety of information about the security. For this example, we will be printing the symbol name and official closing report. The category list contains all available categories and indication if they are active. All categories from the previous day will be sent 4 minutes into the file and new categories will be added through the day.
Code
import net.nanex.NxCoreClass; class CategorySample extends NxCoreClass{ @Override public int OnNxCoreCallback(NxCoreSystem nxCoreSys, NxCoreMessage nxCoreMsg) { if (nxCoreMsg.MessageType == defines.NxMSG_CATEGORY) { NxCoreHeader header = nxCoreMsg.coreHeader; String symbol = header.pnxStringSymbol.String; NxTime timestamp = header.nxExgTimestamp; NxDate sessionDate = header.nxSessionDate; NxCoreCategory category = nxCoreMsg.coreData.Category; int categoryID = category.pnxStringCategory.Atom; NxCategoryField[] fields = category.pnxFields; if (categoryID == 4) { if (fields[0].Set == 1) { String name = fields[0].data.StringZ; System.out.println(symbol + " name is " + name); } } if (categoryID == 50 || categoryID == 52 || categoryID == 54){ double high = 0.0; double low = 0.0; double last = 0.0; long volume = 0; if (fields[2].Set == 1) high = PriceToDouble(fields[2].data.nxPrice.Price, fields[2].data.nxPrice.PriceType); if (fields[3].Set == 1) low = PriceToDouble(fields[3].data.nxPrice.Price, fields[3].data.nxPrice.PriceType); if (fields[4].Set == 1) last = PriceToDouble(fields[4].data.nxPrice.Price, fields[4].data.nxPrice.PriceType); if (fields[6].Set == 1) volume = fields[6].data.i64Bit; System.out.println( symbol + " on " + String.format("%d%02d%02d", sessionDate.Year, sessionDate.Month, sessionDate.Day) + " had a high of $" +String.format("%.2f", high) + ", low of $"+ String.format("%.2f", low) +", last of $" + String.format("%.2f", last) + ", and volume of " + volume + " shares"); } } 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"); } }
Callback Function
Since we only want to process categories we check that MessageType equals NxMSG_CATEGORY. |
@Override public int OnNxCoreCallback(NxCoreSystem nxCoreSys, NxCoreMessage nxCoreMsg) { if (nxCoreMsg.MessageType == defines.NxMSG_CATEGORY) { ... } return defines.NxCALLBACKRETURN_CONTINUE; } |
Message Header
In addition to symbol and timestamp, the coreHeader also contains the session date of the message. For most cases the session date will be the same as the current date but for some messages, such as the official trade summary report, the session date may be from a previous date. |
NxCoreHeader header = nxCoreMsg.coreHeader; String symbol = header.pnxStringSymbol.String; NxTime timestamp = header.nxExgTimestamp; NxDate sessionDate = header.nxSessionDate; |
Category Message
The Category object in the coreData object in NxCoreMessage contains category information when MessageType is NxMSG_Category. The Atom of the pnxStringCategory object identifies the category and corresponds to the ID column of the category list. The array pnxFields contains the data of the category message. |
NxCoreCategory category = nxCoreMsg.coreData.Category; int categoryID = category.pnxStringCategory.Atom; NxCategoryField[] fields = category.pnxFields; |
Equity Symbol Information Category
Symbol name is sent in the Equity Symbol Info category. On the web page for that category we can see the atom is 4, so we will check for the atom of 4. Additionally on the web page we can see the Security Name is index 0 of fields and is stored in data.StringZ. The data object is only be populated when Set equals 1. |
if (categoryID == 4) { if (fields[0].Set == 1) { String name = fields[0].data.StringZ; ... } } |
Official Closing Report Category
Categories CTA_ClosingReport(50), OTC_ClosingReport(52), and Nasdaq_ClosingReport(54) have the same fields and contain the official High, Low, and Last prices as well as volume for a day. The sessionDate is the date the category message corresponds to. |
if (categoryID == 50 || categoryID == 52 || categoryID == 54){ double high = 0.0; double low = 0.0; double last = 0.0; long volume = 0; if (fields[2].Set == 1) high = PriceToDouble( fields[2].data.nxPrice.Price, fields[2].data.nxPrice.PriceType); if (fields[3].Set == 1) low = PriceToDouble( fields[3].data.nxPrice.Price, fields[3].data.nxPrice.PriceType); if (fields[4].Set == 1) last = PriceToDouble( fields[4].data.nxPrice.Price, fields[4].data.nxPrice.PriceType); if (fields[6].Set == 1) volume = fields[6].data.i64Bit; ... } |
Next:
Status