Transformer SDK For C  8.0.3.342099-79b6f512
demo-spread.c

This example generates a Bid/Ask spread based on the incoming Last price for a symbol.

Note
This example has been written using the legacy API.

Sample Configuraiton:

symbols    /DEMO/*
spread     TEST 2.0

Source code:

/*
* Transformer - Sample Module
*
* Copyright CAPLIN Systems Ltd 2000-2006
*
* This module generates a number of symbols with Bid/Ask values
* spread as configured.
*
* Example configuration:
*
* symbols /DEMO/MSFT
* spread TEST 2
*
* If an update is received for /DEMO/MSFT with Last = 2
* then a record called /DEMO/MSFT-TEST will be published with Ask = 4, Bid = 1
*
*/
#include "transformer.h"
static char **symbols = NULL;
static int symbols_num = 0;
static char **spread = NULL;
static int spread_num = 0;
static void demo_recv_update(int feed, ds_data_t *pkt, time_t arr_tim, 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);
/* Add configuration option to obtain spread suffix and scaling factor. We use an array of
strings to simplify the code. Normally, one would use config groups.
n = spread suffix 1
n+1 = spread factor 1 (for bid, inverted for ask)
n+2 = spread suffix 2
n+3 = spread factor 2
....
*/
ds_config_add_array_option("spread","Configuration of the spread",DS_CONFIG_STR_ARRAY,&spread,&spread_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;
}
/* Check to see if any spread symbols are defined or if there's an odd number defined */
if ( spread_num == 0 || spread_num % 2 ) {
ds_printf_time(rtas_log,"(%s) Invalid spread configuration\n",module_name);
return;
}
/* Now register an interest in the symbols */
for ( i = 0; i < symbols_num; i++ ) {
register_interest(symbols[i],demo_recv_update,0,NULL);
}
return;
}
static void demo_recv_update(int feed, ds_data_t *pkt, time_t arr_tim, int id, void *data)
{
char subject_buf[1024];
ds_data_t *dsdata;
char *last_str;
double last;
int i;
/* Loop over the number of defined spreads */
for ( i = 0; i < spread_num ; i += 2 ) {
/* Generate the spread symbol name */
snprintf(subject_buf,sizeof(subject_buf),"%s-%s",pkt->subject,spread[i]);
/* Create a new update packet for the spread record */
dsdata = ds_init_data(subject_buf,DS_RECORD_TYPE,F_CREATEPARENT|F_CREATEOBJECT);
/* Obtain the Last within the update */
last_str = packet_get_field_byname(pkt,"dLast");
/* If there was a Last field present then do something */
if ( last_str != NULL ) {
last = atof(last_str);
if ( last != 0.0 ) { /* Basic sanity checking */
/* Now calculate the spread lasts */
ds_add_record_float(dsdata,"dBid",last / atof(spread[i+1]));
ds_add_record_float(dsdata,"dAsk",last * atof(spread[i+1]));
/* And publish the generated spread to DataSource and cache in the Transformer */
}
}
}
return;
}

Generated on Wed Dec 4 2024 17:41:36 for Transformer SDK For C