From c5faaa1111091280365100c95e7e06930b98ee4b Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 23 Dec 2025 16:21:44 +0800
Subject: [PATCH] feat(indicator): 添加15分钟交易策略和指标详细说明
---
src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java | 439 ++++++++++++++++++++++++++++++------------------------
1 files changed, 241 insertions(+), 198 deletions(-)
diff --git a/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java b/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
index 050b2fa..304ad4c 100644
--- a/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/okxNewPrice/celue/CaoZuoServiceImpl.java
@@ -2,6 +2,8 @@
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
+import com.xcong.excoin.modules.okxNewPrice.indicator.strategy.CoreTechnicalStrategy;
+import com.xcong.excoin.modules.okxNewPrice.indicator.strategy.TechnicalIndicatorStrategy;
import com.xcong.excoin.modules.okxNewPrice.okxWs.*;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.CoinEnums;
import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.OrderParamEnums;
@@ -36,6 +38,83 @@
private final WangGeListService wangGeListService;
private final RedisUtils redisUtils;
+ private final TechnicalIndicatorStrategy technicalIndicatorStrategy;
+
+ @Override
+ public TradeRequestParam caoZuoStrategy(String accountName, String markPx, String posSide) {
+ TradeRequestParam tradeRequestParam = new TradeRequestParam();
+ tradeRequestParam.setAccountName(accountName);
+ tradeRequestParam.setInstId(CoinEnums.HE_YUE.getCode());
+ tradeRequestParam.setTdMode(CoinEnums.CROSS.getCode());
+ tradeRequestParam.setPosSide(posSide);
+ tradeRequestParam.setOrdType(CoinEnums.ORDTYPE_MARKET.getCode());
+
+ log.info("操作账户:{},当前价格: {},仓位方向: {}", accountName,markPx,posSide);
+ /**
+ * 准备工作
+ * 1、准备好下单的基本信息
+ */
+ // 系统设置的开关,等于冷静中,则代表不开仓
+ String outStr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.OUT.name());
+ if (OrderParamEnums.OUT_YES.getValue().equals(outStr)){
+ log.error("冷静中,不允许下单......");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+ BigDecimal cashBal = WsMapBuild.parseBigDecimalSafe(AccountWs.getAccountMap(accountName).get("cashBal"));
+ /**
+ * 判断止损抗压
+ */
+ BigDecimal realKuiSunAmount = WsMapBuild.parseBigDecimalSafe(AccountWs.getAccountMap(accountName).get("upl"));
+ log.info("实际盈亏金额: {}", realKuiSunAmount);
+ String zhiSunPercent = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.ZHI_SUN.name());
+ BigDecimal zhiSunAmount = cashBal.multiply(new BigDecimal(zhiSunPercent));
+ log.info("预期亏损金额: {}", zhiSunAmount);
+ String kangYaPercent = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.KANG_CANG.name());
+ BigDecimal kangYaAmount = cashBal.multiply(new BigDecimal(kangYaPercent));
+ log.info("预期抗仓金额: {}", kangYaAmount);
+
+ if (realKuiSunAmount.compareTo(BigDecimal.ZERO) < 0){
+ realKuiSunAmount = realKuiSunAmount.multiply(new BigDecimal("-1"));
+ // 账户预期亏损金额比这个还小时,立即止损
+ if (realKuiSunAmount.compareTo(zhiSunAmount) > 0){
+ log.error("账户冷静止损......");
+ WsMapBuild.saveStringToMap(InstrumentsWs.getAccountMap(accountName), CoinEnums.OUT.name(), OrderParamEnums.OUT_YES.getValue());
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_YES.getValue());
+ return caoZuoZhiSunEvent(accountName, markPx, posSide);
+ }
+ // 判断抗压
+ if (realKuiSunAmount.compareTo(kangYaAmount) > 0 && realKuiSunAmount.compareTo(zhiSunAmount) <= 0){
+ log.error("账户紧张扛仓......");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+ }
+
+ String positionAccountName = PositionsWs.initAccountName(accountName, posSide);
+ // 判断是否保证金超标
+ if (PositionsWs.getAccountMap(positionAccountName).get("imr") == null){
+ log.error("没有获取到持仓信息,等待初始化......");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+ BigDecimal ordFrozImr = PositionsWs.getAccountMap(positionAccountName).get("imr");
+ BigDecimal totalOrderUsdt = WsMapBuild.parseBigDecimalSafe(AccountWs.getAccountMap(accountName).get(CoinEnums.TOTAL_ORDER_USDT.name()))
+ .divide(new BigDecimal("2"), RoundingMode.DOWN);
+ if (ordFrozImr.compareTo(totalOrderUsdt) >= 0){
+ log.error("已满仓......");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+
+ if (PositionsWs.getAccountMap(positionAccountName).get("pos") == null){
+ log.error("没有获取到持仓信息,等待初始化......");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_YES.getValue());
+ return chooseEvent(tradeRequestParam);
+ }
/**
* 执行主要的操作逻辑,包括读取合约状态、获取市场价格信息,
@@ -171,30 +250,31 @@
public TradeRequestParam caoZuoInitEvent(String accountName, String markPx, String posSide) {
log.info("当前网格初始化:操作账户:{},当前价格: {},仓位方向: {}", accountName,markPx,posSide);
- /**
- * 初始化订单请求参数
- * 获取仓位数量
- * 获取仓位方向
- */
+
+ // 根据技术指标策略获取开仓信号
+ TradeRequestParam strategySignal = technicalIndicatorStrategy.getSignal(accountName, markPx, posSide);
+
+ // 如果策略返回无信号或无效信号,则不进行开仓
+ if (strategySignal == null || "NO_SIGNAL".equals(strategySignal.getTradeType())) {
+ log.info("技术指标策略无开仓信号,不进行初始化开仓");
+ TradeRequestParam tradeRequestParam = new TradeRequestParam();
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
+ }
+
+ // 使用策略信号进行开仓
TradeRequestParam tradeRequestParam = new TradeRequestParam();
tradeRequestParam.setAccountName(accountName);
tradeRequestParam.setMarkPx(markPx);
tradeRequestParam.setInstId(CoinEnums.HE_YUE.getCode());
tradeRequestParam.setTdMode(CoinEnums.CROSS.getCode());
- tradeRequestParam.setPosSide(posSide);
+ tradeRequestParam.setPosSide(strategySignal.getPosSide());
tradeRequestParam.setOrdType(CoinEnums.ORDTYPE_MARKET.getCode());
tradeRequestParam.setTradeType(OrderParamEnums.TRADE_YES.getValue());
- String side = null;
- if (CoinEnums.POSSIDE_LONG.getCode().equals(posSide)){
- side = CoinEnums.SIDE_BUY.getCode();
- }
- if (CoinEnums.POSSIDE_SHORT.getCode().equals(posSide)){
- side = CoinEnums.SIDE_SELL.getCode();
- }
- tradeRequestParam.setSide(side);
+ tradeRequestParam.setSide(strategySignal.getSide());
- String clOrdId = WsParamBuild.getOrderNum(side);
+ String clOrdId = WsParamBuild.getOrderNum(strategySignal.getSide());
tradeRequestParam.setClOrdId(clOrdId);
String sz = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.BUY_CNT_INIT.name());
tradeRequestParam.setSz(sz);
@@ -235,98 +315,80 @@
BigDecimal markPx = new BigDecimal(markPxStr);
BigDecimal avgPx = PositionsWs.getAccountMap(positionAccountName).get("avgPx");
log.info("持仓价格: {}, 当前价格:{},匹配队列中......", avgPx, markPx);
- // 初始化网格队列
- PriorityBlockingQueue<AscBigDecimal> queueAsc = WangGeListQueue.getQueueAsc();
- PriorityBlockingQueue<DescBigDecimal> queueKaiCang = wangGeListService.initKaiCang(avgPx, queueAsc);
- PriorityBlockingQueue<AscBigDecimal> queuePingCang = wangGeListService.initPingCang(avgPx, queueAsc);
-
- // 处理订单价格在队列中的情况
- String orderPrice = OrderInfoWs.getAccountMap(accountName).get("orderPrice");
- log.info("上一次网格触发价格: {}", orderPrice);
- handleOrderPriceInQueues(orderPrice, queueKaiCang, queuePingCang);
- // 判断是加仓还是减仓
- if (avgPx.compareTo(markPx) > 0) {
- log.info("开始买入开多...");
- if (!queueKaiCang.isEmpty()) {
- DescBigDecimal kaiCang = queueKaiCang.peek();
- log.info("买入开多队列价格{}", kaiCang.getValue());
- if (kaiCang != null && markPx.compareTo(kaiCang.getValue()) <= 0 && avgPx.compareTo(kaiCang.getValue()) >= 0) {
- log.info("开始买入开多...买入开多队列价格价格大于当前价格{}>{}", kaiCang.getValue(), markPx);
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- String side = CoinEnums.SIDE_BUY.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- String sz = buyCntTimeLongEvent(accountName, avgPx, markPx);
- tradeRequestParam.setSz(sz);
- log.info("买入开多参数准备成功......");
- } else {
- log.info("未触发加仓......,等待");
- }
- }else{
- // 队列为空
- log.info("超出了网格设置...");
- }
- } else if (avgPx.compareTo(markPx) < 0) {
- log.info("开始卖出平多...");
- if (!queuePingCang.isEmpty()) {
- AscBigDecimal pingCang = queuePingCang.peek();
- log.info("卖出平多队列价格:{}", pingCang.getValue());
- if (pingCang != null && avgPx.compareTo(pingCang.getValue()) < 0) {
- log.info("开始卖出平多...卖出平多队列价格大于开仓价格{}>{}", pingCang.getValue(), avgPx);
- // 手续费
- BigDecimal feeValue = PositionsWs.getAccountMap(positionAccountName).get("fee").multiply(new BigDecimal(2));
- //未实现收益
- BigDecimal uplValue = PositionsWs.getAccountMap(positionAccountName).get("upl");
- //已实现收益
- BigDecimal realizedPnlValue = PositionsWs.getAccountMap(positionAccountName).get("realizedPnl");
- realizedPnlValue = realizedPnlValue.add(feeValue);
-
- //持仓保证金
- BigDecimal imr = PositionsWs.getAccountMap(positionAccountName).get("imr");
- String pingCangImr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.PING_CANG_SHOUYI.name());
- BigDecimal imrValue = imr.multiply(new BigDecimal(pingCangImr));
-
- if (realizedPnlValue.compareTo(BigDecimal.ZERO) <= 0) {
- BigDecimal realizedPnlValueZheng = realizedPnlValue.multiply(new BigDecimal("-1"));
- if (uplValue.compareTo(realizedPnlValue) > 0 && uplValue.compareTo(imrValue.add(realizedPnlValueZheng)) >= 0) {
- log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(realizedPnlValueZheng));
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- String side = CoinEnums.SIDE_SELL.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
- tradeRequestParam.setSz(String.valueOf( sz));
- log.info("卖出平多参数准备成功......");
- }else{
- log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(realizedPnlValueZheng));
- }
- }else {
- if (uplValue.compareTo(imrValue.add(feeValue)) >= 0) {
- log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(feeValue));
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- String side = CoinEnums.SIDE_SELL.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
- tradeRequestParam.setSz(String.valueOf( sz));
- log.info("卖出平多参数准备成功......");
- }else{
- log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(feeValue));
- }
- }
- } else {
- log.info("未触发减仓......,等待");
- }
- }else{
- // 队列为空
- log.info("超出了网格设置...");
- }
- } else {
- log.info("价格波动较小......,等待");
+
+ // 根据技术指标策略获取交易信号
+ TradeRequestParam strategySignal = technicalIndicatorStrategy.getSignal(accountName, markPxStr, posSide);
+
+ // 如果策略返回无信号,则不进行操作
+ if (strategySignal == null || "NO_SIGNAL".equals(strategySignal.getTradeType())) {
+ log.info("技术指标策略无信号,不进行操作");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
}
+
+ // 根据策略信号进行操作
+ log.info("技术指标策略信号: {}, 执行相应操作", strategySignal.getTradeType());
+
+ // 处理平仓信号
+ if (CoinEnums.SIDE_SELL.getCode().equals(strategySignal.getSide())) {
+ log.info("开始卖出平多...");
+ // 手续费
+ BigDecimal feeValue = PositionsWs.getAccountMap(positionAccountName).get("fee").multiply(new BigDecimal(2));
+ //未实现收益
+ BigDecimal uplValue = PositionsWs.getAccountMap(positionAccountName).get("upl");
+ //已实现收益
+ BigDecimal realizedPnlValue = PositionsWs.getAccountMap(positionAccountName).get("realizedPnl");
+ realizedPnlValue = realizedPnlValue.add(feeValue);
+
+ //持仓保证金
+ BigDecimal imr = PositionsWs.getAccountMap(positionAccountName).get("imr");
+ String pingCangImr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.PING_CANG_SHOUYI.name());
+ BigDecimal imrValue = imr.multiply(new BigDecimal(pingCangImr));
+
+ if (realizedPnlValue.compareTo(BigDecimal.ZERO) <= 0) {
+ BigDecimal realizedPnlValueZheng = realizedPnlValue.multiply(new BigDecimal("-1"));
+ if (uplValue.compareTo(realizedPnlValue) > 0 && uplValue.compareTo(imrValue.add(realizedPnlValueZheng)) >= 0) {
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(realizedPnlValueZheng));
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ tradeRequestParam.setSide(CoinEnums.SIDE_SELL.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_SELL.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
+ tradeRequestParam.setSz(String.valueOf( sz));
+ log.info("卖出平多参数准备成功......");
+ }else{
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(realizedPnlValueZheng));
+ }
+ }else {
+ if (uplValue.compareTo(imrValue.add(feeValue)) >= 0) {
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(feeValue));
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ tradeRequestParam.setSide(CoinEnums.SIDE_SELL.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_SELL.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
+ tradeRequestParam.setSz(String.valueOf( sz));
+ log.info("卖出平多参数准备成功......");
+ }else{
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(feeValue));
+ }
+ }
+ }
+ // 处理加仓信号
+ else if (CoinEnums.SIDE_BUY.getCode().equals(strategySignal.getSide()) && avgPx.compareTo(markPx) > 0) {
+ log.info("开始买入加仓...");
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ tradeRequestParam.setSide(CoinEnums.SIDE_BUY.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_BUY.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ String sz = buyCntTimeLongEvent(accountName, avgPx, markPx);
+ tradeRequestParam.setSz(sz);
+ log.info("买入加仓参数准备成功......");
+ }
+ else {
+ log.info("未触发操作......,等待");
+ }
+
return tradeRequestParam;
} catch (NumberFormatException e) {
log.error("开多方向异常", e);
@@ -348,99 +410,80 @@
BigDecimal markPx = new BigDecimal(markPxStr);
BigDecimal avgPx = PositionsWs.getAccountMap(positionAccountName).get("avgPx");
log.info("持仓价格: {}, 当前价格:{},匹配队列中......", avgPx, markPx);
-
- // 初始化网格队列
- PriorityBlockingQueue<AscBigDecimal> queueAsc = WangGeListQueue.getQueueAsc();
- PriorityBlockingQueue<DescBigDecimal> queueKaiCang = wangGeListService.initKaiCang(avgPx, queueAsc);
- PriorityBlockingQueue<AscBigDecimal> queuePingCang = wangGeListService.initPingCang(avgPx, queueAsc);
-
- // 处理订单价格在队列中的情况
- String orderPrice = OrderInfoWs.getAccountMap(accountName).get("orderPrice");
- log.info("上一次网格触发价格:{}", orderPrice);
- handleOrderPriceInQueues(orderPrice, queueKaiCang, queuePingCang);
- // 判断是加仓还是减仓
- if (avgPx.compareTo(markPx) > 0) {
- log.info("开始买入平空...");
- if (!queueKaiCang.isEmpty()) {
- DescBigDecimal kaiCang = queueKaiCang.peek();
- log.info("买入平空队列价格{}", kaiCang.getValue());
- if (kaiCang != null && avgPx.compareTo(kaiCang.getValue()) >= 0) {
- log.info("开始买入平空...买入平空队列价格小于开仓价格{}<{}", kaiCang.getValue(), avgPx);
-
- // 手续费
- BigDecimal feeValue = PositionsWs.getAccountMap(positionAccountName).get("fee");
- //未实现收益
- BigDecimal uplValue = PositionsWs.getAccountMap(positionAccountName).get("upl");
- //已实现收益
- BigDecimal realizedPnlValue = PositionsWs.getAccountMap(positionAccountName).get("realizedPnl");
- realizedPnlValue = realizedPnlValue.add(feeValue);
-
- //持仓保证金
- BigDecimal imr = PositionsWs.getAccountMap(positionAccountName).get("imr");
- String pingCangImr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.PING_CANG_SHOUYI.name());
- BigDecimal imrValue = imr.multiply(new BigDecimal(pingCangImr));
-
- if (realizedPnlValue.compareTo(BigDecimal.ZERO) <= 0) {
- BigDecimal realizedPnlValueZheng = realizedPnlValue.multiply(new BigDecimal("-1"));
- if (uplValue.compareTo(realizedPnlValue) > 0 && uplValue.compareTo(imrValue.add(realizedPnlValueZheng)) >= 0) {
- log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(realizedPnlValueZheng));
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- String side = CoinEnums.SIDE_BUY.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
- tradeRequestParam.setSz(String.valueOf( sz));
- log.info("买入平空参数准备成功......");
- }else{
- log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(realizedPnlValueZheng));
- }
- }else {
- if (uplValue.compareTo(imrValue.add(feeValue)) >= 0) {
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(feeValue));
- String side = CoinEnums.SIDE_BUY.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
- tradeRequestParam.setSz(String.valueOf( sz));
- log.info("买入平空参数准备成功......");
- }else{
- log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(feeValue));
- }
- }
- } else {
- log.info("未触发减仓......,等待");
- }
- }else{
- log.info("开始减仓,但是超出了网格设置...");
- }
- } else if (avgPx.compareTo(markPx) < 0) {
- log.info("开始卖出开空...");
- if (!queuePingCang.isEmpty()) {
- AscBigDecimal pingCang = queuePingCang.peek();
- log.info("上限队列价格: {}", pingCang.getValue());
- if (pingCang != null && markPx.compareTo(pingCang.getValue()) >= 0 && avgPx.compareTo(pingCang.getValue()) < 0) {
- log.info("开始加仓...上限队列价格小于当前价格{}<={}", pingCang.getValue(), markPx);
- WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
- String side = CoinEnums.SIDE_SELL.getCode();
- tradeRequestParam.setSide(side);
- String clOrdId = WsParamBuild.getOrderNum(side);
- tradeRequestParam.setClOrdId(clOrdId);
- String sz = buyCntTimeShortEvent(accountName, avgPx, markPx);
- tradeRequestParam.setSz(sz);
- log.info("卖出开空参数准备成功......");
- } else {
- log.info("未触发加仓......,等待");
- }
- }else{
- // 队列为空
- log.info("超出了网格设置...");
- }
- } else {
- log.info("价格波动较小......,等待");
+
+ // 根据技术指标策略获取交易信号
+ TradeRequestParam strategySignal = technicalIndicatorStrategy.getSignal(accountName, markPxStr, posSide);
+
+ // 如果策略返回无信号,则不进行操作
+ if (strategySignal == null || "NO_SIGNAL".equals(strategySignal.getTradeType())) {
+ log.info("技术指标策略无信号,不进行操作");
+ tradeRequestParam.setTradeType(OrderParamEnums.TRADE_NO.getValue());
+ return tradeRequestParam;
}
+
+ // 根据策略信号进行操作
+ log.info("技术指标策略信号: {}, 执行相应操作", strategySignal.getTradeType());
+
+ // 处理平仓信号
+ if (CoinEnums.SIDE_BUY.getCode().equals(strategySignal.getSide())) {
+ log.info("开始买入平空...");
+ // 手续费
+ BigDecimal feeValue = PositionsWs.getAccountMap(positionAccountName).get("fee");
+ //未实现收益
+ BigDecimal uplValue = PositionsWs.getAccountMap(positionAccountName).get("upl");
+ //已实现收益
+ BigDecimal realizedPnlValue = PositionsWs.getAccountMap(positionAccountName).get("realizedPnl");
+ realizedPnlValue = realizedPnlValue.add(feeValue);
+
+ //持仓保证金
+ BigDecimal imr = PositionsWs.getAccountMap(positionAccountName).get("imr");
+ String pingCangImr = InstrumentsWs.getAccountMap(accountName).get(CoinEnums.PING_CANG_SHOUYI.name());
+ BigDecimal imrValue = imr.multiply(new BigDecimal(pingCangImr));
+
+ if (realizedPnlValue.compareTo(BigDecimal.ZERO) <= 0) {
+ BigDecimal realizedPnlValueZheng = realizedPnlValue.multiply(new BigDecimal("-1"));
+ if (uplValue.compareTo(realizedPnlValue) > 0 && uplValue.compareTo(imrValue.add(realizedPnlValueZheng)) >= 0) {
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(realizedPnlValueZheng));
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ tradeRequestParam.setSide(CoinEnums.SIDE_BUY.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_BUY.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
+ tradeRequestParam.setSz(String.valueOf( sz));
+ log.info("买入平空参数准备成功......");
+ }else{
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(realizedPnlValueZheng));
+ }
+ }else {
+ if (uplValue.compareTo(imrValue.add(feeValue)) >= 0) {
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ log.info("当前未实现盈亏:{}大于预计收益>{},赚钱咯", uplValue, imrValue.add(feeValue));
+ tradeRequestParam.setSide(CoinEnums.SIDE_BUY.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_BUY.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ BigDecimal sz = PositionsWs.getAccountMap(positionAccountName).get("pos");
+ tradeRequestParam.setSz(String.valueOf( sz));
+ log.info("买入平空参数准备成功......");
+ }else{
+ log.info("当前未实现盈亏:{}没有大于预计收益>{},钱在路上了", uplValue, imrValue.add(feeValue));
+ }
+ }
+ }
+ // 处理加仓信号
+ else if (CoinEnums.SIDE_SELL.getCode().equals(strategySignal.getSide()) && avgPx.compareTo(markPx) < 0) {
+ log.info("开始卖出加仓...");
+ WsMapBuild.saveStringToMap(OrderInfoWs.getAccountMap(accountName), "orderPrice", String.valueOf(markPx));
+ tradeRequestParam.setSide(CoinEnums.SIDE_SELL.getCode());
+ String clOrdId = WsParamBuild.getOrderNum(CoinEnums.SIDE_SELL.getCode());
+ tradeRequestParam.setClOrdId(clOrdId);
+ String sz = buyCntTimeShortEvent(accountName, avgPx, markPx);
+ tradeRequestParam.setSz(sz);
+ log.info("卖出加仓参数准备成功......");
+ }
+ else {
+ log.info("未触发操作......,等待");
+ }
+
return tradeRequestParam;
} catch (NumberFormatException e) {
log.error("开空方向异常", e);
--
Gitblit v1.9.1