@watsonserve/stock-base 0.0.24 → 0.0.25

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/history-loader.js CHANGED
@@ -40,7 +40,7 @@ export default class HistoryLoader extends WebRequest {
40
40
  });
41
41
  }
42
42
  async __getSGXHistory(nc) {
43
- const rsp = await super.get(`https://api.sgx.com/securities/v1.1//charts/historic/stocks/code/${nc}/5y`, { params: 'trading_time,vl,lt' });
43
+ const rsp = await super.get(`https://api.sgx.com/securities/v1.1/charts/historic/stocks/code/${nc}/5y`, { params: 'trading_time,vl,lt' });
44
44
  const { historic } = rsp.data;
45
45
  return historic.map(st => {
46
46
  const { trading_time, vl: v, lt: c } = st;
@@ -51,6 +51,40 @@ export default class HistoryLoader extends WebRequest {
51
51
  return { market: EnMarket.SGX, timestamp, st: { nc, n: '', o: 0, c, h: 0, l: 0, v } };
52
52
  });
53
53
  }
54
+ async loadFxHistory() {
55
+ const fxs = await Promise.all(['USDHKD', 'USDSGD', 'USDCNY'].map(code => this.get('https://finance.pae.baidu.com/vapi/v1/getquotation', { group: 'huilv_kline', ktype: 'day', code, finClientType: 'pc' })));
56
+ const [hk, sg, cn] = fxs.map(body => body.Result.newMarketData.marketData.split(';'));
57
+ const base = new Map(hk.map(line => {
58
+ const [_0, date, _1, c] = line.split(',');
59
+ const stamp = getMarketCloseTime(EnMarket.FX, new Date(`${date}T15:00:00Z`).getTime());
60
+ return [stamp, { date, HKD: +c }];
61
+ }));
62
+ sg.forEach(line => {
63
+ const [_0, date, _1, c] = line.split(',');
64
+ const stamp = getMarketCloseTime(EnMarket.FX, new Date(`${date}T15:00:00Z`).getTime());
65
+ const fx = base.get(stamp) || { date };
66
+ fx.SGD = +c;
67
+ base.set(stamp, fx);
68
+ });
69
+ cn.map(line => {
70
+ const [_0, date, _1, c] = line.split(',');
71
+ const stamp = getMarketCloseTime(EnMarket.FX, new Date(`${date}T15:00:00Z`).getTime());
72
+ const fx = base.get(stamp) || { date };
73
+ fx.CNY = +c;
74
+ base.set(stamp, fx);
75
+ });
76
+ const result = [];
77
+ const remain = [];
78
+ [...base.entries()].forEach(([stamp, fx]) => {
79
+ const _fx = { stamp, ...fx };
80
+ if (!fx.HKD || !fx.SGD || !fx.CNY) {
81
+ remain.push(_fx);
82
+ return;
83
+ }
84
+ result.push(_fx);
85
+ });
86
+ return { result, remain };
87
+ }
54
88
  loadHistory(fullNc) {
55
89
  const { nc, market } = splitFullNc(fullNc);
56
90
  switch (market) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watsonserve/stock-base",
3
- "version": "0.0.24",
3
+ "version": "0.0.25",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,7 +1,11 @@
1
- import { type StockPoint } from './stock.js';
1
+ import { IFx, type IStockPoint } from './stock.js';
2
2
  import WebRequest from './web-request.js';
3
3
  export default class HistoryLoader extends WebRequest {
4
4
  private __getHKOrUSAHistory;
5
5
  private __getSGXHistory;
6
- loadHistory(fullNc: string): Promise<StockPoint[]>;
6
+ loadFxHistory(): Promise<{
7
+ result: IFx[];
8
+ remain: IFx[];
9
+ }>;
10
+ loadHistory(fullNc: string): Promise<IStockPoint[]>;
7
11
  }
@@ -1,10 +1,8 @@
1
- import { EnMarket, IStock } from './stock.js';
1
+ import { EnMarket, type IFx, IStock } from './stock.js';
2
2
  import HistoryLoader from './history-loader.js';
3
3
  export default class PriceLoader extends HistoryLoader {
4
4
  private __loadFx;
5
- loadFx(): Promise<{
6
- stamp: number;
7
- }>;
5
+ loadFx(): Promise<IFx>;
8
6
  protected sgx(): Promise<IStock[]>;
9
7
  protected hkex(): Promise<IStock[]>;
10
8
  private __sp500;
package/types/stock.d.ts CHANGED
@@ -13,6 +13,12 @@ export declare enum EnCurrency {
13
13
  HKD = "HKD",
14
14
  CNY = "CNY"
15
15
  }
16
+ export interface IFx {
17
+ stamp: number;
18
+ [EnCurrency.SGD]: number;
19
+ [EnCurrency.HKD]: number;
20
+ [EnCurrency.CNY]: number;
21
+ }
16
22
  export interface IStockBase {
17
23
  c: number;
18
24
  o?: number;
@@ -32,7 +38,7 @@ export interface IStock extends IStockBase, Partial<IStockNum> {
32
38
  nc: string;
33
39
  n: string;
34
40
  }
35
- export interface StockPoint {
41
+ export interface IStockPoint {
36
42
  market: string;
37
43
  timestamp: number;
38
44
  st: IStock;