in simulator/src/main/java/com/google/cloud/simulator/Simulator.java [136:200]
private List<OrderBookEvent> generateOrder() {
long qty = (long)(minQty + (maxQty - minQty) * r.nextDouble());
// Set back to 0.02
if (r.nextDouble() < 0.0) {
buySellBias = r.nextDouble();
if (buySellBias > 0.65)
buySellBias = 0.65;
else if (buySellBias < 0.35)
buySellBias = 0.35;
}
// Adjust midprice to trailing average traded price
if (trailingShares > 0)
midprice = Math.round(trailingSV / trailingShares);
// Adjust buy sell bias by how close we are to the outer edges of trading (+/- 50)
double priceShift = (r.nextDouble() * range) - (range / 2.0);
if (midprice < anchorMidprice) {
priceShift += Math.pow((anchorMidprice - midprice)/50, 2) * r.nextDouble() * 3;
} else {
priceShift -= Math.pow((midprice - anchorMidprice)/50, 2) * r.nextDouble() * 3;
}
long price;
OrderBookEvent.Side side;
if (r.nextDouble() < buySellBias) {
side = OrderBookEvent.Side.BUY;
price = Math.round(midprice + (priceShift - shift));
} else {
side = OrderBookEvent.Side.SELL;
price = Math.round(midprice + (priceShift + shift));
}
// Determine the Order
final Order o = context.newOrder(side, price, qty);
context.add(orderTicks, new Callable<List<OrderBookEvent>>() {
@Override
public List<OrderBookEvent> call() throws Exception {
return generateOrder();
}
});
// Remove the order in the future
context.add(orderTicks + cancelTicks, new Callable<List<OrderBookEvent>>() {
@Override
public List<OrderBookEvent> call() throws Exception {
return m.remove(o);
}
});
// Add the order
List<OrderBookEvent> b = m.add(o);
// Adjust the fills based on execution events
for (OrderBookEvent obe : b) {
if (obe.getQuantityFilled() > 0) {
addExecution(obe.getPrice(), obe.getQuantityFilled());
}
}
// Return the events
return b;
}