Contents
This sample application takes a simulated New York Stock Exchange feed and returns two output streams representing the best bid and the best ask, respectively.
The implementation uses a Query Table to track best bids and asks while the application is running. A Query operator reads the bid price and ask price fields of each new tuple and compares it to the current best price and ask price values in the Query Table. It updates the table if the tuple's bid price is higher than the table's best bid, or if the ask price is lower than the current best ask.
The Query operator passes the data along two identical streams into two Filter operators. The IsNewBestAsk filter compares the new tuple's ask price to the best ask value that is stored in the table. A match signifies a new best ask, and the data is passed through a Map operator and emitted on the application's BestAsks output stream. If there is no match, no data is emitted on the output stream for that tuple. The IsNewBestBid filter performs the same work for bids, and the application emits the result on the BestBids output stream.
This topic explains how we built the Best Bids and Asks sample.
For information about running these samples, see the section below.
In StreamBase Studio, import this sample with the following steps:
-
From the top menu, click → .
-
Select this application from the Applications list.
-
Click OK.
StreamBase Studio creates a project for each sample.
By default, the sample files are installed in:
-
On Windows:
C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\bestbidsandasks -
On UNIX:
/opt/streambase/sample/bestbidsandasks
When you load the sample into StreamBase Studio, Studio copies the
sample project's files to your Studio workspace. StreamBase Systems
recommends that you use the workspace copy of the sample, especially on UNIX, where
you may not have write access to /opt/streambase. In
the default installation, the path to this sample in your Studio workspace is:
UNIX: ~/streambase-studio-n.m-workspace/sample_bestbidsandasks Windows XP: C:\Documents and Settings\username\My Documents\StreamBase Studion.mWorkspace\ sample_bestbidsandasks Windows Vista: C:\Users\username\Documents\StreamBase Studion.mWorkspace\ sample_bestbidsandasks
The Best Bids and Asks sample includes the following files:
-
The application,
BestBidsAsks.sbapp -
A feed simulation configuration file,
NYSE.sbfs -
A feed simulation data file,
data.csv
To run this sample:
-
In the Package Explorer, double-click to open the
BestBidsAsks.sbappapplication. Make sure the application is the currently active tab in the EventFlow Editor. -
Click the
Run button. This opens the
SB Test/Debug perspective and starts the application.
-
Open the Feed Simulations tab.
-
Select the
NYSE.sbfsfeed simulation file and click . -
When done, press F9 or click the
Stop Running Application button.
This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.
-
Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.
-
In window 1, launch StreamBase Server on
BestBidsAsks.sbapp:sbd BestBidsAsks.sbapp -
In window 2, enter the following command to dequeue tuples from the server's output streams:
sbc dequeue BestAsks BestBids -
In window 3, run sbfeedsim on the provided configuration file, suppressing the printout of tuples.
sbfeedsim -q NYSE.sbfs -
In window 3, type the following command to terminate the server and dequeuer:
sbadmin shutdown
-
Launched StreamBase Studio
-
Created the sample_bestbidsandasks project.
-
From the top menu, in the SB Authoring perspective, selected → → , chose the sample_bestbidsandasks project and entered
BestBidsAsksfor the diagram name. -
Created an input stream:
-
Dragged an input stream from the palette to the canvas.
-
Clicked the input stream on the canvas, which invoked the Input Stream Properties dialog window.
-
On the General tab, added Name:
NYSE_Feed -
On the Edit Schema tab, added:
-
Field Name:
time, Type:int -
Field Name:
symbol, Size:9, Type:string -
Field Name:
bid_price, Type:double -
Field Name:
bid_size, Type:int -
Field Name:
ask_price, Type:double -
Field Name:
ask_size, Type:int -
Field Name:
sequence, Type:int
-
-
-
Created a Query Table:
-
Dragged a Query Table data construct from the palette to the canvas.
-
Clicked the Query Table icon on the canvas to invoke its Properties view.
-
On the General tab, Name:
Bids_and_Asks -
On the Table Settings tab, Type:
In memory -
On the Edit Schema tab, added:
-
Field Name:
symbol, Type:string, Size:9 -
Field Name:
best_bid, Type:double, Size:8 -
Field Name:
best_ask, Type:double, Size:8
-
-
On the Primary Index tab:
Select the symbol field and click the button to move this field into the Selected Fields window.
-
On the Secondary Index tab:
We did not use a secondary index in this sample.
-
-
Created a Query operator for write operations:
-
Dragged a Query operator from the palette to the canvas.
-
Connected the Bids_and_Asks data construct to the Query operator (this will help fill in some symbol names).
-
Connected the NYSE_Feed input stream to the Query operator
-
Clicked on Query operator.
-
On the General tab, Name:
Update_Bids_and_Asks -
On the Query Settings tab:
-
Operation:
Write -
Where:
Primary IndexThe symbol key is automatically loaded under Matches.
-
Matches:
symbol field Expression:
symbol
-
-
On the Operation Settings tab:
-
Type of Write:
update. -
Values to update:
-
best_bid Expression:
if bid_price > best_bid then bid_price else best_bid -
best_ask Expression:
if ask_price < best_ask then ask_price else best_ask -
If insert fails because no row was found:
Insert new row using new values...
-
-
Values to update:
-
best_bid Expression:
bid_price -
best_ask Expression:
bid_price
-
-
-
On the Output Settings tab:
-
Fields Available from Input Stream, checked:
-
input.time
-
input.symbol
-
input.bid_price
-
input.ask_price
-
-
Fields Available from Query Table, checked:
-
new.best_bid
-
new.best_ask
-
Renamed the outputs to
best_bidandbest_ask, respectively.
-
-
-
-
Created a Filter operator for best asks:
-
Dragged a Filter operator from the palette to the canvas.
-
On the General tab, Name:
IsNewBestAsk -
Connected the Update_Bids_and_Asks output port to this operator.
-
On the Predicate Settings tab, clicked the Add (+) button and entered:
-
Output Port:
1 -
Predicate:
best_ask == ask_price
-
-
-
Created a Map operator for best asks:
-
Dragged a Map operator from the palette to the canvas.
-
On the General tab, Name:
PreserveAskInfo -
Connected the IsNewBestAsk output port to this operator.
-
On the Output Settings tab, clicked the (+) button three times and entered:
-
Output Field Name:
time, Expression:time -
Output Field Name:
symbol, Expression:symbol -
Output Field Name:
best_ask, Expression:best_ask
-
-
-
Created an output stream for best asks:
-
Dragged an output stream from the palette to the canvas.
-
On the General tab, Name:
BestAsks -
Connected the myFilter operator to the StockOut output stream.
-
-
Created a Filter operator for best bids:
-
Dragged a Filter operator from the palette to the canvas.
-
On the General tab, Name:
IsNewBestBid -
Drew a second arc from the Update_Bids_and_Asks output port to this operator (splitting the output of the Query Operator into two identical streams).
-
On the Predicate Settings tab, clicked the Add (+) button and entered:
-
Output Port:
1 -
Predicate:
best_bid == bid_price
-
-
-
Created a Map operator for best bids:
-
Dragged a Map operator from the palette to the canvas.
-
On the General tab, Name:
PreserveBidInfo -
Connected the IsNewBestBid output port to this operator.
-
On the Output Settings tab, clicked the Add (+) button three times and entered:
-
Output Field Name:
time, Expression:time -
Output Field Name:
symbol, Expression:symbol -
Output Field Name:
best_bid, Expression:best_bid
-
-
-
Created an output stream for best asks:
-
Dragged an output stream from the palette to the canvas.
-
On the General tab, Name:
BestBids -
Connected the myFilter operator to the StockOut output stream.
-
