Skip to content

Commit 41ef939

Browse files
committed
docs: add stop limit and stop market documentations
1 parent 9f062c4 commit 41ef939

File tree

3 files changed

+92
-20
lines changed

3 files changed

+92
-20
lines changed

README.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,16 @@ const lob = new OrderBook()
5757
Then you'll be able to use next primary functions:
5858

5959
```js
60-
lob.createOrder({ type: 'limit' | 'market', side: 'buy' | 'sell', size: number, price: number, id?: string, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
60+
lob.createOrder({ type: 'limit' | 'market' | 'stop_limit' | 'stop_market', side: 'buy' | 'sell', size: number, price?: number, id?: string, stopPrice?: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
6161

6262
lob.limit({ id: string, side: 'buy' | 'sell', size: number, price: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
6363

6464
lob.market({ side: 'buy' | 'sell', size: number })
6565

66+
lob.stopLimit({ id: string, side: 'buy' | 'sell', size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
67+
68+
lob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })
69+
6670
lob.modify(orderID: string, { side: 'buy' | 'sell', size: number, price: number })
6771

6872
lob.cancel(orderID: string)
@@ -191,6 +195,39 @@ partial - null
191195
quantityLeft - 4
192196
```
193197

198+
### Create Stop Limit Order
199+
200+
```js
201+
/**
202+
* Create a stop limit order. See {@link StopLimitOrderOptions} for details.
203+
*
204+
* @param options
205+
* @param options.side - `sell` or `buy`
206+
* @param options.id - Unique order ID
207+
* @param options.size - How much of currency you want to trade in units of base currency
208+
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
209+
* @param options.stopPrice - The price at which the order will be triggered.
210+
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
211+
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
212+
*/
213+
stopLimit({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
214+
```
215+
216+
### Create Stop Market Order
217+
218+
```js
219+
/**
220+
* Create a stop market order. See {@link StopMarketOrderOptions} for details.
221+
*
222+
* @param options
223+
* @param options.side - `sell` or `buy`
224+
* @param options.size - How much of currency you want to trade in units of base currency
225+
* @param options.stopPrice - The price at which the order will be triggered.
226+
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
227+
*/
228+
stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })
229+
```
230+
194231
### Modify an existing order
195232

