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

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 Studio n.m Workspace\
      sample_bestbidsandasks
Windows Vista:
  C:\Users\username\Documents\StreamBase Studio n.m Workspace\
      sample_bestbidsandasks

This Sample's Files

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

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 button. This opens the SB Test/Debug perspective and starts the application.

  3. Open the Feed Simulations tab.

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

  5. When done, press F9 or click the Stop Running Application button.

Running BestBidsAsks.sbapp in Terminal Windows

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.

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

  2. In window 1, launch StreamBase Server on BestBidsAsks.sbapp:

    sbd BestBidsAsks.sbapp

  3. In window 2, enter the following command to dequeue tuples from the server's output streams:

    sbc dequeue BestAsks BestBids

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

    sbfeedsim -q NYSE.sbfs

  5. In window 3, type the following command to terminate the server and dequeuer:

    sbadmin shutdown

Back to Top ^

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

Back to Top ^