#下載資料套件
import urllib3
from bs4 import BeautifulSoup
#資料處理套件
import pandas as pd
from datetime import datetime, date, timedelta
#畫圖套件
import matplotlib.pyplot as plt
%matplotlib inline
台灣期貨交易所: 臺指選擇權 Put/Call Ratios 統計表 https://www.taifex.com.tw/cht/3/pcRatio
注意:查詢區間不可超過30日
from IPython.display import Image
Image("img/pc_ratio_error.png")
from IPython.display import Image
Image("img/pc_ratio_1.png")
from IPython.display import Image
Image("img/pc_ratio_2.png")
def date_range(start_year, start_month, start_day, end_year, end_month, end_day):
# 將開始日跟結束日轉為時間型態
start_date = date(start_year, start_month, start_day)
end_date = date(end_year, end_month, end_day)
# 在開始日與結束日區間內 以每30天為單位切分 不含結束日
date_list = pd.date_range(start_date, end_date, freq='30D').tolist()
# 加上結束日
if date_list[-1] != end_date:
date_list.append(end_date)
return date_list
date_list_sample = date_range(start_year = 2021,
start_month = 1,
start_day = 1,
end_year = 2021,
end_month = 3,
end_day = 31)
for day in date_list_sample:
print(day)
2021-01-01 00:00:00 2021-01-31 00:00:00 2021-03-02 00:00:00 2021-03-31
from IPython.display import Image
Image("img/pc_ratio_2.png")
def pc_ratio(date_list):
# 設定urllib物件,下載網址,儲存資料的df
http = urllib3.PoolManager()
url = "https://www.taifex.com.tw/cht/3/pcRatio"
df = pd.DataFrame()
# 下載期交所P/C比的迴圈
for idx in range(len(date_list) - 1):
# 設定下載資料的起始日 queryStartDate & queryEndDate
if idx == 0:
start_date = date_list[idx].strftime("%Y/%m/%d")
else:
start_date = (date_list[idx] + timedelta(days = 1)).strftime("%Y/%m/%d")
end_date = date_list[idx + 1].strftime("%Y/%m/%d")
print("迴圈索引值: ", idx, "; 下載時間區間: ", start_date, "-",end_date)
# 從期交所下載資料
res = http.request(
'POST',
url,
fields={
'queryStartDate': start_date,
'queryEndDate': end_date
}
)
html_doc = res.data
table = BeautifulSoup(html_doc, 'html.parser').table
pc_ratio_df = pd.read_html(str(table))[3]
df = df.append(pc_ratio_df, ignore_index = True)
# 日期欄位資料型態從文字轉為時間
for row in range(df.shape[0]):
date2 = df.iloc[row,0].split('/')
df.iloc[row, 0] = datetime(int(date2[0]), int(date2[1]), int(date2[2]))
# dataframe 按照日期排序
df.sort_values("日期", inplace=True, ignore_index = True)
# 設定 dataframe 的欄位名稱
df.columns = ['日期', '賣權成交量', '買權成交量', '買賣權成交量比率%', '賣權未平倉量', '買權未平倉量', '買賣權未平倉量比率%']
return df
date_list = date_range(start_year = 2021,
start_month = 1,
start_day = 1,
end_year = 2021,
end_month = 3,
end_day = 31)
for day in date_list:
print(day)
2021-01-01 00:00:00 2021-01-31 00:00:00 2021-03-02 00:00:00 2021-03-31
df = pc_ratio(date_list)
df
迴圈索引值: 0 ; 下載時間區間: 2021/01/01 - 2021/01/31
/Users/hou/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)
迴圈索引值: 1 ; 下載時間區間: 2021/02/01 - 2021/03/02
/Users/hou/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)
迴圈索引值: 2 ; 下載時間區間: 2021/03/03 - 2021/03/31
/Users/hou/anaconda3/lib/python3.6/site-packages/urllib3/connectionpool.py:858: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings InsecureRequestWarning)
日期 | 賣權成交量 | 買權成交量 | 買賣權成交量比率% | 賣權未平倉量 | 買權未平倉量 | 買賣權未平倉量比率% | |
---|---|---|---|---|---|---|---|
0 | 2021-01-04 00:00:00 | 409683 | 362995 | 112.86 | 291020 | 209950 | 138.61 |
1 | 2021-01-05 00:00:00 | 521319 | 434658 | 119.94 | 340035 | 231535 | 146.86 |
2 | 2021-01-06 00:00:00 | 1150550 | 943431 | 121.95 | 215027 | 168662 | 127.49 |
3 | 2021-01-07 00:00:00 | 433433 | 379735 | 114.14 | 317416 | 206419 | 153.77 |
4 | 2021-01-08 00:00:00 | 433851 | 467386 | 92.82 | 350448 | 236988 | 147.88 |
5 | 2021-01-11 00:00:00 | 414587 | 398297 | 104.09 | 383837 | 265502 | 144.57 |
6 | 2021-01-12 00:00:00 | 522605 | 487200 | 107.27 | 402205 | 296298 | 135.74 |
7 | 2021-01-13 00:00:00 | 793967 | 679732 | 116.81 | 256637 | 177664 | 144.45 |
8 | 2021-01-14 00:00:00 | 283951 | 252432 | 112.49 | 319138 | 234448 | 136.12 |
9 | 2021-01-15 00:00:00 | 617126 | 616305 | 100.13 | 385256 | 283160 | 136.06 |
10 | 2021-01-18 00:00:00 | 553805 | 562515 | 98.45 | 407732 | 305657 | 133.40 |
11 | 2021-01-19 00:00:00 | 560395 | 515819 | 108.64 | 444489 | 294527 | 150.92 |
12 | 2021-01-20 00:00:00 | 860030 | 834177 | 103.10 | 128261 | 93177 | 137.65 |
13 | 2021-01-21 00:00:00 | 494863 | 454141 | 108.97 | 246034 | 161893 | 151.97 |
14 | 2021-01-22 00:00:00 | 301032 | 307720 | 97.83 | 281333 | 212212 | 132.57 |
15 | 2021-01-25 00:00:00 | 472911 | 476655 | 99.21 | 309024 | 253842 | 121.74 |
16 | 2021-01-26 00:00:00 | 647840 | 688223 | 94.13 | 328158 | 305439 | 107.44 |
17 | 2021-01-27 00:00:00 | 860451 | 779037 | 110.45 | 201712 | 150560 | 133.97 |
18 | 2021-01-28 00:00:00 | 393291 | 401653 | 97.92 | 270372 | 238170 | 113.52 |
19 | 2021-01-29 00:00:00 | 516424 | 588018 | 87.82 | 297698 | 277392 | 107.32 |
20 | 2021-02-01 00:00:00 | 566623 | 488366 | 116.02 | 356384 | 293264 | 121.52 |
21 | 2021-02-02 00:00:00 | 652658 | 567952 | 114.91 | 414301 | 266005 | 155.75 |
22 | 2021-02-03 00:00:00 | 688831 | 730135 | 94.34 | 217424 | 153493 | 141.65 |
23 | 2021-02-04 00:00:00 | 256883 | 245288 | 104.73 | 250485 | 184353 | 135.87 |
24 | 2021-02-05 00:00:00 | 304360 | 296813 | 102.54 | 289803 | 199044 | 145.60 |
25 | 2021-02-17 00:00:00 | 583449 | 517751 | 112.69 | 121320 | 76249 | 159.11 |
26 | 2021-02-18 00:00:00 | 323889 | 275626 | 117.51 | 181074 | 135380 | 133.75 |
27 | 2021-02-19 00:00:00 | 313731 | 313992 | 99.92 | 217481 | 176557 | 123.18 |
28 | 2021-02-22 00:00:00 | 440692 | 392588 | 112.25 | 277179 | 199576 | 138.88 |
29 | 2021-02-23 00:00:00 | 498553 | 420883 | 118.45 | 310441 | 221148 | 140.38 |
30 | 2021-02-24 00:00:00 | 795069 | 804298 | 98.85 | 176362 | 134606 | 131.02 |
31 | 2021-02-25 00:00:00 | 316618 | 324550 | 97.56 | 239761 | 177368 | 135.18 |
32 | 2021-02-26 00:00:00 | 421570 | 458830 | 91.88 | 265144 | 240473 | 110.26 |
33 | 2021-03-02 00:00:00 | 537507 | 619839 | 86.72 | 290812 | 271561 | 107.09 |
34 | 2021-03-03 00:00:00 | 777310 | 701802 | 110.76 | 199865 | 148560 | 134.53 |
35 | 2021-03-04 00:00:00 | 416714 | 434897 | 95.82 | 274281 | 221488 | 123.84 |
36 | 2021-03-05 00:00:00 | 453716 | 447765 | 101.33 | 314330 | 252434 | 124.52 |
37 | 2021-03-08 00:00:00 | 560989 | 537009 | 104.47 | 341855 | 291385 | 117.32 |
38 | 2021-03-09 00:00:00 | 571628 | 563675 | 101.41 | 371244 | 318167 | 116.68 |
39 | 2021-03-10 00:00:00 | 643243 | 693545 | 92.75 | 216534 | 184970 | 117.06 |
40 | 2021-03-11 00:00:00 | 356305 | 305007 | 116.82 | 272208 | 215665 | 126.22 |
41 | 2021-03-12 00:00:00 | 271907 | 267708 | 101.57 | 297644 | 251097 | 118.54 |
42 | 2021-03-15 00:00:00 | 334905 | 308926 | 108.41 | 326384 | 277300 | 117.70 |
43 | 2021-03-16 00:00:00 | 410092 | 397092 | 103.27 | 348940 | 280911 | 124.22 |
44 | 2021-03-17 00:00:00 | 643634 | 584907 | 110.04 | 105918 | 89180 | 118.77 |
45 | 2021-03-18 00:00:00 | 318678 | 328274 | 97.08 | 174029 | 146853 | 118.51 |
46 | 2021-03-19 00:00:00 | 369551 | 399828 | 92.43 | 204342 | 201660 | 101.33 |
47 | 2021-03-22 00:00:00 | 378409 | 352204 | 107.44 | 246796 | 218669 | 112.86 |
48 | 2021-03-23 00:00:00 | 384437 | 397338 | 96.75 | 271371 | 249400 | 108.81 |
49 | 2021-03-24 00:00:00 | 572422 | 538172 | 106.36 | 149872 | 125593 | 119.33 |
50 | 2021-03-25 00:00:00 | 297353 | 295719 | 100.55 | 210868 | 179805 | 117.28 |
51 | 2021-03-26 00:00:00 | 383597 | 344962 | 111.20 | 263742 | 205837 | 128.13 |
52 | 2021-03-29 00:00:00 | 389760 | 396654 | 98.26 | 318945 | 225292 | 141.57 |
53 | 2021-03-30 00:00:00 | 422515 | 352273 | 119.94 | 341427 | 239788 | 142.39 |
54 | 2021-03-31 00:00:00 | 495630 | 533282 | 92.94 | 168107 | 139503 | 120.50 |
fig = plt.figure(figsize = (12, 5))
plt.title('Put/Call Ratio')
plt.plot(df['日期'], df['買賣權成交量比率%'])
plt.plot(df['日期'], df['買賣權未平倉量比率%'])
plt.legend(['Volume%', 'Open Interest Volume%'])
/Users/hou/anaconda3/lib/python3.6/site-packages/matplotlib/cbook/__init__.py:2062: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead. x[:, None] /Users/hou/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py:248: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead. x = x[:, np.newaxis] /Users/hou/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py:250: FutureWarning: Support for multi-dimensional indexing (e.g. `obj[:, None]`) is deprecated and will be removed in a future version. Convert to a numpy array before indexing instead. y = y[:, np.newaxis]
<matplotlib.legend.Legend at 0x7fa1bb934748>