package com.xcong.excoin.modules.okxNewPrice.okxpi.config.impl;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.Dto.SubmitOrderReqDto;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeInfoEnum;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeLoginEventService;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.ExchangeLoginService;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.OKXAccount;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.DefaultUrls;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.HttpMethod;
|
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.OKXContants;
|
import lombok.extern.slf4j.Slf4j;
|
|
import java.math.BigDecimal;
|
import java.util.*;
|
|
@Slf4j
|
public class ExchangeLoginEventServiceImpl implements ExchangeLoginEventService {
|
|
|
private OKXAccount OKXAccount;
|
private String apiKey;
|
private String secretKey;
|
private String passphrase;
|
private boolean accountType;
|
|
public ExchangeLoginEventServiceImpl(String apiKey, String secretKey, String passphrase, boolean accountType) {
|
this.apiKey = apiKey;
|
this.secretKey = secretKey;
|
this.passphrase = passphrase;
|
this.accountType = accountType;
|
OKXAccount = new OKXAccount(
|
accountType ? DefaultUrls.USDM_PROD_URL : DefaultUrls.USDM_UAT_URL,
|
apiKey,
|
secretKey,
|
passphrase,
|
!accountType);
|
}
|
|
@Override
|
public String exchangeInfo(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendPublicRequest(OKXAccount.baseUrl, OKXContants.INSTRUMENTS,parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public String lineHistory(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendPublicRequest(OKXAccount.baseUrl, OKXContants.K_LINE_HISTORY,parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public String balance(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.BALANCE, parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public String positions(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.POSITIONS, parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public String positionsHistory(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.POSITIONS_HISTORY, parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public boolean cancelOrder(String originOrderId,String instId) {
|
LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
|
parameters.put("instId", instId);
|
parameters.put("clOrdId", originOrderId);
|
|
String s = OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.CANCEL_ORDER, parameters, HttpMethod.POST, OKXAccount.isSimluate());
|
log.info("[{}] 收到撤单请求,返回", s);
|
return true;
|
}
|
|
@Override
|
public String submitOrder(SubmitOrderReqDto submitOrderReq) {
|
log.info("收到下单请求,参数:[{}]", submitOrderReq);
|
LinkedHashMap<String, Object> parameters = new LinkedHashMap<>();
|
|
String side = submitOrderReq.getSide();
|
String type = submitOrderReq.getOrdType();
|
String positionSides = submitOrderReq.getPosSide();
|
// 开仓
|
//开多:买多BUY、LONG
|
//开空:卖空SELL、SHORT
|
if ("buy".equals(side)) {
|
if ("limit".equals(type)) {
|
parameters.put("px", submitOrderReq.getPx());
|
parameters.put("ordType", "limit");
|
}
|
// 持仓方向
|
parameters.put("posSide", positionSides);
|
//placeOrderReq.setPosSide(positionSide);
|
// slTriggerPx 止损触发价,如果填写此参数,必须填写 止损委托价
|
// slOrdPx 止损委托价,如果填写此参数,必须填写 止损触发价
|
|
if (new BigDecimal(submitOrderReq.getSlTriggerPx()).compareTo(BigDecimal.ZERO) > 0) {
|
Map<String, Object> attachAlgoOrder = new HashMap<>();
|
// 如果是开多 止损价小于传来的价格
|
|
List<Map<String, Object>> attachAlgoOrds = new ArrayList<>();
|
// 如果是开空 止损价小于传来的价格
|
attachAlgoOrder.put("slTriggerPx", submitOrderReq.getSlTriggerPx());
|
attachAlgoOrder.put("slOrdPx", submitOrderReq.getSlOrdPx());
|
attachAlgoOrds.add(attachAlgoOrder);
|
parameters.put("attachAlgoOrds", attachAlgoOrds);
|
} else {
|
BigDecimal price = new BigDecimal(submitOrderReq.getPrice());
|
BigDecimal stopPrice = BigDecimal.ZERO;
|
if ("buy".equalsIgnoreCase(side)) {
|
stopPrice = price.multiply(new BigDecimal("0.99")).setScale(2,BigDecimal.ROUND_DOWN);
|
} else {
|
stopPrice = price.multiply(new BigDecimal("1.01")).setScale(2,BigDecimal.ROUND_DOWN);
|
}
|
Map<String, Object> attachAlgoOrder = new HashMap<>();
|
// 如果是开多 止损价小于传来的价格
|
List<Map<String, Object>> attachAlgoOrds = new ArrayList<>();
|
// 如果是开空 止损价小于传来的价格
|
attachAlgoOrder.put("slTriggerPx", stopPrice);
|
attachAlgoOrder.put("slOrdPx", stopPrice);
|
attachAlgoOrds.add(attachAlgoOrder);
|
parameters.put("attachAlgoOrds", attachAlgoOrds);
|
}
|
} else {
|
// 平仓
|
//平空:卖空BUY、SHORT
|
//平多:卖多SELL、LONG
|
//side = (CoreEnum.DirectionEnum.D_Buy.getNumber() == direction.getNumber()) ? "BUY" : "SELL";
|
// 平仓方向
|
//positionSide = (CoreEnum.DirectionEnum.D_Buy.getNumber() == direction.getNumber()) ? "SHORT" : "LONG";
|
if ("limit".equals(type)) {
|
//placeOrderReq.setPx(new BigDecimal(submitOrderReq.price()));
|
parameters.put("px", submitOrderReq.getPrice());
|
//parameters.put("timeInForce", timeInForce);
|
}
|
//placeOrderReq.setPosSide(positionSide);
|
parameters.put("posSide", positionSides);
|
}
|
|
//订单种类,市价单不传价格
|
parameters.put("instId", submitOrderReq.getInstId());
|
parameters.put("side", side);
|
//placeOrderReq.setSide(side);
|
parameters.put("ordType", type);
|
//placeOrderReq.setSz(new BigDecimal(quantity));
|
parameters.put("sz", submitOrderReq.getSz());
|
//placeOrderReq.setClOrdId(submitOrderReq.originOrderId());
|
parameters.put("clOrdId", submitOrderReq.getClOrdId());
|
parameters.put("tdMode", submitOrderReq.getTdMode());
|
log.info("下单参数:[{}]",JSON.toJSONString(parameters));
|
String placeOrderRspOkxRestResponse = OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.ORDER, parameters, HttpMethod.POST, OKXAccount.isSimluate());
|
log.info("收到下单返回,响应:[{}]", JSON.parseObject(placeOrderRspOkxRestResponse).get("data"));
|
return submitOrderReq.getClOrdId();
|
}
|
|
@Override
|
public String tickerMess(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.TICKER, parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
@Override
|
public String getOrderMessage(LinkedHashMap<String, Object> parameters) {
|
return OKXAccount.requestHandler.sendSignedRequest(OKXAccount.baseUrl, OKXContants.TICKER, parameters, HttpMethod.GET, OKXAccount.isSimluate());
|
}
|
|
}
|