Skip to content

Commit

Permalink
Not deleting irrelevant orders.
Browse files Browse the repository at this point in the history
  • Loading branch information
davesade committed Oct 27, 2016
1 parent 6058721 commit b593f64
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
40 changes: 40 additions & 0 deletions MySQLdb/database_setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
CREATE SCHEMA `microexchange` ;

CREATE TABLE `history` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`full` varchar(45) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`full`)
) ENGINE=InnoDB AUTO_INCREMENT=561 DEFAULT CHARSET=latin1;

CREATE TABLE `orderbook` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`order_type` varchar(4) NOT NULL,
`product_name` varchar(10) NOT NULL,
`price` int(11) NOT NULL,
`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`order_id`,`order_type`,`price`,`product_name`)
) ENGINE=InnoDB AUTO_INCREMENT=233 DEFAULT CHARSET=latin1;

CREATE TABLE `order_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`type` varchar(4) NOT NULL,
PRIMARY KEY (`id`,`type`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

CREATE TABLE `products` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(45) NOT NULL,
PRIMARY KEY (`product_id`,`product_name`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

INSERT INTO `microexchange`.`order_types`
(`type`)
VALUES
('buy'), ('sell');

INSERT INTO `microexchange`.`products`
(
`product_name`)
VALUES
('wood'), ('stone'), ('gold'), ('oil');
4 changes: 2 additions & 2 deletions dataAccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ exports.deleteLowestBid = function(data, callback)
exports.deleteHighestAsk = function(data, callback)
{
var query = "DELETE FROM orderbook WHERE product_name='" + data.product + "' AND order_type='" + data.command + "' ORDER BY price DESC LIMIT 1";

console.log(query);
sqlBase.getSingleRecord(query, callback);
}
// This is actually match making. If this succeeds, it will inform user about successful trade!
exports.deleteMatchedOrders = function(data, callback)
{
var query = "DELETE t1,t2 FROM orderbook t1, orderbook t2 WHERE t1.product_name = '" + data.product +"' AND t1.product_name = t2.product_name AND t1.price = " + data.price + " AND t1.price = t2.price AND t1.order_type <> t2.order_type";
var query = "delete from microexchange.orderbook where order_id in ( select order_id from ( (select order_id from microexchange.orderbook where price = " + data.price + " and product_name = '" + data.product +"' and order_type='SELL' limit 1) union (select order_id from microexchange.orderbook where price = " + data.price + " and product_name = '" + data.product +"' and order_type='BUY' limit 1) ) as t1 )"

sqlBase.getStaticData(query, callback);
}
34 changes: 17 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,29 +93,29 @@ exports.handler = function(event, context) {
var totalOrders = '';
_.forEach(countRows, function(value) {
totalOrders = value;
if (totalOrders => market_depth) {


if (totalOrders > market_depth) {
// SWITCH pro rozeznani, zda-li potrebujeme smazat na buy nebo sell side.
switch (data.command) {
case "BUY":
da.deleteLowestBid(data, function(err, delRows) {
if (err !== null) {
context.fail(err);
} else {
console.log('Irrelevant BUY orders found!');
}
});
da.deleteLowestBid(data, function(err, delRows) {
if (err !== null) {
context.fail(err);
} else {
console.log('Irrelevant BUY orders found!');
}
});
break;
case "SELL":
console.log('Amount of orders in the book: ' + totalOrders);
da.deleteHighestAsk(data, function(err, delRows) {
if (err !== null) {
context.fail(err);
} else {
console.log('Irrelevant SELL orders found!');
}
});
break;
if (err !== null) {
console.log(err + 'here');
context.fail(err);
} else {
console.log('Irrelevant SELL orders found!' + delRows);
}
});
break;
}
// Konec Switche.

Expand Down
18 changes: 10 additions & 8 deletions sqlBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ var mysql = require('mysql');
function connectionStart()
{
var connection = mysql.createConnection({
host : 'futuredb.cbhsjvpjrptr.us-west-2.rds.amazonaws.com',
host : 'microexchange.cbhsjvpjrptr.us-west-2.rds.amazonaws.com',
database : 'microexchange',
user : 'marty',
password : 'martymarty',
user : 'microadmin',
password : 'micropassword',
});
return connection;
}
Expand Down Expand Up @@ -34,18 +34,20 @@ exports.getStaticData = function(sqlQuery, callback)
exports.getSingleRecord = function(sqlQuery, callback)
{
var connection = connectionStart();
//console.log(callback);
//console.log(sqlQuery);
console.log(callback);
console.log(sqlQuery);
connection.connect();
connection.query(sqlQuery, function(err, rows, fields){
if (err)
{
callback(err,null);
}
else
// console.log('ROWS ' + rows[0]);
// console.log('FIELDS ' + fields[0]);
callback(null, rows[0]);
{
console.log('ROWS ' + rows);
console.log('FIELDS ' + fields[0]);
callback(null, rows[0]);
}
});
connection.end();
}
2 changes: 1 addition & 1 deletion test_events/event_unique.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"text" : "sell iron 99"
"text" : "buy wood 21"
}

0 comments on commit b593f64

Please sign in to comment.