@watsonserve/stock-base 0.0.4 → 0.0.6
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/dao.js +43 -0
- package/log.js +1 -1
- package/package.json +1 -1
- package/types/dao.d.ts +3 -1
- package/types/stock.d.ts +9 -0
package/dao.js
CHANGED
|
@@ -34,6 +34,22 @@ export function toStockPoint(market, st, timestamp) {
|
|
|
34
34
|
.floatField('ma_250', st.ma_250 || 0)
|
|
35
35
|
.timestamp(timestamp);
|
|
36
36
|
}
|
|
37
|
+
export function toDivPoints(market, nc, data) {
|
|
38
|
+
const { curr, amount, ex, paid } = data;
|
|
39
|
+
return [
|
|
40
|
+
new Point('Dividend').tag('nc', `${nc}.${market}`).tag('type', 'ex').tag('currency', curr).floatField('amount', amount).timestamp(ex),
|
|
41
|
+
new Point('Dividend').tag('nc', `${nc}.${market}`).tag('type', 'paid').tag('currency', curr).floatField('amount', amount).timestamp(paid)
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
function groupBy(key, list) {
|
|
45
|
+
return list.reduce((pre, item) => {
|
|
46
|
+
const foo = item[key];
|
|
47
|
+
const fooList = pre[foo] || [];
|
|
48
|
+
pre[foo] = fooList;
|
|
49
|
+
fooList.push(item);
|
|
50
|
+
return pre;
|
|
51
|
+
}, {});
|
|
52
|
+
}
|
|
37
53
|
export class InfluxDAO {
|
|
38
54
|
origin = '';
|
|
39
55
|
org = '';
|
|
@@ -100,4 +116,31 @@ export class InfluxDAO {
|
|
|
100
116
|
const start = getStartTime();
|
|
101
117
|
return this.__query(['c', 'v'], start, [EnMarket.SGX, EnMarket.USA, EnMarket.HKEX], ncs);
|
|
102
118
|
}
|
|
119
|
+
async readDividend(ncs) {
|
|
120
|
+
const { origin: url, token, org, bucket } = this;
|
|
121
|
+
const client = new InfluxDB({ url, token });
|
|
122
|
+
const queryClient = client.getQueryApi(org);
|
|
123
|
+
const ncQuery = ncs.map(nc => `r["nc"] == "${nc}"`).join(' or ');
|
|
124
|
+
const fluxQuery = `from(bucket: "${bucket}")
|
|
125
|
+
|> range(start: -1y, stop: 1y)
|
|
126
|
+
|> filter(fn: (r) => r["_measurement"] == "Dividend")
|
|
127
|
+
|> filter(fn: (r) => r["_field"] == "amount")
|
|
128
|
+
|> filter(fn: (r) => ${ncQuery})
|
|
129
|
+
|> filter(fn: (r) => r["type"] == "ex")
|
|
130
|
+
`;
|
|
131
|
+
return new Promise((resolve, reject) => {
|
|
132
|
+
const list = [];
|
|
133
|
+
queryClient.queryRows(fluxQuery, {
|
|
134
|
+
next: (row, tableMeta) => {
|
|
135
|
+
const item = tableMeta.toObject(row);
|
|
136
|
+
const { result, table, _start, _stop, _measurement, _time, nc, ...content } = item;
|
|
137
|
+
list.push({ time: new Date(_time), market: _measurement, nc, ...content });
|
|
138
|
+
},
|
|
139
|
+
error: reject,
|
|
140
|
+
complete: () => {
|
|
141
|
+
resolve(Object.entries(groupBy('nc', list)));
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
}
|
|
103
146
|
}
|
package/log.js
CHANGED
|
@@ -7,7 +7,7 @@ export var EnLogLevel;
|
|
|
7
7
|
EnLogLevel[EnLogLevel["ERROR"] = 3] = "ERROR";
|
|
8
8
|
})(EnLogLevel || (EnLogLevel = {}));
|
|
9
9
|
function findAppName() {
|
|
10
|
-
const __dirPath =
|
|
10
|
+
const __dirPath = process.argv.find(arg => arg.endsWith('.js'))?.split('/').reverse().slice(1) || [];
|
|
11
11
|
if (['release', 'src', 'dist'].includes(__dirPath[0])) {
|
|
12
12
|
__dirPath.shift();
|
|
13
13
|
}
|
package/package.json
CHANGED
package/types/dao.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Point } from '@influxdata/influxdb-client';
|
|
2
|
-
import { EnMarket, type IStock } from './stock.js';
|
|
2
|
+
import { EnMarket, IDividend, type IStock } from './stock.js';
|
|
3
3
|
export interface IDBConf {
|
|
4
4
|
origin: string;
|
|
5
5
|
org: string;
|
|
@@ -8,6 +8,7 @@ export interface IDBConf {
|
|
|
8
8
|
}
|
|
9
9
|
export declare function toFxPoint(sgd: number, hkd: number, cny: number, timestamp: number): Point;
|
|
10
10
|
export declare function toStockPoint(market: EnMarket, st: IStock, timestamp: number): Point;
|
|
11
|
+
export declare function toDivPoints(market: string, nc: string, data: IDividend): Point[];
|
|
11
12
|
export declare class InfluxDAO {
|
|
12
13
|
private origin;
|
|
13
14
|
private org;
|
|
@@ -19,4 +20,5 @@ export declare class InfluxDAO {
|
|
|
19
20
|
private __query;
|
|
20
21
|
readFxLatest(): Promise<any[]>;
|
|
21
22
|
readStockLatest(ncs: string[]): Promise<any[]>;
|
|
23
|
+
readDividend(ncs: string[]): Promise<any[]>;
|
|
22
24
|
}
|
package/types/stock.d.ts
CHANGED
|
@@ -37,6 +37,15 @@ export interface StockPoint {
|
|
|
37
37
|
timestamp: number;
|
|
38
38
|
st: IStock;
|
|
39
39
|
}
|
|
40
|
+
export interface IDividend {
|
|
41
|
+
curr: string;
|
|
42
|
+
amount?: number;
|
|
43
|
+
ratio?: number;
|
|
44
|
+
annc: Date;
|
|
45
|
+
ex: Date;
|
|
46
|
+
paid: Date;
|
|
47
|
+
announcement_URL?: string;
|
|
48
|
+
}
|
|
40
49
|
export declare class Stock {
|
|
41
50
|
nc: string;
|
|
42
51
|
name: string;
|