package com.xcong.excoin.trade;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.xcong.excoin.modules.coin.entity.OrderCoinsEntity;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.math.BigDecimal;
|
import java.util.LinkedList;
|
|
/**
|
* 盘口信息
|
*/
|
@Data
|
@Slf4j
|
public class TradePlate {
|
private LinkedList<TradePlateItem> items;
|
//最大深度
|
private int maxDepth = 100;
|
//方向 订单类型 1、买入2、卖出
|
private int direction;
|
private String symbol;
|
public TradePlate(){
|
|
}
|
|
public TradePlate(String symbol,int direction) {
|
this.direction = direction;
|
this.symbol = symbol;
|
items = new LinkedList<>();
|
}
|
|
public boolean add(OrderCoinsEntity exchangeOrder) {
|
//log.info("add TradePlate order={}",exchangeOrder);
|
synchronized (items) {
|
int index = 0;
|
if (exchangeOrder.getTradeType() == OrderCoinsEntity.TRADETYPE_MARKETPRICE) {
|
return false;
|
}
|
if (exchangeOrder.getOrderType() != direction) {
|
return false;
|
}
|
if (items.size() > 0) {
|
for (index = 0; index < items.size(); index++) {
|
TradePlateItem item = items.get(index);
|
if ((exchangeOrder.getTradeType() == OrderCoinsEntity.ORDERTYPE_BUY && item.getPrice().compareTo(exchangeOrder.getEntrustPrice()) > 0)
|
|| (exchangeOrder.getTradeType() == OrderCoinsEntity.ORDERTYPE_SELL && item.getPrice().compareTo(exchangeOrder.getEntrustPrice()) < 0)) {
|
continue;
|
} else if (item.getPrice().compareTo(exchangeOrder.getEntrustPrice()) == 0) {
|
BigDecimal deltaAmount = exchangeOrder.getEntrustCnt().subtract(exchangeOrder.getDealCnt());
|
item.setAmount(item.getAmount().add(deltaAmount));
|
return true;
|
} else {
|
break;
|
}
|
}
|
}
|
if(index < maxDepth) {
|
TradePlateItem newItem = new TradePlateItem();
|
newItem.setAmount(exchangeOrder.getEntrustCnt().subtract(exchangeOrder.getDealCnt()));
|
newItem.setPrice(exchangeOrder.getEntrustPrice());
|
items.add(index, newItem);
|
}
|
}
|
return true;
|
}
|
|
public void remove(OrderCoinsEntity order,BigDecimal amount) {
|
synchronized (items) {
|
//log.info("items>>init_size={},orderPrice={}",items.size(),order.getPrice());
|
for (int index = 0; index < items.size(); index++) {
|
TradePlateItem item = items.get(index);
|
if (item.getPrice().compareTo(order.getEntrustPrice()) == 0) {
|
item.setAmount(item.getAmount().subtract(amount));
|
if (item.getAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
items.remove(index);
|
}
|
//log.info("items>>final_size={},itemAmount={},itemPrice={}",items.size(),item.getAmount(),item.getPrice());
|
return;
|
}
|
}
|
log.info("items>>return_size={}",items.size());
|
}
|
}
|
|
public void remove(OrderCoinsEntity order){
|
remove(order,order.getEntrustCnt().subtract(order.getDealCnt()));
|
}
|
|
public void setItems(LinkedList<TradePlateItem> items){
|
this.items = items;
|
}
|
|
public BigDecimal getHighestPrice(){
|
if(items.size() == 0) {
|
return BigDecimal.ZERO;
|
}
|
if(direction == OrderCoinsEntity.ORDERTYPE_BUY){
|
return items.getFirst().getPrice();
|
}
|
else{
|
return items.getLast().getPrice();
|
}
|
}
|
|
public int getDepth(){
|
return items.size();
|
}
|
|
|
public BigDecimal getLowestPrice(){
|
if(items.size() == 0) {
|
return BigDecimal.ZERO;
|
}
|
if(direction == OrderCoinsEntity.ORDERTYPE_BUY){
|
return items.getLast().getPrice();
|
}
|
else{
|
return items.getFirst().getPrice();
|
}
|
}
|
|
/**
|
* 获取委托量最大的档位
|
* @return
|
*/
|
public BigDecimal getMaxAmount(){
|
if(items.size() == 0) {
|
return BigDecimal.ZERO;
|
}
|
BigDecimal amount = BigDecimal.ZERO;
|
for(TradePlateItem item:items){
|
if(item.getAmount().compareTo(amount)>0){
|
amount = item.getAmount();
|
}
|
}
|
return amount;
|
}
|
|
/**
|
* 获取委托量最小的档位
|
* @return
|
*/
|
public BigDecimal getMinAmount(){
|
if(items.size() == 0) {
|
return BigDecimal.ZERO;
|
}
|
BigDecimal amount = items.getFirst().getAmount();
|
for(TradePlateItem item:items){
|
if(item.getAmount().compareTo(amount) < 0){
|
amount = item.getAmount();
|
}
|
}
|
return amount;
|
}
|
|
public JSONObject toJSON(){
|
JSONObject json = new JSONObject();
|
json.put("direction",direction);
|
json.put("maxAmount",getMaxAmount());
|
json.put("minAmount",getMinAmount());
|
json.put("highestPrice",getHighestPrice());
|
json.put("lowestPrice",getLowestPrice());
|
json.put("symbol",getSymbol());
|
json.put("items",items);
|
return json;
|
}
|
|
public JSONObject toJSON(int limit){
|
JSONObject json = new JSONObject();
|
json.put("direction",direction);
|
json.put("maxAmount",getMaxAmount());
|
json.put("minAmount",getMinAmount());
|
json.put("highestPrice",getHighestPrice());
|
json.put("lowestPrice",getLowestPrice());
|
json.put("symbol",getSymbol());
|
json.put("items",items.size() > limit ? items.subList(0,limit) : items);
|
return json;
|
}
|
}
|