-
Notifications
You must be signed in to change notification settings - Fork 0
/
runRandomOrderGenerator.js
161 lines (138 loc) · 3.59 KB
/
runRandomOrderGenerator.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var uuid = require('uuid');
var WebSocket = require('ws');
var http = require('http');
var events = require('./eventEmitter.js');
var operations = 0;
var accountId = '58774677cd7cf607521df629';
var orderList = [];
var options = {
host: 'localhost',
port: 3033,
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
};
var callback = function(response) {
/*var str = '';
response.on('data', function (chunk) {
str += chunk;
});
response.on('end', function () {
console.log(str);
});*/
response.on('error', function (err) {
console.log('ERROR:', err);
});
};
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});
events.on('newOrder', function(order){
var path = '/submitOrder?'
path += 'timestamp='+order.timestamp;
path += '&id='+order.id;
path += '&accountId='+accountId;
path += '&type='+order.type;
path += '&price='+order.price;
path += '&amount='+order.amount;
options.path = path;
http.request(options, callback).end();
operations++;
});
function getBidOrAsk(){
var res = Math.random() >= 0.5;
if(res){
return 'bid';
} else {
return 'ask';
}
}
function getOffer(){
var price = 700 + Math.floor((Math.random() * 100) + 1);
var amount = Number((Math.floor((Math.random() * 1000) + 1)/100).toFixed(2));
return {price: price, amount: amount};
}
function getAndRemoveRandomOrder(){
var rInt = Math.floor(Math.random() * (orderList.length-1));
return orderList.splice(rInt, 1)[0];
}
function createCancelOrder(cb){
var orderTimestamp = new Date().getTime();
var bidOrAsk = getBidOrAsk();
var orderToCancel = getAndRemoveRandomOrder();
if(orderToCancel !== undefined){
bidOrAsk = 'cancel' + bidOrAsk;
var order = {
timestamp: orderTimestamp,
id: orderToCancel.id,
accountId: orderToCancel.accountId,
type: bidOrAsk,
price: orderToCancel.price,
amount: orderToCancel.amount
};
cb(order);
}
}
// Asks: willing to sell currency1 for currency2
// Bids: willing to buy currency1 with currency2
function createOrder(cb){
var orderTimestamp = new Date().getTime();
var orderId = uuid.v1();
var bidOrAsk = getBidOrAsk();
var offer = getOffer();
var order = {
timestamp: orderTimestamp,
id: orderId,
accountId: accountId,
type: bidOrAsk,
price: offer.price,
amount: offer.amount
};
// TODO: this needs to move to a place where account balances are checked before the order is
// allowed on the orderbook
/*if(order.type == 'ask' && order.amount > (account.currency1 - account.reservedCurrency1)){
cb(null);
} else if(order.type == 'bid'
&& (order.amount*order.price) > (account.currency2 - account.reservedCurrency2)){
cb(null)
} else {
cb(order);
}*/
cb(order);
}
function createNewOrders(){
setInterval(function(){
for(var i = 1; i--;){
createOrder(function(order){
if(order){
orderList.push(order);
if(order.type == 'bid'){
order.amount *= 1.1;
}
events.emit('newOrder', order);
}
});
}
}, 1);
}
function createCancelOrders(){
setInterval(function(){
if(orderList.length > 200){
createCancelOrder(function(order){
events.emit('newOrder', order);
});
}
}, 10);
}
function run(){
var startTime = new Date().getTime();
setInterval(function(){
var currTime = new Date().getTime();
console.log('Ops:', (operations/(currTime-startTime)*1000).toFixed(2));
}, 2000);
createNewOrders();
createCancelOrders();
}
run();