Skip to content

Instantly share code, notes, and snippets.

@mikeb01
Created December 22, 2011 15:33
Show Gist options
  • Save mikeb01/1510699 to your computer and use it in GitHub Desktop.
Save mikeb01/1510699 to your computer and use it in GitHub Desktop.
Tracked Execution for an Order
public class ExecutionTracking implements OrderEventListener, ExecutionEventListener
{
private final Session tradingSession;
final Map<Long, Order> lastOrderStateByInstructionId = new HashMap<Long, Order>();
Order currentOrder = null;
long currentFilledQuantity;
long currentCancelledQuantity;
public ExecutionTracking(final Session tradingSession)
{
this.tradingSession = tradingSession;
}
/**
* The order event will always arrive before the associated execution events for that order.
*/
public void notify(final Order order)
{
final Order previousOrderState = lastOrderStateByInstructionId.get(order.getInstructionId());
if (null != previousOrderState)
{
// Track the current filled/cancelled quantity as a delta between this
// order event and the previous one of the same order.
currentFilledQuantity = previousOrderState.getFilledQuantity().longValue();
currentCancelledQuantity = previousOrderState.getCancelledQuantity().longValue();
}
else
{
// The is the first order event for this order, so start from zero.
currentFilledQuantity = 0;
currentCancelledQuantity = 0;
}
currentOrder = order;
}
/**
*
*/
public void notify(final Execution execution)
{
// As we receive the executions for the order increment the quantities for each execution
currentFilledQuantity += execution.getQuantity().longValue();
currentCancelledQuantity += execution.getCancelledQuantity().longValue();
// Once our per execution tracking of the order matches the totals on the
// order itself, we've found the last execution for a given order event.
if (currentOrder.getFilledQuantity().longValue() == currentFilledQuantity &&
currentOrder.getCancelledQuantity().longValue() == currentCancelledQuantity)
{
System.out.printf("Last Execution: %s for order: %s%n", execution, currentOrder);
if (isComplete(execution.getOrder()))
{
// The order has completed, all quantity is filled or cancelled, so remove the order from
lastOrderStateByInstructionId.remove(currentOrder.getInstructionId());
}
else
{
// Track the order event for the next match
lastOrderStateByInstructionId.put(currentOrder.getInstructionId(), currentOrder);
}
currentOrder = null;
}
}
private void isComplete(Order order)
{
long completedQuantity = order.getFilledQuantity().longValue() + order.getCancelledQuantity().longValue();
return order.getQuantity().longValue() == completedQuantity;
}
public void subscribe()
{
// Listen for both Order and Execution events
tradingSession.registerOrderEventListener(this);
tradingSession.registerExecutionEventListener(this);
// Only subscription to execution events is required
tradingSession.subscribe(new ExecutionSubscriptionRequest(), executionSubscriptionRequestCallBack);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment