Skip to content

Commit

Permalink
fix: correct field mapping for institutional investors' trades in TPE…
Browse files Browse the repository at this point in the history
…x stocks
  • Loading branch information
chunkai1312 committed Oct 24, 2024
1 parent fc4d7b1 commit d5e1906
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 62 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ const { TwStock } = require('node-twstock');

* `options`: {Object} 可配置選項,用於設定速率限制 (Rate Limiting)
- `ttl`: 每個請求持續的毫秒數。**Default:** `5000`
- `limit`: 在 TTL 限制內的最大請求數。**Default:** `3`
- `limit`: 在 TTL 限制內的最大請求數。**Default:** `1`

> 注意:過於頻繁的請求可能導致被交易所禁止訪問。預設設定為每 5 秒最多發送 3 個請求。
> 注意:過於頻繁的請求可能導致被交易所禁止訪問。預設設定為每 5 秒最多發送 1 個請求。
```js
const twstock = new TwStock({ ttl: 5000, limit: 3 });
Expand Down Expand Up @@ -273,7 +273,7 @@ twstock.stocks.institutional({ date: '2023-01-30', symbol: '2330' })
// difference: 937673
// },
// {
// investor: '自營商合計',
// investor: '自營商',
// difference: 880408
// },
// {
Expand All @@ -289,7 +289,7 @@ twstock.stocks.institutional({ date: '2023-01-30', symbol: '2330' })
// difference: 439408
// },
// {
// investor: '合計',
// investor: '三大法人',
// difference: 82459130
// }
// ]
Expand Down Expand Up @@ -983,7 +983,7 @@ twstock.market.institutional({ date: '2023-01-30', exchange: 'TWSE' })
// difference: -36789050
// },
// {
// investor: '合計',
// investor: '三大法人',
// totalBuy: 226200542418,
// totalSell: 143066882919,
// difference: 83133659499
Expand Down
131 changes: 83 additions & 48 deletions src/scrapers/tpex-scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,54 +65,89 @@ export class TpexScraper extends Scraper {
data.exchange = Exchange.TPEx;
data.symbol = symbol;
data.name = name.trim();
data.institutional = [
{
investor: '外資及陸資(不含外資自營商)',
totalBuy: numeral(values[0]).value(),
totalSell: numeral(values[1]).value(),
difference: numeral(values[2]).value(),
},
{
investor: '外資自營商',
totalBuy: numeral(values[3]).value(),
totalSell: numeral(values[4]).value(),
difference: numeral(values[5]).value(),
},
{
investor: '外資及陸資合計',
totalBuy: numeral(values[6]).value(),
totalSell: numeral(values[7]).value(),
difference: numeral(values[8]).value(),
},
{
investor: '投信',
totalBuy: numeral(values[9]).value(),
totalSell: numeral(values[10]).value(),
difference: numeral(values[11]).value(),
},
{
investor: '自營商(自行買賣)',
totalBuy: numeral(values[12]).value(),
totalSell: numeral(values[13]).value(),
difference: numeral(values[14]).value(),
},
{
investor: '自營商(避險)',
totalBuy: numeral(values[15]).value(),
totalSell: numeral(values[16]).value(),
difference: numeral(values[17]).value(),
},
{
investor: '自營商合計',
totalBuy: numeral(values[18]).value(),
totalSell: numeral(values[19]).value(),
difference: numeral(values[20]).value(),
},
{
investor: '合計',
difference: numeral(values[21]).value(),
},
];
data.institutional = (values.length < 22)
? [
{
investor: '外資及陸資',
totalBuy: numeral(values[0]).value(),
totalSell: numeral(values[1]).value(),
difference: numeral(values[2]).value(),
},
{
investor: '投信',
totalBuy: numeral(values[3]).value(),
totalSell: numeral(values[4]).value(),
difference: numeral(values[5]).value(),
},
{
investor: '自營商',
difference: numeral(values[6]).value(),
},
{
investor: '自營商(自行買賣)',
totalBuy: numeral(values[7]).value(),
totalSell: numeral(values[8]).value(),
difference: numeral(values[9]).value(),
},
{
investor: '自營商(避險)',
totalBuy: numeral(values[10]).value(),
totalSell: numeral(values[11]).value(),
difference: numeral(values[12]).value(),
},
{
investor: '三大法人',
difference: numeral(values[13]).value(),
},
]
: [
{
investor: '外資及陸資(不含外資自營商)',
totalBuy: numeral(values[0]).value(),
totalSell: numeral(values[1]).value(),
difference: numeral(values[2]).value(),
},
{
investor: '外資自營商',
totalBuy: numeral(values[3]).value(),
totalSell: numeral(values[4]).value(),
difference: numeral(values[5]).value(),
},
{
investor: '外資及陸資',
totalBuy: numeral(values[6]).value(),
totalSell: numeral(values[7]).value(),
difference: numeral(values[8]).value(),
},
{
investor: '投信',
totalBuy: numeral(values[9]).value(),
totalSell: numeral(values[10]).value(),
difference: numeral(values[11]).value(),
},
{
investor: '自營商(自行買賣)',
totalBuy: numeral(values[12]).value(),
totalSell: numeral(values[13]).value(),
difference: numeral(values[14]).value(),
},
{
investor: '自營商(避險)',
totalBuy: numeral(values[15]).value(),
totalSell: numeral(values[16]).value(),
difference: numeral(values[17]).value(),
},
{
investor: '自營商',
totalBuy: numeral(values[18]).value(),
totalSell: numeral(values[19]).value(),
difference: numeral(values[20]).value(),
},
{
investor: '三大法人',
difference: numeral(values[21]).value(),
},
];
return data;
}) as Record<string, any>[];

Expand Down
6 changes: 3 additions & 3 deletions src/scrapers/twse-scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class TwseScraper extends Scraper {
difference: numeral(values[8]).value(),
},
{
investor: '自營商合計',
investor: '自營商',
difference: numeral(values[9]).value(),
},
{
Expand All @@ -96,7 +96,7 @@ export class TwseScraper extends Scraper {
difference: numeral(values[15]).value(),
},
{
investor: '合計',
investor: '三大法人',
difference: numeral(values[16]).value(),
},
];
Expand Down Expand Up @@ -577,7 +577,7 @@ export class TwseScraper extends Scraper {
data.date = date;
data.exchange = Exchange.TWSE,
data.institutional = json.data.map((row: string[]) => ({
investor: row[0].trim(),
investor: row[0] === '合計' ? '三大法人' : row[0],
totalBuy: numeral(row[1]).value(),
totalSell: numeral(row[2]).value(),
difference: numeral(row[3]).value(),
Expand Down

Large diffs are not rendered by default.

56 changes: 53 additions & 3 deletions test/scrapers/tpex-scraper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('TpexScraper', () => {
difference: 0,
},
{
investor: '外資及陸資合計',
investor: '外資及陸資',
totalBuy: 3059748,
totalSell: 2331441,
difference: 728307,
Expand All @@ -123,19 +123,69 @@ describe('TpexScraper', () => {
difference: 60167,
},
{
investor: '自營商合計',
investor: '自營商',
totalBuy: 395767,
totalSell: 224000,
difference: 171767,
},
{
investor: '合計',
investor: '三大法人',
difference: 1298978,
},
],
});
});

it('should fetch stocks institutional investors\' trades without foreign dealers for the specified stock on the given date', async () => {
mockAxios.get.mockResolvedValueOnce({ data: require('../fixtures/tpex-stocks-institutional-without-foreign-dealers.json') });

const data = await scraper.fetchStocksInstitutional({ date: '2014-12-01', symbol: '006201' });
expect(mockAxios.get).toHaveBeenCalledWith(
'https://www.tpex.org.tw/web/stock/3insti/daily_trade/3itrade_hedge_result.php?d=103%2F12%2F01&se=EW&t=D&o=json',
);
expect(data).toBeDefined();
expect(data).toEqual({
date: '2014-12-01',
exchange: 'TPEx',
symbol: '006201',
name: '寶富櫃',
institutional: [
{
investor: '外資及陸資',
totalBuy: 0,
totalSell: 0,
difference: 0,
},
{
investor: '投信',
totalBuy: 0,
totalSell: 0,
difference: 0,
},
{
investor: '自營商',
difference: -14000,
},
{
investor: '自營商(自行買賣)',
totalBuy: 0,
totalSell: 0,
difference: 0,
},
{
investor: '自營商(避險)',
totalBuy: 0,
totalSell: 14000,
difference: -14000,
},
{
investor: '三大法人',
difference: -14000,
},
],
});
});

it('should return null when no data is available', async () => {
mockAxios.get.mockResolvedValueOnce({ data: require('../fixtures/tpex-stocks-institutional-no-data.json') });

Expand Down
6 changes: 3 additions & 3 deletions test/scrapers/twse-scraper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('TwseScraper', () => {
difference: 937673,
},
{
investor: '自營商合計',
investor: '自營商',
difference: 880408,
},
{
Expand All @@ -124,7 +124,7 @@ describe('TwseScraper', () => {
difference: 439408,
},
{
investor: '合計',
investor: '三大法人',
difference: 82459130,
},
],
Expand Down Expand Up @@ -962,7 +962,7 @@ describe('TwseScraper', () => {
difference: -36789050,
},
{
investor: '合計',
investor: '三大法人',
totalBuy: 226200542418,
totalSell: 143066882919,
difference: 83133659499,
Expand Down

0 comments on commit d5e1906

Please sign in to comment.