Skip to content

Proposal for market depth snapshot #19

@BusinessDuck

Description

@BusinessDuck

Requirements

For using onDepth hook we need to get full depth snapshot, because onDepth stream using delta updates for price and volume at this price in current time.

Transport snapshot api call

getDepthSnapshot(): Map<number, number> // method returns object with price and volume at this price

Core hook

// Core method noop (implementation in strategy required by developer)
protected async onDepth(depth: Depth): Promise<void> {}

Strategy hook

async onDepthSnapshot(snapshot) {
   this.orderBookAggregator.useSnapshot(snapshot);
}

Market Depth builder example

class OrderBookAggregator {
    private bids = new Map<number, number>();
    private asks = new Map<number, number>();
    private splitPrice = 0;
    private maxPercent = 10;
    private precision = 1;
    private round = 50;

    constructor() {}

    update(depth: Depth) {
        const bidsSize = depth.bids.length - 1;

        depth.bids.forEach((item, idx) => {
            this.bids.set(item.price, item.qty);

            if (idx === bidsSize) {
                this.splitPrice = item.price;
            }
        });

        depth.asks.forEach((item) => {
            this.asks.set(item.price, item.qty);
        });

        // Keep memory clean
        this.bids.forEach((qty, price) => {
            const percent = math.percentChange(this.splitPrice, price);

            if (percent > this.maxPercent || percent < -this.maxPercent || price > this.splitPrice) {
                this.bids.delete(price);
            }
        });

        this.asks.forEach((qty, price) => {
            const percent = math.percentChange(this.splitPrice, price);

            if (percent > this.maxPercent || percent < -this.maxPercent || price <= this.splitPrice) {
                this.asks.delete(price);
            }
        });

        console.clear();

        this.printAsks();
        console.log('--------');
        this.printBids();
        // console.log(this.asks);
    }

    printBids() {
        console.log('Buyers (BID):');
        const result = {};
        Array.from(this.bids.keys()).forEach((key) => {
            const transformedKey = this.getKey(key);
            result[transformedKey] = result[transformedKey] || 0 + this.bids.get(key);
        });

        Object.keys(result)
            .sort()
            .reverse()
            .slice(0, 15)
            .forEach((key) => {
                console.log(key, result[key]);
            });
    }

    printAsks() {
        console.log('Sellers (ASK):');
        const result = {};
        Array.from(this.asks.keys()).forEach((key) => {
            const transformedKey = this.getKey(key);
            result[transformedKey] = result[transformedKey] || 0 + this.asks.get(key);
        });

        Object.keys(result)
            .sort()
            .reverse()
            .slice(-15)
            .forEach((key) => {
                console.log(key, result[key]);
            });

        // console.log(sorted);
    }

    private getKey(key: number) {
        if (this.round) {
            key = ~~(key / this.round) * this.round;
        }

        return math.toFixed(key, this.precision);
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationenhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions