mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 17:28:08 +00:00
feat: 更新网格引擎,优化平仓逻辑以处理实际持仓情况,确保安全平仓数量计算
This commit is contained in:
@@ -724,7 +724,19 @@ export class GridEngine {
|
|||||||
const totalLongClose = toCloseLongLevels.reduce((acc, item) => acc + item.quantity, 0);
|
const totalLongClose = toCloseLongLevels.reduce((acc, item) => acc + item.quantity, 0);
|
||||||
const totalShortClose = toCloseShortLevels.reduce((acc, item) => acc + item.quantity, 0);
|
const totalShortClose = toCloseShortLevels.reduce((acc, item) => acc + item.quantity, 0);
|
||||||
|
|
||||||
if (totalLongClose > EPSILON) {
|
const actualLong = Math.max(this.position.positionAmt, 0);
|
||||||
|
const actualShort = Math.max(-this.position.positionAmt, 0);
|
||||||
|
|
||||||
|
if (actualLong <= EPSILON && totalLongClose > EPSILON) {
|
||||||
|
this.resyncLongExposure(actualLong);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actualShort <= EPSILON && totalShortClose > EPSILON) {
|
||||||
|
this.resyncShortExposure(actualShort);
|
||||||
|
}
|
||||||
|
|
||||||
|
const safeLongClose = Math.min(totalLongClose, Math.max(actualLong - EPSILON, 0));
|
||||||
|
if (safeLongClose > EPSILON) {
|
||||||
try {
|
try {
|
||||||
await placeMarketOrder(
|
await placeMarketOrder(
|
||||||
this.exchange,
|
this.exchange,
|
||||||
@@ -734,7 +746,7 @@ export class GridEngine {
|
|||||||
this.timers,
|
this.timers,
|
||||||
this.pendings,
|
this.pendings,
|
||||||
"SELL",
|
"SELL",
|
||||||
totalLongClose,
|
safeLongClose,
|
||||||
this.log,
|
this.log,
|
||||||
true,
|
true,
|
||||||
{ expectedPrice: referencePrice },
|
{ expectedPrice: referencePrice },
|
||||||
@@ -743,13 +755,15 @@ export class GridEngine {
|
|||||||
for (const { level } of toCloseLongLevels) {
|
for (const { level } of toCloseLongLevels) {
|
||||||
this.longExposure.delete(level);
|
this.longExposure.delete(level);
|
||||||
}
|
}
|
||||||
|
this.deferPositionAlignment();
|
||||||
this.schedulePersist();
|
this.schedulePersist();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.log("error", `市价平仓多单失败: ${extractMessage(error)}`);
|
this.log("error", `市价平仓多单失败: ${extractMessage(error)}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (totalShortClose > EPSILON) {
|
const safeShortClose = Math.min(totalShortClose, Math.max(actualShort - EPSILON, 0));
|
||||||
|
if (safeShortClose > EPSILON) {
|
||||||
try {
|
try {
|
||||||
await placeMarketOrder(
|
await placeMarketOrder(
|
||||||
this.exchange,
|
this.exchange,
|
||||||
@@ -759,7 +773,7 @@ export class GridEngine {
|
|||||||
this.timers,
|
this.timers,
|
||||||
this.pendings,
|
this.pendings,
|
||||||
"BUY",
|
"BUY",
|
||||||
totalShortClose,
|
safeShortClose,
|
||||||
this.log,
|
this.log,
|
||||||
true,
|
true,
|
||||||
{ expectedPrice: referencePrice },
|
{ expectedPrice: referencePrice },
|
||||||
@@ -768,6 +782,7 @@ export class GridEngine {
|
|||||||
for (const { level } of toCloseShortLevels) {
|
for (const { level } of toCloseShortLevels) {
|
||||||
this.shortExposure.delete(level);
|
this.shortExposure.delete(level);
|
||||||
}
|
}
|
||||||
|
this.deferPositionAlignment();
|
||||||
this.schedulePersist();
|
this.schedulePersist();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.log("error", `市价平仓空单失败: ${extractMessage(error)}`);
|
this.log("error", `市价平仓空单失败: ${extractMessage(error)}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user