From 1fc078c2357e0ef45bac1222e34ec47ed158fe2b Mon Sep 17 00:00:00 2001 From: kevinypfan Date: Thu, 14 Mar 2024 17:56:47 +0800 Subject: [PATCH] fix: correct trading unit conversion (#9) --- src/client.ts | 8 ++++-- src/placed-order.ts | 14 +++++++++++ test/placed-order.spec.ts | 51 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 665fe0f..9d145c3 100644 --- a/src/client.ts +++ b/src/client.ts @@ -75,7 +75,9 @@ export class Client { // Must login first async replacePrice(placedOrder: PlacedOrder, price: number | PriceFlag): Promise { - const order = placedOrder.toObject(); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const unit = this.sdk.getVolumePerUnit(placedOrder.payload.stockNo!); + const order = placedOrder.toModifiedObject(unit); const response = (typeof price === 'number') ? this.sdk.modifyPrice(order, price, PriceFlag.Limit) : this.sdk.modifyPrice(order, null, price); @@ -85,7 +87,9 @@ export class Client { // Must login first async replaceQuantity(placedOrder: PlacedOrder, quantity: number): Promise { - const order = placedOrder.toObject(); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + const unit = this.sdk.getVolumePerUnit(placedOrder.payload.stockNo!); + const order = placedOrder.toModifiedObject(unit); const response = this.sdk.modifyVolume(order, quantity); const parsed = JSON.parse(response) as ParsedReplaceOrderResponse; return parsed.data; diff --git a/src/placed-order.ts b/src/placed-order.ts index d220d24..84d2ef8 100644 --- a/src/placed-order.ts +++ b/src/placed-order.ts @@ -47,4 +47,18 @@ export class PlacedOrder { {} as OrderResult, ); } + + toModifiedObject(unit: number): OrderResult { + return Object.entries(this.payload).reduce((object, [key, value]) => { + if ( + key.endsWith("Qty") && + this.payload.apCode && + [ApCode.Odd, ApCode.Emg, ApCode.IntradayOdd].includes( + this.payload.apCode + ) + ) + return { ...object, [key]: String(Math.floor(value * unit)) }; + return { ...object, [key]: String(value) }; + }, {} as OrderResult); + } } diff --git a/test/placed-order.spec.ts b/test/placed-order.spec.ts index 024847f..46d3dcb 100644 --- a/test/placed-order.spec.ts +++ b/test/placed-order.spec.ts @@ -85,4 +85,55 @@ describe('PlacedOrder', () => { }); }); }); + + describe('.toModifiedObject()', () => { + it('should return ModifiedObject', () => { + const placedOrder = new PlacedOrder({ + workDate: '20220222', + ordDate: '20220222', + ordTime: '130000000', + ordStatus: '2', + ordNo: 'B9999', + preOrdNo: '', + stockNo: '0050', + buySell: 'B', + apCode: '5', + priceFlag: '0', + trade: '0', + odPrice: '140.0', + orgQty: '0.001', + matQty: '0', + celQty: '0', + celable: '2', + errCode: '00000000', + errMsg: '', + avgPrice: '0.0', + bsFlag: 'R', + }); + + expect(placedOrder.toModifiedObject(1000)).toEqual({ + workDate: '20220222', + ordDate: '20220222', + ordTime: '130000000', + ordStatus: '2', + ordNo: 'B9999', + preOrdNo: '', + stockNo: '0050', + buySell: 'B', + apCode: '5', + priceFlag: '0', + trade: '0', + odPrice: '140', + orgQty: '1', + matQty: '0', + celQty: '0', + celable: '2', + errCode: '00000000', + errMsg: '', + avgPrice: '0', + bsFlag: 'R', + }); + }); + }); + });