196233
```js

src/orderbook.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ export class OrderBook {
215215
/* c8 ignore stop */
216216
}
217217

218+
/**
219+
* Create a stop market order. See {@link StopMarketOrderOptions} for details.
220+
*
221+
* @param options
222+
* @param options.side - `sell` or `buy`
223+
* @param options.size - How much of currency you want to trade in units of base currency
224+
* @param options.stopPrice - The price at which the order will be triggered.
225+
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
226+
*/
218227
public stopMarket = (options: StopMarketOrderOptions): IProcessOrder => {
219228
return this._stopMarket(options)
220229
}
@@ -284,6 +293,18 @@ export class OrderBook {
284293
/* c8 ignore stop */
285294
}
286295

296+
/**
297+
* Create a stop limit order. See {@link StopLimitOrderOptions} for details.
298+
*
299+
* @param options
300+
* @param options.side - `sell` or `buy`
301+
* @param options.id - Unique order ID
302+
* @param options.size - How much of currency you want to trade in units of base currency
303+
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
304+
* @param options.stopPrice - The price at which the order will be triggered.
305+
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
306+
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
307+
*/
287308
public stopLimit = (options: StopLimitOrderOptions): IProcessOrder => {
288309
return this._stopLimit(options)
289310
}

test/orderbook.test.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ void test('test limit', ({ equal, end }) => {
9292
addDepth(ob, '', 2)
9393
equal(ob.marketPrice, 0)
9494
const process1 =
95-
// { done, partial, partialQuantityProcessed, quantityLeft, err }
96-
ob.limit({ side: Side.BUY, id: 'order-b100', size: 1, price: 100 })
95+
// { done, partial, partialQuantityProcessed, quantityLeft, err }
96+
ob.limit({ side: Side.BUY, id: 'order-b100', size: 1, price: 100 })
9797

9898
equal(ob.marketPrice, 100)
9999
equal(process1.err === null, true)
@@ -103,8 +103,8 @@ void test('test limit', ({ equal, end }) => {
103103
equal(process1.partialQuantityProcessed, 1)
104104

105105
const process2 =
106-
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
107-
ob.limit({ side: Side.BUY, id: 'order-b150', size: 10, price: 150 })
106+
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
107+
ob.limit({ side: Side.BUY, id: 'order-b150', size: 10, price: 150 })
108108
equal(process2.err === null, true)
109109
equal(process2.done.length, 5)
110110
equal(process2.partial?.id, 'order-b150')
@@ -135,7 +135,6 @@ void test('test limit', ({ equal, end }) => {
135135
price: 100
136136
})
137137
equal(process5.err?.message, ERROR.ErrInvalidSide)
138-
139138
const removed = ob.cancel('order-b100')
140139
equal(removed === undefined, true)
141140
// Test also the createOrder method
@@ -287,17 +286,17 @@ void test('test market', ({ equal, end }) => {
287286
addDepth(ob, '', 2)
288287

289288
const process1 =
290-
// { done, partial, partialQuantityProcessed, quantityLeft, err }
291-
ob.market({ side: Side.BUY, size: 3 })
289+
// { done, partial, partialQuantityProcessed, quantityLeft, err }
290+
ob.market({ side: Side.BUY, size: 3 })
292291

293292
equal(process1.err === null, true)
294293
equal(process1.quantityLeft, 0)
295294
equal(process1.partialQuantityProcessed, 1)
296295

297296
// Test also the createOrder method
298297
const process3 =
299-
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
300-
ob.createOrder({ type: OrderType.MARKET, side: Side.SELL, size: 12 })
298+
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
299+
ob.createOrder({ type: OrderType.MARKET, side: Side.SELL, size: 12 })
301300

302301
equal(process3.done.length, 5)
303302
equal(process3.err === null, true)
@@ -347,10 +346,10 @@ void test('createOrder error', ({ equal, end }) => {
347346
})
348347

349348
/**
350-
* Stop-Market Order:
351-
* Buy: marketPrice < stopPrice
352-
* Sell: marketPrice > stopPrice
353-
*/
349+
* Stop-Market Order:
350+
* Buy: marketPrice < stopPrice
351+
* Sell: marketPrice > stopPrice
352+
*/
354353
void test('test stop_market order', ({ equal, end }) => {
355354
const ob = new OrderBook()
356355

@@ -374,8 +373,11 @@ void test('test stop_market order', ({ equal, end }) => {
374373
stopPrice: ob.marketPrice
375374
}) // Same as market price
376375
equal(wrongStopPrice2.err?.message, ERROR.ErrInvalidStopPrice)
377-
// @ts-expect-error invalid side
378-
const wrongOtherOrderOption1 = ob.stopMarket({ side: 'wrong-side', size: 1 })
376+
const wrongOtherOrderOption1 = ob.stopMarket({
377+
// @ts-expect-error invalid side
378+
side: 'wrong-side',
379+
size: 1
380+
})
379381
equal(wrongOtherOrderOption1.err != null, true)
380382

381383
// @ts-expect-error size must be greather than 0
@@ -496,16 +498,28 @@ void test('test stop_limit order', ({ equal, end }) => {
496498
price: ob.marketPrice
497499
}) // Same as market price
498500
equal(wrongStopPrice2.err?.message, ERROR.ErrInvalidStopPrice)
499-
// @ts-expect-error invalid side
500-
const wrongOtherOrderOption1 = ob.stopLimit({ side: 'wrong-side', size: 1, price: 10 })
501+
const wrongOtherOrderOption1 = ob.stopLimit({
502+
// @ts-expect-error invalid side
503+
side: 'wrong-side',
504+
size: 1,
505+
price: 10
506+
})
501507
equal(wrongOtherOrderOption1.err != null, true)
502508

503509
// @ts-expect-error size must be greather than 0
504-
const wrongOtherOrderOption2 = ob.stopLimit({ side: Side.BUY, size: 0, price: 10 })
510+
const wrongOtherOrderOption2 = ob.stopLimit({
511+
side: Side.BUY,
512+
size: 0,
513+
price: 10
514+
})
505515
equal(wrongOtherOrderOption2.err != null, true)
506516

507517
// @ts-expect-error price must be greather than 0
508-
const wrongOtherOrderOption3 = ob.stopLimit({ side: Side.BUY, size: 1, price: 0 })
518+
const wrongOtherOrderOption3 = ob.stopLimit({
519+
side: Side.BUY,
520+
size: 1,
521+
price: 0
522+
})
509523
equal(wrongOtherOrderOption3.err != null, true)
510524

511525
// Add a stop limit BUY order

0 commit comments

Comments
 (0)