Apama Capital Markets Foundation Documentation : Capital Markets Foundation : Analytics : Using the position service framework : Using default position trackers : Creating and subscribing to the open position tracker
Creating and subscribing to the open position tracker
The default implementation of the open position tracker tracks the quantity and cash position of order executions. To use the open position tracker, you create an instance of it in the context in which your application receives orders. You then use the position service interface object to subscribe to the open position tracker you created.
To create an instance of an open position tracker, execute the create() action defined in com.apama.position.tracker.OpenPositionTrackerFactory. This action takes three parameters:
Parameter
Description
mainContext
A reference to the main context. When the Position Management Service bundle is in your application the correlator automatically sets up the position service manager in the main context.
trackerName
A string that specifies a name for this tracker. This name must be unique in your application.
cbCreated
This callback action returns a boolean parameter that indicates whether creation of the tracker was successful, followed by a string parameter that can hold a message that can be logged.
After an open position tracker is set up, subscribe to it by calling the subscribeAndMonitor() action or the subscribe() action, which are both defined in com.apama.position.PSInterface. Both actions subscribe to the specified tracker. The subscribeAndMonitor() action also registers an update callback each time there is an update to the position being tracked. A tracker can accept any number of subscriptions. The subscribeAndMonitor() action takes the following parameters:
*trackerName is the name of the open position tracker to subscribe to. This is the unique name that was specified when the tracker was created.
*trackerType indicates the type of tracker you are subscribing to. When subscribing to an instance of the default open position tracker, specify the tracker type as the constant TRACKER_TYPE string defined in the com.apama.position.tracker.OpenPositionTrackerConstants event.
*config is a com.apama.position.PositionConfigParams object that contains any configuration for this subscription, such as the slice criteria you want to match orders against.
*updateCallback is a user-defined update callback action that the tracker will call when there is an update to the position subscribed to.
*subscribedCb is a user-defined callback action that the tracker calls in response to the subscription request.
When there is a change in the position being tracked, the update callback (or callbacks) that have been registered for the subscription will be called. This callback action contains a com.apama.position.Position event object that defines the current position for that subscription. By default, the open position tracker calculates net values for the combined long and short quantity traded and cash position:
Field
Description
minQtyPosition
maxQtyPosition
Each of these fields contain the same value. The value indicates the net quantity for all long and short executed orders that match the subscription configuration. A negative number indicates a short position.
minCashPosition
maxCashPosition
Each of these fields contain the same value. The value indicates the net cash position, which is the sum of (quantity times price) for each executed order.
For example, if you bought 12 and sold 5 the minQtyPosition and maxQtyPosition fields each contain 7 and so you are long 7. If you bought 5 and sold 12 the minQtyPosition and maxQtyPosition fields each contain -7 and so you are short 7.
If you want to separately track open long and short positions specify the "TRACKER_CONFIG_TRACK_SEPARATE_LONG_SHORT" constant in the PositionConfigParams object you specify when you call the subscribeAndMonitor() action or the subscribe() action. If this parameter is set, then the Position event field values indicate the following:
Field
Description
minQtyPosition
Sum of the quantities of all executed short orders. This is always a negative number.
maxQtyPosition
Sum of the quantities of all executed long orders. This is always a positive number.
minCashPosition
Sum of (quantity times price) for all executed short orders.
maxCashPosition
Sum of (quantity times price) for all executed long orders.
The following code shows an example of creating two open position trackers in two different contexts.
monitor TrackerMonitor {
 
context mainContext := context.current();
 
// Spawning to a new context to show that the position
// service framework can be used in multiple contexts.
action onload() {
// Create an instance of the open position tracker and
// give it a unique name to be used when subscribing to it.
(new com.apama.position.tracker.OpenPositionTrackerFactory).create
( mainContext, "MyFirstOpenPositionTracker", cbCreatedTracker );
 
context newCtx := context("TESTCTX", false);
spawn startTest() to newCtx;
}
 
action startTest() {
// Create another instance of the open position tracker and
// give it a unique name to be used when subscribing to it.
(new com.apama.position.tracker.OpenPositionTrackerFactory).create
(mainContext, "MySecondOpenPositionTracker", cbCreatedTracker );
}
 
// This action is called after the position tracker instance
// has been created
action cbCreatedTracker( boolean success, string msg ) {
log "OPEN POSITION TRACKER CREATED: "+success.toString()+" : "+msg;
}
}
The following code shows an example of creating a position service interface and then subscribing to the open position trackers created in the previous sample.
monitor PositionServicesDemo {
context mainContext := context.current();
 
// To set up the position service framework, you create an
// instance of PSInterface, which provides the user interface.
com.apama.position.PSInterface psIface;
 
// A constant string that will be used:
com.apama.position.tracker.OpenPositionTrackerConstants openPosConsts;
 
action onload() {
 
// Create a position service configuration object.
// In this case, with no specific configuration.
// Then create an instance of PSInterface:
com.apama.position.PositionConfigParams config :=
new com.apama.position.PositionConfigParams;
(new com.apama.position.PSFactory).create
( mainContext, config, cbCreated );
}
 
action cbCreated( com.apama.position.PSInterface psInterface,
boolean success, string msg ) {
psIface := psInterface;
 
// After creating the interface object, subscribe to trackers.
// Create a set of slice details to track the positions for.
// In this case, slice on symbols and traders:
sequence<string> symbolSlice := ["APMA","MSFT" ];
sequence<string> traderSlice := ["ggekko"];
com.apama.position.PositionConfigParams config :=
new com.apama.position.PositionConfigParams;
config.addParam(openPosConsts.getGenericConsts().
TRACKER_CONFIG_SYMBOL_SLICE, symbolSlice.toString() );
config.addParam(openPosConsts.getGenericConsts().
TRACKER_CONFIG_TRADERID_SLICE, traderSlice.toString() );
 
// Subscribe to the open position tracker you previously created:
psIface.subscribeAndMonitor( "MyFirstOpenPositionTracker",
openPosConsts.TRACKER_TYPE, config, positionChanged,
cbSubscribedWithCallback );
 
// OR, if you do not want to register an update callback,
// call subscribe().
psIface.subscribe( "MySecondOpenPositionTracker",
openPosConsts.TRACKER_TYPE, config, cbSubscribed );
}
 
// This callback action confirms a subscription has been made.
// It provides the details for the original subscription,
// a boolean indicating success/failure and an optional message
// field (primarily used for error strings). Also, this is
// the callback action registered for the subscribeAndMonitor()
// action. You do not need to write an update callback action.
// This action contains the same parameters as the
// cbSubscribed() callback action, but with an extra reference
// Id so you can remove an update callback if required later.
action cbSubscribedWithCallback(
integer subscriptionId,
com.apama.position.SubscriptionDetails subscriptionDetails,
integer updateCallbackRefId, boolean success, string msg ) {
...
}
 
// This callback action is called whenever a position changes.
// It provides the subscriptionID that has been updated, and
// the new position for that subscription.
action positionChanged(
integer subscriptionId, com.apama.position.Position newPosition ) {
// Do something now that the position has been updated
...
}
 
// This callback action confirms a subscription has been made.
// It provides the details for the original subscription,
// a boolean indicating success/failure and an optional message
// field (primarily used for error strings).
action cbSubscribed( integer subscriptionId,
com.apama.position.SubscriptionDetails subscriptionDetails,
boolean success, string msg ) {
// A user-defined update callback can be added to obtain updates for
// position changes. Typically, this needs to be done for custom
// trackers. The action returns a unique ID so that this specific
// update callback can be removed at a later date if required.
integer cbRefId :=
psIface.addUpdateCallback( subscriptionId, positionChanged );
}
}
The following code provides an example of separately tracking currencies.
using com.apama.position.PSInterface;
using com.apama.position.PositionConfigParams;
using com.apama.position.SubscriptionDetails;
 
monitor TrackCurrenciesSeparately {
 
...
action cbCreated(PSInterface psInterface, boolean success, string msg ) {
PositionConfigParams trackerConfig := new PositionConfigParams;
sequence<string> symbolSlice := [ "EUR/USD", "EUR/JPY" ];
trackerConfig.addParam( openPosConsts.getGenericConsts()
.TRACKER_CONFIG_SYMBOL_SLICE, symbolSlice.toString() );
trackerConfig.addParam( openPosConsts.getGenericConsts()
.TRACKER_CONFIG_TRACK_SEPARATE_CURRENCIES, true );
 
// Subscribe to the open position tracker.
// This will also track EUR, USD and JPY separately.
psInterface.subscribe( "MyOpenPositionTracker", openPosConsts.TRACKER_TYPE,
trackerConfig, cbSubscribed );
}
 
action cbSubscribed( integer subId, SubscriptionDetails subDetails,
boolean success, string msg ) {
// Subscribe just to the EUR slice updates
psInterface.addSymbolSliceUpdateCallback(subId, "EUR", callback);
}
...
}
Copyright © 2013-2016 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback