Skip to content

Commit

Permalink
Support MarketDataIncrementalRefresh message.
Browse files Browse the repository at this point in the history
  • Loading branch information
sutra committed Apr 22, 2017
1 parent ce392f9 commit fbfa44d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@
</message>
<message name="MarketDataIncrementalRefresh" msgtype="X" msgcat="app">
<field name="MDReqID" required="N"/>
<field name="Symbol" required="Y"/>
<group name="NoMDEntries" required="Y">
<field name="MDUpdateAction" required="Y"/>
<field name="DeleteReason" required="N"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.AccountInfo;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.OrderBookUpdate;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trade;
import org.knowm.xchange.dto.trade.LimitOrder;
Expand All @@ -30,11 +31,15 @@
import quickfix.field.MDEntryPx;
import quickfix.field.MDEntrySize;
import quickfix.field.MDEntryType;
import quickfix.field.MDUpdateAction;
import quickfix.field.MDUpdateType;
import quickfix.field.NoMDEntries;
import quickfix.field.OrigTime;
import quickfix.field.SendingTime;
import quickfix.field.Side;
import quickfix.field.SubscriptionRequestType;
import quickfix.field.Symbol;
import quickfix.fix44.MarketDataIncrementalRefresh;
import quickfix.fix44.MarketDataSnapshotFullRefresh;

/**
Expand All @@ -46,6 +51,8 @@ public class OKCoinXChangeApplication extends OKCoinApplication {

private final Map<String, String> mdReqIds = new HashMap<>();

private volatile OrderBook orderBook;

public OKCoinXChangeApplication(String apiKey, String secretKey) {
super(apiKey, secretKey);
}
Expand Down Expand Up @@ -108,15 +115,17 @@ public void unsubscribeOrderBook(CurrencyPair currencyPair, SessionID sessionId)
unsubscribeOrderBook(OKCoinFIXAdapters.adaptSymbol(currencyPair), sessionId);
}

/**
* {@inheritDoc}
*/
@Override
public void onMessage(MarketDataSnapshotFullRefresh message,
SessionID sessionId) throws FieldNotFound, UnsupportedMessageType,
IncorrectTagValue {
public void onMessage(final MarketDataSnapshotFullRefresh message,
final SessionID sessionId)
throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
Date origTime = message.getField(new OrigTime()).getValue();
String symbol = message.getSymbol().getValue();
String mdReqId = message.isSetMDReqID() ? message.getMDReqID().getValue() : null;
String[] symbols = symbol.split("/");
CurrencyPair currencyPair = new CurrencyPair(symbols[0], symbols[1]);
CurrencyPair currencyPair = toCurrencyPair(symbol);

log.debug("OrigTime: {}", origTime);
log.debug("Symbol: {}, currency pair: {}", symbol, currencyPair);
Expand All @@ -135,7 +144,7 @@ public void onMessage(MarketDataSnapshotFullRefresh message,
char type = group.getChar(MDEntryType.FIELD);
BigDecimal px = group.getField(new MDEntryPx()).getValue();
BigDecimal size = group.isSetField(MDEntrySize.FIELD) ? group.getField(new MDEntrySize()).getValue() : null;
log.debug("type: {}, px: {}, size: {}", type, px, size);
log.trace("{} {}@{}", type, size, px);

switch (type) {
case MDEntryType.BID:
Expand Down Expand Up @@ -197,6 +206,79 @@ public void onMessage(MarketDataSnapshotFullRefresh message,
}
}

/**
* {@inheritDoc}
*/
@Override
public void onMessage(final MarketDataIncrementalRefresh message,
final SessionID sessionId)
throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
final Date date = message.getHeader().getUtcTimeStamp(SendingTime.FIELD);
final String symbol = message.getField(new Symbol()).getValue();
final CurrencyPair currencyPair = toCurrencyPair(symbol);

for (int i = 1, l = message.getNoMDEntries().getValue(); i <= l; i++) {
final Group group = message.getGroup(i, NoMDEntries.FIELD);

final char action = group.getChar(MDUpdateAction.FIELD);
final char type = group.getChar(MDEntryType.FIELD);
final BigDecimal px = group.getDecimal(MDEntryPx.FIELD);
final BigDecimal size = group.getDecimal(MDEntrySize.FIELD);

log.trace("{} {} {}@{}", action, type, size, px);

final LimitOrder limitOrder;

switch (type) {
case MDEntryType.BID:
limitOrder = new LimitOrder.Builder(OrderType.BID, currencyPair).limitPrice(px).tradableAmount(size).timestamp(date).build();
break;
case MDEntryType.OFFER:
limitOrder = new LimitOrder.Builder(OrderType.ASK, currencyPair).limitPrice(px).tradableAmount(size).timestamp(date).build();
break;
default:
log.warn("Unsupported MDEntryType: {}.", type);
limitOrder = null;
break;
}

if (limitOrder != null && this.orderBook != null) {
switch (action) {
case MDUpdateAction.NEW:
this.orderBook.update(limitOrder);
break;
case MDUpdateAction.CHANGE:
this.orderBook.update(limitOrder);
break;
case MDUpdateAction.DELETE:
final List<LimitOrder> orders = this.orderBook.getOrders(limitOrder.getType());
final int idx = Collections.binarySearch(orders, limitOrder);
if (idx >= 0) {
final LimitOrder oldLimitOrder = orders.get(idx);
if (!oldLimitOrder.getLimitPrice().equals(limitOrder.getLimitPrice())) {
log.error("FIXME: The order book has been changed.");
} else {
final BigDecimal newSize = oldLimitOrder.getTradableAmount().subtract(limitOrder.getTradableAmount());
final OrderBookUpdate orderBookUpdate = new OrderBookUpdate(oldLimitOrder.getType(), oldLimitOrder.getTradableAmount(), oldLimitOrder.getCurrencyPair(), oldLimitOrder.getLimitPrice(), oldLimitOrder.getTimestamp(), newSize);
this.orderBook.update(orderBookUpdate);
}
} else {
log.trace("{} {}@{} was not found.", limitOrder.getType(), limitOrder.getTradableAmount(), limitOrder.getLimitPrice());
}
break;
default:
log.warn("Unsupported MDUpdateAction {}.", action);
break;
}
}
}

onOrderBook(this.orderBook, sessionId);
}

/**
* {@inheritDoc}
*/
@Override
public void onMessage(AccountInfoResponse message, SessionID sessionId)
throws FieldNotFound, UnsupportedMessageType, IncorrectTagValue {
Expand Down Expand Up @@ -237,8 +319,8 @@ public void onOrderBook(Date origTime, List<LimitOrder> asks, List<LimitOrder> b
// asks should be sorted by limit price ascending
Collections.sort(asks);

OrderBook orderBook = new OrderBook(origTime, asks, bids);;
onOrderBook(orderBook, sessionId);
this.orderBook = new OrderBook(origTime, asks, bids);;
onOrderBook(this.orderBook, sessionId);
}

/**
Expand All @@ -259,4 +341,10 @@ public void onTicker(Ticker ticker) {
public void onAccountInfo(AccountInfo accountInfo, SessionID sessionId) {
}

private CurrencyPair toCurrencyPair(String symbol) {
String[] symbols = symbol.split("/");
CurrencyPair currencyPair = new CurrencyPair(symbols[0], symbols[1]);
return currencyPair;
}

}

0 comments on commit fbfa44d

Please sign in to comment.