Contents
This sample demonstrates how to use capture fields in conjunction with parent schemas to create a reusable module that can match orders for FX trading in one instance and can match orders for Equities trading in another copy of the identical, unchanged module. The module uses abstract schemas for its input stream and for its Query Table that become concrete schemas in actual use.
This sample consists of the following files:
File | Purpose |
---|---|
SharedSchemas.sbint
|
A StreamBase interface file that defines named schemas and a table schema used by the two EventFlow modules. |
TopLevelFX.sbapp
|
A simple EventFlow module used in this sample as the top-level outer module.
It accepts a stream of foreign exchange (FX) trade orders and sends them to
the OrderMatcher.sbapp inner module for
processing. This module has an output stream that reports filled orders, and
a second input stream that triggers a read-all-rows operation of the inner
module's Query Table.
|
OrderMatcher.sbapp
|
An EventFlow module that implements an asset-agnostic order matching engine,
used in this sample as the inner module. This module matches parties who
would like to sell assets with parties who would like to buy those assets.
Notice that there are no FX-specific schemas in this module. The input stream and Query Table are both defined with abstract schemas that include capture fields, which means the module can handle any generic traded asset. This allows the same module to be re-used to handle different asset classes. |
TestFxOrderMatching.java
|
A StreamBase test file in the src/test/java/ directory. Run this file
as a StreamBase unit test to verify that the sample accepts input and returns
the expected results. Select the file, right-click, and invoke > .
|
The important lessons of this sample are in the six named schemas and one table
schema defined in SharedSchemas.sbint
. The alphabetic
order of the schemas in the interface file is also the best order to study them and
understand how they work.
Named Schema | Description |
---|---|
AbstractInstrument |
Consists of a single capture field named placeholder with data type placeholderFields .
|
AbstractOrder |
Consists of a number of fields for data that is in orders for all instrument
types, plus a tuple field named Instrument
consisting of a single sub-field, the capture field with data type
placeholderFields . This schema is used in the
OrderMatcher.sbapp module's input stream, which
allows copies of that module to handle orders for varying asset types.
|
EquitiesInstrument |
A schema for an equities trade instrument, consisting of parent schema
AbstractInstrument plus a single Symbol field.
|
EquitiesOrder |
Consists of one parent schema, AbstractOrder ,
plus one concrete override field that replaces the abstract field of the same
name. The Instrument field with data type
EquitiesInstrument overrides the abstract
Instrument field with type AbstractInstrument because and only because it has the exact
same field name, case sensitive.
Notice that the Fields grid that defines this schema uses seven rows to
define six fields. This is because the |
FXInstrument |
A schema for a foreign exchange trade instrument, consisting of parent schema
AbstractInstrument plus two string fields to
hold the two currencies being traded.
|
FXOrder |
Like EquitiesOrder above, consists of one parent
schema, AbstractOrder , plus one concrete
override field that replaces the abstract field of the same name. The
Instrument field with data type FXInstrument overrides the abstract Instrument field with type AbstractInstrument because it has the exact same field name,
case sensitive.
|
Table Schema | Description |
---|---|
AbstractOrderBookSchema |
A schema for Query Tables that uses the AbstractOrder named schema and defines keys for it.
It is important to understand that a Query Table can only use a concrete
schema at runtime. Using the |
In StreamBase Studio, import this sample with the following steps:
-
From the top-level menu, select
> . -
Enter
capture field
to narrow the list of options. -
Select Capture fields and parent schemas from the Data Constructs and Operators category.
-
Click
.
StreamBase Studio creates a single project containing the sample files.
Follow these steps to run the provided unit test in StreamBase Studio:
-
In the Project Explorer view, navigate to
src/test/java/
. Select thepackage-name
TestFxOrderMatching.java
file. (You can also open the file for inspection, but this is not required.) -
Right-click the
TestFxOrderMatching.java
file, and from the context menu, invoke > . -
In the JUnit view, observe the test running to completion, with a solid green bar indicating success.
-
Edit and re-run the test file as desired to test different scenarios.
-
In the Project Explorer view, open this sample's folder.
Keep an eye on the bottom right status bar of the Studio window. Make sure any
Updating
,Downloading
,Building
, orRebuild project
messages finish before you proceed. -
Open the
src/main/eventflow/
folder.packageName
-
Double-click to open the
TopLevelFX.sbapp
module. Make sure the module is the currently active tab in the EventFlow Editor. -
Click the Run button. This opens the SB Test/Debug perspective and starts the module.
-
Wait for the Waiting for fragment to initialize message to clear.
-
Open the Manual Input view and select the Orders stream. Notice that the
Instrument
tuple field shows only the concreteCurrency1
andCurrency2
fields. The capture field that comprised the original abstractInstrument
field was overridden by the concreteInstrument
field. This replaced the capture field that has data typeplaceholderFields
with the concrete fieldsCurrency1
andCurrency2
. -
Enqueue two tuples to the Orders stream in the Manual Input view:
Field Tuple 1 Tuple 2 Id 1 2 Instrument.Currency1 USD USD Instrument.Currency2 CAD CAD Price 1.0525 1.0564 IsBid no yes Quantity 1000 1000 Party A B -
Observe a matching order emitted from the Fills stream in the Output Streams view.
-
Enter other tuples as desired. Remember that no order emits from the Fills stream until there is a match between IsBid=no and IsBid=yes tuples.
-
When done, press F9 or click the Terminate EventFlow Fragment button.
To understand how the inner module can be re-used to handle a different asset type, perform the following additional steps:
-
Open the
TopLevelFX.sbapp
module. -
Select all six components and invoke Ctrl+C, Ctrl+V to make a second copy of the module on the same canvas. The copied components take the same names as their counterparts with "Copy" appended.
-
Double-click the OrdersCopy input stream to open its Properties view.
-
In the Edit Schema tab, select the EquitiesOrder schema in the dropdown control. This is the only step necessary to convert the OrderMatcherRefCopy module to process order matching for equities orders instead of FX orders.
-
Save the module and click the Run button.
-
In the Manual Input view, select the OrdersCopy stream. Notice that the
Instrument
tuple field now shows only a single field,Symbol
. -
Enqueue two tuples to the OrdersCopy stream in the Manual Input view:
Field Tuple 1 Tuple 2 Id 10 20 Instrument.Symbol GOOG GOOG Price 580.50 580.75 IsBid no yes Quantity 50 50 Party AA BB -
Observe a matching order emitted from the FillsCopy stream in the Output Streams view.
-
Enter other tuples as desired, matching IsBid=no and IsBid=yes tuples to see output from the FillsCopy stream.
-
When done, press F9 or click the Terminate EventFlow Fragment button.
When you load the sample into StreamBase® Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.
Important
Load this sample in StreamBase® Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.
Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:
studio-workspace
/sample_CaptureParentSchemas
See Default Installation
Directories for the default location of studio-workspace
on your system.