Apama Capital Markets Foundation Documentation : Capital Markets Foundation : Analytics : Using the position service framework : Implementing custom position trackers : Sample code for implementing a custom position tracker
Sample code for implementing a custom position tracker
In the following sample code, a custom tracker increments a position based on NewOrders being placed, and filters based on a symbol name slice.
package com.apama.cmf.demo;
 
monitor PSCustomTrackerDemo {
 
context mainContext := context.current();
com.apama.position.tracker.PSTrackerManagerInterface trackerIface;
dictionary<integer/*instanceId*/,listener> trackerListeners;
action<> completedSetup;
 
action onload() {
com.apama.position.tracker.PSTrackerInterface trackerCbIface :=
new com.apama.position.tracker.PSTrackerInterface;
trackerCbIface.createInstance := createInstance;
trackerCbIface.deleteInstance := deleteInstance;
trackerCbIface.positionExternallyUpdated := positionExternallyUpdated;
 
// Use the position service tracker factory to create an instance
// of a position service tracker interface.
// This call is asynchronous so it requires a callback.
com.apama.position.PositionConfigSchema configSchema :=
new com.apama.position.PositionConfigSchema;
configSchema.addItemMinimal(
"TRACKER_CONFIG_SYMBOL_SLICE", "sequence<string>", "",
"Allows a set of symbols to match against to be defined");
 
(new com.apama.position.tracker.PSTrackerFactory).registerTracker(
"MyDemoPositionTracker",
"MyDemoPositionTrackerType",
mainContext,
configSchema,
trackerCbIface,
trackerRegistered );
}
 
action trackerRegistered(
com.apama.position.tracker.PSTrackerManagerInterface psTrackerIface,
boolean success, string msg ) {
// Inform the CMF Service Framework that this is fully created:
completedSetup();
}
 
// Example implementation of the createInstance() action that
// in this case creates a new subscription for each request
// and creates a listener for new orders matching the configuration
// provided. A unique requestId value is required to allow the
// subscription manager service to match the asynchronous response
// to the right request.
action createInstance(
integer requestId, integer instanceId,
com.apama.position.PositionConfigParams config,
action<integer/*requestId*/,integer/*subscriptionId*/,
boolean/*success*/, string/*msg*/> cbCreated ) {
 
// This sample tracker supports filtering on only a symbol name.
sequence<string> symbols := [];
if( config.hasParam( "TRACKER_CONFIG_SYMBOL_SLICE" ) ) then {
string param := config.getParam( "TRACKER_CONFIG_SYMBOL_SLICE" );
if( sequence<string>.canParse( param ) ) then {
symbols := sequence<string>.parse( param );
}
}
 
string symbol;
for symbol in symbols {
com.apama.oms.NewOrder no;
on all com.apama.oms.NewOrder( symbol=symbol ):no {

// Based on the new order's quantity/price...
com.apama.position.Position newPosition :=
new com.apama.position.Position;
newPosition.setPosition( no.quantity, no.price );

// Adjust the position relative to the quantity/price provided
// and ask the position tracker interface to publish it:
trackerIface.updateRelativePosition(instanceId, newPosition);
}
}
 
// Inform the subscription manager that a new instance
// has been created:
cbCreated( requestId, instanceId, true, "" );
}
 
// Example implementation of the deleteInstance() action that
// cleans up all state for the tracker instance that is being
// deleted (in this case the listener that was created).
// A unique requestId value is required to allow the
// subscription manager service to match the asynchronous
// response to the right request.
action deleteInstance( integer requestId, integer instanceId,
action<integer/*requestId*/,integer/*subscriptionId*/,
boolean/*success*/, string/*msg*/> cbDeleted ) {
if( trackerListeners.hasKey( instanceId ) ) then {
trackerListeners[ instanceId ].quit();
cbDeleted( requestId, instanceId, true, "" );
} else {
cbDeleted( requestId, instanceId, false,
"Tracker instance not found!" );
}
}
 
// Example implementation of the positionExternallyUpdated()
// action that in this case logs the update and sends back a
// true value to indicate that the position update should be
// published immediately.
action positionExternallyUpdated(
integer requestId, integer subscriptionId,
com.apama.position.Position positionAdjustment,
boolean isRelative, com.apama.position.PositionConfigParams config,
action<integer/*requestId*/, integer/*subscriptionId*/,
boolean/*updateTable*/,
boolean/*success*/,string/*msg*/> cbAdjustedPosition ) {
log "External Position Update:
isRelative="+isRelative.toString()+" :
Position="+positionAdjustment.toString() at DEBUG;
 
cbAdjustedPosition(
requestId, subscriptionId, true, true, "Update Successful");
}
}
Copyright © 2013-2017 Software AG, Darmstadt, Germany. (Innovation Release)

Product LogoContact Support   |   Community   |   Feedback