Back to UserData & Performance
UserDataSample2.cpp
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <vector> #include "NxCoreAPI.h" #include "NxCoreAPI_Wrapper_C++.h" NxCoreClass NxCore; // Tiny Composite structure typedef struct { NxString *pSymbol; // Pointer to NxCore Symbol String pointer double CurrentPrice; // Current composite price long long TotalVolume; // Current total volume }CompositeType; std::vector<CompositeType> CompositeRecs; // vector to hold symbol data //------------------------------------------------------------ int __stdcall OnNxCoreCallback(const NxCoreSystem* pNxCoreSys,const NxCoreMessage* pNxCoreMsg) { // If this is an NxCore Status message pass it to the status // message handler function. switch(pNxCoreMsg->MessageType) { // Process a status message case NxMSG_STATUS: // If we just ticked a minute, dump out the composite data if (pNxCoreSys->ClockUpdateInterval >= NxCLOCK_MINUTE && pNxCoreSys->nxTime.Hour >= 9) { // Print the date and time printf("\nNxCore Time: %02d/%02d/%d %02d:%02d:%02d\n", pNxCoreSys->nxDate.Month,pNxCoreSys->nxDate.Day,pNxCoreSys->nxDate.Year, pNxCoreSys->nxTime.Hour,pNxCoreSys->nxTime.Minute,pNxCoreSys->nxTime.Second); // Now dump out the current values stored in composite recs for (int i=0;i < CompositeRecs.size();i++) { printf("Symbol=%s, UserData1=%d, Price=%0.4f, Volume=%d\n", CompositeRecs[i].pSymbol->String, CompositeRecs[i].pSymbol->UserData1, CompositeRecs[i].CurrentPrice, CompositeRecs[i].TotalVolume); } } break; // Process Symbol Spin and Symbol Change add messages case NxMSG_SYMBOLCHANGE: if(pNxCoreMsg->coreData.SymbolChange.Status != NxSS_ADD) return NxCALLBACKRETURN_CONTINUE; case NxMSG_SYMBOLSPIN: // First check to see if this is an equity symbol type.... if (pNxCoreMsg->coreHeader.pnxStringSymbol->String[0]=='e') { // Check to see if this is one of the stocks we are interested in. // If it is, set UserData1 to 1. if ((strcmp(pNxCoreMsg->coreHeader.pnxStringSymbol->String,"eAAPL")==0)|| (strcmp(pNxCoreMsg->coreHeader.pnxStringSymbol->String,"eIBM")==0)|| (strcmp(pNxCoreMsg->coreHeader.pnxStringSymbol->String,"eGE")==0)) { printf("Interest Set for %s\n",pNxCoreMsg->coreHeader.pnxStringSymbol->String); // Set the composite rec symbol pointer to the nxcore symbol string pointer CompositeRecs.push_back({ pNxCoreMsg->coreHeader.pnxStringSymbol, 0.0, 0}); // Set userdata1 to one greater than the index which equals the current size. // This means the actual array slot 0 will be a value of 1 (so that UserData1 can // be used for both the interest test and an index value). pNxCoreMsg->coreHeader.pnxStringSymbol->UserData1 = CompositeRecs.size(); } } break; // Process a trade message case NxMSG_TRADE: // If UserData1 is set to any value then we are interested in this symbol.... if (pNxCoreMsg->coreHeader.pnxStringSymbol->UserData1) { // Get the composite array index by subtracting 1 from UserData. int i=pNxCoreMsg->coreHeader.pnxStringSymbol->UserData1 - 1; // Set the composite values. const NxCoreTrade& nt = pNxCoreMsg->coreData.Trade; CompositeRecs[i].CurrentPrice = NxCore.PriceToDouble(nt.Price, nt.PriceType); CompositeRecs[i].TotalVolume += nt.Size; } break; } // Continue running the tape return NxCALLBACKRETURN_CONTINUE; } int main(int argc, char** argv) { if (!NxCore.LoadNxCore("NxCoreAPI64.dll") && !NxCore.LoadNxCore("NxCoreAPI.dll")) { fprintf(stderr, "loading library failed\n"); return -1; } NxCore.ProcessTape(argv[1], 0, NxCF_EXCLUDE_CRC_CHECK | NxCF_EXCLUDE_QUOTES | NxCF_EXCLUDE_QUOTES2 | NxCF_EXCLUDE_OPRA, 0, OnNxCoreCallback); return 0; } |
import net.nanex.NxCoreClass; import java.util.Vector; class UserDataSample2 extends NxCoreClass{ class CompositeType{ public MyData(String Symbol) { this.Symbol = Symbol; } String Symbol; // Symbol string double CurrentPrice = 0.0; // Current composite price long TotalVolume = 0; // Current total volume } private Vector<CompositeType> CompositeRecs = new Vector<CompositeType>(); @Override public int OnNxCoreCallback(NxCoreSystem nxCoreSys, NxCoreMessage nxCoreMsg) { switch (nxCoreMsg.MessageType) { // Process a status message case defines.NxMSG_STATUS: // If we just ticked a minute, dump out the composite data if (nxCoreSys.ClockUpdateInterval >= defines.NxCLOCK_MINUTE && nxCoreSys.nxTime.Hour >= 9) { // Print the date and time System.out.println(String.format("\nNxCore Time: %02d/%02d/%d %02d:%02d:%02d", nxCoreSys.nxDate.Month,nxCoreSys.nxDate.Day,nxCoreSys.nxDate.Year, nxCoreSys.nxTime.Hour,nxCoreSys.nxTime.Minute,nxCoreSys.nxTime.Second)); // Now dump out the current values stored in composite recs for (int i=0;i < CompositeRecs.size();i++) { System.out.println(String.format( "Symbol=%s, UserData1=%d, Price=%.4f, Volume=%d", CompositeRecs.get(i).Symbol, i+1, CompositeRecs.get(i).CurrentPrice, CompositeRecs.get(i).TotalVolume)); } } break; // Process Symbol Spin and Symbol Change add messages case defines.NxMSG_SYMBOLCHANGE: if(nxCoreMsg.coreData.SymbolChange.Status != defines.NxSS_ADD) return defines.NxCALLBACKRETURN_CONTINUE; case defines.NxMSG_SYMBOLSPIN: // First check to see if this is an equity symbol type.... if (nxCoreMsg.coreHeader.pnxStringSymbol.String.charAt(0)=='e') { // Check to see if this is one of the stocks we are interested in. // If it is, set UserData1 to 1. if (nxCoreMsg.coreHeader.pnxStringSymbol.String.equals("eAAPL")|| nxCoreMsg.coreHeader.pnxStringSymbol.String.equals("eIBM")|| nxCoreMsg.coreHeader.pnxStringSymbol.String.equals("eGE")) { System.out.println(String.format( "Interest Set for %s\n",nxCoreMsg.coreHeader.pnxStringSymbol.String)); // Set the composite rec symbol pointer to the nxcore symbol string pointer CompositeRecs.addElement(new CompositeType(nxCoreMsg.coreHeader.pnxStringSymbol.String)); // Set userdata1 to one greater than the index which equals the current size. // This means the actual array slot 0 will be a value of 1 (so that UserData1 can // be used for both the interest test and an index value). nxCoreMsg.coreHeader.pnxStringSymbol.UserData1 = CompositeRecs.size(); } } break; case defines.NxMSG_TRADE: // If UserData1 is set then we are interested in this symbol.... if (nxCoreMsg.coreHeader.pnxStringSymbol.UserData1 != 0) { // Get the composite array index by subtracting 1 from UserData. int i = nxCoreMsg.coreHeader.pnxStringSymbol.UserData1 - 1; // Set the composite values. NxCoreTrade nt = nxCoreMsg.coreData.Trade; CompositeRecs.get(i).CurrentPrice = PriceToDouble(nt.Price, nt.PriceType); CompositeRecs.get(i).TotalVolume += nt.Size; } break; } return defines.NxCALLBACKRETURN_CONTINUE; } public static void main(String args[]) { UserDataSample2 nxCore = new UserDataSample2(); if (nxCore.LoadNxCore("NxCoreAPI64.dll") != 0){ nxCore.ProcessTape(args[0], 0, defines.NxCF_EXCLUDE_CRC_CHECK | defines.NxCF_EXCLUDE_QUOTES | defines.NxCF_EXCLUDE_QUOTES2 | defines.NxCF_EXCLUDE_OPRA, 0); } else System.out.println("loading library failed"); } } |
import NxCore tapePath = "" class CompositeType: def __init__(self, Symbol): self.Symbol = Symbol# Symbol string CurrentPrice = 0.0 # Current composite price TotalVolume = 0 # Current total volume CompositeRecs = [] def OnNxCoreCallback(NxCoreSys, NxCoreMsg): global CompositeRecs if NxCoreMsg.MessageType == NxCore.NxMSG_STATUS: # If we just ticked a minute, dump out the composite data if NxCoreSys.ClockUpdateInterval >= NxCore.NxCLOCK_MINUTE and NxCoreSys.nxTime.Hour == 10: # Print the date and time print("\nNxCore Time: {:02d}/{:02d}/{} {:02d}:{:02d}:{:02d}".format( NxCoreSys.nxDate.Month,NxCoreSys.nxDate.Day,NxCoreSys.nxDate.Year, NxCoreSys.nxTime.Hour,NxCoreSys.nxTime.Minute,NxCoreSys.nxTime.Second)) # Now dump out the current values stored in composite recs for i in range(len(CompositeRecs)): print("Symbol={}, UserData1={}, Price={:.4f}, Volume={}".format( CompositeRecs[i].Symbol, i+1, CompositeRecs[i].CurrentPrice, CompositeRecs[i].TotalVolume)); #Process Symbol Spin and Symbol Change add messages elif ((NxCoreMsg.MessageType == NxCore.NxMSG_SYMBOLCHANGE and NxCoreMsg.coreData.SymbolChange.Status != NxCore.NxSS_ADD) or NxCoreMsg.MessageType == NxCore.NxMSG_SYMBOLSPIN): ch = NxCoreMsg.coreHeader t = NxCoreSys.nxTime # First check to see if this is an equity symbol type.... if NxCoreMsg.coreHeader.pnxStringSymbol.String[0]=='e': # Check to see if this is one of the stocks we are interested in. # If it is, set UserData1 to 1. if (NxCoreMsg.coreHeader.pnxStringSymbol.String == "eAAPL" or NxCoreMsg.coreHeader.pnxStringSymbol.String == "eIBM" or NxCoreMsg.coreHeader.pnxStringSymbol.String == "eGE"): NxCoreMsg.coreHeader.pnxStringSymbol.UserData1=1 print("Interest Set for {}\n".format(NxCoreMsg.coreHeader.pnxStringSymbol.String)) # Set the composite rec symbol pointer to the nxcore symbol string pointer CompositeRecs.append(CompositeType( NxCoreMsg.coreHeader.pnxStringSymbol.String)) # Set userdata1 to one greater than the index which equals the current array length. # This means the actual array slot 0 will be a value of 1 (so that UserData1 can # be used for both the interest test and an index value). NxCoreMsg.coreHeader.pnxStringSymbol.UserData1 = len(CompositeRecs) elif NxCoreMsg.MessageType == NxCore.NxMSG_TRADE: # If UserData1 is set then we are interested in this symbol.... if NxCoreMsg.coreHeader.pnxStringSymbol.UserData1 != 0: nt = NxCoreMsg.coreData.Trade # Get the composite array index by subtracting 1 from UserData. i = NxCoreMsg.coreHeader.pnxStringSymbol.UserData1 - 1 # Set the composite values. nt = NxCoreMsg.coreData.Trade; CompositeRecs[i].CurrentPrice = NxCore.PriceToFloat(nt.Price, nt.PriceType) CompositeRecs[i].TotalVolume += nt.Size return NxCore.NxCALLBACKRETURN_CONTINUE if NxCore.LoadNxCore("NxCoreAPI64.dll"): NxCore.ProcessTape(tapePath, 0, 0, NxCore.NxCF_EXCLUDE_CRC_CHECK | NxCore.NxCF_EXCLUDE_QUOTES | NxCore.NxCF_EXCLUDE_QUOTES2 | NxCore.NxCF_EXCLUDE_OPRA, OnNxCoreCallback) else: print("loading library failed") |