-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderbook.js
89 lines (77 loc) · 2.06 KB
/
orderbook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function OrderObject(buyOrSell, price, size, exchange) {
this.done = false,
this.getFirstCoinOfExchange = function(exchange)
{
return exchange.substr(0,3);
},
this.getSecondCoinOfExchange = function(exchange)
{
return exchange.substr(4,3);
},
this.printOrder = function()
{
console.log("*----Order Object----*");
for (var key in this) {
if(typeof this[key] != "function")
console.log(key, ": ", this[key]);
}
console.log("......................");
},
this.generateClientID = function ()
{
var id = "trex" + Math.random();
console.log("Generating id ", id);
return id;
},
this.clientID = this.generateClientID(),
this.side = buyOrSell,
this.product_id = exchange,
this.priceCoin = this.getSecondCoinOfExchange(exchange),
this.price = price.toString(),
this.sizeCoin = this.getFirstCoinOfExchange(exchange),
this.size = size.toString()
};
function OrderBook() {
this.book = [],
this.checkIfIDExists = function (id) {
for (var i = 0; i < this.book.length; i++) {
if(this.book[i].clientID == id)
return true;
}
return false;
},
this.createOrderAndGetID = function (buyOrSell, price, size, exchange) {
var newOrder = new OrderObject(buyOrSell, price, size, exchange);
this.book.push(newOrder);
return newOrder.clientID;
},
this.markOrderComplete = function (id) {
for (var i = 0; i < this.book.length; i++) {
if(this.book[i].clientID == id)
{
this.book[i].done = true;
return true;
}
}
return false;
},
this.printOrderBook = function()
{
for (var i = 0; i < this.book.length; i++) {
this.book[i].printOrder();
}
}
}
function testOrderBook()
{
var myOrderBook = new OrderBook();
for (var i = 0; i < 5; i++) {
var buyOrSell = Math.random() < 0.5 ? 'buy' : 'sell';
var price = Math.floor((Math.random() * 10) / 0.01) * 0.01;
var size = Math.floor((Math.random() * 10) / 0.01) * 0.01;
var exchange = Math.random() < 0.5 ? 'BTC-USD' : 'ETH-LTC';
myOrderBook.createOrderAndGetID(buyOrSell, price, size, exchange);
}
myOrderBook.printOrderBook();
}
testOrderBook();