Skip to content

Commit

Permalink
fix: check if sell FOK order can be filled
Browse files Browse the repository at this point in the history
  • Loading branch information
fasenderos committed May 8, 2023
1 parent 1f132d0 commit 2814b3e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,13 +469,20 @@ export class OrderBook {
}

let cumulativeSize = 0
orderSide.priceTree().forEach((_key, priceLevel) => {
if (price <= priceLevel.price() && cumulativeSize < size) {
cumulativeSize += priceLevel.volume()
} else {
return true // break the loop
}
})
let iterator = orderSide.priceTree().end
let continueLoop = true
while (continueLoop) {
if (iterator?.node) {
if (price <= iterator.node.key) {
cumulativeSize += iterator.node.value.volume()
if (cumulativeSize < size && iterator.hasPrev) {
iterator = orderSide.priceTree().at(iterator.index - 1)
} else {
continueLoop = false
}
} else continueLoop = false
} else continueLoop = false
}
return cumulativeSize >= size
}
}
49 changes: 49 additions & 0 deletions test/orderbook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,57 @@ describe('OrderBook', () => {
)
expect(process2.err?.message).toBe(ERROR.ErrLimitFOKNotFillable)

const process3 = ob.limit(
Side.BUY,
'buy-order-size-greather-than-order-side-volume',
30,
100,
TimeInForce.FOK
)
expect(process3.err?.message).toBe(ERROR.ErrLimitFOKNotFillable)

const process4 = ob.limit(
Side.SELL,
'sell-order-size-greather-than-order-side-volume',
30,
90,
TimeInForce.FOK
)
expect(process4.err?.message).toBe(ERROR.ErrLimitFOKNotFillable)

ob.limit(Side.BUY, 'order-ioc-b100', 3, 100, TimeInForce.IOC)
expect(ob.order('order-ioc-b100')).toBeUndefined()

const processIOC = ob.limit(
Side.SELL,
'order-ioc-s90',
3,
90,
TimeInForce.IOC
)
expect(ob.order('order-ioc-s90')).toBeUndefined()
expect(processIOC.partial?.id).toBe('order-ioc-s90')

const processFOKBuy = ob.limit(
Side.BUY,
'order-fok-b110',
2,
120,
TimeInForce.FOK
)

expect(processFOKBuy.err).toBeNull()
expect(processFOKBuy.quantityLeft).toBe(0)

const processFOKSell = ob.limit(
Side.SELL,
'order-fok-s80',
4,
70,
TimeInForce.FOK
)
expect(processFOKSell.err).toBeNull()
expect(processFOKSell.quantityLeft).toBe(0)
})

test('test market', () => {
Expand Down

0 comments on commit 2814b3e

Please sign in to comment.