From 010ff3c3d5c305e8e77e4e733a66c8583900f4e0 Mon Sep 17 00:00:00 2001 From: Ahmed Mohsen Date: Thu, 23 Jan 2025 16:06:33 +0200 Subject: [PATCH] fix: floating number round --- src/broker.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/broker.ts b/src/broker.ts index 8cc2dfb..3dd0a67 100644 --- a/src/broker.ts +++ b/src/broker.ts @@ -60,20 +60,35 @@ export class Broker { return Math.max(0, this.equity - marginUsed); } + isGreaterWithPrecision(a: number, b: number, precision = 10) { + return (a - b) > -Math.pow(10, -precision); + } + isLessWithPrecision(a: number, b: number, precision = 10) { + return (b - a) > -Math.pow(10, -precision); + } + public newOrder(options: OrderOptions) { const { price, size, stopPrice, limitPrice, slPrice, tpPrice, parentTrade } = options; const isLong = size > 0; const adjustedPrice = this.adjustPrice({ price, size }); if (isLong) { - if (!((limitPrice || stopPrice || adjustedPrice) > (slPrice || Number.NEGATIVE_INFINITY) && (limitPrice || stopPrice || adjustedPrice) < (tpPrice || Number.POSITIVE_INFINITY))) { - throw new RangeError(`Long orders require: SL (${slPrice}) < LIMIT (${limitPrice || stopPrice || adjustedPrice}) < TP (${tpPrice})`); + const basePrice = limitPrice || stopPrice || adjustedPrice; + const slBoundary = slPrice || Number.NEGATIVE_INFINITY; + const tpBoundary = tpPrice || Number.POSITIVE_INFINITY; + + if (!(this.isGreaterWithPrecision(basePrice, slBoundary) && this.isLessWithPrecision(basePrice, tpBoundary))) { + throw new RangeError(`Long orders require: SL (${slPrice}) < LIMIT (${basePrice}) < TP (${tpPrice})`); } - } else { - if (!((limitPrice || stopPrice || adjustedPrice) > (tpPrice || Number.NEGATIVE_INFINITY) && (limitPrice || stopPrice || adjustedPrice) < (slPrice || Number.POSITIVE_INFINITY))) { - throw new RangeError(`Short orders require: TP (${tpPrice}) < LIMIT (${limitPrice || stopPrice || adjustedPrice}) < SL (${slPrice})`); + } else { + const basePrice = limitPrice || stopPrice || adjustedPrice; + const tpBoundary = tpPrice || Number.NEGATIVE_INFINITY; + const slBoundary = slPrice || Number.POSITIVE_INFINITY; + + if (!(this.isGreaterWithPrecision(basePrice, tpBoundary) && this.isLessWithPrecision(basePrice, slBoundary))) { + throw new RangeError(`Short orders require: TP (${tpPrice}) < LIMIT (${basePrice}) < SL (${slPrice})`); } - } + } const order = new Order(this, options);