Transformer SDK For C  6.2.11.309924
demo-calcvalue.c

This example demonstrates how to calculate a derived field from input data. An interest is registered in a selection of symbols, and a Mid price is derived from the incoming Bid Ask values.

Note
This example has been written using the legacy API.

Sample configuration:

symbols     /DEMO/*

Source code:

/*
* Transformer - Sample Module
*
* Copyright CAPLIN Systems Ltd 2000-2006
*
* This module demonstrates how to calculate a derived field from input data. We take in
* symbols and based on the Bid and Ask calculate a "Mid" price which is simply the
* midpoint between the two.
*
* Once the value has been calculated, the update is published into DataSource, passed
* onto subsequent modules and cached within the Transformer core memory.
*
*/
#include "transformer.h"
static char **symbols = NULL;
static int symbols_num = 0;
static tf_handle_t **handles = NULL;
static int demo_recv_update(tf_handle_t *handle, int feed, ds_data_t *pkt, int flags, int id, void *data);
void mod_init(char *module_name)
{
char buf[FILENAME_MAX+1];
int i;
/* Add config option for the symbols we want to receive */
ds_config_add_array_option("symbols","Symbols to process",DS_CONFIG_STR_ARRAY,&symbols,&symbols_num);
/* Load a config file based on the name of the module */
snprintf(buf,sizeof(buf),"%s.conf",module_name);
/* Check whether any symbols are required, if not then log this condition to the main Transformer
log file
*/
if ( symbols_num == 0 ) {
ds_printf_time(rtas_log,"(%s) No symbols required\n",module_name);
return;
}
handles = calloc(symbols_num, sizeof(handles[0]));
/* Now register an interest in the symbols */
for ( i = 0; i < symbols_num; i++ ) {
handles[i] = tf_add_listener(symbols[i], 0, demo_recv_update, NULL, NULL, 0, NULL, NULL);
}
return;
}
void mod_exit()
{
int i;
/* We have to loop over all known symbols */
for ( i = 0; i < symbols_num; i++ ) {
tf_handle_delete(handles[i]);
free(symbols[i]);
}
if ( symbols ) {
free(symbols);
}
return;
}
static int demo_recv_update(tf_handle_t *handle, int feed, ds_data_t *pkt, int flags, int id, void *data)
{
ds_data_t *dsdata;
char *bid_str, *ask_str;
double bid,ask;
/* Pick out the Bid and Ask field updates within this received update */
if ( ( bid_str = packet_get_field_byname(pkt,"dBestBid") ) != NULL ) {
if ( ( ask_str = packet_get_field_byname(pkt,"dBestAsk") ) != NULL ) {
bid = atof(bid_str);
ask = atof(ask_str);
/* Create a new ds_data_t object for our calculated Mid value */
dsdata = ds_init_data(pkt->subject,DS_RECORD_TYPE,F_CREATEPARENT|F_CREATEOBJECT);
/* Add the field into our new ds_data_t object */
ds_add_record_float(dsdata,"Mid", ( ask - bid ) / 2. + bid );
/* We should now add the calculated Mid value into the incoming packet. This
* permits modules which run after this one to access the value and also to
* be published onto DataSource and onto any connected Liberator (see above)
* Using packet_merge() rather than say, ds_add_data_float() ensures that
* we only have one copy of a field in an update. We could alternately have
* used packet_update_field_byname().
*/
packet_merge(pkt,dsdata,NULL);
/* Cache the newly generated Mid value in the Transformer core memory */
}
}
return 1;
}

Generated on Sat Aug 26 2017 12:36:31 for Transformer SDK For C