@icgio/icg-utils 1.7.21 → 1.7.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/html-parser.js +29 -25
- package/lib/influxdb.js +40 -26
- package/lib/logger.js +7 -7
- package/lib/mail.js +5 -5
- package/lib/ohlcv.js +2 -1
- package/lib/rate-limiter.js +6 -4
- package/lib/utils.js +203 -148
- package/lib/wechat-bot-client.js +3 -3
- package/package.json +1 -1
package/lib/html-parser.js
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
1
|
const htmlparser = require('htmlparser2')
|
|
2
2
|
const needle = require('needle')
|
|
3
3
|
|
|
4
|
-
function tag_parser
|
|
4
|
+
function tag_parser(body, tag_name, tag_attrs, cb) {
|
|
5
5
|
let result = []
|
|
6
|
-
let parser = new htmlparser.Parser(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
flag = false
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
6
|
+
let parser = new htmlparser.Parser(
|
|
7
|
+
{
|
|
8
|
+
onopentag: (name, attrs) => {
|
|
9
|
+
let flag = true
|
|
10
|
+
for (let tag_attr in tag_attrs) {
|
|
11
|
+
if (!attrs[tag_attr] || tag_attrs[tag_attr] !== attrs[tag_attr]) flag = false
|
|
12
|
+
}
|
|
13
|
+
if (flag && name === tag_name) {
|
|
14
|
+
result.push(attrs)
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
onend: () => {
|
|
18
|
+
cb(result)
|
|
19
|
+
},
|
|
16
20
|
},
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
}, { decodeEntities: true })
|
|
21
|
+
{ decodeEntities: true }
|
|
22
|
+
)
|
|
21
23
|
parser.write(body)
|
|
22
24
|
parser.end()
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
function text_parser
|
|
27
|
+
function text_parser(body, include_new_line = false, cb) {
|
|
26
28
|
let result = []
|
|
27
|
-
let parser = new htmlparser.Parser(
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
result.push(text)
|
|
29
|
+
let parser = new htmlparser.Parser(
|
|
30
|
+
{
|
|
31
|
+
ontext: (text) => {
|
|
32
|
+
if (!include_new_line && !text.startsWith('\n')) result.push(text)
|
|
33
|
+
},
|
|
34
|
+
onend: () => {
|
|
35
|
+
cb(result)
|
|
36
|
+
},
|
|
31
37
|
},
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
}, { decodeEntities: true })
|
|
38
|
+
{ decodeEntities: true }
|
|
39
|
+
)
|
|
36
40
|
parser.write(body)
|
|
37
41
|
parser.end()
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
function html_parser
|
|
44
|
+
function html_parser(url, options, cb) {
|
|
41
45
|
let tag_name = options.tag_name
|
|
42
46
|
let tag_attrs = options.tag_attrs
|
|
43
47
|
let text = options.text
|
package/lib/influxdb.js
CHANGED
|
@@ -2,7 +2,7 @@ const _ = require('lodash')
|
|
|
2
2
|
|
|
3
3
|
const InfluxDB = require('influx')
|
|
4
4
|
|
|
5
|
-
function schema_check
|
|
5
|
+
function schema_check(fields) {
|
|
6
6
|
let result = true
|
|
7
7
|
for (let key in fields) {
|
|
8
8
|
if (!fields[key] && fields[key] !== 0) {
|
|
@@ -12,8 +12,8 @@ function schema_check (fields) {
|
|
|
12
12
|
return result
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
function check_points
|
|
16
|
-
return points.filter(point => {
|
|
15
|
+
function check_points(points) {
|
|
16
|
+
return points.filter((point) => {
|
|
17
17
|
if (schema_check(point.fields) && (!point.timestamp || point.timestamp instanceof Date)) {
|
|
18
18
|
return true
|
|
19
19
|
} else {
|
|
@@ -24,10 +24,10 @@ function check_points (points) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
module.exports = class Influx {
|
|
27
|
-
constructor
|
|
27
|
+
constructor(config) {
|
|
28
28
|
let config_db = {
|
|
29
29
|
host: config.server,
|
|
30
|
-
database: config.database
|
|
30
|
+
database: config.database,
|
|
31
31
|
}
|
|
32
32
|
if (config.schema) {
|
|
33
33
|
config_db.schema = config.schema
|
|
@@ -40,26 +40,37 @@ module.exports = class Influx {
|
|
|
40
40
|
this.database = config.database
|
|
41
41
|
this.database_exist = false
|
|
42
42
|
}
|
|
43
|
-
static get FLOAT() {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
static get
|
|
43
|
+
static get FLOAT() {
|
|
44
|
+
return InfluxDB.FieldType.FLOAT
|
|
45
|
+
}
|
|
46
|
+
static get INTEGER() {
|
|
47
|
+
return InfluxDB.FieldType.INTEGER
|
|
48
|
+
}
|
|
49
|
+
static get STRING() {
|
|
50
|
+
return InfluxDB.FieldType.STRING
|
|
51
|
+
}
|
|
52
|
+
static get BOOLEAN() {
|
|
53
|
+
return InfluxDB.FieldType.BOOLEAN
|
|
54
|
+
}
|
|
47
55
|
query(query_string, cb) {
|
|
48
|
-
this.db
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
this.db
|
|
57
|
+
.query(query_string)
|
|
58
|
+
.then((res) => {
|
|
59
|
+
res.map((e) => {
|
|
60
|
+
e.date = new Date(e.time.toNanoISOString())
|
|
61
|
+
return e
|
|
62
|
+
})
|
|
63
|
+
cb({ success: true, body: res })
|
|
64
|
+
})
|
|
65
|
+
.catch((err) => {
|
|
66
|
+
cb({ success: false, error: err })
|
|
52
67
|
})
|
|
53
|
-
cb ({ success: true, body: res })
|
|
54
|
-
}).catch(err => {
|
|
55
|
-
cb ({ success: false, error: err })
|
|
56
|
-
})
|
|
57
68
|
}
|
|
58
|
-
write_points_with_check
|
|
69
|
+
write_points_with_check(points, cb) {
|
|
59
70
|
if (!this.database_exist) {
|
|
60
|
-
this.db.getDatabaseNames().then(res => {
|
|
71
|
+
this.db.getDatabaseNames().then((res) => {
|
|
61
72
|
if (res.indexOf(this.database) === -1) {
|
|
62
|
-
this.db.createDatabase(this.database).then(res => {
|
|
73
|
+
this.db.createDatabase(this.database).then((res) => {
|
|
63
74
|
this.database_exist = true
|
|
64
75
|
})
|
|
65
76
|
} else {
|
|
@@ -67,13 +78,16 @@ module.exports = class Influx {
|
|
|
67
78
|
}
|
|
68
79
|
})
|
|
69
80
|
} else if (check_points(points)) {
|
|
70
|
-
this.db
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
81
|
+
this.db
|
|
82
|
+
.writePoints(points)
|
|
83
|
+
.then((res) => {
|
|
84
|
+
cb({ success: true, body: res })
|
|
85
|
+
})
|
|
86
|
+
.catch((err) => {
|
|
87
|
+
cb({ success: false, error: err })
|
|
88
|
+
})
|
|
75
89
|
} else {
|
|
76
|
-
cb
|
|
90
|
+
cb({ success: false, error: 'points-error' })
|
|
77
91
|
}
|
|
78
92
|
}
|
|
79
93
|
}
|
package/lib/logger.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
const winston = require('winston')
|
|
2
2
|
const Papertrail = require('winston-papertrail').Papertrail
|
|
3
3
|
|
|
4
|
-
let logger = new
|
|
4
|
+
let logger = new winston.Logger({
|
|
5
5
|
levels: {
|
|
6
6
|
error: 0,
|
|
7
7
|
warn: 1,
|
|
8
8
|
info: 2,
|
|
9
9
|
verbose: 3,
|
|
10
10
|
debug: 4,
|
|
11
|
-
silly: 5
|
|
11
|
+
silly: 5,
|
|
12
12
|
},
|
|
13
13
|
colors: {
|
|
14
14
|
silly: 'grey',
|
|
@@ -16,7 +16,7 @@ let logger = new(winston.Logger)({
|
|
|
16
16
|
debug: 'blue',
|
|
17
17
|
info: 'green',
|
|
18
18
|
warn: 'yellow',
|
|
19
|
-
error: 'red'
|
|
19
|
+
error: 'red',
|
|
20
20
|
},
|
|
21
21
|
transports: [
|
|
22
22
|
// new(Papertrail)({
|
|
@@ -25,16 +25,16 @@ let logger = new(winston.Logger)({
|
|
|
25
25
|
// colorize: true,
|
|
26
26
|
// level: 'debug'
|
|
27
27
|
// }),
|
|
28
|
-
new
|
|
28
|
+
new winston.transports.Console({
|
|
29
29
|
prettyPrint: true,
|
|
30
30
|
colorize: true,
|
|
31
31
|
timestamp: true,
|
|
32
|
-
level: 'debug'
|
|
33
|
-
})
|
|
32
|
+
level: 'debug',
|
|
33
|
+
}),
|
|
34
34
|
// new (winston.transports.File)({
|
|
35
35
|
// filename: 'out.log',
|
|
36
36
|
// level: 'debug'
|
|
37
37
|
// })
|
|
38
|
-
]
|
|
38
|
+
],
|
|
39
39
|
})
|
|
40
40
|
module.exports = logger
|
package/lib/mail.js
CHANGED
|
@@ -5,12 +5,12 @@ const nodemailer = require('nodemailer')
|
|
|
5
5
|
module.exports.send = function (address, subject, data, config) {
|
|
6
6
|
const mailgun = require('mailgun-js')({
|
|
7
7
|
apiKey: config.mail_api_key,
|
|
8
|
-
domain: config.mail_domain
|
|
8
|
+
domain: config.mail_domain,
|
|
9
9
|
})
|
|
10
10
|
let options = {
|
|
11
11
|
from: config.mail_from_address,
|
|
12
12
|
to: address,
|
|
13
|
-
subject: config.message_prefix() + subject
|
|
13
|
+
subject: config.message_prefix() + subject,
|
|
14
14
|
}
|
|
15
15
|
if (/<[a-z][\s\S]*>/i.test(data)) {
|
|
16
16
|
options.html = data
|
|
@@ -33,13 +33,13 @@ module.exports.send_cn = function (address, subject, data, config, cd) {
|
|
|
33
33
|
secure: true,
|
|
34
34
|
auth: {
|
|
35
35
|
user: config.mail_from_address_china,
|
|
36
|
-
pass: cd.mail_user_password
|
|
37
|
-
}
|
|
36
|
+
pass: cd.mail_user_password,
|
|
37
|
+
},
|
|
38
38
|
})
|
|
39
39
|
let options = {
|
|
40
40
|
from: config.mail_from_address_china,
|
|
41
41
|
to: address,
|
|
42
|
-
subject: subject
|
|
42
|
+
subject: subject,
|
|
43
43
|
}
|
|
44
44
|
if (/<[a-z][\s\S]*>/i.test(data)) {
|
|
45
45
|
options.html = data
|
package/lib/ohlcv.js
CHANGED
package/lib/rate-limiter.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
module.exports = class RateLimiter {
|
|
2
|
-
constructor
|
|
2
|
+
constructor(settings) {
|
|
3
3
|
this.max_concurrent = settings.max_concurrent ? settings.max_concurrent : null
|
|
4
4
|
this.interval_concurrent = settings.interval_concurrent ? settings.interval_concurrent : null
|
|
5
5
|
this.count = 0
|
|
6
6
|
}
|
|
7
|
-
process
|
|
7
|
+
process(func, ...args) {
|
|
8
8
|
if (!this.max_concurrent || !this.interval_concurrent) {
|
|
9
9
|
func(...args)
|
|
10
10
|
return
|
|
11
11
|
}
|
|
12
12
|
if (this.count === 0) {
|
|
13
|
-
setTimeout(() => {
|
|
13
|
+
setTimeout(() => {
|
|
14
|
+
this.count = 0
|
|
15
|
+
}, this.interval_concurrent)
|
|
14
16
|
}
|
|
15
17
|
if (this.count < this.max_concurrent) {
|
|
16
|
-
this.count
|
|
18
|
+
this.count++
|
|
17
19
|
func(...args)
|
|
18
20
|
} else {
|
|
19
21
|
setTimeout(() => {
|
package/lib/utils.js
CHANGED
|
@@ -24,70 +24,72 @@ const _ = require('lodash')
|
|
|
24
24
|
|
|
25
25
|
// console.log(0.000000000000000000000000001232312421.toFixed(30))
|
|
26
26
|
|
|
27
|
-
Number.prototype.toStringNoExponents = function() {
|
|
28
|
-
let data= String(this).split(/[eE]/)
|
|
29
|
-
if(data.length== 1) return data[0]
|
|
30
|
-
let
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
27
|
+
Number.prototype.toStringNoExponents = function () {
|
|
28
|
+
let data = String(this).split(/[eE]/)
|
|
29
|
+
if (data.length == 1) return data[0]
|
|
30
|
+
let z = '',
|
|
31
|
+
sign = this < 0 ? '-' : '',
|
|
32
|
+
str = data[0].replace('.', ''),
|
|
33
|
+
mag = Number(data[1]) + 1
|
|
34
|
+
if (mag < 0) {
|
|
35
|
+
z = sign + '0.'
|
|
36
|
+
while (mag++) z += '0'
|
|
37
|
+
return z + str.replace(/^\-/, '')
|
|
37
38
|
}
|
|
38
39
|
mag -= str.length
|
|
39
|
-
while(mag--) z += '0'
|
|
40
|
+
while (mag--) z += '0'
|
|
40
41
|
return str + z
|
|
41
42
|
}
|
|
42
43
|
|
|
43
|
-
Date.prototype.addHours = function(h) {
|
|
44
|
-
return new Date(this.getTime() +
|
|
44
|
+
Date.prototype.addHours = function (h) {
|
|
45
|
+
return new Date(this.getTime() + h * 60 * 60 * 1000)
|
|
45
46
|
}
|
|
46
47
|
|
|
47
|
-
Date.prototype.roundHours = function(m) {
|
|
48
|
+
Date.prototype.roundHours = function (m) {
|
|
48
49
|
let coeff = m * 60 * 60 * 1000
|
|
49
50
|
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
50
51
|
}
|
|
51
52
|
|
|
52
|
-
Date.prototype.ceilHours = function(m) {
|
|
53
|
+
Date.prototype.ceilHours = function (m) {
|
|
53
54
|
let coeff = m * 60 * 60 * 1000
|
|
54
55
|
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
Date.prototype.floorHours = function(m) {
|
|
58
|
+
Date.prototype.floorHours = function (m) {
|
|
58
59
|
let coeff = m * 60 * 60 * 1000
|
|
59
60
|
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
Date.prototype.addMinutes = function(m) {
|
|
63
|
-
return new Date(this.getTime() +
|
|
63
|
+
Date.prototype.addMinutes = function (m) {
|
|
64
|
+
return new Date(this.getTime() + m * 60 * 1000)
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
Date.prototype.roundMinutes = function(m) {
|
|
67
|
+
Date.prototype.roundMinutes = function (m) {
|
|
67
68
|
let coeff = m * 60 * 1000
|
|
68
69
|
return new Date(Math.round(this.getTime() / coeff) * coeff)
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
Date.prototype.ceilMinutes = function(m) {
|
|
72
|
+
Date.prototype.ceilMinutes = function (m) {
|
|
72
73
|
let coeff = m * 60 * 1000
|
|
73
74
|
return new Date(Math.ceil(this.getTime() / coeff) * coeff)
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
Date.prototype.floorMinutes = function(m) {
|
|
77
|
+
Date.prototype.floorMinutes = function (m) {
|
|
77
78
|
let coeff = m * 60 * 1000
|
|
78
79
|
return new Date(Math.floor(this.getTime() / coeff) * coeff)
|
|
79
80
|
}
|
|
80
81
|
|
|
81
|
-
Date.prototype.addSeconds = function(s) {
|
|
82
|
-
return new Date(this.getTime() +
|
|
82
|
+
Date.prototype.addSeconds = function (s) {
|
|
83
|
+
return new Date(this.getTime() + s * 1000)
|
|
83
84
|
}
|
|
84
85
|
|
|
85
|
-
String.prototype.replaceAt = function(index, replacement) {
|
|
86
|
+
String.prototype.replaceAt = function (index, replacement) {
|
|
86
87
|
return this.substr(0, index) + replacement + this.substr(index + replacement.length)
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
module.exports.ordered_dict = function (is_ask) {
|
|
90
|
-
let keys = [],
|
|
91
|
+
let keys = [],
|
|
92
|
+
vals = {}
|
|
91
93
|
return {
|
|
92
94
|
push: function (k, v) {
|
|
93
95
|
k = parseFloat(k)
|
|
@@ -100,7 +102,7 @@ module.exports.ordered_dict = function (is_ask) {
|
|
|
100
102
|
let search = binary_find(k, is_ask, keys)
|
|
101
103
|
let pos = search.index
|
|
102
104
|
if (!vals[k]) {
|
|
103
|
-
keys.splice(pos,0,k)
|
|
105
|
+
keys.splice(pos, 0, k)
|
|
104
106
|
vals[k] = v
|
|
105
107
|
} else {
|
|
106
108
|
vals[k] = v
|
|
@@ -112,50 +114,60 @@ module.exports.ordered_dict = function (is_ask) {
|
|
|
112
114
|
let search = binary_find(k, is_ask, keys)
|
|
113
115
|
let pos = search.index
|
|
114
116
|
if (search.found) {
|
|
115
|
-
keys = keys.slice(0, pos).concat(keys.slice(pos+1, keys.length))
|
|
117
|
+
keys = keys.slice(0, pos).concat(keys.slice(pos + 1, keys.length))
|
|
116
118
|
delete vals[k]
|
|
117
119
|
}
|
|
118
120
|
},
|
|
119
|
-
val: function (k) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
121
|
+
val: function (k) {
|
|
122
|
+
return vals[k]
|
|
123
|
+
},
|
|
124
|
+
length: function () {
|
|
125
|
+
return keys.length
|
|
126
|
+
},
|
|
127
|
+
keys: function () {
|
|
128
|
+
return keys
|
|
129
|
+
},
|
|
130
|
+
values: function () {
|
|
131
|
+
return vals
|
|
132
|
+
},
|
|
133
|
+
entries: function () {
|
|
134
|
+
return keys.map((key) => [key, vals[key]])
|
|
135
|
+
},
|
|
124
136
|
}
|
|
125
137
|
}
|
|
126
138
|
|
|
127
|
-
function binary_find
|
|
139
|
+
function binary_find(searchElement, is_ask, keys) {
|
|
128
140
|
let minIndex = 0
|
|
129
141
|
let maxIndex = keys.length - 1
|
|
130
142
|
let currentIndex
|
|
131
143
|
let currentElement
|
|
132
144
|
|
|
133
145
|
while (minIndex <= maxIndex) {
|
|
134
|
-
currentIndex = (minIndex + maxIndex) / 2 | 0
|
|
146
|
+
currentIndex = ((minIndex + maxIndex) / 2) | 0
|
|
135
147
|
currentElement = keys[currentIndex]
|
|
136
148
|
|
|
137
|
-
if (currentElement < searchElement && is_ask || currentElement > searchElement && !is_ask) {
|
|
149
|
+
if ((currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask)) {
|
|
138
150
|
minIndex = currentIndex + 1
|
|
139
|
-
}
|
|
140
|
-
else if (currentElement > searchElement && is_ask || currentElement < searchElement && !is_ask) {
|
|
151
|
+
} else if ((currentElement > searchElement && is_ask) || (currentElement < searchElement && !is_ask)) {
|
|
141
152
|
maxIndex = currentIndex - 1
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
|
|
153
|
+
} else {
|
|
154
|
+
return {
|
|
155
|
+
// Modification
|
|
145
156
|
found: true,
|
|
146
|
-
index: currentIndex
|
|
157
|
+
index: currentIndex,
|
|
147
158
|
}
|
|
148
159
|
}
|
|
149
160
|
}
|
|
150
161
|
|
|
151
|
-
return {
|
|
162
|
+
return {
|
|
163
|
+
// Modification
|
|
152
164
|
found: false,
|
|
153
|
-
index: (currentElement < searchElement && is_ask || currentElement > searchElement && !is_ask) ? currentIndex + 1 : currentIndex
|
|
165
|
+
index: (currentElement < searchElement && is_ask) || (currentElement > searchElement && !is_ask) ? currentIndex + 1 : currentIndex,
|
|
154
166
|
}
|
|
155
167
|
}
|
|
156
168
|
|
|
157
169
|
module.exports.random_id = function () {
|
|
158
|
-
return _.random(100000000,
|
|
170
|
+
return _.random(100000000, 999999999)
|
|
159
171
|
}
|
|
160
172
|
|
|
161
173
|
module.exports.get_hhmmss = function () {
|
|
@@ -163,12 +175,11 @@ module.exports.get_hhmmss = function () {
|
|
|
163
175
|
}
|
|
164
176
|
|
|
165
177
|
module.exports.round = function (number, precision) {
|
|
166
|
-
if (typeof number === 'string')
|
|
167
|
-
number = parseFloat(number)
|
|
178
|
+
if (typeof number === 'string') number = parseFloat(number)
|
|
168
179
|
return Math.round(number * Math.pow(10, precision)) / Math.pow(10, precision)
|
|
169
180
|
}
|
|
170
181
|
|
|
171
|
-
module.exports.cut_orderbook = function(input_array, level_target) {
|
|
182
|
+
module.exports.cut_orderbook = function (input_array, level_target) {
|
|
172
183
|
return input_array.slice(level_target)
|
|
173
184
|
}
|
|
174
185
|
|
|
@@ -177,7 +188,10 @@ module.exports.level_position = function (input_array, level_target) {
|
|
|
177
188
|
level_target--
|
|
178
189
|
return module.exports.level_position(input_array, level_target)
|
|
179
190
|
}
|
|
180
|
-
let vol = 0,
|
|
191
|
+
let vol = 0,
|
|
192
|
+
price = 0,
|
|
193
|
+
sum = 0,
|
|
194
|
+
pos = 0
|
|
181
195
|
for (let i = 0; i < level_target; i++) {
|
|
182
196
|
vol = vol + input_array[i][1]
|
|
183
197
|
sum = sum + input_array[i][0] * input_array[i][1]
|
|
@@ -194,14 +208,15 @@ module.exports.price_position = function (input_array, price) {
|
|
|
194
208
|
avg_price: input_array[0][0],
|
|
195
209
|
vol: input_array[0][1],
|
|
196
210
|
sum: input_array[0][0] * input_array[0][1],
|
|
197
|
-
pos: 0
|
|
211
|
+
pos: 0,
|
|
198
212
|
}
|
|
199
213
|
}
|
|
200
|
-
let vol = 0,
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
214
|
+
let vol = 0,
|
|
215
|
+
sum = 0,
|
|
216
|
+
pos = 0,
|
|
217
|
+
type
|
|
218
|
+
if (input_array[0][0] > input_array[1][0]) type = 'bid'
|
|
219
|
+
else type = 'ask'
|
|
205
220
|
for (let i = 0; i < input_array.length; i++) {
|
|
206
221
|
if (input_array[i][0] <= price && type === 'ask') {
|
|
207
222
|
vol = vol + input_array[i][1]
|
|
@@ -226,10 +241,14 @@ module.exports.sum_position = function (input_array, sum_target) {
|
|
|
226
241
|
vol: input_array[0][1],
|
|
227
242
|
sum: input_array[0][0] * input_array[0][1],
|
|
228
243
|
sum_original: input_array[0][0] * input_array[0][1],
|
|
229
|
-
pos: 0
|
|
244
|
+
pos: 0,
|
|
230
245
|
}
|
|
231
246
|
}
|
|
232
|
-
let vol = 0,
|
|
247
|
+
let vol = 0,
|
|
248
|
+
price = 0,
|
|
249
|
+
sum = 0,
|
|
250
|
+
pos = 0,
|
|
251
|
+
sum_original = 0
|
|
233
252
|
for (let i = 0; i < input_array.length; i++) {
|
|
234
253
|
if (sum < sum_target) {
|
|
235
254
|
vol = vol + input_array[i][1]
|
|
@@ -252,10 +271,14 @@ module.exports.vol_position = function (input_array, vol_target) {
|
|
|
252
271
|
vol: input_array[0][1],
|
|
253
272
|
vol_original: input_array[0][1],
|
|
254
273
|
sum: input_array[0][0] * input_array[0][1],
|
|
255
|
-
pos: 0
|
|
274
|
+
pos: 0,
|
|
256
275
|
}
|
|
257
276
|
}
|
|
258
|
-
let vol = 0,
|
|
277
|
+
let vol = 0,
|
|
278
|
+
price = 0,
|
|
279
|
+
sum = 0,
|
|
280
|
+
pos = 0,
|
|
281
|
+
vol_original = 0
|
|
259
282
|
for (let i = 0; i < input_array.length; i++) {
|
|
260
283
|
if (vol < vol_target) {
|
|
261
284
|
vol = vol + input_array[i][1]
|
|
@@ -273,6 +296,9 @@ module.exports.vol_position = function (input_array, vol_target) {
|
|
|
273
296
|
}
|
|
274
297
|
|
|
275
298
|
module.exports.sum_position_orderbook = (input_array, sum_target) => {
|
|
299
|
+
if (sum_target === 0) {
|
|
300
|
+
return []
|
|
301
|
+
}
|
|
276
302
|
let { sum_original, pos } = module.exports.sum_position(input_array, sum_target)
|
|
277
303
|
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
278
304
|
result[pos][1] += (sum_target - sum_original) / result[pos][0]
|
|
@@ -280,56 +306,75 @@ module.exports.sum_position_orderbook = (input_array, sum_target) => {
|
|
|
280
306
|
}
|
|
281
307
|
|
|
282
308
|
module.exports.vol_position_orderbook = (input_array, vol_target) => {
|
|
309
|
+
if (vol_target === 0) {
|
|
310
|
+
return []
|
|
311
|
+
}
|
|
283
312
|
let { vol_original, pos } = module.exports.vol_position(input_array, vol_target)
|
|
284
313
|
let result = JSON.parse(JSON.stringify(input_array.slice(0, pos + 1)))
|
|
285
314
|
result[pos][1] += vol_target - vol_original
|
|
286
315
|
return result
|
|
287
316
|
}
|
|
288
317
|
|
|
289
|
-
// console.log(
|
|
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
|
-
|
|
318
|
+
// console.log(
|
|
319
|
+
// module.exports.sum_position_orderbook(
|
|
320
|
+
// [
|
|
321
|
+
// [2, 1],
|
|
322
|
+
// [6, 6],
|
|
323
|
+
// [7, 7],
|
|
324
|
+
// ],
|
|
325
|
+
// 0
|
|
326
|
+
// )
|
|
327
|
+
// )
|
|
328
|
+
// console.log(
|
|
329
|
+
// module.exports.vol_position_orderbook(
|
|
330
|
+
// [
|
|
331
|
+
// [0.00004637, 14.12],
|
|
332
|
+
// [0.00004636, 19.37],
|
|
333
|
+
// [0.0000463, 77.76],
|
|
334
|
+
// [0.00004625, 17.93],
|
|
335
|
+
// [0.00004623, 14.89],
|
|
336
|
+
// [0.00004621, 34.25],
|
|
337
|
+
// [0.0000462, 131.77],
|
|
338
|
+
// [0.00004619, 24.79],
|
|
339
|
+
// [0.00004618, 36.71],
|
|
340
|
+
// [0.00004617, 18.36],
|
|
341
|
+
// [0.00004616, 9],
|
|
342
|
+
// [0.00004615, 67.22],
|
|
343
|
+
// [0.00004614, 74.74],
|
|
344
|
+
// [0.00004613, 31.89],
|
|
345
|
+
// [0.00004608, 31.25],
|
|
346
|
+
// [0.00004606, 72],
|
|
347
|
+
// [0.00004605, 11.69],
|
|
348
|
+
// [0.00004604, 36.68],
|
|
349
|
+
// [0.00004601, 29.77],
|
|
350
|
+
// [0.00004598, 11.17],
|
|
351
|
+
// [0.00004596, 25.19],
|
|
352
|
+
// [0.00004593, 9.01],
|
|
353
|
+
// [0.00004592, 15.56],
|
|
354
|
+
// [0.00004589, 15.21],
|
|
355
|
+
// [0.00004588, 21.25],
|
|
356
|
+
// [0.00004583, 25.19],
|
|
357
|
+
// [0.00004582, 81.02],
|
|
358
|
+
// [0.00004581, 24.55],
|
|
359
|
+
// [0.0000458, 0.26],
|
|
360
|
+
// [0.00004577, 71.32],
|
|
361
|
+
// [0.00004576, 43.39],
|
|
362
|
+
// [0.00004574, 41.3],
|
|
363
|
+
// [0.00004573, 0.75],
|
|
364
|
+
// [0.0000457, 10.84],
|
|
365
|
+
// [0.00004567, 0.26],
|
|
366
|
+
// [0.00004549, 239.91],
|
|
367
|
+
// [0.00004548, 115.89],
|
|
368
|
+
// [0.00004542, 192.91],
|
|
369
|
+
// [0.00004512, 0.64],
|
|
370
|
+
// [0.000045, 3.24],
|
|
371
|
+
// ],
|
|
372
|
+
// 1
|
|
373
|
+
// )
|
|
374
|
+
// )
|
|
375
|
+
|
|
376
|
+
module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
|
|
377
|
+
function merge_sorted(type) {
|
|
333
378
|
let count = {}
|
|
334
379
|
for (let exchange in orderbooks_dict) {
|
|
335
380
|
count[exchange] = 0
|
|
@@ -338,10 +383,8 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
338
383
|
while (_.sum(_.values(count)) !== _.keys(orderbooks_dict).length * -1) {
|
|
339
384
|
let picked
|
|
340
385
|
for (let exchange in orderbooks_dict) {
|
|
341
|
-
if (count[exchange] === -1)
|
|
342
|
-
|
|
343
|
-
if (_.get(orderbooks_dict, [exchange, type, count[exchange], 'length']) === 2)
|
|
344
|
-
orderbooks_dict[exchange][type][count[exchange]].push(exchange)
|
|
386
|
+
if (count[exchange] === -1) continue
|
|
387
|
+
if (_.get(orderbooks_dict, [exchange, type, count[exchange], 'length']) === 2) orderbooks_dict[exchange][type][count[exchange]].push(exchange)
|
|
345
388
|
if (!picked) {
|
|
346
389
|
picked = orderbooks_dict[exchange][type][count[exchange]]
|
|
347
390
|
} else if (type === 'asks' && picked[0] > orderbooks_dict[exchange][type][count[exchange]][0]) {
|
|
@@ -356,8 +399,7 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
356
399
|
} else {
|
|
357
400
|
count[picked[2]] = -1
|
|
358
401
|
}
|
|
359
|
-
if (result.length === length)
|
|
360
|
-
break
|
|
402
|
+
if (result.length === length) break
|
|
361
403
|
}
|
|
362
404
|
return result
|
|
363
405
|
}
|
|
@@ -380,17 +422,21 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
380
422
|
// bids: [[0.95, 0.9], [0.7, 0.5], [0.4, 0.2]]
|
|
381
423
|
// }
|
|
382
424
|
// }, 3))
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
let
|
|
387
|
-
|
|
388
|
-
|
|
425
|
+
// console.log(module.exports.combine_orderbooks({}, 100))
|
|
426
|
+
|
|
427
|
+
module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0) => {
|
|
428
|
+
let i = 0,
|
|
429
|
+
j = 0,
|
|
430
|
+
ask_amount = 0,
|
|
431
|
+
bid_amount = 0
|
|
432
|
+
let asks = orderbook_combined.asks,
|
|
433
|
+
bids = orderbook_combined.bids
|
|
434
|
+
if (asks[0][0] * (1 + profit_percentage) >= bids[0][0]) return {}
|
|
389
435
|
let last_step = ''
|
|
390
436
|
while (i < asks.length && j < bids.length) {
|
|
391
437
|
if (asks[i][0] * (1 + profit_percentage) < bids[j][0]) {
|
|
392
438
|
if (ask_amount === 0 && bid_amount === 0) {
|
|
393
|
-
ask_amount = asks[i][1], bid_amount = bids[j][1]
|
|
439
|
+
;(ask_amount = asks[i][1]), (bid_amount = bids[j][1])
|
|
394
440
|
} else if (ask_amount <= bid_amount) {
|
|
395
441
|
i++
|
|
396
442
|
ask_amount += asks[i][1]
|
|
@@ -411,21 +457,22 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
411
457
|
break
|
|
412
458
|
}
|
|
413
459
|
}
|
|
414
|
-
let opportunities = {},
|
|
460
|
+
let opportunities = {},
|
|
461
|
+
diff = ask_amount - bid_amount
|
|
415
462
|
asks = asks.slice(0, i + 1)
|
|
416
463
|
bids = bids.slice(0, j + 1)
|
|
417
464
|
if (diff > 0) {
|
|
418
|
-
asks[i][1] -= diff
|
|
465
|
+
asks[i][1] -= diff
|
|
419
466
|
} else {
|
|
420
467
|
bids[j][1] += diff
|
|
421
468
|
}
|
|
422
|
-
asks.map(ask => {
|
|
469
|
+
asks.map((ask) => {
|
|
423
470
|
if (!opportunities[ask[2]]) {
|
|
424
471
|
opportunities[ask[2]] = {
|
|
425
472
|
price: 0,
|
|
426
473
|
amount: 0,
|
|
427
474
|
exchange: ask[2],
|
|
428
|
-
method: 'buy'
|
|
475
|
+
method: 'buy',
|
|
429
476
|
}
|
|
430
477
|
}
|
|
431
478
|
opportunities[ask[2]].avg_price = (opportunities[ask[2]].price * opportunities[ask[2]].amount + ask[0] * ask[1]) / (opportunities[ask[2]].amount + ask[1])
|
|
@@ -433,13 +480,13 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
433
480
|
opportunities[ask[2]].amount += ask[1]
|
|
434
481
|
opportunities[ask[2]].total = opportunities[ask[2]].avg_price * opportunities[ask[2]].amount
|
|
435
482
|
})
|
|
436
|
-
bids.map(bid => {
|
|
483
|
+
bids.map((bid) => {
|
|
437
484
|
if (!opportunities[bid[2]]) {
|
|
438
485
|
opportunities[bid[2]] = {
|
|
439
486
|
price: 0,
|
|
440
487
|
amount: 0,
|
|
441
488
|
exchange: bid[2],
|
|
442
|
-
method: 'sell'
|
|
489
|
+
method: 'sell',
|
|
443
490
|
}
|
|
444
491
|
}
|
|
445
492
|
opportunities[bid[2]].avg_price = (opportunities[bid[2]].price * opportunities[bid[2]].amount + bid[0] * bid[1]) / (opportunities[bid[2]].amount + bid[1])
|
|
@@ -481,21 +528,28 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
481
528
|
vol: input_array[0][1],
|
|
482
529
|
vol_exchange: { [input_array[0][2]]: input_array[0][1] },
|
|
483
530
|
sum: input_array[0][0] * input_array[0][1],
|
|
484
|
-
pos: 0
|
|
531
|
+
pos: 0,
|
|
485
532
|
}
|
|
486
533
|
}
|
|
487
|
-
let vol = 0,
|
|
534
|
+
let vol = 0,
|
|
535
|
+
price = 0,
|
|
536
|
+
sum = 0,
|
|
537
|
+
pos = 0,
|
|
538
|
+
vol_exchange = {},
|
|
539
|
+
price_exchange = {},
|
|
540
|
+
sum_exchange = {},
|
|
541
|
+
pos_exchange = {}
|
|
488
542
|
for (let i = 0; i < input_array.length; i++) {
|
|
489
543
|
if (vol < vol_target) {
|
|
490
544
|
let exchange = input_array[i][2]
|
|
491
545
|
vol = vol + input_array[i][1]
|
|
492
|
-
vol_exchange[exchange] =
|
|
546
|
+
vol_exchange[exchange] = vol_exchange[exchange] === undefined ? input_array[i][1] : vol_exchange[exchange] + input_array[i][1]
|
|
493
547
|
sum = sum + input_array[i][0] * input_array[i][1]
|
|
494
|
-
sum_exchange[exchange] =
|
|
548
|
+
sum_exchange[exchange] = sum_exchange[exchange] === undefined ? input_array[i][0] * input_array[i][1] : sum_exchange[exchange] + input_array[i][0] * input_array[i][1]
|
|
495
549
|
price = input_array[i][0]
|
|
496
550
|
price_exchange[exchange] = price
|
|
497
551
|
pos = i
|
|
498
|
-
pos_exchange[exchange] =
|
|
552
|
+
pos_exchange[exchange] = pos_exchange[exchange] === undefined ? 0 : pos_exchange[exchange] + 1
|
|
499
553
|
} else if (vol >= vol_target) {
|
|
500
554
|
sum_exchange[input_array[i - 1][2]] -= input_array[i - 1][0] * (vol - vol_target)
|
|
501
555
|
sum -= input_array[i - 1][0] * (vol - vol_target)
|
|
@@ -518,16 +572,16 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
518
572
|
// [ 7, 7, 'poloniex' ],
|
|
519
573
|
// [ 8, 8, 'bittrex' ] ], 5))
|
|
520
574
|
|
|
521
|
-
module.exports.sort_dict = function(dict, mode) {
|
|
522
|
-
var items = Object.keys(dict).map(function(key) {
|
|
523
|
-
|
|
575
|
+
module.exports.sort_dict = function (dict, mode) {
|
|
576
|
+
var items = Object.keys(dict).map(function (key) {
|
|
577
|
+
return [key, dict[key]]
|
|
524
578
|
})
|
|
525
|
-
items.sort(function(first, second) {
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
579
|
+
items.sort(function (first, second) {
|
|
580
|
+
if (mode === 0) {
|
|
581
|
+
return second[1] - first[1]
|
|
582
|
+
} else {
|
|
583
|
+
return first[1] - second[1]
|
|
584
|
+
}
|
|
531
585
|
})
|
|
532
586
|
return items
|
|
533
587
|
}
|
|
@@ -602,16 +656,13 @@ module.exports.similar = function (a, b, scale) {
|
|
|
602
656
|
module.exports.parse_pair = function (pair) {
|
|
603
657
|
pair = pair.toUpperCase()
|
|
604
658
|
let time
|
|
605
|
-
[pair, time] = _.split(pair, '_')
|
|
659
|
+
;[pair, time] = _.split(pair, '_')
|
|
606
660
|
let result
|
|
607
661
|
if (pair.endsWith('USDT') || pair.endsWith('USDC') || pair.endsWith('TUSD') || pair.endsWith('BUSD') || pair.endsWith('USDS') || pair.endsWith('DKKT') || pair.endsWith('CNYT'))
|
|
608
662
|
result = [pair.substr(0, pair.length - 4), pair.substr(pair.length - 4, pair.length)]
|
|
609
|
-
else if (pair.endsWith('QC') || pair.endsWith('ZB'))
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
result = [pair.substr(0, pair.length - 3), pair.substr(pair.length - 3, pair.length)]
|
|
613
|
-
if (time)
|
|
614
|
-
result[1] += '_' + time
|
|
663
|
+
else if (pair.endsWith('QC') || pair.endsWith('ZB')) result = [pair.substr(0, pair.length - 2), pair.substr(pair.length - 2, pair.length)]
|
|
664
|
+
else result = [pair.substr(0, pair.length - 3), pair.substr(pair.length - 3, pair.length)]
|
|
665
|
+
if (time) result[1] += '_' + time
|
|
615
666
|
return result
|
|
616
667
|
}
|
|
617
668
|
|
|
@@ -621,7 +672,10 @@ module.exports.reverse_pair = function (pair) {
|
|
|
621
672
|
}
|
|
622
673
|
|
|
623
674
|
module.exports.cur_dict_to_pair_list = function (cur_dict) {
|
|
624
|
-
return [].concat.apply(
|
|
675
|
+
return [].concat.apply(
|
|
676
|
+
[],
|
|
677
|
+
Object.entries(cur_dict).map((e) => e[1].map((j) => j + e[0]))
|
|
678
|
+
)
|
|
625
679
|
}
|
|
626
680
|
|
|
627
681
|
module.exports.number_with_commas = (x) => {
|
|
@@ -631,18 +685,19 @@ module.exports.number_with_commas = (x) => {
|
|
|
631
685
|
}
|
|
632
686
|
|
|
633
687
|
module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
634
|
-
let exchange_pairs = {},
|
|
688
|
+
let exchange_pairs = {},
|
|
689
|
+
exchange_curs = {}
|
|
635
690
|
for (let exchange in exchange_basecur_curs) {
|
|
636
691
|
_.set(exchange_pairs, [exchange], [])
|
|
637
692
|
_.set(exchange_curs, [exchange], [])
|
|
638
693
|
for (let base_cur in exchange_basecur_curs[exchange]) {
|
|
639
|
-
exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][base_cur], cur => cur + base_cur))
|
|
694
|
+
exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][base_cur], (cur) => cur + base_cur))
|
|
640
695
|
exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_basecur_curs[exchange][base_cur], [base_cur]))
|
|
641
696
|
}
|
|
642
697
|
}
|
|
643
698
|
let pair_exchanges = {}
|
|
644
699
|
for (let exchange in exchange_pairs) {
|
|
645
|
-
exchange_pairs[exchange].map(pair => {
|
|
700
|
+
exchange_pairs[exchange].map((pair) => {
|
|
646
701
|
if (!pair_exchanges[pair]) {
|
|
647
702
|
pair_exchanges[pair] = []
|
|
648
703
|
}
|
|
@@ -653,10 +708,10 @@ module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
|
653
708
|
exchange_basecur_curs,
|
|
654
709
|
exchange_pairs,
|
|
655
710
|
exchange_curs,
|
|
656
|
-
pair_exchanges
|
|
711
|
+
pair_exchanges,
|
|
657
712
|
}
|
|
658
713
|
}
|
|
659
714
|
|
|
660
715
|
module.exports.sort_keys = (dict) => {
|
|
661
|
-
return _.fromPairs(_.sortBy(_.toPairs(dict), e => e[0]))
|
|
716
|
+
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
662
717
|
}
|
package/lib/wechat-bot-client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const websocketclient = require('websocket').client
|
|
2
2
|
|
|
3
3
|
module.exports = class Wechat {
|
|
4
|
-
constructor
|
|
4
|
+
constructor(server_addr = 'http://172.105.199.80:8080', origin = '') {
|
|
5
5
|
this.client = new websocketclient()
|
|
6
6
|
this.server_addr = server_addr
|
|
7
7
|
this.connection
|
|
@@ -31,12 +31,12 @@ module.exports = class Wechat {
|
|
|
31
31
|
})
|
|
32
32
|
this.client.connect(this.server_addr, 'wechat-protocol', origin)
|
|
33
33
|
}
|
|
34
|
-
send
|
|
34
|
+
send(name, content, isgroup) {
|
|
35
35
|
if (this.connection_status === 'opened') {
|
|
36
36
|
let json = {
|
|
37
37
|
name,
|
|
38
38
|
content,
|
|
39
|
-
isgroup
|
|
39
|
+
isgroup,
|
|
40
40
|
}
|
|
41
41
|
this.connection.sendUTF(JSON.stringify(json))
|
|
42
42
|
} else {
|