fasenderos / hft-limit-order-book

Ultra-fast Node.js Limit Order Book for high-frequency trading (HFT) :rocket::rocket:
https://github.com/fasenderos/hft-limit-order-book
MIT License
128 stars 31 forks source link

TypeError: Cannot read properties of undefined (reading 'toString') on init OrderBook #404

Closed unauthxrized closed 1 week ago

unauthxrized commented 2 weeks ago

this.orderbook = new OrderBook({snapshot: snapshot.snapshot, enableJournaling: true}); on create orderbook got an error:

TypeError: Cannot read properties of undefined (reading 'toString') at OrderSide.append (/Users/una/Desktop/tests/matching/node_modules/hft-limit-order-book/dist/cjs/orderside.js:44:34) at OrderBook.restoreSnapshot (/Users/una/Desktop/tests/matching/node_modules/hft-limit-order-book/dist/cjs/orderbook.js:284:32) at new OrderBook (/Users/una/Desktop/tests/matching/node_modules/hft-limit-order-book/dist/cjs/orderbook.js:518:18)

snapshot: {"snapshot":{"bids":[{"price":30,"orders":["{\"id\":\"14:22:1720293920736\",\"side\":\"buy\",\"origSize\":1,\"size\":1,\"price\":30,\"time\":1720293920736,\"isMaker\":true}","{\"id\":\"14:22:1720293921301\",\"side\":\"buy\",\"origSize\":1,\"size\":1,\"price\":30,\"time\":1720293921301,\"isMaker\":true}"]}],"asks":[{"price":33,"orders":["{\"id\":\"14:22:1720293904645\",\"side\":\"sell\",\"origSize\":1,\"size\":1,\"price\":33,\"time\":1720293904646,\"isMaker\":true}","{\"id\":\"14:22:1720293912873\",\"side\":\"sell\",\"origSize\":1,\"size\":1,\"price\":33,\"time\":1720293912873,\"isMaker\":true}","{\"id\":\"14:22:1720293913076\",\"side\":\"sell\",\"origSize\":1,\"size\":1,\"price\":33,\"time\":1720293913076,\"isMaker\":true}"]}],"ts":1720293921302,"lastOp":5}}

how to fix pls?

unauthxrized commented 1 week ago

sry, err is not gone(

unauthxrized commented 1 week ago

i think we need "fromJSON" function

fasenderos commented 1 week ago

Hi thanks for reporting. I wasn't able to reproduce your error. What I noticed is that each object in the orders array is actually a string. If you remove the double quotes before and after the opening and closing braces of each object, you can initialize the order book without any problems.

const snapshot = {
  snapshot: {
    bids: [
      {
        price: 30,
        orders: [
          {
            id: '14:22:1720293920736',
            side: 'buy',
            origSize: 1,
            size: 1,
            price: 30,
            time: 1720293920736,
            isMaker: true
          },
          {
            id: '14:22:1720293921301',
            side: 'buy',
            origSize: 1,
            size: 1,
            price: 30,
            time: 1720293921301,
            isMaker: true
          }
        ]
      }
    ],
    asks: [
      {
        price: 33,
        orders: [
          {
            id: '14:22:1720293904645',
            side: 'sell',
            origSize: 1,
            size: 1,
            price: 33,
            time: 1720293904646,
            isMaker: true
          },
          {
            id: '14:22:1720293912873',
            side: 'sell',
            origSize: 1,
            size: 1,
            price: 33,
            time: 1720293912873,
            isMaker: true
          },
          {
            id: '14:22:1720293913076',
            side: 'sell',
            origSize: 1,
            size: 1,
            price: 33,
            time: 1720293913076,
            isMaker: true
          }
        ]
      }
    ],
    ts: 1720293921302,
    lastOp: 5
  }
}

const book = new OrderBook({ snapshot: snapshot.snapshot, enableJournaling: true })
console.log(book.toString())

33 -> 3
------------------------------------
30 -> 2
fasenderos commented 1 week ago

I think you need JSON.parse() each order in the bids and asks arrays.

unauthxrized commented 1 week ago

I think you need JSON.parse() each order in the bids and asks arrays.

yes, i think about it. Thx.