Giter VIP home page Giter VIP logo

Comments (4)

fasenderos avatar fasenderos commented on June 15, 2024

Hi, before sending an order to the orderbook your application have to check if the user can send an order, next decrease the balance as per the order size, execute the order and if something goes wrong or the order is not fully executed increase again the balance (pseudo code):

function userCanTrade(user, order) {
  return user.isActive && user.availableBalance >= order.size
}

function setAvailableBalance(user, newBalance) {
  return this.userService.updateById(user.id, {availableBalance: newBalance})
}

function async createOrder(user, order) {
  try {
    let orderExecuted;
    let updatedUser;
    if (userCanTrade(user)) {
      // Decrease available balance
      updatedUser = await setAvailableBalance(user, user.availableBalance - order.size);
      orderExecuted = lob.market(order.side, order.size)
      if (orderExecuted.quantityLeft > 0) {
        // Increase available balance for the quantity not executed
        await setAvailableBalance(updatedUser, updatedUser.availableBalance + response.quantityLeft);
      }
    } else {
     throw new Error('Something wrong')
    }
  } catch((e) => {
    console.error(e.message)
    if (!orderExecuted && updatedUser) {
      // User balance has been updated but the order has not been executed
      // so we restore the original balance
       await setAvailableBalance(user, user.availableBalance);
    }
  }
}

from hft-limit-order-book.

ohari5336 avatar ohari5336 commented on June 15, 2024

When user 2 add order of buy 10 matic according my given situation, and according your code it's lock user matic how it's possible.

In real scenario user 2 usd lock but without price how i do this (in buy order every time amount multiply by size), and also my situation user have nor enough balance soo if your order matching engine match this with 2-3 buy orders then how i rollback this because user don't have enough balance in his account

from hft-limit-order-book.

fasenderos avatar fasenderos commented on June 15, 2024

Yes of course was a pseudo code for the sake of example. The responsibility of the orderbook is to matching orders. Every other aspect (user can trade?? user has enough money?? current price?? persist order on DB??) must be managed by the application by reading every response of the order book and updating the state accordingly

So in a real exchange you keep track of the market price for several reasons, say to show data in a graph or execute some cron job at specific price etc. and in order to not degrading the orderbook performance you can't call it every time to known the current price, so you have to keep track of the current price in a separate service (like redis and persist as a timeseries on DB every minute for the OHLC chart) by updating it at every order that move the price.

from hft-limit-order-book.

fasenderos avatar fasenderos commented on June 15, 2024

Closed for inactivity. Reopen if you need more help.

from hft-limit-order-book.

Related Issues (14)

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.