forked from bitcoinvsalts/node-binance-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
570 lines (498 loc) · 20 KB
/
server.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
const express = require("express")
const io_client = require("socket.io-client")
const path = require("path")
const binance = require("binance-api-node").default
const moment = require("moment")
const BigNumber = require("bignumber.js")
const _ = require("lodash")
const tulind = require("tulind")
const axios = require("axios")
const { Client } = require("pg")
const env = require("./env")
const PORT = env.SERVER_PORT
const INDEX = path.join(__dirname, "index.html")
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// PLEASE EDIT THE FOLLOWING VARIABLES JUST BELOW
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
const insert_into_db = env.DATABASE_INSERT_PAIR_HISTORY
const pg_connectionString = env.DATABASE_URL
const pg_connectionSSL = env.DATABASE_CONNECT_VIA_SSL
// to monitor your strategy you can send your buy and sell signals to http://bitcoinvsaltcoins.com
const send_signal_to_bva = env.CONNECT_SERVER_TO_BVA
const bva_key = env.BVA_API_KEY
const wait_time = 800
const timeframe = env.STRATEGY_TIMEFRAME
const nbt_vers = env.VERSION
const pairs = ["BTCUSDT"] //, 'ETHBTC', 'XRPBTC', 'XRPETH']
const stratname = "DEMO STRATS"
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
console.log("insert_into_db: ", insert_into_db)
console.log("send_signal_to_bva: ", send_signal_to_bva)
/////////////////////
let pairData = {}
let openSignals = {}
const nbt_prefix = "nbt_"
const interv_time = 10000
/////////////////////////////////////////////////////////////////////////////////
let socket_client = {}
if (send_signal_to_bva) {
console.log("Connection to NBT HUB...")
// retrieve previous open signals from HUB
axios
.get("https://bitcoinvsaltcoins.com/api/useropensignals?key=" + bva_key)
.then((response) => {
response.data.rows.map((s) => {
openSignals[s.pair + s.stratname.replace(/\s+/g, "")] = {
buy_price: BigNumber(s.buy_price),
sell_price: BigNumber(s.sell_price),
stop_profit: Number(s.stop_profit),
stop_loss: Number(s.stop_loss),
type: s.type,
}
})
console.log("Open Trades:", _.values(openSignals).length)
console.log(_.keys(openSignals))
})
.catch((e) => {
console.log("ERROR 8665")
console.log(e.response.data)
})
// create a socket client connection to send your signals to NBT Hub (http://bitcoinvsaltcoins.com)
socket_client = io_client("https://nbt-hub.herokuapp.com", {
query: "v=" + nbt_vers + "&type=server&key=" + bva_key,
})
}
//////////////////////////////////////////////////////////////////////////////////
let pg_client
if (insert_into_db && pg_connectionString) {
console.log("Connecting to the Postgresql db...")
pg_client = new Client({
ssl: pg_connectionSSL,
connectionString: pg_connectionString,
})
pg_client.connect()
}
//////////////////////////////////////////////////////////////////////////////////
const server = express()
.use((req, res) => res.sendFile(INDEX))
.listen(PORT, () => console.log(`NBT server running on port ${PORT}`))
//////////////////////////////////////////////////////////////////////////////////
const binance_client = binance()
//////////////////////////////////////////////////////////////////////////////////
async function run() {
console.log(" ")
console.log("Total pairs: " + pairs.length)
console.log(" ")
console.log(JSON.stringify(pairs))
console.log(" ")
await sleep(wait_time)
await trackData()
}
//////////////////////////////////////////////////////////////////////////////////
async function trackData() {
console.log("----")
for (var i = 0, len = pairs.length; i < len; i++) {
console.log("--> " + pairs[i])
if (insert_into_db) await createPgPairTable(pairs[i])
await trackPairData(pairs[i])
await sleep(wait_time) //let's be safe with the api biance calls
}
console.log("----")
}
function addCandle(pair, candle) {
pairData[pair].candle_opens.push(Number(candle.open))
pairData[pair].candle_closes.push(Number(candle.close))
pairData[pair].candle_lows.push(Number(candle.low))
pairData[pair].candle_highs.push(Number(candle.high))
pairData[pair].candle_volumes.push(Number(candle.volume))
}
function updateLastCandle(pair, candle) {
let index = pairData[pair].candle_opens.length - 1
pairData[pair].candle_opens[index] = Number(candle.open)
pairData[pair].candle_closes[index] = Number(candle.close)
pairData[pair].candle_lows[index] = Number(candle.low)
pairData[pair].candle_highs[index] = Number(candle.high)
pairData[pair].candle_volumes[index] = Number(candle.volume)
}
function initPairData(pair) {
pairData[pair] = {}
// price info
pairData[pair].price = BigNumber(0)
pairData[pair].prev_price = BigNumber(0)
// depth data
pairData[pair].sum_bids = BigNumber(0)
pairData[pair].sum_asks = BigNumber(0)
pairData[pair].first_bid_qty = BigNumber(0)
pairData[pair].first_ask_qty = BigNumber(0)
pairData[pair].first_bid_price = BigNumber(0)
pairData[pair].first_ask_price = BigNumber(0)
pairData[pair].volumes = []
pairData[pair].makers = []
pairData[pair].trades = []
// candle data
pairData[pair].candle_opens = []
pairData[pair].candle_closes = []
pairData[pair].candle_highs = []
pairData[pair].candle_lows = []
pairData[pair].candle_volumes = []
pairData[pair].interv_vols_sum = []
// indicator data
pairData[pair].srsi = null
}
async function trackPairData(pair) {
initPairData(pair)
// get start candles
const candles = await binance_client.candles({
symbol: pair,
interval: timeframe,
})
for (var i = 0, len = candles.length; i < len; i++) {
addCandle(pair, candles[i])
}
await sleep(wait_time)
// setup candle websocket
const candlesWs = binance_client.ws.candles(
pair,
timeframe,
async (candle) => {
updateLastCandle(pair, candle)
if (candle.isFinal) {
addCandle(pair, candle)
}
try {
await tulind.indicators.stochrsi
.indicator([pairData[pair].candle_closes], [100])
.then((results) => {
pairData[pair].srsi = BigNumber(
results[0][results[0].length - 1] * 100
)
})
} catch (e) {
console.log(pair, "SRSI ERROR!!!")
pairData[pair].srsi = null
}
}
)
await sleep(wait_time)
// setup depth websocket
const depthWs = binance_client.ws.partialDepth(
{ symbol: pair, level: 10 },
(depth) => {
pairData[pair].sum_bids = _.sumBy(depth.bids, (o) => {
return Number(o.quantity)
})
pairData[pair].sum_asks = _.sumBy(depth.asks, (o) => {
return Number(o.quantity)
})
pairData[pair].first_bid_qty = BigNumber(depth.bids[0].quantity)
pairData[pair].first_ask_qty = BigNumber(depth.asks[0].quantity)
pairData[pair].first_bid_price = BigNumber(depth.bids[0].price)
pairData[pair].first_ask_price = BigNumber(depth.asks[0].price)
}
)
await sleep(wait_time)
// setup trade (1 per second)
const tradesWs = binance_client.ws.trades([pair], (trade) => {
pairData[pair].price = BigNumber(trade.price)
pairData[pair].volumes.unshift({
timestamp: Date.now(),
volume: parseFloat(trade.quantity),
})
pairData[pair].makers.unshift({
timestamp: Date.now(),
maker: trade.maker,
})
})
// loop to create signals (& save values to db)
setInterval(async () => {
let depth_report = ""
const last_sum_bids_bn = BigNumber(pairData[pair].sum_bids)
const last_sum_asks_bn = BigNumber(pairData[pair].sum_asks)
if (last_sum_bids_bn.isLessThan(last_sum_asks_bn)) {
depth_report =
"-" +
last_sum_asks_bn
.dividedBy(last_sum_bids_bn)
.decimalPlaces(2)
.toString()
} else {
depth_report =
"+" +
last_sum_bids_bn
.dividedBy(last_sum_asks_bn)
.decimalPlaces(2)
.toString()
}
// calculate some extra values which depend on others
pairData[pair].interv_vols_sum.push(
Number(_.sumBy(pairData[pair].volumes, "volume"))
)
pairData[pair].trades.push(pairData[pair].volumes.length)
const makers_count = BigNumber(
_.filter(pairData[pair].makers, (o) => {
if (o.maker) return o
}).length
)
const makers_total = BigNumber(pairData[pair].makers.length)
const maker_ratio =
makers_count > 0
? makers_count.dividedBy(makers_total).times(100)
: BigNumber(0)
// if data ready
if (
pairData[pair].price.isGreaterThan(0) &&
pairData[pair].candle_closes.length &&
last_sum_bids_bn.isGreaterThan(0) &&
last_sum_asks_bn.isGreaterThan(0) &&
pairData[pair].interv_vols_sum.length &&
pairData[pair].first_bid_price > 0 &&
pairData[pair].first_ask_price > 0
) {
const price_open = Number(
pairData[pair].candle_opens[
pairData[pair].candle_opens.length - 1
]
)
const price_high = Number(
pairData[pair].candle_highs[
pairData[pair].candle_highs.length - 1
]
)
const price_low = Number(
pairData[pair].candle_lows[
pairData[pair].candle_lows.length - 1
]
)
const price_last = Number(
pairData[pair].candle_closes[
pairData[pair].candle_closes.length - 1
]
)
const first_ask_price = pairData[pair].first_ask_price
const first_bid_price = pairData[pair].first_bid_price
// DATABASE INSERT
if (insert_into_db) {
const insert_values = [
Date.now(),
moment(Date.now()).format(),
Number(pairData[pair].price.toString()),
price_open,
price_high,
price_low,
price_last,
pairData[pair].interv_vols_sum[
pairData[pair].interv_vols_sum.length - 1
],
pairData[pair].trades[pairData[pair].trades.length - 1],
Number(maker_ratio.decimalPlaces(2).toString()),
Number(depth_report),
Number(last_sum_bids_bn),
Number(last_sum_asks_bn),
Number(pairData[pair].first_bid_price),
Number(pairData[pair].first_ask_price),
Number(pairData[pair].first_bid_qty),
Number(pairData[pair].first_ask_qty),
pairData[pair].srsi === null
? null
: Number(
pairData[pair].srsi.decimalPlaces(2).toString()
),
]
const insert_query =
"INSERT INTO " +
nbt_prefix +
pair +
"(eventtime, datetime, price, candle_open, candle_high, candle_low, candle_close, sum_interv_vols, trades, makers_count, depth_report, sum_bids, sum_asks, first_bid_price, first_ask_price, first_bid_qty, first_ask_qty, srsi)" +
" VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18) RETURNING * "
pg_client
.query(insert_query, insert_values)
.then((res) => {})
.catch((e) => {
console.log(e)
})
}
const signal_key = stratname.replace(/\s+/g, "")
//////// SIGNAL CONDITION ///////
let signalCheck = await checkSignal(pair)
if (signalCheck && !openSignals[pair + signal_key]) {
const signal = {
key: bva_key,
stratname: stratname,
pair: pair,
stop_loss: signalCheck.stopLoss,
stop_profit: signalCheck.takeProfit,
}
// store signal
const openSignal = {
type: signalCheck.isBuy ? "LONG" : "SHORT",
stop_loss: signal.stop_loss,
stop_profit: signal.stop_profit,
}
if (openSignal.type === "LONG") {
signal.buy_price = Number(first_ask_price)
openSignal.buy_price = Number(first_ask_price)
} else {
signal.sell_price = Number(first_bid_price)
openSignal.sell_price = Number(first_bid_price)
}
console.log("OPEN", openSignal.type, signal)
if (send_signal_to_bva) {
socket_client.emit(
openSignal.type === "LONG"
? "buy_signal"
: "sell_signal",
signal
)
}
// store open signal
openSignals[pair + signal_key] = openSignal
} else if (openSignals[pair + signal_key]) {
// check if needs to be closed
const openSignal = openSignals[pair + signal_key]
const pnl =
openSignal.type === "LONG"
? first_bid_price
.minus(openSignal.buy_price)
.times(100)
.dividedBy(openSignal.buy_price)
: BigNumber(openSignal.sell_price)
.minus(first_ask_price)
.times(100)
.dividedBy(openSignal.sell_price)
if (
pnl.isLessThan(openSignals[pair + signal_key].stop_loss) ||
pnl.isGreaterThan(
openSignals[pair + signal_key].stop_profit
)
) {
const signal = {
key: bva_key,
stratname: stratname,
pair: pair,
}
if (openSignal.type === "LONG") {
signal.sell_price = Number(first_bid_price)
} else {
signal.buy_price = Number(first_ask_price)
}
console.log("CLOSE", openSignal.type, signal)
if (send_signal_to_bva) {
socket_client.emit(
openSignal.type === "LONG"
? "sell_signal"
: "buy_signal",
signal
)
}
// remove open signal
delete openSignals[pair + signal_key]
}
}
pairData[pair].prev_price = price_last
}
// clean up arrays...
pairData[pair].makers = _.filter(pairData[pair].makers, (v) => {
return v.timestamp >= Date.now() - interv_time
})
pairData[pair].volumes = _.filter(pairData[pair].volumes, (v) => {
return v.timestamp >= Date.now() - interv_time
})
pairData[pair].candle_opens = pairData[pair].candle_opens.slice(
pairData[pair].candle_opens.length - 10000,
10000
)
pairData[pair].candle_closes = pairData[pair].candle_closes.slice(
pairData[pair].candle_closes.length - 10000,
10000
)
pairData[pair].candle_highs = pairData[pair].candle_highs.slice(
pairData[pair].candle_highs.length - 10000,
10000
)
pairData[pair].candle_lows = pairData[pair].candle_lows.slice(
pairData[pair].candle_lows.length - 10000,
10000
)
pairData[pair].candle_volumes = pairData[pair].candle_volumes.slice(
pairData[pair].candle_volumes.length - 10000,
10000
)
pairData[pair].interv_vols_sum = pairData[pair].interv_vols_sum.slice(
pairData[pair].interv_vols_sum.length - 10000,
10000
)
pairData[pair].trades = pairData[pair].trades.slice(
pairData[pair].trades.length - 10000,
10000
)
}, 1000)
}
/////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SIGNAL DECLARATION - START /////////////////////////////////
//////////////////////////////// THIS IS WHERE YOU CODE YOUR STRATEGY ///////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
async function checkSignal(pair) {
try {
let rsi = await tulind.indicators.stochrsi.indicator(
[pairData[pair].candle_closes],
[14]
)
let rsiLatest = rsi[0][rsi[0].length - 1]
let macd = await tulind.indicators.macd.indicator(
[pairData[pair].candle_closes],
[12, 26, 9]
)
let macdOldest = macd[2][macd[2].length - 3]
let macdOlder = macd[2][macd[2].length - 2]
let macdNewest = macd[2][macd[2].length - 1]
if (
macdNewest >= 0 &&
macdOlder < 0 &&
macdOldest < 0 &&
rsiLatest < 0.3
) {
return { isBuy: true, takeProfit: 1, stopLoss: -1 }
}
if (
macdNewest < 0 &&
macdOlder >= 0 &&
macdOldest >= 0 &&
rsiLatest > 0.7
) {
return { isBuy: false, takeProfit: 1, stopLoss: -1 }
}
} catch (e) {
console.log(e)
}
return null
}
///////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////// SIGNAL DECLARATION - END /////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
async function createPgPairTable(pair) {
return pg_client
.query(
"CREATE TABLE " +
nbt_prefix +
pair +
"(id bigserial primary key, eventtime bigint NOT NULL, datetime varchar(200), price decimal, candle_open decimal, candle_high decimal, candle_low decimal, candle_close decimal, sum_interv_vols decimal, trades integer, makers_count real, depth_report decimal, sum_bids real, sum_asks real, first_bid_price decimal, first_ask_price decimal, first_bid_qty decimal, first_ask_qty decimal, srsi real)"
)
.then((res) => {
console.log("TABLE " + nbt_prefix + pair + " CREATION SUCCESS")
})
.catch((e) => {
//console.log(e)
})
}
sleep = (x) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true)
}, x)
})
}
run()