twstock

repository·dev·Indexed 23 days ago

https://github.com/mlouielu/twstock

A Python library for scraping and analyzing stock price data from the Taiwan Stock Exchange (TWSE) and the Taipei Exchange (TPEX). It provides tools for retrieving historical and real-time stock data, calculating moving averages and bias ratios, and identifying trading signals via the BestFourPoint analysis class. The library includes a command-line interface for quick data summaries and stock list updates, as well as proxy configuration to manage request limits.

Tokens
7.5K
Snippets
13
Records
58
Agent score
77%

What's inside twstock

  1. Overview of twstock

    dev
    twstock is a concise and useful tool for retrieving Taiwan stock market information and real-time stock status. It is designed to support Python 3 and above. While it maintains compatibility with Python 2.7 performance levels, the use of Python 3 is highly encouraged.
  2. Use the Analytics module for stock analysis

    dev

    The analytics module provides two main analysis capabilities: basic technical analysis via the Analytics class and the "Best Four Point" (四大買賣點) trading signal analysis via the BestFourPoint class.

    Note that Analytics is designed to be inherited by stock.Stock, allowing you to call its methods directly on a stock instance. However, these analyses are only applicable to historical data provided by stock.Stock; they cannot be used for realtime data.

  3. Understand the Fetcher hierarchy

    dev

    The library uses a fetcher pattern to retrieve data from different exchanges. The Stock class manages these fetchers internally.

    BaseFetcher (Base Class): Provides common logic for all fetchers, including:

    • fetch(year, month, sid, retry): Fetches stock data for a specific year and month.
    • _convert_date(date): Converts Republic of China (ROC) era dates to Gregorian years (e.g., '106/05/01' becomes '2017/05/01').
    • _make_datatuple(data): Converts raw daily data into a DATATUPLE object.
    • purify(original_data: list): Converts a list of raw data into a list of DATATUPLE objects.

    Concrete Fetchers:

    • TWSEFetcher: Used for stocks listed on the Taiwan Stock Exchange (上市).
    • TPEXFetcher: Used for stocks listed on the Taipei Exchange (上櫃).
  4. Access parsed Taiwan stock codes via twstock

    dev

    While the codes module provides the underlying logic for querying Taiwan stock codes, end-users typically interact with the pre-parsed data collections provided by the top-level package. You can access stock code information through the following attributes:

    • twstock.twse: Access Taiwan Stock Exchange (上市) stock codes.
    • twstock.tpex: Access Taipei Exchange (上櫃) stock codes.
    • twstock.codes: Access general Taiwan stock codes.

    Each entry in these collections provides information structured as a StockCodeInfo object.

  5. Update TPEX/TWSE Codes

    dev

    If you are using twstock for the first time, you may need to update the TPEX/TWSE stock codes to ensure accuracy.

    You can update them via the CLI or within a Python script.

    # Via CLI
    $ twstock -U
    
    # Via Python
    import twstock
    twstock.__update_codes()
  6. Install twstock from source using flit

    dev

    If you have cloned the source code and wish to install it manually, you must use flit.

    First, install flit via pip, then use it to install twstock from within the project directory:

    $ pip install flit
    $ flit install
  7. Update TWSE and TPEX stock lists

    dev

    When using twstock for the first time, you may need to update the stock lists for the Taiwan Stock Exchange (TWSE) and Taipei Exchange (TPEX). You can do this via the CLI or within a Python script.

    Via CLI:

    twstock -U

    Via Python:

    import twstock
    twstock.__update_codes()
    twstock.__update_codes()
  8. Install twstock

    dev

    You can install twstock via PyPI, or by installing from the source code.

    Via PyPI:

    python -m pip install --user twstock

    Via Source (Editable mode):

    git clone https://github.com/mlouielu/twstock
    cd twstock
    python -m pip install -e .

    Via Source (Standard install using flit):

    git clone https://github.com/mlouielu/twstock
    cd twstock
    python -m pip install --user flit
    flit install
    python -m pip install --user twstock
  9. Install twstock via pip

    dev

    The fastest way to install twstock is using pip. Ensure you are using Python 3 or higher, as Python 2 is not supported.

    To install for the current user:

    $ pip install --user twstock

    To install globally for all users (requires administrative privileges):

    $ sudo pip install twstock
  10. Use the twstock package entrypoint

    dev

    The twstock package provides tools for accessing Taiwan stock market data, including real-time data, stock analytics, and CLI tools. The following modules are available for import:

    • twstock.stock: Core stock data functionality.
    • twstock.analytics: Tools for analyzing stock trends and patterns.
    • twstock.realtime: Real-time data access.
    • twstock.cli: Command-line interface tools.
    • twstock.mock: Mock data for testing.
    • twstock.codes: Stock code management and updates.