Back to UserData & Performance


UserDataSample2.cpp

////////////////////////////////////////////////////////////////////////////////
//// UserDataSample2.cpp                                                    ////
//// Sample App for NxCore in C/C++                                         ////
//// Author: Jeffrey Donovan                                                ////
//// Date: 10-29-10                                                         ////
////////////////////////////////////////////////////////////////////////////////
//// Demonstrates:                                                          ////
//// 1) Using the SYMBOLSPIN message to filter symbols at startup.          ////
//// 2) Setting UserData1 for quick determination of symbols interested in. ////
//// 3) Setting UserData1 for quick reference into a composite structure.   ////
//// 4) Processing trades for only the symbols we are interested in.        ////
//// 5) Dumping composite structure values out once a minute.               ////
////////////////////////////////////////////////////////////////////////////////
//// To Read as Written:                                                    ////
//// Tab Size: 4  Indent Size: 2, Keep Tabs                                 ////
////////////////////////////////////////////////////////////////////////////////
#include "stdio.h"
    
#include "NxCoreLoadLib.h"  // NxCore DLL Load Library
    
// Tiny Composite structure
typedef struct
{
  NxString *pSymbol;   // Pointer to NxCore Symbol String pointer
  double CurrentPrice; // Current composite price
  int TotalVolume;     // Current total volume
}CompositeType;
    
CompositeType CompositeRecs[3]; // 3 element composite array (increase for your own needs)
int NumCompositeRecs=0;         // Number of recs in composite array
    
// Forward define for callback
int __stdcall OnNxCoreCallback(const NxCoreSystem* pNxCoreSys,const NxCoreMessage* pNxCoreMsg);
    
// Main Entry Point for app.	
int main(int argc, char* argv[])
{
    printf("NxCore C UserDataSample2 Start.\n");
    
    // If we can load the NxCore DLL
    if (LoadNxCore("NxCoreAPI.dll"))
    {        
	  printf("NxCoreAPI.dll Loaded.\n");
    
	  // If a tape filename was passed in command line argument,
	  // call ProcessTape with that argument. Use control flags
	  // to eliminate Level1 quotes, Level2 quotes and all OPRA
	  // quotes.
	  if (argv[1])
            pfNxCoreProcessTape(argv[1],
                            NULL,
                            (NxCF_EXCLUDE_QUOTES | NxCF_EXCLUDE_QUOTES2 | NxCF_EXCLUDE_OPRA),
				 	        0,OnNxCoreCallback);
	  else
		pfNxCoreProcessTape("",
                            NULL,
                            (NxCF_EXCLUDE_QUOTES | NxCF_EXCLUDE_QUOTES2 | NxCF_EXCLUDE_OPRA),
				 	        0,OnNxCoreCallback);
                    
        // Unload NxCore DLL
        UnloadNxCore();
    }
    
    printf("NxCore C UserDataSample2 Stop.\n");
    
    
    // Exit app
    return 0;
}
    
//------------------------------------------------------------
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 < NumCompositeRecs;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);	      
                }
	     }
    
		 
	     // Print the specific NxCore status message
           switch( pNxCoreSys->Status ) 
           {
	       case NxCORESTATUS_COMPLETE:printf("NxCore Complete Message.\n");break;
             case NxCORESTATUS_INITIALIZING:printf("NxCore Initialize Message.\n");break;
             case NxCORESTATUS_ERROR:printf("NxCore Error.\n");break;
           }
	     break;
    
      // Process SymbolSpin to initially set/filter symbols. 
      // This message is sent once at the start of the tape for each symbol.
	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[NumCompositeRecs].pSymbol=pNxCoreMsg->coreHeader.pnxStringSymbol;
    
		   
			   // Inc the number of composite recs, set userdata1 to the new value 
			   // 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).
			   ++NumCompositeRecs;
			   pNxCoreMsg->coreHeader.pnxStringSymbol->UserData1=NumCompositeRecs;
		   }
    
		 }
	     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.
		  CompositeRecs[i].CurrentPrice=pfNxCorePriceToDouble(pNxCoreMsg->coreData.Trade.Price,pNxCoreMsg->coreData.Trade.PriceType);
		  CompositeRecs[i].TotalVolume+=pNxCoreMsg->coreData.Trade.Size;
	    }
	    break;
  }
    
	  
  // Continue running the tape
  return NxCALLBACKRETURN_CONTINUE;  
}