Functional requirement of currency exchange system development:
1.Digital currency exchange:The currency exchange system provides exchange services between digital currencies,and many digital currencies can be traded between these exchanges.
2.Asset Custody:The exchange provides users with a digital currency wallet,in which their digital currency assets are placed for trading and asset storage.
3.Trading matching:The Coin Exchange provides trading matching services to match the orders of buyers and sellers,connecting the two parties involved in the transaction.
4.Market analysis:Exchanges can also provide market charts and analysis tools for digital currencies,helping users understand market dynamics,trends,and price change
关于撮合交易引擎
采用内存撮合的方式进行,以开发做撮合订单信息传输,MongoDB持久化订单成交明细,MySQL记录订单总体成交。其中行情模块主要负责订单成交持久化、行情生成、行情推送等服务,包括:
K线数据,间隔分别为:1分钟、5分钟、15分钟、30分钟、1小时、1天、1周、1月
所有交易对的市场深度(market depth)数据
所有交易对的最新价格
最近成交的交易对
内存撮合交易支持的模式
限价订单与限价订单撮合
市价订单与限价订单撮合
限价订单与市价订单撮合
市价订单与市价订单撮合
数字货币交易所系统开发示例代码如下:
/**
*限价委托单与市价队列匹配
*param mpList市价对手单队列
*param focusedOrder交易订单
*/
public void matchLimitPriceWithMPList(LinkedList<ExchangeOrder>mpList,ExchangeOrder focusedOrder){
List<ExchangeTrade>exchangeTrades=new ArrayList<>();
List<ExchangeOrder>completedOrders=new ArrayList<>();
synchronized(mpList){
Iterator<ExchangeOrder>iterator=mpList.iterator();
while(iterator.hasNext()){
ExchangeOrder matchOrder=iterator.next();
ExchangeTrade trade=processMatch(focusedOrder,matchOrder);
logger.info(">>>>>"+trade);
if(trade!=null){
exchangeTrades.add(trade);
}
//判断匹配单是否完成,市价单amount为成交量
if(matchOrder.isCompleted()){
iterator.remove();
completedOrders.add(matchOrder);
}
//判断吃单是否完成,判断成交量是否完成
if(focusedOrder.isCompleted()){
//交易完成
completedOrders.add(focusedOrder);
//退出循环
break;
}
}
}
//如果还没有交易完,订单压入列表中
if(focusedOrder.getTradedAmount().compareTo(focusedOrder.getAmount())<0){
addLimitPriceOrder(focusedOrder);
}
//每个订单的匹配批量推送
handleExchangeTrade(exchangeTrades);
orderCompleted(completedOrders);
}
/**
*市价委托单与限价对手单列表交易
*param lpList限价对手单列表
*param focusedOrder待交易订单
*/
public void matchMarketPriceWithLPList(TreeMap<BigDecimal,MergeOrder>lpList,ExchangeOrder focusedOrder){
List<ExchangeTrade>exchangeTrades=new ArrayList<>();
List<ExchangeOrder>completedOrders=new ArrayList<>();
synchronized(lpList){
Iterator<Map.Entry<BigDecimal,MergeOrder>>mergeOrderIterator=lpList.entrySet().iterator();
boolean exitLoop=false;
while(!exitLoop&&mergeOrderIterator.hasNext()){
Map.Entry<BigDecimal,MergeOrder>entry=mergeOrderIterator.next();
MergeOrder mergeOrder=entry.getValue();
Iterator<ExchangeOrder>orderIterator=mergeOrder.iterator();
while(orderIterator.hasNext()){
ExchangeOrder matchOrder=orderIterator.next();
//处理匹配
ExchangeTrade trade=processMatch(focusedOrder,matchOrder);
if(trade!=null){
exchangeTrades.add(trade);
}
//判断匹配单是否完成
if(matchOrder.isCompleted()){
//当前匹配的订单完成交易,删除该订单
orderIterator.remove();
completedOrders.add(matchOrder);
}
//判断焦点订单是否完成
if(focusedOrder.isCompleted()){
completedOrders.add(focusedOrder);
//退出循环
exitLoop=true;
break;
}
}
if(mergeOrder.size()==0){
mergeOrderIterator.remove();
}
}
}
//如果还没有交易完,订单压入列表中,市价买单按成交量算
if(focusedOrder.getDirection()==ExchangeOrderDirection.SELL&&focusedOrder.getTradedAmount().compareTo(focusedOrder.getAmount())<0
||focusedOrder.getDirection()==ExchangeOrderDirection.BUY&&focusedOrder.getTurnover().compareTo(focusedOrder.getAmount())<0){
addMarketPriceOrder(focusedOrder);
}
//每个订单的匹配批量推送
handleExchangeTrade(exchangeTrades);
if(completedOrders.size()>0){
orderCompleted(completedOrders);
TradePlate plate=focusedOrder.getDirection()==ExchangeOrderDirection.BUY?sellTradePlate:buyTradePlate;
sendTradePlateMessage(plate);
}
}