Skip to content

Commit

Permalink
docs: add stop limit and stop market documentations
Browse files Browse the repository at this point in the history
  • Loading branch information
fasenderos committed Jul 23, 2024
1 parent 9f062c4 commit 41ef939
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 20 deletions.
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ const lob = new OrderBook()
Then you'll be able to use next primary functions:

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

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

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

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

lob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })

lob.modify(orderID: string, { side: 'buy' | 'sell', size: number, price: number })

lob.cancel(orderID: string)
Expand Down Expand Up @@ -191,6 +195,39 @@ partial - null
quantityLeft - 4
```

### Create Stop Limit Order

```js
/**
* Create a stop limit order. See {@link StopLimitOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.id - Unique order ID
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
* @param options.stopPrice - The price at which the order will be triggered.
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
stopLimit({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })
```

### Create Stop Market Order

```js
/**
* Create a stop market order. See {@link StopMarketOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.stopPrice - The price at which the order will be triggered.
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })
```

### Modify an existing order

```js
Expand Down
21 changes: 21 additions & 0 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ export class OrderBook {
/* c8 ignore stop */
}

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

/**
* Create a stop limit order. See {@link StopLimitOrderOptions} for details.
*
* @param options
* @param options.side - `sell` or `buy`
* @param options.id - Unique order ID
* @param options.size - How much of currency you want to trade in units of base currency
* @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
* @param options.stopPrice - The price at which the order will be triggered.
* @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
* @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
*/
public stopLimit = (options: StopLimitOrderOptions): IProcessOrder => {
return this._stopLimit(options)
}
Expand Down
52 changes: 33 additions & 19 deletions test/orderbook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ void test('test limit', ({ equal, end }) => {
addDepth(ob, '', 2)
equal(ob.marketPrice, 0)
const process1 =
// { done, partial, partialQuantityProcessed, quantityLeft, err }
ob.limit({ side: Side.BUY, id: 'order-b100', size: 1, price: 100 })
// { done, partial, partialQuantityProcessed, quantityLeft, err }
ob.limit({ side: Side.BUY, id: 'order-b100', size: 1, price: 100 })

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

const process2 =
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
ob.limit({ side: Side.BUY, id: 'order-b150', size: 10, price: 150 })
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
ob.limit({ side: Side.BUY, id: 'order-b150', size: 10, price: 150 })
equal(process2.err === null, true)
equal(process2.done.length, 5)
equal(process2.partial?.id, 'order-b150')
Expand Down Expand Up @@ -135,7 +135,6 @@ void test('test limit', ({ equal, end }) => {
price: 100
})
equal(process5.err?.message, ERROR.ErrInvalidSide)

const removed = ob.cancel('order-b100')
equal(removed === undefined, true)
// Test also the createOrder method
Expand Down Expand Up @@ -287,17 +286,17 @@ void test('test market', ({ equal, end }) => {
addDepth(ob, '', 2)

const process1 =
// { done, partial, partialQuantityProcessed, quantityLeft, err }
ob.market({ side: Side.BUY, size: 3 })
// { done, partial, partialQuantityProcessed, quantityLeft, err }
ob.market({ side: Side.BUY, size: 3 })

equal(process1.err === null, true)
equal(process1.quantityLeft, 0)
equal(process1.partialQuantityProcessed, 1)

// Test also the createOrder method
const process3 =
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
ob.createOrder({ type: OrderType.MARKET, side: Side.SELL, size: 12 })
// { done, partial, partialQuantityProcessed, quantityLeft, err } =
ob.createOrder({ type: OrderType.MARKET, side: Side.SELL, size: 12 })

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

/**
* Stop-Market Order:
* Buy: marketPrice < stopPrice
* Sell: marketPrice > stopPrice
*/
* Stop-Market Order:
* Buy: marketPrice < stopPrice
* Sell: marketPrice > stopPrice
*/
void test('test stop_market order', ({ equal, end }) => {
const ob = new OrderBook()

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

// @ts-expect-error size must be greather than 0
Expand Down Expand Up @@ -496,16 +498,28 @@ void test('test stop_limit order', ({ equal, end }) => {
price: ob.marketPrice
}) // Same as market price
equal(wrongStopPrice2.err?.message, ERROR.ErrInvalidStopPrice)
// @ts-expect-error invalid side
const wrongOtherOrderOption1 = ob.stopLimit({ side: 'wrong-side', size: 1, price: 10 })
const wrongOtherOrderOption1 = ob.stopLimit({
// @ts-expect-error invalid side
side: 'wrong-side',
size: 1,
price: 10
})
equal(wrongOtherOrderOption1.err != null, true)

// @ts-expect-error size must be greather than 0
const wrongOtherOrderOption2 = ob.stopLimit({ side: Side.BUY, size: 0, price: 10 })
const wrongOtherOrderOption2 = ob.stopLimit({
side: Side.BUY,
size: 0,
price: 10
})
equal(wrongOtherOrderOption2.err != null, true)

// @ts-expect-error price must be greather than 0
const wrongOtherOrderOption3 = ob.stopLimit({ side: Side.BUY, size: 1, price: 0 })
const wrongOtherOrderOption3 = ob.stopLimit({
side: Side.BUY,
size: 1,
price: 0
})
equal(wrongOtherOrderOption3.err != null, true)

// Add a stop limit BUY order
Expand Down

0 comments on commit 41ef939

Please sign in to comment.