mirror of
https://github.com/discountry/ritmex-bot.git
synced 2026-09-11 01:08:07 +00:00
fix trend position
This commit is contained in:
+11
-5
@@ -10,14 +10,20 @@ export function getPosition(snapshot: AsterAccountSnapshot | null, symbol: strin
|
|||||||
if (!snapshot) {
|
if (!snapshot) {
|
||||||
return { positionAmt: 0, entryPrice: 0, unrealizedProfit: 0 };
|
return { positionAmt: 0, entryPrice: 0, unrealizedProfit: 0 };
|
||||||
}
|
}
|
||||||
const pos = snapshot.positions?.find((p) => p.symbol === symbol);
|
const positions = snapshot.positions?.filter((p) => p.symbol === symbol) ?? [];
|
||||||
if (!pos) {
|
if (positions.length === 0) {
|
||||||
return { positionAmt: 0, entryPrice: 0, unrealizedProfit: 0 };
|
return { positionAmt: 0, entryPrice: 0, unrealizedProfit: 0 };
|
||||||
}
|
}
|
||||||
|
const NON_ZERO_EPS = 1e-8;
|
||||||
|
const withExposure = positions.filter((p) => Math.abs(Number(p.positionAmt)) > NON_ZERO_EPS);
|
||||||
|
const selected =
|
||||||
|
withExposure.find((p) => p.positionSide === "BOTH") ??
|
||||||
|
withExposure.sort((a, b) => Math.abs(Number(b.positionAmt)) - Math.abs(Number(a.positionAmt)))[0] ??
|
||||||
|
positions[0];
|
||||||
return {
|
return {
|
||||||
positionAmt: Number(pos.positionAmt),
|
positionAmt: Number(selected.positionAmt) || 0,
|
||||||
entryPrice: Number(pos.entryPrice),
|
entryPrice: Number(selected.entryPrice) || 0,
|
||||||
unrealizedProfit: Number(pos.unrealizedProfit),
|
unrealizedProfit: Number(selected.unrealizedProfit) || 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user