Comments (4)
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.
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.
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.
Closed for inactivity. Reopen if you need more help.
from hft-limit-order-book.
Related Issues (14)
- Order: traderID HOT 2
- Simple test reveal bug HOT 1
- Stop-limit, stop-market and OCO orders HOT 6
- time in force parameter: GTC, FOK, IOC HOT 2
- Taker/Maker flag for each order in response HOT 1
- buyOrderCanBeFilled/sellOrderCanBeFilled optimization HOT 1
- Discussion: canceled orders
- Question: market buy quantity HOT 4
- How to use this library with DB HOT 3
- How I create orderbook UI with your lib data HOT 1
- stop self buy sell orders match HOT 3
- add benchmark img
- Modifying the order price should run the matching process HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hft-limit-order-book.