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 22837b3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export class Client {

// Must login first
async replacePrice(placedOrder: PlacedOrder, price: number | PriceFlag): Promise<ReplaceOrderResponse> {
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);
Expand All @@ -85,7 +87,9 @@ export class Client {

// Must login first
async replaceQuantity(placedOrder: PlacedOrder, quantity: number): Promise<ReplaceOrderResponse> {
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;
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,
);
}

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);
}
}
51 changes: 51 additions & 0 deletions test/placed-order.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});
});

});

0 comments on commit 22837b3

Please sign in to comment.