This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
orderbooks: convert base classes to TS
- Loading branch information
1 parent
ba21d75
commit 837d8ba
Showing
6 changed files
with
107 additions
and
94 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */ | ||
export class L2Point { | ||
public price: number; | ||
public size: number; | ||
public timestamp: number; | ||
public meta: any; | ||
|
||
constructor(price: number, size: number, timestamp?: number, meta?: any) { | ||
this.price = price; | ||
this.size = size; | ||
this.timestamp = timestamp; | ||
this.meta = meta; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Level 3 order book point | ||
*/ | ||
export class L3Point { | ||
public orderId: string; | ||
public price: number; | ||
public size: number; | ||
public timestamp: number; | ||
|
||
constructor(orderId: string, price: number, size: number, timestamp?: number) { | ||
this.orderId = orderId; | ||
this.price = price; | ||
this.size = size; | ||
this.timestamp = timestamp; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { L2Point } from "./L2Point"; | ||
import { L3Point } from "./L3Point"; | ||
|
||
export class L3PointStore { | ||
public points: Map<string, L3Point>; | ||
|
||
constructor() { | ||
this.points = new Map(); | ||
} | ||
|
||
public get(orderId: string): L3Point { | ||
return this.points.get(orderId); | ||
} | ||
|
||
public set(point: L3Point) { | ||
this.points.set(point.orderId, point); | ||
} | ||
|
||
public delete(orderId: string) { | ||
this.points.delete(orderId); | ||
} | ||
|
||
public has(orderId: string): boolean { | ||
return this.points.has(orderId); | ||
} | ||
|
||
public clear() { | ||
this.points.clear(); | ||
} | ||
|
||
public snapshot(depth: number, dir: "asc" | "desc"): L2Point[] { | ||
let sorter; | ||
switch (dir) { | ||
case "asc": | ||
sorter = sortAsc; | ||
break; | ||
case "desc": | ||
sorter = sortDesc; | ||
break; | ||
default: | ||
throw new Error("Unknown sorter"); | ||
} | ||
|
||
return Array.from(aggByPrice(this.points).values()).sort(sorter).slice(0, depth); | ||
} | ||
} | ||
|
||
function aggByPrice(map: Map<string, L3Point>): Map<number, L2Point> { | ||
// Aggregate the values into price points | ||
const aggMap: Map<number, L2Point> = new Map(); | ||
for (const point of map.values()) { | ||
const price = Number(point.price); | ||
const size = Number(point.size); | ||
|
||
// If we don't have this price point in the aggregate then we create | ||
// a new price point with empty values. | ||
if (!aggMap.has(price)) { | ||
aggMap.set(price, new L2Point(price, 0, 0)); | ||
} | ||
|
||
// Obtain the price point from the aggregation | ||
const aggPoint = aggMap.get(price); | ||
|
||
// Update the size | ||
aggPoint.size += size; | ||
} | ||
|
||
return aggMap; | ||
} | ||
|
||
function sortAsc(a: L3Point, b: L3Point) { | ||
return a.price - b.price; | ||
} | ||
|
||
function sortDesc(a: L3Point, b: L3Point) { | ||
return b.price - a.price; | ||
} |