quote-observer 1.3.0 → 1.3.4

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.
Files changed (3) hide show
  1. package/cmd/ma.js +35 -15
  2. package/index.js +4 -2
  3. package/package.json +1 -1
package/cmd/ma.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @Author: kael
3
3
  * @Date: 2020-11-05 18:19:03
4
4
  * @Last Modified by: kael
5
- * @Last Modified time: 2021-08-13 15:18:14
5
+ * @Last Modified time: 2021-11-09 10:43:03
6
6
  */
7
7
 
8
8
  const logger = require('../logger')();
@@ -11,11 +11,15 @@ const Api = require('../api');
11
11
 
12
12
  // https://github.com/Marak/colors.js/blob/56de9f0983f68cd0a08c5b76d10a783e4b881716/lib/styles.js
13
13
  const codes = {
14
+ black: [30, 39],
14
15
  red: [31, 39],
15
- cyan: [36, 39],
16
- gray: [90, 39],
17
16
  green: [32, 39],
17
+ yellow: [33, 39],
18
+ blue: [34, 39],
19
+ magenta: [35, 39],
20
+ cyan: [36, 39],
18
21
  white: [37, 39],
22
+ gray: [90, 39],
19
23
  };
20
24
 
21
25
  function colors(string, name) {
@@ -48,10 +52,23 @@ function marginal_value(datas) {
48
52
  };
49
53
  }
50
54
 
51
- const padding = [16, 16, 15, 15, 15, 15, 15, 15, 15];
55
+ const padding = [
56
+ 16,
57
+ 16,
58
+ 15,
59
+ 15,
60
+ 15,
61
+ 15,
62
+ 15,
63
+ { size: 15, color: (v) => /^\+/.test(v) ? 'cyan' : 'white' },
64
+ { size: 15, color: (v) => /-\]/.test(v) ? 'blue' : 'white' },
65
+ ];
52
66
  const pad = (items) => {
53
67
  return items.map((item, index) => {
54
- return item[index ? 'padStart' : 'padEnd'](padding[index] - item.split(/[^\x00-\xff]/).length - 1);
68
+ let conf = padding[index];
69
+ let size = conf.size || conf;
70
+ let text = item[index ? 'padStart' : 'padEnd'](size - item.split(/[^\x00-\xff]/).length - 1);
71
+ return conf.color ? colors(text, conf.color(item)) : text;
55
72
  }).join('');
56
73
  };
57
74
 
@@ -69,12 +86,17 @@ function roc(MV) {
69
86
  return (v > 0 ? '+' : '') + v.toFixed(2) + '%';
70
87
  }
71
88
 
89
+ function ceil(v, d = 2) {
90
+ return Math.ceil(Math.abs(v)).toString().padStart(d, '0');
91
+ }
92
+
72
93
  function sale_signal(code, now) {
73
94
  let stock = STOCKS_MAP.get(code);
74
- if (!stock) return '';
75
95
  let { cost_price } = stock;
96
+ if (!cost_price) return '[=====][00]';
76
97
  let irate = 100 * (now - cost_price) / cost_price;
77
- return [-25, -15, -8, -5, -2].map(signal => irate < signal ? '-' : '').join('');
98
+ let srate = [-25, -15, -8, -5, -2].map(signal => irate < signal ? '-' : ' ').join('');
99
+ return `[${srate}][${ceil(irate)}]`;
78
100
  }
79
101
 
80
102
  function start(codes) {
@@ -85,7 +107,7 @@ function start(codes) {
85
107
  console.log(LINE);
86
108
  console.log(pad(['ID', '名称', 'NOW', 'MA3', 'MA5', 'MVC', 'MVN', 'MVN-ROC', 'S-SIG']));
87
109
  console.log(LINE);
88
- let hold_line = false;
110
+ let rows = [[], LINE.replace(/=/g, '-'), []];
89
111
  let errors = resps.filter((data, index) => {
90
112
  let { fid, id, info, data: resp } = data;
91
113
  if (!id || !info || !resp) return false;
@@ -96,16 +118,14 @@ function start(codes) {
96
118
  if (!MV.now) return global.MARKET[id] = `us${info[2]}`;
97
119
  let hold_star = holding(fid);
98
120
  let row = pad([
99
- fid + hold_star, name,
121
+ fid + hold_star, String(name).replace(/-\w+$/, ''),
100
122
  MV.now.toFixed(3), MA3.toFixed(3), MA5.toFixed(3),
101
- MV.curr.toFixed(3), MV.next.toFixed(3), roc(MV), sale_signal(fid, MV.now),
123
+ MV.curr.toFixed(3), MV.next.toFixed(3), roc(MV),
124
+ sale_signal(fid, MV.now),
102
125
  ]);
103
- if (!hold_line && !hold_star) {
104
- console.log(LINE.replace(/=/g, '-'));
105
- hold_line = true;
106
- }
107
- console.log(colors(row, MA3 > MA5 ? 'red' : MA3 < MA5 ? 'green' : 'white'));
126
+ rows[hold_star ? 0 : 2].push(colors(row, MA3 > MA5 ? 'red' : MA3 < MA5 ? 'green' : 'white'))
108
127
  });
128
+ console.log(rows.flat().join('\n'));
109
129
  console.log(LINE);
110
130
  if (errors.length) throw new Error('Errors occur.');
111
131
  setTimeout(() => start(codes), 10 * 1000);
package/index.js CHANGED
@@ -4,7 +4,7 @@
4
4
  * @Author: kael
5
5
  * @Date: 2020-08-05 11:04:09
6
6
  * @Last Modified by: kael
7
- * @Last Modified time: 2020-11-11 10:31:24
7
+ * @Last Modified time: 2021-11-29 15:14:17
8
8
  */
9
9
 
10
10
  global.MARKET = {};
@@ -23,8 +23,10 @@ program
23
23
  program.parse(process.argv);
24
24
  global.config.args = program.opts();
25
25
 
26
+ // https://stackoverflow.com/questions/62499125/how-to-clear-terminal-and-scroll-back-in-nodejs
26
27
  console.reset = function () {
27
- return process.stdout.write('\033c\n');
28
+ process.stdout.write('\u001b[3J\u001b[1J');
29
+ console.clear();
28
30
  };
29
31
 
30
32
  cmd.ma();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "quote-observer",
3
- "version": "1.3.0",
3
+ "version": "1.3.4",
4
4
  "description": "quote observer",
5
5
  "main": "index.js",
6
6
  "scripts": {