Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyyyeh committed Dec 4, 2020
2 parents 0b59ab9 + 21e6516 commit 2a42275
Show file tree
Hide file tree
Showing 19 changed files with 611 additions and 75 deletions.
Empty file added .env
Empty file.
90 changes: 89 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
- Getting daily stock margin trading data from official website.
- [上市](https://www.twse.com.tw/zh/page/trading/exchange/MI_MARGN.html)
- [上櫃](https://www.tpex.org.tw/web/stock/margin_trading/margin_balance/margin_bal.php?l=zh-tw)
- Getting p/e ratio, dividend yield and p/b ratio from official website.
- [上市(全)](https://www.twse.com.tw/zh/page/trading/exchange/BWIBBU_d.html)
- [上市(個股)](https://www.twse.com.tw/zh/page/trading/exchange/BWIBBU.html)
- [上櫃(全)](https://www.tpex.org.tw/web/stock/aftertrading/peratio_analysis/pera.php?l=zh-tw)
- [上櫃(個股)](https://www.tpex.org.tw/web/stock/aftertrading/peratio_stk/pera.php?l=zh-tw)
- Getting stock shareholdings data from official website.
- [上市/上櫃](https://www.tdcc.com.tw/portal/zh/smWeb/qryStock)
- Check if the date is open date in stock market.
Expand Down Expand Up @@ -284,6 +289,69 @@ print(margin_trading_3529.margin_purchase)
print(margin_trading_3529.short_covering)
```

### P/E Ratio
```python
"""
You can get all data with specific date or get only single one stock history data.
Attribute:
- P/E Ratio Data
- 上市:
- code: 股票代碼
- name: 股票名稱
- yield_ratio: 殖利率(%)
- dividend_year: 股利年度
- per: 本益比
- pbr: 股價淨值比
- fiscal_year_quarter: 財報年/季
- 上櫃:
- code: 股票代碼
- name: 股票名稱
- yield_ratio: 殖利率(%)
- dividend_year: 股利年度
- per: 本益比
- pbr: 股價淨值比
- dividend_per_share: 每股股利
- Monthly P/E Ratio Data
- 上市:
- yield_ratio: 殖利率(%)
- dividend_year: 股利年度
- per: 本益比
- pbr: 股價淨值比
- fiscal_year_quarter: 財報年/季
- 上櫃:
- yield_ratio: 殖利率(%)
- dividend_year: 股利年度
- per: 本益比
- pbr: 股價淨值比
"""
from datetime import datetime
from tw_stock_plugin.core.stock_peratio import StockPERatio

date_ = datetime(2020, 11, 6).date()

# init stock p/e ratio, dividend yield and p/b ratio object with specific date
stock_p_e_ratio = StockPERatio(date_=date_)
# getting all p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_all = stock_p_e_ratio.get_all()
# getting 2330 p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_2330 = p_e_ratio_all['2330']
# print 2330 pbr
print(p_e_ratio_2330.pbr)
# print 2330 per
print(p_e_ratio_2330.per)
# getting monthly history p/e ratio, dividend yield and p/b ratio data of 1101 in 2020/10
p_e_ratio_history_1101 = stock_p_e_ratio.get_history(code=1101)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data
print(p_e_ratio_history_1101[date_])
# getting monthly history p/e ratio, dividend yield and p/b ratio data data of 9962 in 2020/10
p_e_ratio_history_9962 = stock_p_e_ratio.get_history(code=9962)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data data
print(p_e_ratio_history_9962[date_])
```

### Shareholdings
```python
"""
Expand Down Expand Up @@ -332,7 +400,7 @@ print(shareholdings_0050[15])
God Bless,Never Bug
"""
from datetime import datetime
from tw_stock_plugin import StockInfo, StockTrading, StockInstitutionalInvestors, StockMarginTrading, \
from tw_stock_plugin import StockInfo, StockTrading, StockInstitutionalInvestors, StockMarginTrading, StockPERatio, \
StockShareholdings, StockTools, UpdateStock

if __name__ == '__main__':
Expand Down Expand Up @@ -419,6 +487,26 @@ if __name__ == '__main__':
# print 3529 short covering
print(margin_trading_3529.short_covering)

""" P/E ratio """
# init stock p/e ratio, dividend yield and p/b ratio object with specific date
stock_p_e_ratio = StockPERatio(date_=date_)
# getting all p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_all = stock_p_e_ratio.get_all()
# getting 2330 p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_2330 = p_e_ratio_all['2330']
# print 2330 pbr
print(p_e_ratio_2330.pbr)
# print 2330 per
print(p_e_ratio_2330.per)
# getting monthly history p/e ratio, dividend yield and p/b ratio data of 1101 in 2020/10
p_e_ratio_history_1101 = stock_p_e_ratio.get_history(code=1101)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data
print(p_e_ratio_history_1101[date_])
# getting monthly history p/e ratio, dividend yield and p/b ratio data data of 9962 in 2020/10
p_e_ratio_history_9962 = stock_p_e_ratio.get_history(code=9962)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data data
print(p_e_ratio_history_9962[date_])

""" shareholdings """
# init stock shareholdings object
stock_shareholdings = StockShareholdings()
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.5'
services:
python:
build:
context: .
image: tw_stock_plugin
container_name: tw_stock_plugin
env_file: .env
volumes:
- ./tw_stock_plugin:/app/tw_stock_plugin:rw
- ./test:/app/test:rw
tty: true
16 changes: 16 additions & 0 deletions dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.8-slim-buster

LABEL maintainer="Jimmy [email protected]"

ARG WORKDIR="app"
RUN apt update
RUN apt install -y g++ gcc python3-pandas libxslt-dev screen vim tzdata bash
RUN mkdir -p ${WORKDIR}

ENV TZ=Asia/Taipei

WORKDIR ${WORKDIR}
COPY requirements.txt .

RUN pip3 install --no-cache-dir -r requirements.txt
RUN rm requirements.txt
22 changes: 21 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
God Bless,Never Bug
"""
from datetime import datetime
from tw_stock_plugin import StockInfo, StockTrading, StockInstitutionalInvestors, StockMarginTrading, \
from tw_stock_plugin import StockInfo, StockTrading, StockInstitutionalInvestors, StockMarginTrading, StockPERatio, \
StockShareholdings, StockTools, UpdateStock

if __name__ == '__main__':
Expand Down Expand Up @@ -102,6 +102,26 @@
# print 3529 short covering
print(margin_trading_3529.short_covering)

""" P/E ratio """
# init stock p/e ratio, dividend yield and p/b ratio object with specific date
stock_p_e_ratio = StockPERatio(date_=date_)
# getting all p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_all = stock_p_e_ratio.get_all()
# getting 2330 p/e ratio, dividend yield and p/b ratio data in 2020/10/30
p_e_ratio_2330 = p_e_ratio_all['2330']
# print 2330 pbr
print(p_e_ratio_2330.pbr)
# print 2330 per
print(p_e_ratio_2330.per)
# getting monthly history p/e ratio, dividend yield and p/b ratio data of 1101 in 2020/10
p_e_ratio_history_1101 = stock_p_e_ratio.get_history(code=1101)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data
print(p_e_ratio_history_1101[date_])
# getting monthly history p/e ratio, dividend yield and p/b ratio data data of 9962 in 2020/10
p_e_ratio_history_9962 = stock_p_e_ratio.get_history(code=9962)
# get only 2020/10/30 p/e ratio, dividend yield and p/b ratio data data
print(p_e_ratio_history_9962[date_])

""" shareholdings """
# init stock shareholdings object
stock_shareholdings = StockShareholdings()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

setuptools.setup(
name='tw-stock-plugin',
version='0.0.6',
version='0.1.4',
author='Jimmy Yeh',
author_email='[email protected]',
description='Some util function when doing Taiwan stock web scraping and some common stock data parser',
Expand Down
89 changes: 73 additions & 16 deletions test/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from time import sleep

from tw_stock_plugin import StockInfo, StockTrading, StockInstitutionalInvestors, StockMarginTrading, \
StockShareholdings, StockTools, StockInfoObject
StockPERatio, StockShareholdings, StockTools, StockInfoObject


class TwStockPluginTest(unittest.TestCase):
Expand All @@ -30,6 +30,7 @@ def setUp(self):
self.stock_trading = StockTrading(date_=self.testing_date)
self.stock_institutional_investors = StockInstitutionalInvestors(date_=self.testing_date)
self.stock_margin_trading = StockMarginTrading(date_=self.testing_date)
self.stock_p_e_ratio = StockPERatio(date_=self.testing_date)
self.stock_dict = {
'2330': {'code': '2330',
'name': '台積電',
Expand Down Expand Up @@ -247,7 +248,45 @@ def setUp(self):
'note': '11,C'
}
}

self.p_e_ratio_history_dict = {
'2330': {'yield_ratio': 2.2,
'dividend_year': 108,
'per': 24.63,
'pbr': 6.51,
'fiscal_year_quarter': '109/2'},
'1101': {'yield_ratio': 7.40,
'dividend_year': 108,
'per': 9.56,
'pbr': 1.25,
'fiscal_year_quarter': '109/2'},
'9962': {'per': None,
'yield_ratio': 2.2,
'dividend_year': 108,
'pbr': 0.89}
}
self.p_e_ratio_all_dict = {
'1101': {'code': '1101',
'name': '台泥',
'yield_ratio': 7.4,
'dividend_year': 108,
'per': 9.56,
'pbr': 1.25,
'fiscal_year_quarter': '109/2'},
'2330': {'code': '2330',
'name': '台積電',
'yield_ratio': 2.2,
'dividend_year': 108,
'per': 24.63,
'pbr': 6.51,
'fiscal_year_quarter': '109/2'},
'2221': {'code': '2221',
'name': '大甲',
'per': 13.82,
'dividend_per_share': 1.5,
'dividend_year': 108,
'yield_ratio': 6.38,
'pbr': 1.33}
}
self.shareholdings_dict = {
1: {'code': '0050',
'date': datetime(2020, 11, 6).date(),
Expand Down Expand Up @@ -355,7 +394,6 @@ def setUp(self):
'percentage_over_total_shares': 57.36,
'total_shares': 530958130}
}

self.shareholdings_dict_all = {
1: {'code': '0050',
'date': datetime(2020, 11, 13).date(),
Expand Down Expand Up @@ -462,7 +500,6 @@ def setUp(self):
'number_of_shares': '1,000,001以上',
'percentage_over_total_shares': 59.31,
'total_shares': 552192372}}

self.date_convert_dict = {
'2020/10/10': '109/10/10',
'2020/01/01': '109/01/01',
Expand Down Expand Up @@ -548,6 +585,26 @@ def test_margin_trading_data_all(self):
self.assertEqual(value, value_test)
sleep(3)

def test_p_e_ratio_data_history(self):
stock_code = random.choice(list(self.p_e_ratio_history_dict))
stock_p_e_ratio = self.p_e_ratio_history_dict[stock_code]
stock_p_e_ratio_test = self.stock_p_e_ratio.get_history(code=stock_code)[self.testing_date]
for key in stock_p_e_ratio.keys():
value = stock_p_e_ratio[key]
value_test = getattr(stock_p_e_ratio_test, key)
self.assertEqual(value, value_test)
sleep(3)

def test_p_e_ratio_data_all(self):
stock_code = random.choice(list(self.p_e_ratio_all_dict))
stock_p_e_ratio_all = self.p_e_ratio_all_dict[stock_code]
stock_p_e_ratio_all_test = self.stock_p_e_ratio.get_all()[stock_code]
for key in stock_p_e_ratio_all.keys():
value = stock_p_e_ratio_all[key]
value_test = getattr(stock_p_e_ratio_all_test, key)
self.assertEqual(value, value_test)
sleep(3)

def test_shareholdings_data(self):
stock_code = '0050'
date_ = datetime(2020, 11, 6).date()
Expand All @@ -563,18 +620,18 @@ def test_shareholdings_data(self):
self.assertEqual(value, value_test)
sleep(3)

def test_shareholdings_data_all(self):
stock_code = '0050'
stock_shareholdings = StockShareholdings()
stock_shareholdings_test = stock_shareholdings.get_newest()[stock_code]
for index in self.shareholdings_dict_all.keys():
dict_ = self.shareholdings_dict_all[index]
dict_test = stock_shareholdings_test[index]
for key in dict_.keys():
value = dict_[key]
value_test = getattr(dict_test, key)
self.assertEqual(value, value_test)
sleep(3)
# def test_shareholdings_data_all(self):
# stock_code = '0050'
# stock_shareholdings = StockShareholdings()
# stock_shareholdings_test = stock_shareholdings.get_newest()[stock_code]
# for index in self.shareholdings_dict_all.keys():
# dict_ = self.shareholdings_dict_all[index]
# dict_test = stock_shareholdings_test[index]
# for key in dict_.keys():
# value = dict_[key]
# value_test = getattr(dict_test, key)
# self.assertEqual(value, value_test)
# sleep(3)


if __name__ == '__main__':
Expand Down
3 changes: 2 additions & 1 deletion tw_stock_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@
from tw_stock_plugin.core.stock_trading import *
from tw_stock_plugin.core.stock_institutional_investors import *
from tw_stock_plugin.core.stock_margin_trading import *
from tw_stock_plugin.core.stock_shareholdings import *
from tw_stock_plugin.core.stock_peratio import *
from tw_stock_plugin.core.stock_shareholdings import *
6 changes: 6 additions & 0 deletions tw_stock_plugin/core/stock_institutional_investors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ def _fetch_twse_data_all(self):
removed_indices = {11}
for data in data_list:
data = [value for index, value in enumerate(data) if index not in removed_indices]
if len(columns) != len(data):
print(f'{data[0]} MISSING INSTITUTIONAL INVESTORS MISSING')
continue
stock_institutional_investors = InstitutionalInvestorsObject(**dict(zip(columns, data)))
institutional_investors_dict[stock_institutional_investors.code] = stock_institutional_investors
return institutional_investors_dict
Expand Down Expand Up @@ -99,6 +102,9 @@ def _fetch_tpex_data_all(self):
removed_indices = {8, 9, 10, 20, 21, 22, 24}
for data in data_list:
data = [value for index, value in enumerate(data) if index not in removed_indices]
if len(columns) != len(data):
print(f'{data[0]} MISSING INSTITUTIONAL INVESTORS MISSING')
continue
stock_institutional_investors = InstitutionalInvestorsObject(**dict(zip(columns, data)))
institutional_investors_dict[stock_institutional_investors.code] = stock_institutional_investors
return institutional_investors_dict
Expand Down
Loading

0 comments on commit 2a42275

Please sign in to comment.