Apama Capital Markets Foundation Documentation : Capital Markets Foundation : Analytics : Using the position service framework : Using default position trackers : Creating and subscribing to the realized profit and loss tracker
Creating and subscribing to the realized profit and loss tracker
The default implementation of the realized profit and loss tracker tracks actual profit and loss resulting from order executions and normalized to a specified currency. The order executions match the slice criteria provided in the subscription configuration. The profit and loss values are normalized to a currency that is specified when the tracker is created.
To use the realized profit and loss 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 realized profit and loss tracker you created.
To create an instance of a realized profit and loss tracker, execute the create() action defined in com.apama.position.tracker.RealizedPnLTrackerFactory. This action takes the following 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.
currencyConverter
The currency converter interface to use to obtain values needed to normalize profit and loss values. See Creating and configuring a currency converter.
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 a realized profit and loss 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 realized profit and loss 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 realized profit and loss tracker, specify the tracker type as the constant TRACKER_TYPE string defined in the com.apama.position.tracker.RealizedPnLTrackerConstants event.
*config is a com.apama.position.PositionConfigParams object that contains the configuration for this subscription, such as the slice criteria you want to match orders against.
You must specify a value for the TRACKER_CONFIG_SLICE_CURRENCY parameter, which is defined in RealizedPnLTrackerConstants. The TRACKER_CONFIG_SLICE_CURRENCY parameter specifies the currency this slice is traded in. The realized profit and loss tracker will convert values in the specified currency to U. S. dollars. To convert values to a different currency, specify a value for the TRACKER_CONFIG_NORMALIZE_CURRENCY parameter.
If you do not set a value for the TRACKER_CONFIG_SLICE_CURRENCY parameter then the behavior of the realized profit and loss tracker is the same as it would be for the default open position tracker.
*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 realized profit and loss 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 in the specified normalized currency. In other words, this is the sum of (quantity times price) for each executed order normalized with the exchange rate at the time of execution.
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 in the specified normalized currency.
maxCashPosition
Sum of (quantity times price) for all executed long orders in the specified normalized currency.
The following code shows an example of creating two realized profit and loss trackers in two different contexts.
using com.apama.session.SessionHandlerFactory;
using com.apama.session.SessionHandler;
using com.apama.ccyconverter.CurrencyConverterFactory;
using com.apama.ccyconverter.CurrencyConverter;
using com.apama.cmf.sample.BBAMidPriceExtension;
using.com.apama.position.tracker.RealizedPnLTrackerFactory;
using com.apama.cmf.sample.BBAMidPriceExtension;

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 a session handler for the currency converter to use.
SessionHandler sessionHandler := (new SessionHandlerFactory).connect(
mainContext, "MySession", "MyTransport" );
 
// Create a currency converter connected to the session handler,
// for the realized profit and loss tracker to get FX rates from.
CurrencyConverter currencyConverter :=
(new CurrencyConverterFactory).create( mainContext,
"MyCurrencyConverter",
(new BBAMidPriceExtension).create(sessionHandler) );
 
// Create an instance of the realized profit and loss tracker,
// give it a unique name to be used when subscribing to it,
// and specify the currency converter to get FX rates from.
(new RealizedPnLTrackerFactory).create(mainContext,
"MyFirstRealizedPnLTracker", currencyConverter, cbCreatedTracker );
 
context newCtx := context("TESTCTX", false);
spawn startTest() to newCtx;
}
 
action startTest() {
 
// Connect remotely to the currency converter created in onload()
CurrencyConverter currencyConverter := (CurrencyConverterFactory).connect(
mainContext, "MyCurrencyConverter" );
 
// Create another instance of the realized profit and loss tracker,
// give it a unique name to be used when subscribing to it,
// and specify the currency converter to get FX rates from.
(new RealizedPnLTrackerFactory).create(mainContext,
"MySecondRealizedPnLTracker", cbCreatedTracker, currencyConverter );
}
 
// This action is called after the position tracker instance
// has been created.
action cbCreatedTracker( boolean success, string msg ) {
log "REALIZED PnL TRACKER CREATED: "+success.toString()+" : "+msg;
}
}
The following code shows an example of creating a position service interface and then subscribing to the realized profit and loss trackers created in the previous sample.
using com.apama.position.PSInterface;
using com.apama.position.tracker.RealizedPnLTrackerConstants;
using com.apama.position.PositionConfigParams;
using com.apama.position.PSFactory;
using com.apama.position.Position;
using com.apama.position.SubscriptionDetails;

monitor PositionServicesDemo {
context mainContext := context.current();
 
// To set up the position service framework, you create an
// instance of PSInterface, which provides the user interface.
PSInterface psIface;
 
// A constant string that will be used:
RealizedPnLTrackerConstants realizedPnLConsts;
 
action onload() {
 
// Create a position service configuration object.
// In this case, with no specific configuration.
// Then create an instance of PSInterface:
PositionConfigParams config := new PositionConfigParams;
(new PSFactory).create( mainContext, config, cbCreated );
}
 
action cbCreated( 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"];
 
PositionConfigParams config := new PositionConfigParams;
config.addParam(realizedPnLConsts.getGenericConsts().
TRACKER_CONFIG_SYMBOL_SLICE, symbolSlice.toString() );
config.addParam(realizedPnLConsts.getGenericConsts().
TRACKER_CONFIG_TRADERID_SLICE, traderSlice.toString() );
 
// Specify that the slice currency is US Dollars:
config.addParam(realizedPnLConsts.getGenericConsts().
TRACKER_CONFIG_SLICE_CURRENCY, "USD" );
 
// And that the cash positions should to be normalized to Euros:
config.addParam(realizedPnLConsts.getGenericConsts().
TRACKER_CONFIG_NORMALIZE_CURRENCY, "EUR" );
 
// Subscribe to the realized profit and loss tracker you previously
// created:
psIface.subscribeAndMonitor( "MyFirstRealizedPnLTracker",
realizedPnLConsts.TRACKER_TYPE, config, positionChanged,
cbSubscribedWithCallback );
 
// OR, if you do not want to register an update callback,
// call subscribe().
psIface.subscribe( "MySecondRealizedPnLTracker",
realizedPnLConsts.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,
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, 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,
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 );
}
}
Copyright © 2013-2016 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback