mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 09:18:08 +00:00
更新 MakerEngine,添加持仓均价未同步的日志记录功能,优化止损判断逻辑以处理未定义的持仓均价
This commit is contained in:
@@ -68,6 +68,7 @@ export class MakerEngine {
|
|||||||
private initializedPosition = false;
|
private initializedPosition = false;
|
||||||
private initialOrderSnapshotReady = false;
|
private initialOrderSnapshotReady = false;
|
||||||
private initialOrderResetDone = false;
|
private initialOrderResetDone = false;
|
||||||
|
private entryPricePendingLogged = false;
|
||||||
|
|
||||||
constructor(private readonly config: MakerConfig, private readonly exchange: ExchangeAdapter) {
|
constructor(private readonly config: MakerConfig, private readonly exchange: ExchangeAdapter) {
|
||||||
this.tradeLog = createTradeLog(this.config.maxLogEntries);
|
this.tradeLog = createTradeLog(this.config.maxLogEntries);
|
||||||
@@ -195,6 +196,7 @@ export class MakerEngine {
|
|||||||
const desired: DesiredOrder[] = [];
|
const desired: DesiredOrder[] = [];
|
||||||
|
|
||||||
if (absPosition < EPS) {
|
if (absPosition < EPS) {
|
||||||
|
this.entryPricePendingLogged = false;
|
||||||
desired.push({ side: "BUY", price: bidPrice, amount: this.config.tradeAmount, reduceOnly: false });
|
desired.push({ side: "BUY", price: bidPrice, amount: this.config.tradeAmount, reduceOnly: false });
|
||||||
desired.push({ side: "SELL", price: askPrice, amount: this.config.tradeAmount, reduceOnly: false });
|
desired.push({ side: "SELL", price: askPrice, amount: this.config.tradeAmount, reduceOnly: false });
|
||||||
} else {
|
} else {
|
||||||
@@ -317,11 +319,30 @@ export class MakerEngine {
|
|||||||
const absPosition = Math.abs(position.positionAmt);
|
const absPosition = Math.abs(position.positionAmt);
|
||||||
if (absPosition < EPS) return;
|
if (absPosition < EPS) return;
|
||||||
|
|
||||||
|
const hasEntryPrice = Number.isFinite(position.entryPrice) && Math.abs(position.entryPrice) > 1e-8;
|
||||||
|
if (!hasEntryPrice) {
|
||||||
|
if (!this.entryPricePendingLogged) {
|
||||||
|
this.tradeLog.push("info", "做市持仓均价未同步,等待账户快照刷新后再执行止损判断");
|
||||||
|
this.entryPricePendingLogged = true;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.entryPricePendingLogged = false;
|
||||||
|
|
||||||
const pnl = position.positionAmt > 0
|
const pnl = position.positionAmt > 0
|
||||||
? (bidPrice - position.entryPrice) * absPosition
|
? (bidPrice - position.entryPrice) * absPosition
|
||||||
: (position.entryPrice - askPrice) * absPosition;
|
: (position.entryPrice - askPrice) * absPosition;
|
||||||
|
const unrealized = Number.isFinite(position.unrealizedProfit)
|
||||||
|
? position.unrealizedProfit
|
||||||
|
: null;
|
||||||
|
const derivedLoss = pnl < -this.config.lossLimit;
|
||||||
|
const snapshotLoss = Boolean(
|
||||||
|
unrealized != null &&
|
||||||
|
unrealized < -this.config.lossLimit &&
|
||||||
|
Math.sign(unrealized) === Math.sign(pnl)
|
||||||
|
);
|
||||||
|
|
||||||
if (pnl < -this.config.lossLimit || position.unrealizedProfit < -this.config.lossLimit) {
|
if (derivedLoss || snapshotLoss) {
|
||||||
this.tradeLog.push(
|
this.tradeLog.push(
|
||||||
"stop",
|
"stop",
|
||||||
`触发止损,方向=${position.positionAmt > 0 ? "多" : "空"} 当前亏损=${pnl.toFixed(4)} USDT`
|
`触发止损,方向=${position.positionAmt > 0 ? "多" : "空"} 当前亏损=${pnl.toFixed(4)} USDT`
|
||||||
|
|||||||
Reference in New Issue
Block a user