quickfix.examples.ordermatch.Market Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quickfixj-examples-ordermatch Show documentation
Show all versions of quickfixj-examples-ordermatch Show documentation
QuickFIX/J example application - Order Match
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact [email protected] if any conditions of this licensing
* are not clear to you.
******************************************************************************/
package quickfix.examples.ordermatch;
import quickfix.field.OrdType;
import quickfix.field.Side;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Market {
private final List bidOrders = new ArrayList<>();
private final List askOrders = new ArrayList<>();
public boolean match(String symbol, List orders) {
while (true) {
if (bidOrders.size() == 0 || askOrders.size() == 0) {
return orders.size() != 0;
}
Order bidOrder = bidOrders.get(0);
Order askOrder = askOrders.get(0);
if (bidOrder.getType() == OrdType.MARKET || askOrder.getType() == OrdType.MARKET
|| (bidOrder.getPrice() >= askOrder.getPrice())) {
match(bidOrder, askOrder);
if (!orders.contains(bidOrder)) {
orders.add(0, bidOrder);
}
if (!orders.contains(askOrder)) {
orders.add(0, askOrder);
}
if (bidOrder.isClosed()) {
bidOrders.remove(bidOrder);
}
if (askOrder.isClosed()) {
askOrders.remove(askOrder);
}
} else
return orders.size() != 0;
}
}
private void match(Order bid, Order ask) {
double price = ask.getType() == OrdType.LIMIT ? ask.getPrice() : bid.getPrice();
long quantity = bid.getOpenQuantity() >= ask.getOpenQuantity() ? ask.getOpenQuantity() : bid.getOpenQuantity();
bid.execute(price, quantity);
ask.execute(price, quantity);
}
public boolean insert(Order order) {
return order.getSide() == Side.BUY ? insert(order, true, bidOrders) : insert(order, false, askOrders);
}
private boolean insert(Order order, boolean descending, List orders) {
if (orders.size() == 0) {
orders.add(order);
} else if (order.getType() == OrdType.MARKET) {
orders.add(0, order);
} else {
for (int i = 0; i < orders.size(); i++) {
Order o = orders.get(i);
if ((descending ? order.getPrice() > o.getPrice() : order.getPrice() < o.getPrice())
&& order.getEntryTime() < o.getEntryTime()) {
orders.add(i, order);
}
}
orders.add(order);
}
return true;
}
public void erase(Order order) {
if (order.getSide() == Side.BUY) {
bidOrders.remove(find(bidOrders, order.getClientOrderId()));
} else {
askOrders.remove(find(askOrders, order.getClientOrderId()));
}
}
public Order find(String symbol, char side, String id) {
return find(side == Side.BUY ? bidOrders : askOrders, id);
}
private Order find(List orders, String clientOrderId) {
for (Order order : orders) {
if (order.getClientOrderId().equals(clientOrderId)) {
return order;
}
}
return null;
}
public void display() {
displaySide(bidOrders, "BIDS");
displaySide(askOrders, "ASKS");
}
private void displaySide(List orders, String title) {
DecimalFormat priceFormat = new DecimalFormat("#.00");
DecimalFormat qtyFormat = new DecimalFormat("######");
System.out.println(title + ":\n----");
for (Order order : orders) {
System.out.println(" $" + priceFormat.format(order.getPrice()) + " "
+ qtyFormat.format(order.getOpenQuantity()) + " " + order.getOwner() + " "
+ new Date(order.getEntryTime()));
}
}
}