Skip to content

Commit

Permalink
feat: taker and maker support + fix origSize on limit order
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- The `isMaker` property has been removed from the limit order object.
- New properties `takerQty` and `makerQty` have been added to the limit order objecct.
  • Loading branch information
fasenderos committed Aug 2, 2024
1 parent dba8c12 commit 74f5907
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 133 deletions.
54 changes: 25 additions & 29 deletions src/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ abstract class BaseOrder {
readonly _id: string
readonly _side: Side
_size: number
readonly _origSize: number
_time: number
constructor (options: OrderOptions) {
this._id = options.id ?? randomUUID()
this._side = options.side
this._size = options.size
this._origSize = options.origSize ?? options.size
this._time = options.time ?? Date.now()
}

Expand All @@ -48,11 +46,6 @@ abstract class BaseOrder {
this._size = size
}

// Getter for the original size of the order
get origSize (): number {
return this._origSize
}

// Getter for order timestamp
get time (): number {
return this._time
Expand All @@ -72,18 +65,22 @@ abstract class BaseOrder {
}
export class LimitOrder extends BaseOrder {
private readonly _type: OrderType.LIMIT
readonly _origSize: number
private _price: number
private readonly _timeInForce: TimeInForce
private readonly _isMaker: boolean
private readonly _makerQty: number
private readonly _takerQty: number
private readonly _postOnly: boolean
// Refers to the linked Stop Limit order stopPrice
private readonly _ocoStopPrice?: number
constructor (options: InternalLimitOrderOptions) {
super(options)
this._type = options.type
this._origSize = options.origSize
this._price = options.price
this._timeInForce = options.timeInForce
this._isMaker = options.isMaker
this._makerQty = options.makerQty
this._takerQty = options.takerQty
this._postOnly = options.postOnly ?? false
this._ocoStopPrice = options.ocoStopPrice
}
Expand Down Expand Up @@ -113,9 +110,19 @@ export class LimitOrder extends BaseOrder {
return this._postOnly
}

// Getter for order isMaker
get isMaker (): boolean {
return this._isMaker
// Getter for the original size of the order
get origSize (): number {
return this._origSize
}

// Getter for order makerQty
get makerQty (): number {
return this._makerQty
}

// Getter for order takerQty
get takerQty (): number {
return this._takerQty
}

get ocoStopPrice (): number | undefined {
Expand All @@ -131,7 +138,8 @@ export class LimitOrder extends BaseOrder {
price: ${this._price}
time: ${this._time}
timeInForce: ${this._timeInForce}
isMaker: ${this._isMaker as unknown as string}`
makerQty: ${this._makerQty}
takerQty: ${this._takerQty}`

toJSON = (): string => JSON.stringify(this.toObject())

Expand All @@ -144,7 +152,8 @@ export class LimitOrder extends BaseOrder {
price: this._price,
time: this._time,
timeInForce: this._timeInForce,
isMaker: this._isMaker
makerQty: this._makerQty,
takerQty: this._takerQty
})
}

Expand Down Expand Up @@ -172,7 +181,6 @@ export class StopMarketOrder extends BaseOrder {
type: ${this.type}
side: ${this._side}
size: ${this._size}
origSize: ${this._origSize}
stopPrice: ${this._stopPrice}
time: ${this._time}`

Expand All @@ -183,7 +191,6 @@ export class StopMarketOrder extends BaseOrder {
type: this.type,
side: this._side,
size: this._size,
origSize: this._origSize,
stopPrice: this._stopPrice,
time: this._time
})
Expand All @@ -194,7 +201,6 @@ export class StopLimitOrder extends BaseOrder {
private _price: number
private readonly _stopPrice: number
private readonly _timeInForce: TimeInForce
private readonly _isMaker: boolean
// It's true when there is a linked Limit Order
private readonly _isOCO: boolean
constructor (options: InternalStopLimitOrderOptions) {
Expand All @@ -203,7 +209,6 @@ export class StopLimitOrder extends BaseOrder {
this._price = options.price
this._stopPrice = options.stopPrice
this._timeInForce = options.timeInForce
this._isMaker = options.isMaker
this._isOCO = options.isOCO ?? false
}

Expand Down Expand Up @@ -232,11 +237,6 @@ export class StopLimitOrder extends BaseOrder {
return this._timeInForce
}

// Getter for order isMaker
get isMaker (): boolean {
return this._isMaker
}

// Getter for order isOCO
get isOCO (): boolean {
return this._isOCO
Expand All @@ -247,12 +247,10 @@ export class StopLimitOrder extends BaseOrder {
type: ${this.type}
side: ${this._side}
size: ${this._size}
origSize: ${this._origSize}
price: ${this._price}
stopPrice: ${this._stopPrice}
timeInForce: ${this._timeInForce}
time: ${this._time}
isMaker: ${this._isMaker as unknown as string}`
time: ${this._time}`

toJSON = (): string => JSON.stringify(this.toObject())

Expand All @@ -261,12 +259,10 @@ export class StopLimitOrder extends BaseOrder {
type: this.type,
side: this._side,
size: this._size,
origSize: this._origSize,
price: this._price,
stopPrice: this._stopPrice,
timeInForce: this._timeInForce,
time: this._time,
isMaker: this._isMaker
time: this._time
})
}

Expand Down
24 changes: 10 additions & 14 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ export class OrderBook {
const stopLimit = OrderFactory.createOrder({
...options,
type: OrderType.STOP_LIMIT,
isMaker: true,
timeInForce: options.timeInForce ?? TimeInForce.GTC
})
return this._stopOrder(stopLimit, response)
Expand Down Expand Up @@ -650,7 +649,6 @@ export class OrderBook {
size: options.size,
price: options.stopLimitPrice,
stopPrice: options.stopPrice,
isMaker: true,
timeInForce: options.stopLimitTimeInForce ?? TimeInForce.GTC,
isOCO: true
})
Expand Down Expand Up @@ -797,17 +795,21 @@ export class OrderBook {
this.executeConditionalOrder(side, priceBefore, response)

let order: LimitOrder
const takerQty = size - quantityToTrade
const makerQty = quantityToTrade
if (quantityToTrade > 0) {
order = OrderFactory.createOrder({
type: OrderType.LIMIT,
id: orderID,
side,
size: quantityToTrade,
origSize: size,
price,
time: Date.now(),
timeInForce,
postOnly,
isMaker: quantityToTrade === size,
takerQty,
makerQty,
...(ocoStopPrice !== undefined ? { ocoStopPrice } : {})
})
if (response.done.length > 0) {
Expand All @@ -834,11 +836,13 @@ export class OrderBook {
type: OrderType.LIMIT,
side,
size,
origSize: size,
price: totalPrice / totalQuantity,
time: Date.now(),
timeInForce,
postOnly,
isMaker: false
takerQty,
makerQty
})
response.done.push(order)
}
Expand Down Expand Up @@ -995,16 +999,8 @@ export class OrderBook {
if (headOrder !== undefined) {
if (response.quantityLeft < headOrder.size) {
response.partial = OrderFactory.createOrder({
type: OrderType.LIMIT,
id: headOrder.id,
side: headOrder.side,
size: headOrder.size - response.quantityLeft,
origSize: headOrder.origSize,
price: headOrder.price,
time: headOrder.time,
timeInForce: headOrder.timeInForce,
postOnly: headOrder.postOnly,
isMaker: true
...headOrder.toObject(),
size: headOrder.size - response.quantityLeft
})
this.orders[headOrder.id] = response.partial
response.partialQuantityProcessed = response.quantityLeft
Expand Down
10 changes: 2 additions & 8 deletions src/orderside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,10 @@ export class OrderSide {
): LimitOrder => {
this.remove(oldOrder)
const newOrder = OrderFactory.createOrder({
id: oldOrder.id,
type: oldOrder.type,
side: oldOrder.side,
...oldOrder.toObject(),
size: orderUpdate.size !== undefined ? orderUpdate.size : oldOrder.size,
origSize: oldOrder.origSize,
price: orderUpdate.price,
time: Date.now(),
timeInForce: oldOrder.timeInForce,
postOnly: oldOrder.postOnly,
isMaker: oldOrder.isMaker
time: Date.now()
})
this.append(newOrder)
return newOrder
Expand Down
13 changes: 6 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface BaseOrderOptions {
interface InternalBaseOrderOptions extends BaseOrderOptions {
type: OrderType
time?: number
origSize?: number
}
/**
* Specific options for a market order.
Expand All @@ -50,10 +49,12 @@ interface ILimitOrderOptions extends InternalBaseOrderOptions {
id: string
price: number
timeInForce: TimeInForce
isMaker: boolean
}
export interface InternalLimitOrderOptions extends ILimitOrderOptions {
type: OrderType.LIMIT
origSize: number
makerQty: number
takerQty: number
postOnly?: boolean
ocoStopPrice?: number
}
Expand Down Expand Up @@ -107,14 +108,15 @@ export interface IMarketOrder {
*/
export interface ILimitOrder {
id: string
type: OrderType
type: OrderType.LIMIT
side: Side
size: number
origSize: number
price: number
time: number
timeInForce: TimeInForce
isMaker: boolean
takerQty: number
makerQty: number
}

/**
Expand All @@ -125,7 +127,6 @@ export interface IStopMarketOrder {
type: OrderType
side: Side
size: number
origSize: number
stopPrice: number
time: number
}
Expand All @@ -138,12 +139,10 @@ export interface IStopLimitOrder {
type: OrderType
side: Side
size: number
origSize: number
price: number
stopPrice: number
timeInForce: TimeInForce
time: number
isMaker: boolean
}

/**
Expand Down
Loading

0 comments on commit 74f5907

Please sign in to comment.