QuickfixJ: data source simulator

Before getting started with QuickfixJ, in order to simulate market data on the broker side, we need some sort of data source that provides bid and ask prices on demand.  We could use historical data, but in order to keep things interesting lets use randomly generated data.  We will create an object titled MarketIntelligence, which the broker uses to generate quotes for customers.

Symbols on the Hong Kong Stock Exchange are based on numbers and not letters like NYSE, which makes the job of generating the symbols on the fly very easy.  We’ll limit the set of symbols to 1 through 1000, and prices will be updated every 200ms based on java.util.Timer timeouts.  Prices will randomly generated in the range of 0 to 500, and will be increased or decreased in small increments to simulate the movement of the security on the stock exchange.  The code for the MarketIntelligence object is sweet and simple:

import java.util.Random;
import java.util.Map;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

public class MarketIntelligence
{
    private static final int maxPrice = 500;
    private Random generator;
    private HashMap bids;
    private HashMap asks;

    MarketIntelligence()
    {
        bids = new HashMap();
        asks = new HashMap();
        generator = new Random();

        // Generate some random symbols
        Integer basePrice;
        Double decimal, price;
        String symbol, bid_price, ask_price;
        for( int index=1; index<1000; index++)
        {
            basePrice = generator.nextInt(maxPrice);
            decimal = generator.nextDouble();
            price = basePrice.doubleValue() + decimal;
            symbol = String.format("%04d", index);
            bid_price = String.format("%.2f",price);
            ask_price = String.format("%.2f",price+0.01);
            bids.put(symbol, bid_price);
            asks.put(symbol, ask_price);
        }

        // Update market data every 200 ms
        Timer timer = new Timer();
        TimerTask task = new TimerTask(){
             public void run(){
                 update();
             }
        };
        timer.scheduleAtFixedRate( task, 0, 200 );

    }

    public String getBid(String symbol)
    {
        return bids.get(symbol);
    }

    public String getAsk(String symbol)
    {
        return asks.get(symbol);
    }

    public void update()
    {
        Double current_bid, current_ask, new_bid, new_ask;
        Double delta, price;
        Integer up_dn;
        String current_key;

        for(Map.Entry entry : bids.entrySet())
        {
            current_key = entry.getKey();
            current_bid = Double.parseDouble(bids.get(current_key));
            current_ask = Double.parseDouble(asks.get(current_key));

            delta = generator.nextDouble();
            up_dn = generator.nextInt() % 2;
            delta = up_dn.doubleValue() * delta;
            new_bid = current_bid + delta;
            new_ask = new_bid + 0.1;

            bids.put(current_key, String.format("%.2f", new_bid));
            asks.put(current_key, String.format("%.2f", new_ask));
        }
    }
}

The following simple test class shows how we can query the MarketIntelligence object to get the ask price for symbol 0001.

class TestMarketIntelligence
{
    public static void main(String args[]) throws InterruptedException
    {
        MarketIntelligence mi = new MarketIntelligence();
        while( true ){
             String ask = mi.getAsk("0001");
             System.out.println(ask);
             Thread.sleep(1000);
        }
    }
}

Executing the test module, we can verify that the ask prices are moving up and down and in not too drastic of a rise and fall (so we don’t scare our customers).

trantor:quickfix_demo$ javac -classpath . MarketIntelligence.java
trantor:quickfix_demo$ javac -classpath . TestMarketIntelligence.java
trantor:quickfix_demo$ java -cp . TestMarketIntelligence
315.60
314.84
315.60
314.43
312.53
311.86
312.02
312.77
312.93
312.20
311.81
312.69
312.09
310.41
311.16
311.47
310.88

While MarketIntelligence is not so intelligent, with a data source for the broker we can now build the broker’s order management system (OMS).  Stay tuned for more updates!

Advertisements

5 thoughts on “QuickfixJ: data source simulator

  1. Hello, I’m new in quickFIX/J and trying to learn from your examples. They are very interesting, thanks for it.

    I have a problem in the following line: for(Map.Entry entry : bids.entrySet())

    Eclipse tells me: Type mismatch: cannot convert from element type Object to Map.Entry

    Can you help me to figure out which is the problems in order to test your code and learn from it.

    Thank you so much.

    Regards,
    Juan Miguel

    • You are correct, I upgraded by Java and I get the same errors you have found. Unfortunately, at this time I don’t have a fix for it. I’m not much of a Java programmer, so until I can find some free time I don’t have a solution.

      For now, you could skip using the market data and just use hard coded values if you want to move along with learning Quickfix/J. I just wrote that module to make things more interesting. Shame is is borked now!

  2. you may use these:

    Set<Map.Entry> mBids = bids.entrySet();

    for(Map.Entry entry : mBids){
    ……………..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s