Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
orderbooks: convert base classes to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
bmancini55 committed Jun 17, 2021
1 parent ba21d75 commit 837d8ba
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 94 deletions.
9 changes: 0 additions & 9 deletions src/orderbooks/L2Point.js

This file was deleted.

14 changes: 14 additions & 0 deletions src/orderbooks/L2Point.ts
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;
}
}
13 changes: 0 additions & 13 deletions src/orderbooks/L3Point.js

This file was deleted.

16 changes: 16 additions & 0 deletions src/orderbooks/L3Point.ts
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;
}
}
72 changes: 0 additions & 72 deletions src/orderbooks/L3PointStore.js

This file was deleted.

77 changes: 77 additions & 0 deletions src/orderbooks/L3PointStore.ts
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;
}

0 comments on commit 837d8ba

Please sign in to comment.