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 module is distributed as wheel files. These are pre-built binary files that can be installed using pip. While the web documentation is primarily for c++, the name and description of variables will usually be the same. Additionally all elements of the NxCore Python module should contain detailed docstrings that can be consulted. For example print(NxCore.ProcessTape.__doc__) will give details on using the ProcessTape function. Note the Python API has a decreased throughput. For high performance programs it is recomended to use C/C++/C#.
Getting Started
Code
import NxCore def OnNxCoreCallback(NxCoreSys, NxCoreMsg): if NxCoreMsg.MessageType == NxCore.NxMSG_TRADE: header = NxCoreMsg.coreHeader symbol = header.pnxStringSymbol.String timestamp = header.nxExgTimestamp trade = NxCoreMsg.coreData.Trade price = NxCore.PriceToFloat(trade.Price,trade.PriceType) size = trade.Size print("Trade for {} at {:02d}:{:02d}:{:02d} for {} shares at ${:.2f}" \ .format( symbol, timestamp.Hour, timestamp.Minute, timestamp.Second, size, price)) return NxCore.NxCALLBACKRETURN_CONTINUE if NxCore.LoadNxCore("NxCoreAPI64.dll"): returnValue = NxCore.ProcessTape("demo.XU.nx2", 0, 0, 0, OnNxCoreCallback) NxCore.ProcessReturnValue(returnValue) else: print("loading library failed")
Import
This imports the NxCore Python module. If you receive an error ModuleNotFoundError: No module named 'NxCore', ensure the whl install succeeded. |
import NxCore |
Callback Function
The NxCore API uses a callback function to deliver tick data. The callback function must be defined with 2 parameters which will correspond to NxCoreSystem and NxCoreMessage objects respectively. 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 NxCore.NxMSG_TRADE. The callback should return NxCore.NxCALLBACKRETURN_CONTINUE to receive the next message. |
def OnNxCoreCallback(NxCoreSys, NxCoreMsg): if NxCoreMsg.MessageType == NxCore.NxMSG_TRADE: ... return NxCore.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. Note nxExgTimestamp is not a Python time type, intead it is a NxTime type. For most feeds nxExgTimestamp will have millisecond resolution. |
header = NxCoreMsg.coreHeader symbol = header.pnxStringSymbol.String timestamp = header.nxExgTimestamp |
Trade Message
The Trade object in the coreData object in NxCoreMessage contains details of the trade when MessageType is NxCore.NxMSG_TRADE. For this example we will get the size of the trade and the price as a float. PriceToFloat can be used to convert NxCore int Prices into a floating-point price. |
trade = NxCoreMsg.coreData.Trade price = NxCore.PriceToFloat(trade.Price,trade.PriceType) size = trade.Size |
Printing
This simply prints the values we found earlier in the code. |
print("Trade for {} at {:02d}:{:02d}:{:02d} for {} shares at ${:.2f}" .format(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 success LoadNxCore returns non-zero. |
if NxCore.LoadNxCore("NxCoreAPI64.dll"): ... else: print("loading library failed") |
Start Processing
ProcessTape begins processing the file in the first argument. The callback defined earlier should be the fifth argument. For this simple example all other arguments can be ignored. ProcessReturnValue prints the result of ProcessTape. |
if NxCore.LoadNxCore("NxCoreAPI64.dll"): returnValue = NxCore.ProcessTape("demo.XU.nx2", 0, 0, 0, OnNxCoreCallback) NxCore.ProcessReturnValue(returnValue) else: print("loading library failed") |
Next:
Quote
Detailed Trade