Fundamentals¶
Company information, financial statements and announcements from TWSE open data.
Warning
Every call is an HTTP request, the data changes daily, and errors are logged and returned as empty results rather than raised.
cjtrade.pkgs.analytics.fundamental
¶
CJTrade Companies Module
This module provides access to company fundamental data from Taiwan Stock Exchange and other public sources. It includes company basic information, financial ratios, EPS data, and major announcements.
Announcement(symbol, company_name, event_date, announcement_date=None, publish_date=None, title='', content=None, category=None, url=None)
dataclass
¶
A TWSE major announcement (重大訊息).
event_date (事實發生日期) is the main date; announcement_date is when
the company announced it and publish_date when the API published it.
is_recent
property
¶
Whether event_date is within the last 7 days.
Warnings
Depends on the current wall-clock time. As a property it cannot take
days, so the default of 7 is always used.
days_since_event
property
¶
Whole days from event_date to now.
Warnings
Depends on the current wall-clock time.
announcement_delay
property
¶
Days from event_date to announcement_date, or None if not announced.
CompanyBasicInfo(symbol, name, industry, market, listing_date=None, capital=None, chairman=None, ceo=None, address=None, phone=None, website=None, business_scope=None)
dataclass
¶
公司基本資料
BalanceSheetInfo(symbol, company_name, year, quarter, total_assets=None, total_liabilities=None, shareholders_equity=None, current_assets=None, current_liabilities=None, long_term_debt=None, cash_and_equivalents=None, updated_at=None)
dataclass
¶
資產負債表資訊
EPSInfo(symbol, company_name, year, quarter=None, eps=None, revenue=None, net_income=None, updated_at=None)
dataclass
¶
每股盈餘資訊
FinancialRatios(symbol, company_name, pe_ratio=None, pb_ratio=None, dividend_yield=None, roe=None, roa=None, current_ratio=None, debt_ratio=None, updated_at=None)
dataclass
¶
財務比率資訊 (PE/PB Ratio)
IncomeStatementInfo(symbol, company_name, year, quarter, industry_type, revenue=None, operating_income=None, non_operating_income=None, pre_tax_income=None, net_income=None, comprehensive_income=None, eps=None, interest_income_net=None, non_interest_income=None, provision_expense=None, operating_expense=None, insurance_revenue=None, insurance_cost=None, updated_at=None)
dataclass
¶
綜合損益表資訊
TWSEProvider(timeout=30)
¶
台灣證券交易所資料。
Async client for the TWSE open data API (BASE_URL). Use as an async
context manager or call close.
Warnings
Makes HTTP requests to TWSE; data changes daily and endpoints may change. The HTTP session skips TLS certificate verification.
初始化TWSE提供者。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
HTTP請求超時時間(秒) |
30
|
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
61 62 63 64 65 66 67 68 69 70 | |
close()
async
¶
關閉HTTP session。
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
85 86 87 88 | |
get_daily_announcements()
async
¶
獲取每日重大訊息。
Returns:
| Type | Description |
|---|---|
List[Announcement]
|
重大訊息列表; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
get_company_basic_info(symbol=None)
async
¶
獲取上市公司基本資料。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號,若為None則返回所有公司資料 |
None
|
Returns:
| Type | Description |
|---|---|
List[CompanyBasicInfo]
|
公司基本資料列表; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | |
get_eps_info(symbol=None)
async
¶
獲取上市公司EPS資訊。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號,若為None則返回所有公司EPS資料 |
None
|
Returns:
| Type | Description |
|---|---|
List[EPSInfo]
|
EPS資訊列表; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | |
get_financial_ratios(symbol=None)
async
¶
獲取上市公司PE/PB Ratio等財務比率。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號,若為None則返回所有公司財務比率 |
None
|
Returns:
| Type | Description |
|---|---|
List[FinancialRatios]
|
財務比率列表; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | |
get_balance_sheet_info(symbol=None)
async
¶
獲取上市公司資產負債表資訊。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號,若為None則返回所有公司資產負債表資料 |
None
|
Returns:
| Type | Description |
|---|---|
List[BalanceSheetInfo]
|
資產負債表資訊列表; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
get_income_statements(symbol=None)
async
¶
獲取綜合損益表資訊 (自動選擇適當的行業API)。
Queries the general, banking, securities, insurance and financial holding endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號,若為None則返回所有公司綜合損益表資料 |
None
|
Returns:
| Type | Description |
|---|---|
List[IncomeStatementInfo]
|
綜合損益表資訊列表. An industry endpoint that fails is skipped. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
get_company_all_info(symbol)
async
¶
獲取特定公司的所有資訊。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號 |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dict包含所有公司資訊: |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/providers/twse.py
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | |
TWSEDataParser
¶
台灣證券交易所資料解析器
parse_date(date_str, formats=None)
staticmethod
¶
解析日期字串。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_str
|
str
|
日期字串 |
required |
formats
|
List[str]
|
日期格式列表,預設為常見的台灣日期格式 |
None
|
Returns:
| Type | Description |
|---|---|
datetime or None
|
datetime物件,解析失敗返回None |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
parse_float(value, default=None)
staticmethod
¶
安全解析數值。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
(str, int or float)
|
要解析的值 |
required |
default
|
float
|
解析失敗時的預設值 |
None
|
Returns:
| Type | Description |
|---|---|
float or None
|
float值,解析失敗返回default |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
parse_int(value, default=None)
staticmethod
¶
安全解析整數。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str or int
|
要解析的值 |
required |
default
|
int
|
解析失敗時的預設值 |
None
|
Returns:
| Type | Description |
|---|---|
int or None
|
int值,解析失敗返回default |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
safe_get(data, key, default=None)
staticmethod
¶
安全獲取字典值,支援多種key格式。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Dict[str, Any]
|
資料字典 |
required |
key
|
str
|
要獲取的key |
required |
default
|
Any
|
預設值 |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
字典中的值或預設值 |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
normalize_symbol(symbol)
staticmethod
¶
正規化股票代號。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
原始股票代號 |
required |
Returns:
| Type | Description |
|---|---|
str
|
正規化後的股票代號 |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
validate_symbol(symbol)
staticmethod
¶
驗證股票代號格式。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
股票代號 |
required |
Returns:
| Type | Description |
|---|---|
bool
|
是否為有效的股票代號 |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
clean_company_name(name)
staticmethod
¶
清理公司名稱。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
原始公司名稱 |
required |
Returns:
| Type | Description |
|---|---|
str
|
清理後的公司名稱 |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
categorize_announcement(title, content='')
staticmethod
¶
根據公告標題和內容分類。
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
公告標題 |
required |
content
|
str
|
公告內容 |
''
|
Returns:
| Type | Description |
|---|---|
str
|
公告分類 |
Source code in src/cjtrade/pkgs/analytics/fundamental/utils/parser.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
CompanyInfoProvider(default_provider='twse', timeout=30)
¶
Unified company information provider interface.
Integrates multiple data sources and provides a simple API to retrieve company fundamental information
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_provider
|
str
|
Provider used when a method gets no |
'twse'
|
timeout
|
int
|
HTTP timeout in seconds passed to the provider. |
30
|
Warnings
All get_* methods make network calls to TWSE. They log errors and
return None, an empty list or an error dict instead of raising.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
58 59 60 61 | |
close()
async
¶
Close all created providers and forget them.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
76 77 78 79 80 81 | |
get_company_basic_info(symbol, provider=None)
async
¶
Get company basic information.
Returns:
| Type | Description |
|---|---|
CompanyBasicInfo or None
|
None if not found or on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
get_financial_ratios(symbol, provider=None)
async
¶
Get company financial ratios (PE/PB, etc.).
Returns:
| Type | Description |
|---|---|
FinancialRatios or None
|
None if not found or on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
get_eps_info(symbol, provider=None)
async
¶
Get company EPS information.
Returns:
| Type | Description |
|---|---|
list of EPSInfo
|
Empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
get_income_statements(symbol, provider=None)
async
¶
Get company consolidated statements of comprehensive income.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Stock symbol |
required |
provider
|
str
|
Specify the data provider |
None
|
Returns:
| Type | Description |
|---|---|
List[IncomeStatementInfo]
|
List of comprehensive income statement information; empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | |
get_balance_sheet(symbol, provider=None)
async
¶
Get company balance sheet.
Returns:
| Type | Description |
|---|---|
list of BalanceSheetInfo
|
Empty on error. |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
get_recent_announcements(days=7, provider=None)
async
¶
Get recent major announcements.
Keeps announcements whose event_date is within the last days days,
newest first.
Returns:
| Type | Description |
|---|---|
list of Announcement
|
Empty on error. |
Warnings
Makes HTTP requests to TWSE. The cutoff is computed from the current wall-clock time.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_company_summary(symbol, provider=None)
async
¶
Get complete company summary information.
Fetches basic info, ratios, EPS, balance sheet and income statements concurrently.
Returns:
| Type | Description |
|---|---|
dict
|
Keys |
Warnings
Makes HTTP requests to the TWSE open data API; data changes daily and the endpoints or field names may change.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
example_usage()
async
¶
Print a TSMC (2330) summary and the past 7 days of announcements.
Warnings
Demo only; makes network calls to TWSE.
Source code in src/cjtrade/pkgs/analytics/fundamental/__init__.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | |