Skip to content

Instantly share code, notes, and snippets.

@Kailang
Created October 27, 2017 20:44
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/949022f8bd0b83132db4f7113c5a94be to your computer and use it in GitHub Desktop.
Save Kailang/949022f8bd0b83132db4f7113c5a94be to your computer and use it in GitHub Desktop.
var matchOrder = function(corp) {
var self = this;
self.corp = corp;
debug('corp [%s] [%s]', self.corp.symbol, self.corp.name);
return Promise.all([
Order.findOne({
corp: self.corp._id,
action: 'buy',
type: 'limit',
unfilled: { $gt: 0 },
expired: { $gt: Date.now() },
is_failed: false,
}).sort('-price placed'),
Order.findOne({
corp: self.corp._id,
action: 'sell',
type: 'limit',
unfilled: { $gt: 0 },
expired: { $gt: Date.now() },
is_failed: false,
}).sort('price placed'),
Tick.findOne({
corp: self.corp._id,
}).sort('date'),
]).then(results => {
self.bid = results[0];
self.ask = results[1];
self.tick = results[2];
if (!self.bid || !self.ask) {
debug('no order');
throw 'No Order';
}
debug('bid [%d] x [%d]', self.bid.price, self.bid.quantity);
debug('ask [%d] x [%d]', self.ask.price, self.ask.quantity);
debug('last tick [%d] - [%d]', self.tick.bid, self.tick.ask);
if (self.bid.price < self.ask.price) {
if (self.bid.price != self.tick.bid || self.ask.price != self.tick.price) {
debug('new tick [%d] - [%d]', self.bid.price, self.ask.price);
Tick.create({ bid: self.bid.price, ask: self.ask.price, date: Date.now() });
}
debug('no trade');
throw 'No Trade';
}
return Promise.all([
Friend.findById(self.bid.friend),
Friend.findById(self.ask.friend),
Stock.findOne({ friend: self.bid.friend, corp: self.corp._id }),
Stock.findOne({ friend: self.ask.friend, corp: self.corp._id }),
]);
}).then(results => {
self.buyer = results[0];
self.seller = results[1];
self.buyerStock = results[2];
self.sellerStock = results[3];
debug('buyer [%s] [%d]', self.buyer.name, self.buyer.cash);
debug('seller [%s] [%d]', self.seller.name, self.seller.cash);
debug('buyer stock [%d]', self.buyerStock.quantity);
debug('seller stock [%d]', self.sellerStock.quantity);
self.quantity = Math.min(self.bid.unfilled, self.ask.unfilled);
self.buyAmount = self.quantity * self.bid.price;
self.sellAmount = self.quantity * self.ask.price;
self.makerCollect = self.buyAmount - self.sellAmount;
debug('quantity [%d]', self.quantity);
debug('buy amount [%d]', self.buyAmount);
debug('sell amount [%d]', self.sellAmount);
debug('maker collect [%d]', self.makerCollect);
if (self.buyer.cash < self.buyAmount) { // buyer cannot paid for the stock
self.bid.is_failed = true;
self.bid.save();
debug('buyer too poor');
throw 'Buyer Too Poor';
}
if (self.quantity < self.sellerStock.quantity) { // seller cannot sell the stock
self.ask.is_failed = true;
self.ask.save();
debug('seller too poor');
throw 'Seller Too Poor';
}
self.bid.unfilled -= self.quantity;
self.ask.unfilled -= self.quantity;
self.buyer.cash -= self.buyAmount;
self.seller.cash += self.sellAmount;
self.buyerStock.quantity += self.quantity;
self.sellerStock.quantity -= self.quantity;
gskse.maker.cash += self.makerCollect; // maket maker collect the spread
self.bid.save();
self.ask.save();
self.buyer.save();
self.seller.save();
self.buyerStock.save();
self.sellerStock.save();
gskse.maker.save();
return matchOrder(corp);
});
};
exports.matchOrder = matchOrder;
function() {
matchOrder(self.corp).catch(err => debug(err));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment