twstock
repository·dev·Indexed 23 days ago
https://github.com/mlouielu/twstockA 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.
What's inside twstock
- 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.
Use the Analytics module for stock analysis
devThe
analyticsmodule provides two main analysis capabilities: basic technical analysis via theAnalyticsclass and the "Best Four Point" (四大買賣點) trading signal analysis via theBestFourPointclass.Note that
Analyticsis designed to be inherited bystock.Stock, allowing you to call its methods directly on a stock instance. However, these analyses are only applicable to historical data provided bystock.Stock; they cannot be used forrealtimedata.Understand the Fetcher hierarchy
devThe library uses a fetcher pattern to retrieve data from different exchanges. The
Stockclass 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 aDATATUPLEobject.purify(original_data: list): Converts a list of raw data into a list ofDATATUPLEobjects.
Concrete Fetchers:
TWSEFetcher: Used for stocks listed on the Taiwan Stock Exchange (上市).TPEXFetcher: Used for stocks listed on the Taipei Exchange (上櫃).
Access parsed Taiwan stock codes via twstock
devWhile the
codesmodule 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
StockCodeInfoobject.Update TPEX/TWSE Codes
devIf you are using
twstockfor 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()Install twstock from source using flit
devIf you have cloned the source code and wish to install it manually, you must use
flit.First, install
flitvia pip, then use it to installtwstockfrom within the project directory:$ pip install flit $ flit installUpdate TWSE and TPEX stock lists
devWhen using
twstockfor 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 -UVia Python:
import twstock twstock.__update_codes()twstock.__update_codes()Clone the twstock source code
devYou can obtain the source code by cloning the repository from GitHub:
$ git clone https://github.com/mlouielu/twstock $ cd twstockInstall twstock
devYou can install
twstockvia PyPI, or by installing from the source code.Via PyPI:
python -m pip install --user twstockVia 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 installpython -m pip install --user twstockInstall twstock via pip
devThe fastest way to install
twstockis usingpip. Ensure you are using Python 3 or higher, as Python 2 is not supported.To install for the current user:
$ pip install --user twstockTo install globally for all users (requires administrative privileges):
$ sudo pip install twstockUse the twstock package entrypoint
devThe
twstockpackage 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.
Implement a custom ProxyProvider
devYou can extend theProxyProviderabstract base class to implement custom proxy selection logic (e.g., fetching proxies from an API or a database). Your implementation must override theget_proxy()method.