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
The Best Bids and Asks application has the following files:
-
The application,
BestBidsAsks.sbapp -
A feed simulation configuration file,
NYSE.sbfs -
A feed simulation data file,
data.csv
-
Launched StreamBase Studio
-
Created the sample_bestbidsandasks project.
-
From the top menu, in the 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.
-
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 StreamBase Application (Default Launch)
button. (Not the Run button.) This
opens the Test/Debug perspective and starts the application.
-
Open the Feed Simulations tab.
-
Select the
NYSE.sbfsfeed simulation file and click .
-
Open three terminal windows. In each window change directory to the installed Best Bids and Asks sample file location. For example:
cd /opt/streambase/sample/bestbidsandasks -
In the first terminal window, launch the StreamBase server on BestBidsAsks.sbapp:
sbd BestBidsAsks.sbapp -
In the second terminal window, enter the following command to dequeue tuples from the sbd output streams:
sbc dequeue BestAsks BestBids -
In the third terminal window, run sbfeedsim on the provided configuration file, suppressing the printout of tuples.
sbfeedsim -q NYSE.sbfs
-
Open three Command Prompt windows. In each window, change directory to the installed BollingerBand sample file location. For example:
cd C:\Program Files\StreamBase Systems\Streambase.n.m\sample\bestbidsandasks -
In the first command window, launch a StreamBase daemon on BestBidsAsks.sbapp:
sbd.exe BestBidsAsks.sbapp -
In the second terminal window, enter the following command to dequeue tuples from the sbd output streams:
sbc.exe dequeue BestAsks BestBids -
In the third window, run sbfeedsim on the provided configuration file, suppressing the printout of tuples:
sbfeedsim -q NYSE.sbfs
