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.
Getting Started
Code
#include "stdio.h" #include "NxCoreAPI_Wrapper_C++.h" 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& timestamp = header.nxExgTimestamp; const NxCoreTrade& trade = pNxCoreMsg->coreData.Trade; double price = NxCore.PriceToDouble(trade.Price,trade.PriceType); int size = trade.Size; printf("Trade for %s at %02d:%02d:%02d %d shares at $%.02f\n", symbol, timestamp.Hour, timestamp.Minute, timestamp.Second, size, price); } 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; }
Import
This imports the basic IO header for printf as well as the NxCore c++ Header to use the NxCore Class as well as defined values. Next, the NxCore class for this program is declared. |
#include "stdio.h" #include "NxCoreAPI_Wrapper_C++.h" NxCoreClass NxCore; |
Callback Function
The NxCore API uses a callback function to deliver tick data. OnNxCoreCallback must be defined with 2 parameters of type NxCoreSystem and NxCoreMessage, respectively. The callback should return NxCALLBACKRETURN_CONTINUE to receive the next message. |
int OnNxCoreCallback( const NxCoreSystem* pNxCoreSys, const NxCoreMessage* pNxCoreMsg) { if(pNxCoreMsg->MessageType == NxMSG_TRADE) { ... } return NxCALLBACKRETURN_CONTINUE; } |
Message Header
The coreHeader member 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. |
const NxCoreHeader& header = pNxCoreMsg->coreHeader; char* symbol = header.pnxStringSymbol->String; const NxTime& timestamp = header.nxExgTimestamp; |
Trade Message
The Trade member in the coreData member 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. |
const NxCoreTrade& trade = pNxCoreMsg->coreData.Trade; double price = NxCore.PriceToDouble(trade.Price,trade.PriceType); int size = trade.Size; |
Printing
This simply prints the values we found earlier in the code. |
printf("Trade for %s at %02d:%02d:%02d %d shares at $%.02f\n", symbol, timestamp.Hour, timestamp.Minute, timestamp.Second, size, 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 Windows the path can be relative to the working directory or absolute. For all other operating systems the path must be absolute. On success LoadNxCore returns non-zero. |
if (NxCore.LoadNxCore(argv[1])) ... else printf("loading library failed\n"); |
Start Processing
ProcessTape begins processing the file in the first argument. The callback declared earlier should be the fifth argument. For this simple example all other arguments can be ignored. ProcessReturnValue prints the result of ProcessTape. |
int returnValue = NxCore.ProcessTape(argv[2], 0, 0, 0, OnNxCoreCallback); NxCore.ProcessReturnValue(returnValue); |
Next:
Quote
Detailed Trade