|
| 1 | +// eastmoney convertible — on-market convertible bond listing. |
| 2 | +// |
| 3 | +// opencli eastmoney convertible |
| 4 | +// opencli eastmoney convertible --sort premium --limit 30 |
| 5 | + |
| 6 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 7 | +import { CliError } from '@jackwener/opencli/errors'; |
| 8 | + |
| 9 | +const SORTS = { |
| 10 | + change: { fid: 'f3', order: 'desc' }, |
| 11 | + drop: { fid: 'f3', order: 'asc' }, |
| 12 | + turnover: { fid: 'f6', order: 'desc' }, |
| 13 | + price: { fid: 'f2', order: 'desc' }, |
| 14 | + premium: { fid: 'f237', order: 'desc' }, // 转股溢价率 |
| 15 | + value: { fid: 'f236', order: 'desc' }, // 转股价值 |
| 16 | + ytm: { fid: 'f239', order: 'desc' }, // 到期收益率 |
| 17 | +}; |
| 18 | + |
| 19 | +cli({ |
| 20 | + site: 'eastmoney', |
| 21 | + name: 'convertible', |
| 22 | + description: '可转债行情列表(默认按成交额排序)', |
| 23 | + domain: 'push2.eastmoney.com', |
| 24 | + strategy: Strategy.PUBLIC, |
| 25 | + browser: false, |
| 26 | + args: [ |
| 27 | + { name: 'sort', type: 'string', default: 'turnover', help: '排序:turnover / change / drop / price / premium' }, |
| 28 | + { name: 'limit', type: 'int', default: 20, help: '返回数量 (max 100)' }, |
| 29 | + ], |
| 30 | + columns: ['rank', 'bondCode', 'bondName', 'bondPrice', 'bondChangePct', 'stockCode', 'stockName', 'stockPrice', 'stockChangePct', 'convPrice', 'convValue', 'convPremiumPct', 'remainingYears', 'ytm', 'listDate'], |
| 31 | + func: async (_page, args) => { |
| 32 | + const sortKey = String(args.sort ?? 'turnover').toLowerCase(); |
| 33 | + const sort = SORTS[sortKey]; |
| 34 | + if (!sort) throw new CliError('INVALID_ARGUMENT', `Unknown sort "${sortKey}". Valid: ${Object.keys(SORTS).join(', ')}`); |
| 35 | + const limit = Math.max(1, Math.min(Number(args.limit) || 20, 100)); |
| 36 | + |
| 37 | + const url = new URL('https://push2.eastmoney.com/api/qt/clist/get'); |
| 38 | + url.searchParams.set('pn', '1'); |
| 39 | + url.searchParams.set('pz', String(limit)); |
| 40 | + url.searchParams.set('po', sort.order === 'desc' ? '1' : '0'); |
| 41 | + url.searchParams.set('np', '1'); |
| 42 | + url.searchParams.set('fltt', '2'); |
| 43 | + url.searchParams.set('invt', '2'); |
| 44 | + url.searchParams.set('fid', sort.fid); |
| 45 | + url.searchParams.set('fs', 'b:MK0354'); |
| 46 | + url.searchParams.set('fields', 'f12,f14,f2,f3,f6,f229,f230,f232,f234,f235,f236,f237,f238,f239,f243'); |
| 47 | + url.searchParams.set('ut', 'bd1d9ddb04089700cf9c27f6f7426281'); |
| 48 | + |
| 49 | + const resp = await fetch(url, { headers: { 'User-Agent': 'Mozilla/5.0' } }); |
| 50 | + if (!resp.ok) throw new CliError('HTTP_ERROR', `convertible failed: HTTP ${resp.status}`); |
| 51 | + const data = await resp.json(); |
| 52 | + const diff = Array.isArray(data?.data?.diff) ? data.data.diff : []; |
| 53 | + if (diff.length === 0) throw new CliError('NO_DATA', 'eastmoney returned no convertible data'); |
| 54 | + |
| 55 | + return diff.slice(0, limit).map((it, i) => ({ |
| 56 | + rank: i + 1, |
| 57 | + bondCode: it.f12, |
| 58 | + bondName: it.f14, |
| 59 | + bondPrice: it.f2, |
| 60 | + bondChangePct: it.f3, |
| 61 | + stockCode: it.f232, |
| 62 | + stockName: it.f234, |
| 63 | + stockPrice: it.f229, |
| 64 | + stockChangePct: it.f230, |
| 65 | + convPrice: it.f235, |
| 66 | + convValue: it.f236, |
| 67 | + convPremiumPct: it.f237, |
| 68 | + remainingYears: it.f238, |
| 69 | + ytm: it.f239, |
| 70 | + listDate: String(it.f243 ?? ''), |
| 71 | + })); |
| 72 | + }, |
| 73 | +}); |
0 commit comments