Skip to content

Commit

Permalink
fix(sdk): fix modification of price and volume unit conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinypfan committed Mar 13, 2024
1 parent 1424464 commit 9961193
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class Client {

// Must login first
async replacePrice(placedOrder: PlacedOrder, price: number | PriceFlag): Promise<ReplaceOrderResponse> {
const order = placedOrder.toObject();
const unit = this.sdk.getVolumePerUnit(placedOrder.payload.stockNo ?? "");
const order = placedOrder.toModifyObject(unit);
const response = (typeof price === 'number')
? this.sdk.modifyPrice(order, price, PriceFlag.Limit)
: this.sdk.modifyPrice(order, null, price);
Expand All @@ -85,7 +86,8 @@ export class Client {

// Must login first
async replaceQuantity(placedOrder: PlacedOrder, quantity: number): Promise<ReplaceOrderResponse> {
const order = placedOrder.toObject();
const unit = this.sdk.getVolumePerUnit(placedOrder.payload.stockNo ?? "");
const order = placedOrder.toModifyObject(unit);
const response = this.sdk.modifyVolume(order, quantity);
const parsed = JSON.parse(response) as ParsedReplaceOrderResponse;
return parsed.data;
Expand Down
14 changes: 14 additions & 0 deletions src/placed-order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ export class PlacedOrder {
{} as OrderResult,
);
}

toModifyObject(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);
}
}

0 comments on commit 9961193

Please sign in to comment.