Skip to content

Instantly share code, notes, and snippets.

@Kailang
Created October 26, 2017 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kailang/2df6a196921c1a888225ef6c248e6866 to your computer and use it in GitHub Desktop.
Save Kailang/2df6a196921c1a888225ef6c248e6866 to your computer and use it in GitHub Desktop.
router.post('/trade', function(req, res, next) {
req.body.quantity = parseInt(req.body.quantity);
var self = this;
self.corp = res.locals.corp,
self.friend = res.locals.friend;
Order.create({
friend: self.friend._id,
corp: self.corp._id,
quantity: req.body.quantity,
unfilled: req.body.quantity,
deposit: 0,
action: req.body.action,
type: req.body.type,
placed: Date.now(),
expired: gskse.getOrderExpiredDate(req.body.duration),
filled: gskse.epoch,
price: req.body.type == 'limit' ? req.body.price : 0,
deals: [],
is_filling: true,
}).then(order => {
self.order = order;
if (order.action == 'buy') {
debug('buy at limit');
var deposit = order.quantity * order.price;
res.locals.friend.cash -= deposit;
res.locals.friend.save().then(friend => {
order.deposit = deposit;
order.save();
return Order.find({
corp: self.corp._id,
action: 'sell',
type: 'limit',
price: { $lte: order.price },
unfilled: { $gt: 0 },
expired: { $gt: Date.now() },
is_filling: false,
}).sort('price');
}).then(asks => {
if (!asks || !asks.length) {
debug('nothing to fill');
order.is_filling = false;
return;
}
for (var i = 0; i < asks.length; i++) {
var ask = asks[i];
var price = ask.price,
quantity = ask.unfilled,
amount = price * quantity;
dabug('\task [%d] x [%d]', price, quantity);
if (ask.unfilled < order.unfilled) { // order unfilled, ask filled
debug('\t\tdeal: order [unfilled], ask [filled]', price, quantity);
order.unfilled -= quantity;
order.deposit -= amount;
order.deals.push({ order: ask._id, quantity: quantity });
order.save();
ask.unfilled = 0;
ask.deposit += amount;
ask.deal.push({ order: order._id, quantity: quantity });
ask.save();
} else if (ask.unfilled > order.unfilled) { // order filled, ask unfilled
debug('\t\tdeal: order [filled], ask [unfilled]', price, quantity);
order.unfilled = 0;
order.deposit -= amount;
order.deals.push({ order: ask._id, quantity: quantity });
order.save();
ask.unfilled -= quantity;
ask.deposit += amount;
ask.deal.push({ order: order._id, quantity: quantity });
ask.save();
break;
} else { // order filled, ask filled
debug('\t\tdeal: order [unfilled], ask [filled]', price, quantity);
order.unfilled = 0;
order.deposit -= amount;
order.deals.push({ order: ask._id, quantity: quantity });
order.save();
ask.unfilled = 0;
ask.deposit += amount;
ask.deal.push({ order: order._id, quantity: quantity });
ask.save();
break;
}
}
if (order.unfilled > 1) { // order unfilled
debug('order still unfilled');
order.is_filling = false;
order.save();
}
});
} else if (order.action == 'sell') {
debug('sell at limit');
Order.find({
corp: self.corp._id,
action: 'buy',
type: 'limit',
price: { $gte: order.price },
unfilled: { $gt: 0 },
expired: { $gt: Date.now() },
is_filling: false,
}).sort('price').then(bids => {
if (!bids || !bids.length) {
debug('nothing to fill');
order.is_filling = false;
return;
}
for (var i = 0; i < bids.length; i++) {
var bid = bids[i];
var price = bid.price,
quantity = bid.unfilled;
dabug('\tbid [%d] x [%d]', price, quantity);
if (bid.unfilled < order.unfilled) { // order unfilled, bid filled
debug('\t\tdeal: order [unfilled], bid [filled]', price, quantity);
order.unfilled -= quantity;
order.deals.push({ order: bid._id, quantity: quantity });
order.save();
bid.unfilled = 0;
bid.deal.push({ order: order._id, quantity: quantity });
bid.save();
} else if (bid.unfilled > order.unfilled) { // order filled, bid unfilled
debug('\t\tdeal: order [filled], bid [unfilled]', price, quantity);
order.unfilled = 0;
order.deals.push({ order: bid._id, quantity: quantity });
order.save();
bid.unfilled -= quantity;
bid.deal.push({ order: order._id, quantity: quantity });
bid.save();
break;
} else { // order filled, bid filled
debug('\t\tdeal: order [unfilled], bid [filled]', price, quantity);
order.unfilled = 0;
order.deals.push({ order: bid._id, quantity: quantity });
order.save();
bid.unfilled = 0;
bid.deal.push({ order: order._id, quantity: quantity });
bid.save();
break;
}
}
if (order.unfilled > 1) { // order unfilled
debug('order still unfilled');
order.is_filling = false;
order.save();
}
});
}
}).catch(err => next(err));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment