feat: 更新订单处理逻辑,将价格类型改为字符串以避免精度问题,并添加价格格式化函数

This commit is contained in:
discountry
2025-10-03 07:15:21 +08:00
parent 65aaf26879
commit d5c4b04746
7 changed files with 68 additions and 39 deletions
+3 -7
View File
@@ -2,7 +2,7 @@ import type { AsterOrder } from "../../exchanges/types";
export interface OrderTarget {
side: "BUY" | "SELL";
price: number;
price: string; // 改为字符串避免精度问题
amount: number;
reduceOnly: boolean;
}
@@ -15,18 +15,14 @@ export function makeOrderPlan(
const toCancel: AsterOrder[] = [];
for (const order of openOrders) {
const price = Number(order.price);
if (!Number.isFinite(price)) {
toCancel.push(order);
continue;
}
const orderPrice = String(order.price);
const reduceOnly = order.reduceOnly === true;
const matchedIndex = targets.findIndex((target, index) => {
return (
unmatched.has(index) &&
target.side === order.side &&
target.reduceOnly === reduceOnly &&
price === target.price
orderPrice === target.price // 直接使用字符串比较
);
});
if (matchedIndex >= 0) {
+5 -4
View File
@@ -1,6 +1,6 @@
import type { ExchangeAdapter } from "../exchanges/adapter";
import type { AsterOrder, CreateOrderParams } from "../exchanges/types";
import { roundDownToTick, roundQtyDownToStep } from "../utils/math";
import { roundDownToTick, roundQtyDownToStep, formatPriceToString } from "../utils/math";
import { isUnknownOrderError } from "../utils/errors";
import { isOrderPriceAllowedByMark } from "../utils/strategy";
@@ -122,7 +122,7 @@ export async function placeOrder(
timers: OrderTimerMap,
pendings: OrderPendingMap,
side: "BUY" | "SELL",
price: number,
price: string, // 改为字符串价格
amount: number,
log: LogHandler,
reduceOnly = false,
@@ -131,7 +131,8 @@ export async function placeOrder(
): Promise<AsterOrder | undefined> {
const type = "LIMIT";
if (isOperating(locks, type)) return;
if (!enforceMarkPriceGuard(side, price, guard, log, "限价单")) return;
const priceNum = Number(price);
if (!enforceMarkPriceGuard(side, priceNum, guard, log, "限价单")) return;
const priceTick = opts?.priceTick ?? 0.1;
const qtyStep = opts?.qtyStep ?? 0.001;
const params: CreateOrderParams = {
@@ -139,7 +140,7 @@ export async function placeOrder(
side,
type,
quantity: roundQtyDownToStep(amount, qtyStep),
price: roundDownToTick(price, priceTick),
price: priceNum, // 直接使用字符串转换的数字,不再格式化
timeInForce: "GTX",
};
if (reduceOnly) params.reduceOnly = "true";