Coder Social home page Coder Social logo

fasenderos / nodejs-order-book Goto Github PK

View Code? Open in Web Editor NEW
136.0 2.0 30.0 2.61 MB

Ultra-fast Node.js Order Book written in TypeScript for high-frequency trading (HFT) :rocket::rocket:

Home Page: https://github.com/fasenderos/nodejs-order-book

License: MIT License

JavaScript 4.81% TypeScript 95.19%
exchange hft hft-trading limit-order-book matching-algorithm matching-engine nodejs order-book orderbook trading

nodejs-order-book's Issues

Recover order book data when application restarts

I see your package used invertase/denque (stored in memory). My orders data has been saved on the database (application layer)

When my application(server) is restarted, how can I synchronize order data from my database with your package?

Stupid thing I thought of:

const lob = new OrderBook();
const orders = DB.table('orders').where('status', 'pending').all();
for order of orders {
   const result = lob.createOrder(order.type, order.side, order.size, order.price, order.id)
  // do something
}

But when the data is large, this is not feasible and unnecessary.
Any answer can help me, thanks

Modifying the order price should run the matching process

Describe the bug

Hi,
After a user modifies an order price, the updated order should be matched with other orders in the book.

To Reproduce

limit("sell", "id1", 1, 200);
limit("buy", "id2", 1, 100);
modify("id2", { side: "buy", size: 1, price: 200})

Result:
asks: 200 -> 1
--------------  
bids: 200 -> 1

Expected behavior

Empty order book

Question: market buy quantity

When a user makes a market order to buy some symbol, the exchange as a balance controller needs to freeze the user's money. But the market order does not have a price, and of course I do not know what amount to freeze.

In many systems, I noticed a good solution to this issue: for market buy orders, they accept the quantity in the quote currency (BTC/USD as base/quote, so they received quantity in USD), which allows me to correctly calculate the order book and freeze the excess money on the balance sheet.

Question: maybe you have some other solution or you can explain how to freeze money correctly if it is specified in the base currency.

For context: let's say I use your orderbook using asynchronous queues (kafka or nats). If I don't know what money to freeze, the user will have the option of double spending. On another side, I can check result and cancel market order but there are already fulfilled limit orders which are deleted from orderbook.

Thanks!

How to lock user balance on market order

Hello, please help me with this scenario
Pair Matic/USD
wallet balance of user1 is 10 USD
wallet balance of user2 is 5 USD

User1 want to sell 10 Matic at price of 1 USD
User2 place a MARKET order to buy 10 Matic but he use has balance of 5 USD.

How to stop this order and process according to wallet balance

buyOrderCanBeFilled/sellOrderCanBeFilled optimization

Since the OrderSide already has its own volume value, it would be good to first check whether the volume is in principle sufficient for the order.
I think it will speed up the check a bit.

Example:

  buyOrderCanBeFilled(orderSide: OrderSide, size: number, price: number) {

    // THIS IS PRE-CHECK
    if (orderSide.volume() < size) {
      return false
    }

    let cumulativeSize = 0
    orderSide.priceTree().forEach((_key, priceLevel) => {
      if (price >= priceLevel.price() && cumulativeSize < size) {
        cumulativeSize += priceLevel.volume()
      } else {
        return true // break the loop
      }
    })
    return cumulativeSize >= size
  }

v6 Conditional Orders (Help Wanted)

I'm excited to announce that the new version introduces conditional orders: Stop Limit and Stop Market. While the test coverage for these new features is at 100%, I'm not yet considering them stable. I kindly request the community's help in testing these features with real-world scenarios.

Your feedback is invaluable in identifying any potential issues and ensuring the reliability of these functionalities. Please report any bugs or unexpected behaviors you encounter.

Thank you for your support and contribution to improving our project!

How I create orderbook UI with your lib data

Hii, I am a beginner in blockchain & trading. I want to create an order book ui like this:-

Screenshot 2023-05-21 at 5 28 12 PM

This is Binance order ui

I found some data in your library functions

console.log('bids', book.bids);
console.log('asks', book.asks);
console.log('depth', book.depth());

please anyone guide me how i create and manage this.

v6 Conditional Orders - Stop Limit, Stop Market and OCO orders

I'm excited to announce that the new version introduces conditional orders: Stop Limit and Stop Market. While the test coverage for these new features is at 100%, I'm not yet considering them stable. I kindly request the community's help in testing these features with real-world scenarios.

Your feedback is invaluable in identifying any potential issues and ensuring the reliability of these functionalities. Please report any bugs or unexpected behaviors you encounter.

Thank you for your support and contribution to improving this project!

Discussion: canceled orders

If the order has a FOK, then it is not quite an error when it isn't fulfilled, it is simply a canceled order.

I have an idea: to add another property to the response, canceled orders, where are placed those orders that were canceled for several reasons:

  • FOK and IOC
  • OCO orders
  • canceled by user's decision

Let's discuss?

Simple test reveal bug

Describe the bug

Matching a sell to multiple buys does not produced the right results.

To Reproduce

lob.createOrder("limit", "buy", 100, 1.2, "A0");
lob.createOrder("limit", "buy", 100, 1.2, "A1");
console.log(lob.orders, "sell", lob.asks.toString(),"buy", lob.bids.toString() )
//sell buy
//1.2 -> 200
lob.createOrder("limit", "sell", 309, 1.2, "B0");
console.log(lob.orders, "sell", lob.asks.toString(),"buy", lob.bids.toString() )
//sell buy
//1.2 -> -9

Expected behavior

Should output this in the asks
//sell
//1.2 -> 109

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

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?

Order: traderID

First of all, thanks for your great work!


Created orders have no indication of who created them. It follows that I, as a trader, can buy from myself or sell to myself. It seems to me that this is not entirely logical.

I think it would be great to add a Trader ID for each order and just skip orders with the same Trader ID in the matching process.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.