BL Scripting with JavaScript
Introduction
FIXEdge offers a flexible solution for Business Rules purposed to provide routing, transferring, and data manipulations for FIX messages that go through the Business Layer of FIXEdge. In addition to XML Rules, there are two ways to enhance the flexibility of message transferring and data manipulation. Those ways are scripting with JavaScript and XSLT. To use this feature properly, scripts should be written in one of the scripting languages and assigned to the Script instruction of Action section in a Business Rule.
The scripting subsystem gives the user the following advantages:
- Saves time spent on business logic implementation
- Flexibility and simplicity of modification
- Does not bring a significant performance breakdown
FIXEdge uses SpiderMonkey JavaScript engine
Business Rule scripting with JavaScript
It is enough to mention that the script is a JavaScript in the Script tag, in order to run the script and do the proper transformations. This structure facilitates users with all the standard Javascript features and functionality. In addition to this, it enables users to use functions that provide access to the FIX message inside data and allows manipulations with fields and groups as well as whole FIX messages.
JS FIX functionality
A trivial example of a complete JS script for a business rule is shown below:
//take the value the 11th tag from the fix message and get the last 4 characters from it //assign the new value for the tag 11 with format BBBBSSSSCCYYMMDD //where //BBBB - any 4 characters //SSSS - last 4 characters from the old value of field 11 //CC - century //YY- year //MM - month //DD - day field_11 = getStringField(11); if(field_11.length() < 4) return; last4 = field_11.slice(field_11.length() - 4); currDate = getCurrentDateStr(YYYYMMDDUtc); century = currDate.slice(1, 3); century++; result = "bbbb" + last4 + century + currDate.substr(3); setStringField(11, result);
Error handling during script execution
In case of syntax error in script - script execution will be terminated and failed. There is no way to handle syntax errors in script during script execution. User able to stop script execution using throw statement, the result in this case will be the same. User must be able to verify and handle logical errors using verification functions (like isNaN, isMessageValid(), etc) in condition statements.
Logging in JavaScript | |
print(<text>) | |
Example Code print (getCurrentDateStr(DATETIMEUtc)); | Execution of this line results in the following record in the application log: [NOTE] 20131023-08:28:53.651 [4320] [../FixEdge/conf/test.js] - JavaScript '../FixEdge/conf/test.js' output: 20131023-08:28:53 |
Operations with session | |
startSession(<sessionName>)Starts session "sessionName" | |
Example Code startSession("StartByEvent") | Starts the "StartByEvent" session |
startSession(<senderCompId>,<targetCompId>)Starts session where SenderCompID = "senderCompID" and TargetCompID = "targetCompID" | |
Example Code startSession("FIXEdge","SimpleClient") | Starts a session where SenderCompID = "FIXEdge" and TargetCompID = "SimpleClient" |
startSession(<senderCompId>,<targetCompId>,<sessionQualifier>)Starts session where SenderCompID = "senderCompID",TargetCompID = "targetCompID",SessionQualifierValue = "sessionQualifier" | |
Example Code startSession("FIXEdge","SimpleClient","Q1") | Starts a session where SenderCompID = "FIXEdge", TargetCompID = "SimpleClient", SessionQualifierValue = "Q1" |
disconnectSession(<sessionName>,<reason>)Disconnects session "sessionName" with reason "reason" | |
Example Code disconnectSession("DisconnectByEvent","Disconnect by message") | Disconnects the "DisconnectByEvent"session with the "Disconnect by message" reason |
disconnectSession(<senderCompId>,<targetCompId>,<reason>)Disconnects session where SenderCompID = "senderCompId" and TargetCompID = "targetCompId" with reason "reason" | |
Example Code disconnectSession("FIXEdge","SimpleClient","Disconnect by message") | Disconnects a session where SenderCompID = "FIXEdge" and TargetCompID = "SimpleClient" with reason "Disconnect by message" |
disconnectSession(<senderCompId>,<targetCompId>,<sessionQualifier>,<reason>)Starts session where SenderCompID = "senderCompID",TargetCompID = "targetCompID",SessionQualifierValue = "sessionQualifier" with reason "reason" | |
Example Code disconnectSession("FIXEdge","SimpleClient","Disconnect by message","Q1") | Disconnects a session where SenderCompID = "FIXEdge", TargetCompID = "SimpleClient", SessionQualifierValue = "Q1" with reason "Disconnect by message" |
terminateSession(<sessionName>,<reason>)Terminates session "sessionName" with reason "reason" | |
Example Code terminateSession("TerminateByEvent","Terminate by message") | Terminates the "TerminateByEvent" session with the "Terminate by message" reason |
terminateSession(<senderCompId>,<targetCompId>,<reason>)Terminates session where SenderCompID = "senderCompID" and TargetCompID = "targetCompID" with reason "reason" | |
Example Code terminateSession("FIXEdge","SimpleClient","Terminate by message") | Terminates a session where SenderCompID = "FIXEdge" and TargetCompID = "SimpleClient" with reason "Terminate by message" |
terminateSession(<senderCompId>,<targetCompId>,<sessionQualifier>,<reason>)Terminates session where SenderCompID = "senderCompID",TargetCompID = "targetCompID",SessionQualifierValue = "sessionQualifier" | |
Example Code terminateSession("FIXEdge","SimpleClient","Q1","Terminate by message") | Terminates a session where SenderCompID = "FIXEdge", TargetCompID = "SimpleClient", SessionQualifierValue = "Q1" with reason "Terminate by message" |
Please note:
| |
Operations with messages across BL rules | |
setCtxKV(key,value) | |
Example Code setCtxKV("messageID","123") | The function sets the value to the key to transfer information about the message between different BL rules. If the function argument's type is not a string then the function throws an exception. |
getCtxKV(key,[defaultValue]) | |
Example Code var id = getCtxKV("messageID") var id = getCtxKV("messageID","0") | The function gets the value of the key from another BL rule. If the function argument's type is not a string then the function throws an exception. If the default value is specified and there is a key stored in the context then the function returns the value stored for the key. If the specified key is not stored in the context:
|
delCtxKV(key) | |
Example Code delCtxKV("messageID") | The function deletes the value of the key. If the function's argument type is not a string then the function throws an exception. |
Operations with group fields | |
getGroup(<fieldTag>)Returns group handle for <fieldTag> group field | |
Example Code hndl = getGroup(78); isGroupValid(hndl) | Gets a handle on group 78 and tests whether handle 78 is valid |
getGroup(<parentGroupHandle>, <entry>, <fieldTag>)Returns group handle for <fieldTag> group field from <parentGroupHandle>[<entry>] group Numeration of <entry> starts with 0. | |
Example Code hndl = getGroup(552); isGroupValid(hndl); hndl2 = getGroup(hndl, 0, 518); isGroupValid(hndl2); if(hndl2 != hndl){ setNumField(552, 4); } | Gets a handle on group 552 and tests whether the handle 552 is valid Gets a handle from the first record of 552 group on group 518 and tests whether the handle 518 is valid If the group handles are not identical, then increase the number of group 552 elements up to 4 |
removeField(<group handle>, <entry>, <fieldTag>)Removes field from group field Numeration of <entry> starts with 0. | |
Example Code hndl = getGroup(78); isGroupValid(hndl); removeField(hndl, 0, 79); | Gets handle on group 78 and tests whether the handle 78 is valid Removes tag 79 from group 78 |
isGroupValid(<handle>)Returns true or false depending on the handle value | |
Example Code //see the example above | |
Bulk operations with group fieldsAvailable since FIXEdge 6.12.0 | |
bulkSetStringField(<group handle>, <starting entry>, <fieldTag>, <value>, [<max count>])Populates multiple entries' specific tag with constant value, or copies values from list, depending on value type. Numeration of <starting_entry> starts with 0. | |
Example Code 1 setNumField(268, 200); bulkSetStringField(hndl, 0, 269, '0', 100); Example Code 2 bid_qty_list = [10, 20, 30, 40, 50]; | Example 1 Resizes group 268 (NoMDEntries) to 200 Gets a handle on group 268 (NoMDEntries) For entries 0 - 99 set tag 269 (MDEntryType) to '0' ('Bid') For entries 100 - 199 set tag 269 (MDEntryType) to '1' ('offer') This is equivalent to code: for (var i = 0; i < 100; i++) { Example 2 Creates a bid_qty_list variable with 5 elements and ask_qty_list with 3 elements Resizes group 268 (NoMDEntries) to 8 (total size of 2 lists) Gets a handle on group 268 (NoMDEntries) For entries 0-4, copy values from bid_qty_list, so that entry[i][271] = bid_qty_list[i] For entries 5-7, copy values from ask_qty_list, so that entry[5+i][271] = ask_qty_list[i] This is equivalent to code: for (var i = 0; i < bid_qty_list.length; i++) { |
bulkSetStringFieldMapped(<group handle>, <starting entry>, <fieldTag>, <source list>, <key> [, <max count>])Populates multiple entries' specific tag by copying values from nested elements of source Javascript array. Numeration of <starting entry> starts with 0. Numeration of <source list> starts with 0. | |
Example Code bids = [ [0.1, 10], [0.2, 20], [0.3, 30], [0.4, 40], [0.5, 50] ] ; For alternative source structure bids = [ {"price": 0.1, "qty": 10}, | Creates a bids variable with 5 nested pairs of elements, and ask_qty_list with 3 pairs. Resizes group 268 (NoMDEntries) to 8 (total size of 2 lists) Gets a handle on group 268 (NoMDEntries) For entries 0-4, copy Price(270) and Size(271) values from bids; For entries 5-7, copy Price(270) and Size(271) values from asks. This is equivalent to code: for (var i = 0; i < bid_qty_list.length; i++) { For alternative source structure for (var i = 0; i < bid_qty_list.length; i++) { |
Operations with message fields | |
removeField(<fieldTag>)Removes field from message | |
Example Code removeField(58); | Removes tag 58 from the message |
createReject("session")Creating reject | |
Example code createReject("Target42sohSender42"); | Creating reject and sending status message 35=3 |
isSupportedField(<fieldTag>)Returns true if the field is defined in the dictionary, i.e. it can be presented in the message. False means the tag is not expected in this message type | |
Example Code:
| The example shows how to remove tag 58 from the message if it is defined in the dictionary The script checks if tag 58 is defined for the message |
isSupportedField( <group handle> , <fieldTag>)Returns true if the field is defined in the dictionary for the specific group, i.e. it can be presented in the repeating group of the message. False means the tag is not expected in this specific group for this message type | |
Example Code
| The example shows how to remove non required tag 2376 from the Parties group entry #0. The script gets the repeating group 448 handle. |
getNumField(<fieldTag>)Returns numeric value of the <fieldTag> field | |
Example Code getNumField(78); | Returns a numeric value of field 78 |
getNumField(<group handle>, <entry>, <fieldTag>)Returns number value of the <fieldTag> field from group <group handle>[<entry>] |