Best Bids and Asks Sample

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.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, click FileLoad StreamBase Sample.

  • Select this application from the Applications list.

  • Click OK.

StreamBase Studio creates a project for each sample.

Sample Location and Files

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

How we Created the Best Bids and Asks Sample

  1. Launched StreamBase Studio

  2. Created the sample_bestbidsandasks project.

  3. From the top menu, in the Authoring perspective, selected FileNewEventFlow Application, chose the sample_bestbidsandasks project and entered BestBidsAsks for the diagram name.

  4. Created an input stream:

    1. Dragged an input stream from the palette to the canvas.

    2. Clicked the input stream on the canvas, which invoked the Input Stream Properties dialog window.

    3. On the General tab, added Name: NYSE_Feed

    4. 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

  5. Created a Query Table:

    1. Dragged a Query Table data construct from the palette to the canvas.

    2. Clicked the Query Table icon on the canvas to invoke its Properties view.

    3. On the General tab, Name: Bids_and_Asks

    4. On the Table Settings tab, Type: In memory

    5. 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

    6. On the Primary Index tab:

      Select the symbol field and click the > button to move this field into the Selected Fields window.

    7. On the Secondary Index tab:

      We did not use a secondary index in this sample.

  6. Created a Query operator for write operations:

    1. Dragged a Query operator from the palette to the canvas.

    2. Connected the Bids_and_Asks data construct to the Query operator (this will help fill in some symbol names).

    3. Connected the NYSE_Feed input stream to the Query operator

    4. Clicked on Query operator.

    5. On the General tab, Name: Update_Bids_and_Asks

    6. On the Query Settings tab:

      • Operation: Write

      • Where: Primary Index

        The symbol key is automatically loaded under Matches.

      • Matches:

        symbol field Expression: symbol

    7. 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

    8. 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_bid and best_ask, respectively.

  7. Created a Filter operator for best asks:

    1. Dragged a Filter operator from the palette to the canvas.

    2. On the General tab, Name: IsNewBestAsk

    3. Connected the Update_Bids_and_Asks output port to this operator.

    4. On the Predicate Settings tab, clicked the Add (+) button and entered:

      • Output Port: 1

      • Predicate: best_ask == ask_price

  8. Created a Map operator for best asks:

    1. Dragged a Map operator from the palette to the canvas.

    2. On the General tab, Name: PreserveAskInfo

    3. Connected the IsNewBestAsk output port to this operator.

    4. 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_ask, Expression: best_ask

  9. Created an output stream for best asks:

    1. Dragged an output stream from the palette to the canvas.

    2. On the General tab, Name: BestAsks

    3. Connected the myFilter operator to the StockOut output stream.

  10. Created a Filter operator for best bids:

    1. Dragged a Filter operator from the palette to the canvas.

    2. On the General tab, Name: IsNewBestBid

    3. 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).

    4. On the Predicate Settings tab, clicked the Add (+) button and entered:

      • Output Port: 1

      • Predicate: best_bid == bid_price

  11. Created a Map operator for best bids:

    1. Dragged a Map operator from the palette to the canvas.

    2. On the General tab, Name: PreserveBidInfo

    3. Connected the IsNewBestBid output port to this operator.

    4. 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

  12. Created an output stream for best asks:

    1. Dragged an output stream from the palette to the canvas.

    2. On the General tab, Name: BestBids

    3. Connected the myFilter operator to the StockOut output stream.

Running the BestBidsandAsks Sample

Running BestBidsAsks.sbapp in StreamBase Studio

To run this sample:

  1. In the Package Explorer, double-click to open the BestBidsAsks.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run StreamBase Application (Default Launch) button. (Not the Run button.) This opens the Test/Debug perspective and starts the application.

  3. Open the Feed Simulations tab.

  4. Select the NYSE.sbfs feed simulation file and click Run.

Running BestBidsAsks.sbapp in Terminal Windows

On UNIX

  1. 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

  2. In the first terminal window, launch the StreamBase server on BestBidsAsks.sbapp:

    sbd BestBidsAsks.sbapp

  3. In the second terminal window, enter the following command to dequeue tuples from the sbd output streams:

    sbc dequeue BestAsks BestBids

  4. In the third terminal window, run sbfeedsim on the provided configuration file, suppressing the printout of tuples.

    sbfeedsim -q NYSE.sbfs

On Windows

  1. 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

  2. In the first command window, launch a StreamBase daemon on BestBidsAsks.sbapp:

    sbd.exe BestBidsAsks.sbapp

  3. In the second terminal window, enter the following command to dequeue tuples from the sbd output streams:

    sbc.exe dequeue BestAsks BestBids

  4. In the third window, run sbfeedsim on the provided configuration file, suppressing the printout of tuples:

    sbfeedsim -q NYSE.sbfs

Back to Top ^