@icgio/icg-utils 1.7.21 → 1.7.22
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 +137 -105
- 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]
|
|
@@ -328,8 +351,8 @@ module.exports.vol_position_orderbook = (input_array, vol_target) => {
|
|
|
328
351
|
// [ 0.00004512, 0.64 ],
|
|
329
352
|
// [ 0.000045, 3.24 ] ], 0))
|
|
330
353
|
|
|
331
|
-
module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
332
|
-
function merge_sorted
|
|
354
|
+
module.exports.combine_orderbooks = (orderbooks_dict, length = undefined) => {
|
|
355
|
+
function merge_sorted(type) {
|
|
333
356
|
let count = {}
|
|
334
357
|
for (let exchange in orderbooks_dict) {
|
|
335
358
|
count[exchange] = 0
|
|
@@ -338,10 +361,8 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
338
361
|
while (_.sum(_.values(count)) !== _.keys(orderbooks_dict).length * -1) {
|
|
339
362
|
let picked
|
|
340
363
|
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)
|
|
364
|
+
if (count[exchange] === -1) continue
|
|
365
|
+
if (_.get(orderbooks_dict, [exchange, type, count[exchange], 'length']) === 2) orderbooks_dict[exchange][type][count[exchange]].push(exchange)
|
|
345
366
|
if (!picked) {
|
|
346
367
|
picked = orderbooks_dict[exchange][type][count[exchange]]
|
|
347
368
|
} else if (type === 'asks' && picked[0] > orderbooks_dict[exchange][type][count[exchange]][0]) {
|
|
@@ -356,8 +377,7 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
356
377
|
} else {
|
|
357
378
|
count[picked[2]] = -1
|
|
358
379
|
}
|
|
359
|
-
if (result.length === length)
|
|
360
|
-
break
|
|
380
|
+
if (result.length === length) break
|
|
361
381
|
}
|
|
362
382
|
return result
|
|
363
383
|
}
|
|
@@ -381,16 +401,19 @@ module.exports.combine_orderbooks = (orderbooks_dict, length=undefined) => {
|
|
|
381
401
|
// }
|
|
382
402
|
// }, 3))
|
|
383
403
|
|
|
384
|
-
module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage=0, amount_cap=0) => {
|
|
385
|
-
let i = 0,
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
404
|
+
module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined, profit_percentage = 0, amount_cap = 0) => {
|
|
405
|
+
let i = 0,
|
|
406
|
+
j = 0,
|
|
407
|
+
ask_amount = 0,
|
|
408
|
+
bid_amount = 0
|
|
409
|
+
let asks = orderbook_combined.asks,
|
|
410
|
+
bids = orderbook_combined.bids
|
|
411
|
+
if (asks[0][0] * (1 + profit_percentage) >= bids[0][0]) return {}
|
|
389
412
|
let last_step = ''
|
|
390
413
|
while (i < asks.length && j < bids.length) {
|
|
391
414
|
if (asks[i][0] * (1 + profit_percentage) < bids[j][0]) {
|
|
392
415
|
if (ask_amount === 0 && bid_amount === 0) {
|
|
393
|
-
ask_amount = asks[i][1], bid_amount = bids[j][1]
|
|
416
|
+
;(ask_amount = asks[i][1]), (bid_amount = bids[j][1])
|
|
394
417
|
} else if (ask_amount <= bid_amount) {
|
|
395
418
|
i++
|
|
396
419
|
ask_amount += asks[i][1]
|
|
@@ -411,21 +434,22 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
411
434
|
break
|
|
412
435
|
}
|
|
413
436
|
}
|
|
414
|
-
let opportunities = {},
|
|
437
|
+
let opportunities = {},
|
|
438
|
+
diff = ask_amount - bid_amount
|
|
415
439
|
asks = asks.slice(0, i + 1)
|
|
416
440
|
bids = bids.slice(0, j + 1)
|
|
417
441
|
if (diff > 0) {
|
|
418
|
-
asks[i][1] -= diff
|
|
442
|
+
asks[i][1] -= diff
|
|
419
443
|
} else {
|
|
420
444
|
bids[j][1] += diff
|
|
421
445
|
}
|
|
422
|
-
asks.map(ask => {
|
|
446
|
+
asks.map((ask) => {
|
|
423
447
|
if (!opportunities[ask[2]]) {
|
|
424
448
|
opportunities[ask[2]] = {
|
|
425
449
|
price: 0,
|
|
426
450
|
amount: 0,
|
|
427
451
|
exchange: ask[2],
|
|
428
|
-
method: 'buy'
|
|
452
|
+
method: 'buy',
|
|
429
453
|
}
|
|
430
454
|
}
|
|
431
455
|
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 +457,13 @@ module.exports.arb_opportunities_from_combined_orderbook = (orderbook_combined,
|
|
|
433
457
|
opportunities[ask[2]].amount += ask[1]
|
|
434
458
|
opportunities[ask[2]].total = opportunities[ask[2]].avg_price * opportunities[ask[2]].amount
|
|
435
459
|
})
|
|
436
|
-
bids.map(bid => {
|
|
460
|
+
bids.map((bid) => {
|
|
437
461
|
if (!opportunities[bid[2]]) {
|
|
438
462
|
opportunities[bid[2]] = {
|
|
439
463
|
price: 0,
|
|
440
464
|
amount: 0,
|
|
441
465
|
exchange: bid[2],
|
|
442
|
-
method: 'sell'
|
|
466
|
+
method: 'sell',
|
|
443
467
|
}
|
|
444
468
|
}
|
|
445
469
|
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 +505,28 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
481
505
|
vol: input_array[0][1],
|
|
482
506
|
vol_exchange: { [input_array[0][2]]: input_array[0][1] },
|
|
483
507
|
sum: input_array[0][0] * input_array[0][1],
|
|
484
|
-
pos: 0
|
|
508
|
+
pos: 0,
|
|
485
509
|
}
|
|
486
510
|
}
|
|
487
|
-
let vol = 0,
|
|
511
|
+
let vol = 0,
|
|
512
|
+
price = 0,
|
|
513
|
+
sum = 0,
|
|
514
|
+
pos = 0,
|
|
515
|
+
vol_exchange = {},
|
|
516
|
+
price_exchange = {},
|
|
517
|
+
sum_exchange = {},
|
|
518
|
+
pos_exchange = {}
|
|
488
519
|
for (let i = 0; i < input_array.length; i++) {
|
|
489
520
|
if (vol < vol_target) {
|
|
490
521
|
let exchange = input_array[i][2]
|
|
491
522
|
vol = vol + input_array[i][1]
|
|
492
|
-
vol_exchange[exchange] =
|
|
523
|
+
vol_exchange[exchange] = vol_exchange[exchange] === undefined ? input_array[i][1] : vol_exchange[exchange] + input_array[i][1]
|
|
493
524
|
sum = sum + input_array[i][0] * input_array[i][1]
|
|
494
|
-
sum_exchange[exchange] =
|
|
525
|
+
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
526
|
price = input_array[i][0]
|
|
496
527
|
price_exchange[exchange] = price
|
|
497
528
|
pos = i
|
|
498
|
-
pos_exchange[exchange] =
|
|
529
|
+
pos_exchange[exchange] = pos_exchange[exchange] === undefined ? 0 : pos_exchange[exchange] + 1
|
|
499
530
|
} else if (vol >= vol_target) {
|
|
500
531
|
sum_exchange[input_array[i - 1][2]] -= input_array[i - 1][0] * (vol - vol_target)
|
|
501
532
|
sum -= input_array[i - 1][0] * (vol - vol_target)
|
|
@@ -518,16 +549,16 @@ module.exports.vol_position_combined = function (input_array, vol_target) {
|
|
|
518
549
|
// [ 7, 7, 'poloniex' ],
|
|
519
550
|
// [ 8, 8, 'bittrex' ] ], 5))
|
|
520
551
|
|
|
521
|
-
module.exports.sort_dict = function(dict, mode) {
|
|
522
|
-
var items = Object.keys(dict).map(function(key) {
|
|
523
|
-
|
|
552
|
+
module.exports.sort_dict = function (dict, mode) {
|
|
553
|
+
var items = Object.keys(dict).map(function (key) {
|
|
554
|
+
return [key, dict[key]]
|
|
524
555
|
})
|
|
525
|
-
items.sort(function(first, second) {
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
556
|
+
items.sort(function (first, second) {
|
|
557
|
+
if (mode === 0) {
|
|
558
|
+
return second[1] - first[1]
|
|
559
|
+
} else {
|
|
560
|
+
return first[1] - second[1]
|
|
561
|
+
}
|
|
531
562
|
})
|
|
532
563
|
return items
|
|
533
564
|
}
|
|
@@ -602,16 +633,13 @@ module.exports.similar = function (a, b, scale) {
|
|
|
602
633
|
module.exports.parse_pair = function (pair) {
|
|
603
634
|
pair = pair.toUpperCase()
|
|
604
635
|
let time
|
|
605
|
-
[pair, time] = _.split(pair, '_')
|
|
636
|
+
;[pair, time] = _.split(pair, '_')
|
|
606
637
|
let result
|
|
607
638
|
if (pair.endsWith('USDT') || pair.endsWith('USDC') || pair.endsWith('TUSD') || pair.endsWith('BUSD') || pair.endsWith('USDS') || pair.endsWith('DKKT') || pair.endsWith('CNYT'))
|
|
608
639
|
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
|
|
640
|
+
else if (pair.endsWith('QC') || pair.endsWith('ZB')) result = [pair.substr(0, pair.length - 2), pair.substr(pair.length - 2, pair.length)]
|
|
641
|
+
else result = [pair.substr(0, pair.length - 3), pair.substr(pair.length - 3, pair.length)]
|
|
642
|
+
if (time) result[1] += '_' + time
|
|
615
643
|
return result
|
|
616
644
|
}
|
|
617
645
|
|
|
@@ -621,7 +649,10 @@ module.exports.reverse_pair = function (pair) {
|
|
|
621
649
|
}
|
|
622
650
|
|
|
623
651
|
module.exports.cur_dict_to_pair_list = function (cur_dict) {
|
|
624
|
-
return [].concat.apply(
|
|
652
|
+
return [].concat.apply(
|
|
653
|
+
[],
|
|
654
|
+
Object.entries(cur_dict).map((e) => e[1].map((j) => j + e[0]))
|
|
655
|
+
)
|
|
625
656
|
}
|
|
626
657
|
|
|
627
658
|
module.exports.number_with_commas = (x) => {
|
|
@@ -631,18 +662,19 @@ module.exports.number_with_commas = (x) => {
|
|
|
631
662
|
}
|
|
632
663
|
|
|
633
664
|
module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
634
|
-
let exchange_pairs = {},
|
|
665
|
+
let exchange_pairs = {},
|
|
666
|
+
exchange_curs = {}
|
|
635
667
|
for (let exchange in exchange_basecur_curs) {
|
|
636
668
|
_.set(exchange_pairs, [exchange], [])
|
|
637
669
|
_.set(exchange_curs, [exchange], [])
|
|
638
670
|
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))
|
|
671
|
+
exchange_pairs[exchange] = exchange_pairs[exchange].concat(_.map(exchange_basecur_curs[exchange][base_cur], (cur) => cur + base_cur))
|
|
640
672
|
exchange_curs[exchange] = _.union(exchange_curs[exchange], _.union(exchange_basecur_curs[exchange][base_cur], [base_cur]))
|
|
641
673
|
}
|
|
642
674
|
}
|
|
643
675
|
let pair_exchanges = {}
|
|
644
676
|
for (let exchange in exchange_pairs) {
|
|
645
|
-
exchange_pairs[exchange].map(pair => {
|
|
677
|
+
exchange_pairs[exchange].map((pair) => {
|
|
646
678
|
if (!pair_exchanges[pair]) {
|
|
647
679
|
pair_exchanges[pair] = []
|
|
648
680
|
}
|
|
@@ -653,10 +685,10 @@ module.exports.exchange_basecur_curs_converter = (exchange_basecur_curs) => {
|
|
|
653
685
|
exchange_basecur_curs,
|
|
654
686
|
exchange_pairs,
|
|
655
687
|
exchange_curs,
|
|
656
|
-
pair_exchanges
|
|
688
|
+
pair_exchanges,
|
|
657
689
|
}
|
|
658
690
|
}
|
|
659
691
|
|
|
660
692
|
module.exports.sort_keys = (dict) => {
|
|
661
|
-
return _.fromPairs(_.sortBy(_.toPairs(dict), e => e[0]))
|
|
693
|
+
return _.fromPairs(_.sortBy(_.toPairs(dict), (e) => e[0]))
|
|
662
694
|
}
|
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 {
|