Apama Capital Markets Foundation Documentation : Capital Markets Foundation : Order Management : Risk firewall : Understanding risk firewalls : Basic example of using a risk firewall
Basic example of using a risk firewall
The following code is a simple example of how to use a risk firewall.
using com.apama.firewall.RiskFirewallFactory;
using com.apama.firewall.RiskFirewall;
using com.apama.firewall.rules.OrderPriceLimitRiskFirewallRule;
using com.apama.firewall.rules.OrderPriceLimitRiskFirewallRuleConsts;
// This sample demonstrates how to create and configure a risk firewall.
// It also demonstrates placing orders through the risk firewall,
// and receiving approved orders from the risk firewall.
// After running this sample, the correlator log displays two approved
// orders received, and the third order is rejected as it would breach
// the price limit that has been set.
monitor BasicRiskFirewallSample {
// Store the main context.
context mainContext := context.current();
action onload() {
// Create the risk firewall in the main context, and
// call it "MyFirewall".
RiskFirewall rfw :=
(new RiskFirewallFactory).create( mainContext, "MyFirewall" );
// Add a callback to receive approved order events from the risk firewall.
// This callback is executed for only new orders. Similar actions are
// provided for adding callbacks for amendments and cancellations.
// This action returns a reference Id that can be used to remove the
// callback later if required.
integer refId1 := rfw.getOrderReceiver().
addAcceptedOrderCallback( cbOnAcceptedOrderReceived );
// Handle any order updates that come back.
integer refId2 := rfw.getOrderSender().
addOrderUpdateCallback( cbOnOrderUpdates );
// Create and register the Order Price Limit default rule class.
rfw.registerRuleClass( (new OrderPriceLimitRiskFirewallRule).create() );
// Add a new instance of the Order Price Limit rule class.
OrderPriceLimitRiskFirewallRuleConsts priceLimitConsts :=
new OrderPriceLimitRiskFirewallRuleConsts;
// Add some basic configuration for the Price Limit rule class.
// In this case, issue a warning when the price goes over 15.0
// and reject orders with a price over 20.0.
Params ruleParams := new Params;
ruleParams.addFloatParam( priceLimitConsts.PRICE_LIMIT_PARAMETER, 20.0 );
ruleParams.addFloatParam( priceLimitConsts.PRICE_WARNING_PARAMETER, 15.0 );
// Add the new rule class instance to the risk firewall.
integer instanceId := rfw.addRuleInstance(
priceLimitConsts.RULE_CLASS_NAME, ruleParams );
// The risk firewall is now set up. Unlock it to allow orders
// through. The risk firewall will not unlock until the Service
// Framework has been activated, and all of the above operations
// have completed.
rfw.unlockCb( cbOnRiskFirewallUnlocked );
// This action is called when the risk firewall has been unlocked.
action cbOnRiskFirewallUnlocked( RiskFirewall rfw ) {
// Send orders to the risk firewall using the OrderSender interface.
// This order will be approved.
rfw.getOrderSender().sendOrder( NewOrder(
10.0, "BUY", "LIMIT", 10,
"TargetMarket","", "TraderA",
new dictionary<string,string> ) );
// This order will issue a warning as it breaches the price
// warning level that has been set, but is under the price limit.
rfw.getOrderSender().sendOrder( NewOrder(
16.0, "BUY", "LIMIT", 10,
"TargetMarket","", "TraderA",
new dictionary<string,string> ) );
// This order will be rejected as it breaches the price limit
// that has been set.
NewOrder( "orderId_3","APMA",
21.0, "BUY", "LIMIT", 10,
"TargetMarket","", "TraderA",
new dictionary<string,string> ) );
// This action is called whenever the risk firewall has approved
// a new order.
action cbOnAcceptedOrderReceived( NewOrder order ) {
log "RECEIVED APPROVED NEW ORDER: "+order.toString();
// This action is called whenever the risk firewall has received
// an update to an order.
action cbOnOrderUpdates( OrderUpdate update ) {
log "RECEIVED ORDER UPDATE: "+update.toString();
Copyright © 2013-2017 Software AG, Darmstadt, Germany. (Innovation Release)

Product LogoContact Support   |   Community   |   Feedback