-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorder.js
331 lines (281 loc) · 9.45 KB
/
order.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const express = require('express')
const app = express()
const fs = require('fs')
const log = require('ololog').configure({
locate: false
})
const _ = require('lodash')
const chance = require('chance').Chance()
const Client = require('node-rest-client').Client
const client = new Client()
const url = 'https://min-api.cryptocompare.com/data/histominute?fsym=EMC2&tsym=BTC&limit=20&aggregate=1&e=bittrex'
function __timestamp () {
return (new Date).getTime()
}
async function match_sell_order(order) {
log.bright.red('socket.on place_sell_order: ', JSON.stringify(order, null, 2))
return new Promise(async function(resolve) {
//TODO: Change to REST Client
fs.readFile(__dirname + '/' + 'order_book.json', 'utf8', function(err, data) {
let bids = JSON.parse(data).buys
// console.log(JSON.stringify(bids, null, 2))
if (order.side === 'sell') {
for (let i = 0; i < bids.length; i++) {
if (order.prc > bids[i].prc) {
if (order.qty_remaining <= 0) {
order.status = 'filled'
} else {
order.status = 'partial'
}
resolve({
bids: bids,
order: order,
where: 'A: (order.prc > bids[i].prc)'
})
return // Only iterates as far as order meets match engine conditions
} else {
if (order.qty_remaining <= 0) {
resolve({
bids: bids,
order: order,
where: 'B: (order.qty_remaining <= 0)'
})
} else if (order.qty_remaining === bids[i].qty_remaining) {
order.qty_remaining = order.qty_remaining - bids[i].qty_remaining
bids[i].qty_remaining = 0
bids[i].status = 'filled'
log.cyan(JSON.stringify(order, null, 2))
resolve({
bids: bids,
order: order,
where: 'C: (order.qty_remaining === bids[i].qty_remaining)'
})
} else if (order.qty_remaining > bids[i].qty_remaining) {
order.qty_remaining = (order.qty_remaining - bids[i].qty_remaining).toFixed(1)
bids[i].qty_remaining = 0
bids[i].status = 'filled'
} else {
bids[i].qty_remaining = (bids[i].qty_remaining - order.qty_remaining).toFixed(1)
if (bids[i].qty_remaining <= 0) {
bids[i].status = 'filled'
} else {
bids[i].status = 'partial'
}
order.qty_remaining = 0
order.status = 'filled'
resolve({
bids: bids,
order: order,
where: 'D: else'
})
}
}
}
}
})
})
}
async function match_buy_order (order) {
log.lightBlue(JSON.stringify(order, null, 2))
return new Promise(async function (resolve) {
//TODO: Change to REST Client
fs.readFile(__dirname + '/' + 'order_book.json', 'utf8', function (err, data) {
let asks = JSON.parse(data).sells
// console.log(JSON.stringify(asks, null, 2))
if (order.side === 'buy') {
for (let i = asks.length - 1; i >= 0; i--) {
if (order.prc < asks[i].prc) {
if (order.qty_remaining <= 0) {
order.status = 'filled'
} else {
order.status = 'partial'
}
resolve({
asks: asks,
order: order,
where: 'A: (order.prc > asks[i].prc)'
})
return // Only iterates as far as order meets match engine conditions
} else {
if (order.qty_remaining <= 0) {
resolve({
asks: asks,
order: order,
where: 'B: (order.qty_remaining <= 0)'
})
} else if (order.qty_remaining === asks[i].qty_remaining) {
order.qty_remaining = order.qty_remaining - asks[i].qty_remaining
asks[i].qty_remaining = 0
asks[i].status = 'filled'
log.cyan(JSON.stringify(order, null, 2))
resolve({
asks: asks,
order: order,
where: 'C: (order.qty_remaining === asks[i].qty_remaining)'
})
} else if (order.qty_remaining > asks[i].qty_remaining) {
order.qty_remaining = (order.qty_remaining - asks[i].qty_remaining).toFixed(1)
asks[i].qty_remaining = 0
asks[i].status = 'filled'
} else {
asks[i].qty_remaining = (asks[i].qty_remaining - order.qty_remaining).toFixed(1)
if (asks[i].qty_remaining <= 0) {
asks[i].status = 'filled'
} else {
asks[i].status = 'partial'
}
order.qty_remaining = 0
order.status = 'filled'
resolve({
asks: asks,
order: order,
where: 'D: else'
})
}
}
}
}
})
})
}
async function place_sell_order (sell_order) {
match_sell_order(sell_order)
.then(function (resolved) {
/** Bids */
log.lightBlue(JSON.stringify(resolved.bids, null, 2)) //TODO: Remove
for (let i = 0; i < resolved.bids.length; i++) {
if (resolved.bids[i].status === 'filled') {
log.lightYellow('Remove Order from DB: ', JSON.stringify(resolved.bids[i], null, 2))
} else if (resolved.bids[i].status === 'partial') {
log.black('Update Order on DB: ', JSON.stringify(resolved.bids[i], null, 2))
}
}
/** Order */
if (resolved.order.qty === resolved.order.qty_remaining) {
resolved.order.status = 'open'
log.black('Send Sell Order to DB: ', JSON.stringify(resolved.order, null, 2))
} else {
if (resolved.order.status === 'filled') {
log.lightYellow('socket.emit sell_order_filled: ', JSON.stringify(resolved.order, null, 2))
} else {
log.black('Send Sell Order to DB: ', JSON.stringify(resolved.order, null, 2))
}
}
/** Where */ //TODO: Remove
log.bright.cyan(JSON.stringify(resolved.where, null, 2))
})
.catch(function (err) {
log.lightYellow('ERROR: ', JSON.stringify(err.message, null, 2))
})
}
async function place_buy_order (buy_order) {
match_buy_order(buy_order)
.then(function (resolved) {
/** Asks */
log.lightRed(JSON.stringify(resolved.asks, null, 2)) //TODO: Remove
for (let i = 0; i < resolved.asks.length; i++) {
if (resolved.asks[i].status === 'filled') {
log.lightYellow('Remove Order from DB: ', JSON.stringify(resolved.asks[i], null, 2))
} else if (resolved.asks[i].status === 'partial') {
log.black('Update Order on DB: ', JSON.stringify(resolved.asks[i], null, 2))
}
}
/** Order */
if (resolved.order.qty === resolved.order.qty_remaining) {
resolved.order.status = 'open'
log.black('Send Buy Order to DB: ', JSON.stringify(resolved.order, null, 2))
} else {
if (resolved.order.status === 'filled') {
log.bright.blue('socket.emit buy_order_filled: ', JSON.stringify(resolved.order, null, 2))
} else {
log.black('Send Buy Order to DB: ', JSON.stringify(resolved.order, null, 2))
}
}
/** Where */ //TODO: Remove
log.bright.cyan(JSON.stringify(resolved.where, null, 2))
})
.catch(function (err) {
log.lightYellow('ERROR place_buy_order: ', JSON.stringify(err.message, null, 2))
})
}
// (async function () {
// let symbol = 'BTC/USD'
//
// // let qty = 1.8
// // let qty = 2.1
// let qty = 3.5
//
// // let prc = 9080
// let prc = 9110
//
// let sell_order = await SellLimit(symbol, prc, qty)
// await place_sell_order(sell_order)
//
// })();
(async function () {
let symbol = 'BTC/USD'
// let qty = 1.3
// let qty = 2.1
let qty = 3.5
// let prc = 9100
let prc = 9130
let buy_order = await BuyLimit(symbol, prc, qty)
await place_buy_order(buy_order)
})()
/** Transaction Functions */
async function Order (side, type, order) {
const state = {
oid: chance.first(),
id: chance.guid(),
timestamp: __timestamp(),
type,
side
}
return new Promise(async function (resolve) {
resolve(await Object.assign({}, state, order))
})
}
async function SellLimit (symbol, prc, qty) {
try {
let order = {
symbol,
prc,
qty,
qty_remaining: qty,
status: 'open'
}
return await Order('sell', 'limit', order).then(async function (order_res) {
return order_res
}).catch(function (err) {
log.lightYellow('.catch() SellLimit: ', err.message)
})
} catch (__err) {
log.lightYellow('try catch SellLimit: ', __err.message)
}
}
async function BuyLimit (symbol, prc, qty) {
try {
let order = {
symbol,
prc,
qty,
qty_remaining: qty,
status: 'open'
}
return await Order('buy', 'limit', order).then(async function (order_res) {
return order_res
}).catch(function (err) {
log.lightYellow('.catch() BuyLimit: ', err.message)
})
} catch (__err) {
log.lightYellow('try catch BuyLimit: ', __err.message)
}
}
// const server = app.listen(8081, function () {
//
// const host = server.address().address
// const port = server.address().port
//
// console.log('Example app listening at http://%s:%s', host, port)
//
// })