package com.xcong.excoin.modules.okxApi; import java.math.BigDecimal; /** * OKX 交易模块全局配置 — 盈利回收策略的参数入口。 * *

通过 Builder 模式将所有运行参数集中管理,避免策略参数散落在多个类中。 * *

参数分类

* * * * * * * * *
类别参数用途
认证apiKey, apiSecret, passphraseREST/WS 签名认证
交易标的contract, leverage, baseQuantity合约、杠杆、张数
持仓marginMode, positionMode全仓/逐仓、双向持仓
风控maxLoss, maxPositionSize最大亏损阈值、单边持仓上限
盈亏计算contractMultiplier, priceScale, unrealizedPnlPriceMode合约乘数、价格精度、计价模式
策略核心profitTriggerRatio, reinvestRatio盈利触发比例、再投资比例
* * @author Administrator */ public class OkxConfig { /** * 未实现盈亏(unrealizedPnl)计价模式。 */ public enum PnLPriceMode { /** 按最新成交价计算未实现盈亏 */ LAST_PRICE, /** 按标记价格计算未实现盈亏 */ MARK_PRICE } // ==================== 认证信息 ==================== private final String apiKey; private final String apiSecret; private final String passphrase; // ==================== 交易标的 ==================== private final String contract; private final String leverage; private final String marginMode; private final String positionMode; // ==================== 策略参数 ==================== /** 基底开仓张数(初始化时多空各开的张数,如 "10") */ private final String baseQuantity; /** 最大亏损阈值(USDT),触发后策略停止 */ private final BigDecimal maxLoss; /** 是否为生产环境 */ private final boolean isProduction; /** 合约乘数(单张合约代表的基础资产数量,如 BTC-USDT-SWAP=0.01) */ private final BigDecimal contractMultiplier; /** 价格精度(交易所价格的小数位数) */ private final int priceScale; /** 未实现盈亏计价模式 */ private final PnLPriceMode unrealizedPnlPriceMode; // ==================== 盈利回收策略参数 ==================== /** 盈利触发比例(默认0.5=50%),ROI=未实现盈亏/保证金达到此值时触发平仓 */ private final BigDecimal profitTriggerRatio; /** 再投资比例(默认0.5=50%),收益A中用于开反方向仓位的比例 */ private final BigDecimal reinvestRatio; // ==================== 风控参数 ==================== /** 反向仓位倍数上限(默认10),反方向最大持仓 = baseQuantity × 此值 */ private final int maxPositionMultiplier; /** 权益重置比例(默认0.05=5%),账户权益 ≥ 初始权益 × (1+此值) 时全平重置 */ private final BigDecimal equityRestartRatio; // ==================== 构造器 ==================== private OkxConfig(Builder builder) { this.apiKey = builder.apiKey; this.apiSecret = builder.apiSecret; this.passphrase = builder.passphrase; this.contract = builder.contract; this.leverage = builder.leverage; this.marginMode = builder.marginMode; this.positionMode = builder.positionMode; this.baseQuantity = builder.baseQuantity; this.maxLoss = builder.maxLoss; this.isProduction = builder.isProduction; this.contractMultiplier = builder.contractMultiplier; this.priceScale = builder.priceScale; this.unrealizedPnlPriceMode = builder.unrealizedPnlPriceMode; this.profitTriggerRatio = builder.profitTriggerRatio; this.reinvestRatio = builder.reinvestRatio; this.maxPositionMultiplier = builder.maxPositionMultiplier; this.equityRestartRatio = builder.equityRestartRatio; } // ==================== REST/WS 地址 ==================== /** @return REST API 基础路径 */ public String getRestBasePath() { return "https://openapi.okx.com"; } /** @return WebSocket 公开频道地址 */ public String getWsPublicUrl() { return isProduction ? "wss://ws.okx.com:8443/ws/v5/public" : "wss://wspap.okx.com:8443/ws/v5/public"; } /** @return WebSocket 私有频道地址 */ public String getWsPrivateUrl() { return isProduction ? "wss://ws.okx.com:8443/ws/v5/private" : "wss://wspap.okx.com:8443/ws/v5/private"; } // ==================== 认证信息 ==================== public String getApiKey() { return apiKey; } public String getApiSecret() { return apiSecret; } public String getPassphrase() { return passphrase; } // ==================== 交易标的 ==================== public String getContract() { return contract; } public String getLeverage() { return leverage; } // ==================== 持仓配置 ==================== public String getMarginMode() { return marginMode; } public String getPositionMode() { return positionMode; } // ==================== 策略参数 ==================== public String getBaseQuantity() { return baseQuantity; } public BigDecimal getMaxLoss() { return maxLoss; } // ==================== 盈亏计算 ==================== public BigDecimal getContractMultiplier() { return contractMultiplier; } public int getPriceScale() { return priceScale; } public PnLPriceMode getUnrealizedPnlPriceMode() { return unrealizedPnlPriceMode; } // ==================== 盈利回收策略参数 ==================== public BigDecimal getProfitTriggerRatio() { return profitTriggerRatio; } public BigDecimal getReinvestRatio() { return reinvestRatio; } // ==================== 风控参数 ==================== /** @return 反向仓位倍数上限,反方向最大持仓 = baseQuantity × 此值 */ public int getMaxPositionMultiplier() { return maxPositionMultiplier; } /** @return 权益重置比例,账户权益 ≥ 初始权益 × (1+此值) 时全平重置 */ public BigDecimal getEquityRestartRatio() { return equityRestartRatio; } // ==================== 环境 ==================== public boolean isProduction() { return isProduction; } // ==================== Builder ==================== public static Builder builder() { return new Builder(); } /** * OkxConfig 的流式构造器。 * *

必填项

* {@code apiKey}、{@code apiSecret}、{@code passphrase} 必须设置。 * *

默认值

* BTC-USDT-SWAP / 100x / cross / long_short_mode / baseQuantity=10 / * maxLoss=15 / profitTriggerRatio=0.5 / reinvestRatio=0.5 / * maxPositionMultiplier=10 / equityRestartRatio=0.05 */ public static class Builder { private String apiKey; private String apiSecret; private String passphrase; private String contract = "BTC-USDT-SWAP"; private String leverage = "100"; private String marginMode = "cross"; private String positionMode = "long_short_mode"; private String baseQuantity = "10"; private BigDecimal maxLoss = new BigDecimal("15"); private boolean isProduction = false; private BigDecimal contractMultiplier = new BigDecimal("0.01"); private int priceScale = 2; private PnLPriceMode unrealizedPnlPriceMode = PnLPriceMode.LAST_PRICE; private BigDecimal profitTriggerRatio = new BigDecimal("0.5"); private BigDecimal reinvestRatio = new BigDecimal("0.5"); private int maxPositionMultiplier = 10; private BigDecimal equityRestartRatio = new BigDecimal("0.05"); public Builder apiKey(String v) { this.apiKey = v; return this; } public Builder apiSecret(String v) { this.apiSecret = v; return this; } public Builder passphrase(String v) { this.passphrase = v; return this; } public Builder contract(String v) { this.contract = v; return this; } public Builder leverage(String v) { this.leverage = v; return this; } public Builder marginMode(String v) { this.marginMode = v; return this; } public Builder positionMode(String v) { this.positionMode = v; return this; } public Builder baseQuantity(String v) { this.baseQuantity = v; return this; } public Builder maxLoss(BigDecimal v) { this.maxLoss = v; return this; } public Builder isProduction(boolean v) { this.isProduction = v; return this; } public Builder contractMultiplier(BigDecimal v) { this.contractMultiplier = v; return this; } public Builder priceScale(int v) { this.priceScale = v; return this; } public Builder unrealizedPnlPriceMode(PnLPriceMode v) { this.unrealizedPnlPriceMode = v; return this; } public Builder profitTriggerRatio(BigDecimal v) { this.profitTriggerRatio = v; return this; } public Builder reinvestRatio(BigDecimal v) { this.reinvestRatio = v; return this; } public Builder maxPositionMultiplier(int v) { this.maxPositionMultiplier = v; return this; } public Builder equityRestartRatio(BigDecimal v) { this.equityRestartRatio = v; return this; } public OkxConfig build() { return new OkxConfig(this); } } }