package fauxExchange; import java.util.Random; // OrderRunner :: A class that represents a sequence of trades on a particular security // on our exchange... public class OrderRunner extends Thread { private Exchange e; private int[] securityIds; private double[] openingPrices; private double threshhold; private int marketSeed; /** * @marketSeed An integer for use in determining if orders are at Market Price * The number of market orders will be one per the marketSeed on average. * @threshhold The threshhold of change that is allowed * */ public OrderRunner(Exchange e, int[] securityIds, double[] openingPrices, double threshhold, int marketSeed) { this.e = e; this.securityIds = securityIds; this.openingPrices = openingPrices; this.threshhold = threshhold; this.marketSeed = marketSeed; this.setDaemon(true); } public void run () { Random r = new Random(); r.setSeed(System.currentTimeMillis()); Order o = null; int i = 0; while(true){ try { sleep(50); }catch(Exception e){ } // Size is 2000 shares or less int size = Math.abs(r.nextInt()) % 2000; int securityIndex = Math.abs(r.nextInt()) % securityIds.length; // Price is within threshhold from the openingPrice double price = Math.round((openingPrices[securityIndex] + (r.nextInt() % (openingPrices[securityIndex] * (threshhold))))*10.0)/10.0; // Participant code int participant = Math.abs(r.nextInt()); boolean isMarket = ((r.nextInt()%marketSeed) == 0); int securityId = securityIds[securityIndex]; if (r.nextInt() % 2 == 0) o = new BuyOrder(size, price, participant, isMarket, securityId); else o = new SellOrder(size, price, participant, isMarket, securityId); // o.print(null);b try { e.insertOrder(o); }catch(InvalidSecurityIdException isie){ isie.printStackTrace(); } // if (i++ > 100) break; } } public static void main (String [] args) throws Exception { // Create the exchange System.out.println("Creating Exchange..."); Exchange myExchange = new Exchange(); // Create the Segments in this Exchange System.out.println("Creating Segments..."); Segment [] segs = new Segment[1]; int[] securityIds = new int[5]; double[] openingPrices = new double[5]; segs[0] = new Segment(myExchange, 1); for (int i = 0; i < 5; i++){ // Create a Security System.out.println("Creating Security: " + (i+1)); Security sec = new Security(i + 1, segs[0]); securityIds[i] = i+1; // Create an OrderBook for this Security System.out.println("Creating an OrderBook for this Security..."); OrderBook ob = new OrderBook(sec); ob.initializePrice(94.00+i, 98.50+(i+2)); openingPrices[i] = ob.getOpeningPrice(); ob.startWorker(); myExchange.registerOrderBook(ob); } // Add these Segments to the Exchange System.out.println("Setting Segments in Exchange..."); myExchange.setSegments(segs); myExchange.openLog(); myExchange.setLogTrades(true); OrderRunner or = new OrderRunner(myExchange, securityIds, openingPrices, 0.1, 10); or.start(); Thread.currentThread().sleep(100000); } }