# 下載資料套件
import requests as r
#資料處理套件
import pandas as pd
import numpy as np
import json
from datetime import datetime, date
# 畫圖套件
import plotly.graph_objects as go
from plotly.subplots import make_subplots
def get_tw_stock_data(start_year, start_month, end_year, end_month, stock_code):
start_date = str(date(start_year, start_month, 1))
end_date = str(date(end_year, end_month, 1))
month_list = pd.date_range(start_date, end_date, freq='MS').strftime("%Y%m%d").tolist()
df = pd.DataFrame()
for month in month_list:
url = "https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date="+ month + "&stockNo=" + str(stock_code)
res = r.get(url)
stock_json = res.json()
stock_df = pd.DataFrame.from_dict(stock_json['data'])
df = df.append(stock_df, ignore_index = True)
# 資料轉型
for col in [0, 1, 2, 3, 4, 5, 6, 8]:
for row in range(df.shape[0]):
# 把"日期"從字串(string)換成時間(datetime),並將民國年換成西元年
if col == 0:
day = df.iloc[row,0].split('/')
df.iloc[row, 0] = datetime(int(day[0]) + 1911, int(day[1]), int(day[2]))
# 把"開盤價", "最高價", "最低價", "收盤價"帶有逗號的字串(string)換成浮點數(float)
elif col != 0:
df.iloc[row, col] = float(df.iloc[row,col].replace(',', ''))
df.columns = ['日期', '成交股數', '成交金額', '開盤價', '最高價', '最低價', '收盤價', '漲跌價差', '成交筆數']
return df
#下載台股台積電:2330
df = get_tw_stock_data(start_year = 2021,
start_month = 9,
end_year = 2021,
end_month = 10,
stock_code = 2330)
df
日期 | 成交股數 | 成交金額 | 開盤價 | 最高價 | 最低價 | 收盤價 | 漲跌價差 | 成交筆數 | |
---|---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 31242788.0 | 19126702297.0 | 614.0 | 614.0 | 608.0 | 613.0 | -1.00 | 30125.0 |
1 | 2021-09-02 00:00:00 | 26715492.0 | 16341803689.0 | 613.0 | 615.0 | 607.0 | 607.0 | -6.00 | 26153.0 |
2 | 2021-09-03 00:00:00 | 53915006.0 | 33278648609.0 | 610.0 | 620.0 | 610.0 | 620.0 | +13.00 | 62288.0 |
3 | 2021-09-06 00:00:00 | 59223061.0 | 37250623087.0 | 623.0 | 638.0 | 621.0 | 631.0 | +11.00 | 77024.0 |
4 | 2021-09-07 00:00:00 | 27422775.0 | 17191259166.0 | 634.0 | 634.0 | 623.0 | 623.0 | -8.00 | 29823.0 |
5 | 2021-09-08 00:00:00 | 38713621.0 | 23960375113.0 | 622.0 | 627.0 | 612.0 | 619.0 | -4.00 | 38267.0 |
6 | 2021-09-09 00:00:00 | 19309522.0 | 11886107760.0 | 612.0 | 620.0 | 610.0 | 619.0 | 0.00 | 19569.0 |
7 | 2021-09-10 00:00:00 | 16740439.0 | 10365721937.0 | 615.0 | 623.0 | 614.0 | 622.0 | +3.00 | 17067.0 |
8 | 2021-09-13 00:00:00 | 15809768.0 | 9734528161.0 | 619.0 | 620.0 | 613.0 | 615.0 | -7.00 | 21779.0 |
9 | 2021-09-14 00:00:00 | 16024935.0 | 9849961184.0 | 618.0 | 618.0 | 612.0 | 613.0 | -2.00 | 13569.0 |
10 | 2021-09-15 00:00:00 | 26041452.0 | 15872971715.0 | 610.0 | 613.0 | 607.0 | 607.0 | -6.00 | 24839.0 |
11 | 2021-09-16 00:00:00 | 23315040.0 | 14030697248.0 | 603.0 | 607.0 | 599.0 | 600.0 | X0.00 | 31437.0 |
12 | 2021-09-17 00:00:00 | 42383815.0 | 25510152115.0 | 600.0 | 610.0 | 599.0 | 600.0 | 0.00 | 18681.0 |
13 | 2021-09-22 00:00:00 | 42732169.0 | 25056693727.0 | 586.0 | 589.0 | 583.0 | 586.0 | -14.00 | 70389.0 |
14 | 2021-09-23 00:00:00 | 22736266.0 | 13406035166.0 | 588.0 | 593.0 | 588.0 | 588.0 | +2.00 | 22991.0 |
15 | 2021-09-24 00:00:00 | 17302848.0 | 10289114839.0 | 591.0 | 598.0 | 590.0 | 598.0 | +10.00 | 15796.0 |
16 | 2021-09-27 00:00:00 | 20034221.0 | 11993300562.0 | 600.0 | 602.0 | 593.0 | 602.0 | +4.00 | 16349.0 |
17 | 2021-09-28 00:00:00 | 17325896.0 | 10291543822.0 | 595.0 | 596.0 | 592.0 | 594.0 | -8.00 | 16496.0 |
18 | 2021-09-29 00:00:00 | 51834124.0 | 30040354500.0 | 580.0 | 583.0 | 577.0 | 580.0 | -14.00 | 95024.0 |
19 | 2021-09-30 00:00:00 | 36318623.0 | 21054651328.0 | 580.0 | 585.0 | 575.0 | 580.0 | 0.00 | 32973.0 |
20 | 2021-10-01 00:00:00 | 39006124.0 | 22368447571.0 | 579.0 | 579.0 | 571.0 | 574.0 | -6.00 | 78088.0 |
# 設定子圖
fig = make_subplots(rows = 2,
cols = 1,
shared_xaxes = True,
vertical_spacing = 0.05,
row_width=[0.2, 0.7])
# 畫K線圖
fig.add_trace(go.Candlestick(x = df['日期'],
open = df['開盤價'],
high = df['最高價'],
low = df['最低價'],
close = df['收盤價'],
increasing_line_color = 'red',
decreasing_line_color = 'green',
name = 'K線圖'),
row = 1,
col = 1)
# 畫成交量長條圖
fig.add_trace(go.Bar(x = df['日期'],
y = df['成交股數'],
showlegend = False,
name = '成交股數'),
row = 2,
col = 1)
# 設x軸標題
fig.update_xaxes(rangebreaks = [{ 'pattern': 'day of week', 'bounds': [6, 1]}])
fig.update_xaxes(title_text = "日期", row = 2, col = 1)
# 設y軸標題
fig.update_yaxes(title_text = "股價", row = 1, col = 1)
# 設圖標及圖長寬
fig.update_layout(
title_text = "2021/09 - 2021/10 台積電(2330) K線圖",
width = 800,
height = 400
)
fig.update(layout_xaxis_rangeslider_visible = False)
fig.show()
df["成交量增減"] = df['成交股數'].diff()
df
日期 | 成交股數 | 成交金額 | 開盤價 | 最高價 | 最低價 | 收盤價 | 漲跌價差 | 成交筆數 | 成交量增減 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 31242788.0 | 19126702297.0 | 614.0 | 614.0 | 608.0 | 613.0 | -1.00 | 30125.0 | NaN |
1 | 2021-09-02 00:00:00 | 26715492.0 | 16341803689.0 | 613.0 | 615.0 | 607.0 | 607.0 | -6.00 | 26153.0 | -4527296.0 |
2 | 2021-09-03 00:00:00 | 53915006.0 | 33278648609.0 | 610.0 | 620.0 | 610.0 | 620.0 | +13.00 | 62288.0 | 27199514.0 |
3 | 2021-09-06 00:00:00 | 59223061.0 | 37250623087.0 | 623.0 | 638.0 | 621.0 | 631.0 | +11.00 | 77024.0 | 5308055.0 |
4 | 2021-09-07 00:00:00 | 27422775.0 | 17191259166.0 | 634.0 | 634.0 | 623.0 | 623.0 | -8.00 | 29823.0 | -31800286.0 |
5 | 2021-09-08 00:00:00 | 38713621.0 | 23960375113.0 | 622.0 | 627.0 | 612.0 | 619.0 | -4.00 | 38267.0 | 11290846.0 |
6 | 2021-09-09 00:00:00 | 19309522.0 | 11886107760.0 | 612.0 | 620.0 | 610.0 | 619.0 | 0.00 | 19569.0 | -19404099.0 |
7 | 2021-09-10 00:00:00 | 16740439.0 | 10365721937.0 | 615.0 | 623.0 | 614.0 | 622.0 | +3.00 | 17067.0 | -2569083.0 |
8 | 2021-09-13 00:00:00 | 15809768.0 | 9734528161.0 | 619.0 | 620.0 | 613.0 | 615.0 | -7.00 | 21779.0 | -930671.0 |
9 | 2021-09-14 00:00:00 | 16024935.0 | 9849961184.0 | 618.0 | 618.0 | 612.0 | 613.0 | -2.00 | 13569.0 | 215167.0 |
10 | 2021-09-15 00:00:00 | 26041452.0 | 15872971715.0 | 610.0 | 613.0 | 607.0 | 607.0 | -6.00 | 24839.0 | 10016517.0 |
11 | 2021-09-16 00:00:00 | 23315040.0 | 14030697248.0 | 603.0 | 607.0 | 599.0 | 600.0 | X0.00 | 31437.0 | -2726412.0 |
12 | 2021-09-17 00:00:00 | 42383815.0 | 25510152115.0 | 600.0 | 610.0 | 599.0 | 600.0 | 0.00 | 18681.0 | 19068775.0 |
13 | 2021-09-22 00:00:00 | 42732169.0 | 25056693727.0 | 586.0 | 589.0 | 583.0 | 586.0 | -14.00 | 70389.0 | 348354.0 |
14 | 2021-09-23 00:00:00 | 22736266.0 | 13406035166.0 | 588.0 | 593.0 | 588.0 | 588.0 | +2.00 | 22991.0 | -19995903.0 |
15 | 2021-09-24 00:00:00 | 17302848.0 | 10289114839.0 | 591.0 | 598.0 | 590.0 | 598.0 | +10.00 | 15796.0 | -5433418.0 |
16 | 2021-09-27 00:00:00 | 20034221.0 | 11993300562.0 | 600.0 | 602.0 | 593.0 | 602.0 | +4.00 | 16349.0 | 2731373.0 |
17 | 2021-09-28 00:00:00 | 17325896.0 | 10291543822.0 | 595.0 | 596.0 | 592.0 | 594.0 | -8.00 | 16496.0 | -2708325.0 |
18 | 2021-09-29 00:00:00 | 51834124.0 | 30040354500.0 | 580.0 | 583.0 | 577.0 | 580.0 | -14.00 | 95024.0 | 34508228.0 |
19 | 2021-09-30 00:00:00 | 36318623.0 | 21054651328.0 | 580.0 | 585.0 | 575.0 | 580.0 | 0.00 | 32973.0 | -15515501.0 |
20 | 2021-10-01 00:00:00 | 39006124.0 | 22368447571.0 | 579.0 | 579.0 | 571.0 | 574.0 | -6.00 | 78088.0 | 2687501.0 |
df["成交量顏色"] = np.where(df["成交量增減"] >= 0, 'red', 'green')
df
日期 | 成交股數 | 成交金額 | 開盤價 | 最高價 | 最低價 | 收盤價 | 漲跌價差 | 成交筆數 | 成交量增減 | 成交量顏色 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 31242788.0 | 19126702297.0 | 614.0 | 614.0 | 608.0 | 613.0 | -1.00 | 30125.0 | NaN | green |
1 | 2021-09-02 00:00:00 | 26715492.0 | 16341803689.0 | 613.0 | 615.0 | 607.0 | 607.0 | -6.00 | 26153.0 | -4527296.0 | green |
2 | 2021-09-03 00:00:00 | 53915006.0 | 33278648609.0 | 610.0 | 620.0 | 610.0 | 620.0 | +13.00 | 62288.0 | 27199514.0 | red |
3 | 2021-09-06 00:00:00 | 59223061.0 | 37250623087.0 | 623.0 | 638.0 | 621.0 | 631.0 | +11.00 | 77024.0 | 5308055.0 | red |
4 | 2021-09-07 00:00:00 | 27422775.0 | 17191259166.0 | 634.0 | 634.0 | 623.0 | 623.0 | -8.00 | 29823.0 | -31800286.0 | green |
5 | 2021-09-08 00:00:00 | 38713621.0 | 23960375113.0 | 622.0 | 627.0 | 612.0 | 619.0 | -4.00 | 38267.0 | 11290846.0 | red |
6 | 2021-09-09 00:00:00 | 19309522.0 | 11886107760.0 | 612.0 | 620.0 | 610.0 | 619.0 | 0.00 | 19569.0 | -19404099.0 | green |
7 | 2021-09-10 00:00:00 | 16740439.0 | 10365721937.0 | 615.0 | 623.0 | 614.0 | 622.0 | +3.00 | 17067.0 | -2569083.0 | green |
8 | 2021-09-13 00:00:00 | 15809768.0 | 9734528161.0 | 619.0 | 620.0 | 613.0 | 615.0 | -7.00 | 21779.0 | -930671.0 | green |
9 | 2021-09-14 00:00:00 | 16024935.0 | 9849961184.0 | 618.0 | 618.0 | 612.0 | 613.0 | -2.00 | 13569.0 | 215167.0 | red |
10 | 2021-09-15 00:00:00 | 26041452.0 | 15872971715.0 | 610.0 | 613.0 | 607.0 | 607.0 | -6.00 | 24839.0 | 10016517.0 | red |
11 | 2021-09-16 00:00:00 | 23315040.0 | 14030697248.0 | 603.0 | 607.0 | 599.0 | 600.0 | X0.00 | 31437.0 | -2726412.0 | green |
12 | 2021-09-17 00:00:00 | 42383815.0 | 25510152115.0 | 600.0 | 610.0 | 599.0 | 600.0 | 0.00 | 18681.0 | 19068775.0 | red |
13 | 2021-09-22 00:00:00 | 42732169.0 | 25056693727.0 | 586.0 | 589.0 | 583.0 | 586.0 | -14.00 | 70389.0 | 348354.0 | red |
14 | 2021-09-23 00:00:00 | 22736266.0 | 13406035166.0 | 588.0 | 593.0 | 588.0 | 588.0 | +2.00 | 22991.0 | -19995903.0 | green |
15 | 2021-09-24 00:00:00 | 17302848.0 | 10289114839.0 | 591.0 | 598.0 | 590.0 | 598.0 | +10.00 | 15796.0 | -5433418.0 | green |
16 | 2021-09-27 00:00:00 | 20034221.0 | 11993300562.0 | 600.0 | 602.0 | 593.0 | 602.0 | +4.00 | 16349.0 | 2731373.0 | red |
17 | 2021-09-28 00:00:00 | 17325896.0 | 10291543822.0 | 595.0 | 596.0 | 592.0 | 594.0 | -8.00 | 16496.0 | -2708325.0 | green |
18 | 2021-09-29 00:00:00 | 51834124.0 | 30040354500.0 | 580.0 | 583.0 | 577.0 | 580.0 | -14.00 | 95024.0 | 34508228.0 | red |
19 | 2021-09-30 00:00:00 | 36318623.0 | 21054651328.0 | 580.0 | 585.0 | 575.0 | 580.0 | 0.00 | 32973.0 | -15515501.0 | green |
20 | 2021-10-01 00:00:00 | 39006124.0 | 22368447571.0 | 579.0 | 579.0 | 571.0 | 574.0 | -6.00 | 78088.0 | 2687501.0 | red |
# 第一筆資料沒有成交量增減,所以當收盤價>=開盤價標示紅色,收盤價<開盤價標示綠色
if df.iloc[0, 6] - df.iloc[0, 3] >= 0:
df.iloc[0, 10] = 'red'
else:
df.iloc[0, 10] = 'green'
df
日期 | 成交股數 | 成交金額 | 開盤價 | 最高價 | 最低價 | 收盤價 | 漲跌價差 | 成交筆數 | 成交量增減 | 成交量顏色 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 31242788.0 | 19126702297.0 | 614.0 | 614.0 | 608.0 | 613.0 | -1.00 | 30125.0 | NaN | green |
1 | 2021-09-02 00:00:00 | 26715492.0 | 16341803689.0 | 613.0 | 615.0 | 607.0 | 607.0 | -6.00 | 26153.0 | -4527296.0 | green |
2 | 2021-09-03 00:00:00 | 53915006.0 | 33278648609.0 | 610.0 | 620.0 | 610.0 | 620.0 | +13.00 | 62288.0 | 27199514.0 | red |
3 | 2021-09-06 00:00:00 | 59223061.0 | 37250623087.0 | 623.0 | 638.0 | 621.0 | 631.0 | +11.00 | 77024.0 | 5308055.0 | red |
4 | 2021-09-07 00:00:00 | 27422775.0 | 17191259166.0 | 634.0 | 634.0 | 623.0 | 623.0 | -8.00 | 29823.0 | -31800286.0 | green |
5 | 2021-09-08 00:00:00 | 38713621.0 | 23960375113.0 | 622.0 | 627.0 | 612.0 | 619.0 | -4.00 | 38267.0 | 11290846.0 | red |
6 | 2021-09-09 00:00:00 | 19309522.0 | 11886107760.0 | 612.0 | 620.0 | 610.0 | 619.0 | 0.00 | 19569.0 | -19404099.0 | green |
7 | 2021-09-10 00:00:00 | 16740439.0 | 10365721937.0 | 615.0 | 623.0 | 614.0 | 622.0 | +3.00 | 17067.0 | -2569083.0 | green |
8 | 2021-09-13 00:00:00 | 15809768.0 | 9734528161.0 | 619.0 | 620.0 | 613.0 | 615.0 | -7.00 | 21779.0 | -930671.0 | green |
9 | 2021-09-14 00:00:00 | 16024935.0 | 9849961184.0 | 618.0 | 618.0 | 612.0 | 613.0 | -2.00 | 13569.0 | 215167.0 | red |
10 | 2021-09-15 00:00:00 | 26041452.0 | 15872971715.0 | 610.0 | 613.0 | 607.0 | 607.0 | -6.00 | 24839.0 | 10016517.0 | red |
11 | 2021-09-16 00:00:00 | 23315040.0 | 14030697248.0 | 603.0 | 607.0 | 599.0 | 600.0 | X0.00 | 31437.0 | -2726412.0 | green |
12 | 2021-09-17 00:00:00 | 42383815.0 | 25510152115.0 | 600.0 | 610.0 | 599.0 | 600.0 | 0.00 | 18681.0 | 19068775.0 | red |
13 | 2021-09-22 00:00:00 | 42732169.0 | 25056693727.0 | 586.0 | 589.0 | 583.0 | 586.0 | -14.00 | 70389.0 | 348354.0 | red |
14 | 2021-09-23 00:00:00 | 22736266.0 | 13406035166.0 | 588.0 | 593.0 | 588.0 | 588.0 | +2.00 | 22991.0 | -19995903.0 | green |
15 | 2021-09-24 00:00:00 | 17302848.0 | 10289114839.0 | 591.0 | 598.0 | 590.0 | 598.0 | +10.00 | 15796.0 | -5433418.0 | green |
16 | 2021-09-27 00:00:00 | 20034221.0 | 11993300562.0 | 600.0 | 602.0 | 593.0 | 602.0 | +4.00 | 16349.0 | 2731373.0 | red |
17 | 2021-09-28 00:00:00 | 17325896.0 | 10291543822.0 | 595.0 | 596.0 | 592.0 | 594.0 | -8.00 | 16496.0 | -2708325.0 | green |
18 | 2021-09-29 00:00:00 | 51834124.0 | 30040354500.0 | 580.0 | 583.0 | 577.0 | 580.0 | -14.00 | 95024.0 | 34508228.0 | red |
19 | 2021-09-30 00:00:00 | 36318623.0 | 21054651328.0 | 580.0 | 585.0 | 575.0 | 580.0 | 0.00 | 32973.0 | -15515501.0 | green |
20 | 2021-10-01 00:00:00 | 39006124.0 | 22368447571.0 | 579.0 | 579.0 | 571.0 | 574.0 | -6.00 | 78088.0 | 2687501.0 | red |
# 設定子圖
fig = make_subplots(rows = 2,
cols = 1,
shared_xaxes = True,
vertical_spacing = 0.05,
row_width=[0.2, 0.7])
# 畫K線圖
fig.add_trace(go.Candlestick(x = df['日期'],
open = df['開盤價'],
high = df['最高價'],
low = df['最低價'],
close = df['收盤價'],
increasing_line_color = 'red',
decreasing_line_color = 'green',
name = 'K線圖'),
row = 1,
col = 1)
# 畫成交量長條圖
fig.add_trace(go.Bar(x = df['日期'],
y = df['成交股數'],
showlegend = False,
name = '成交股數',
marker_color = df['成交量顏色']),
row = 2,
col = 1)
# 設x軸標題
fig.update_xaxes(rangebreaks = [{ 'pattern': 'day of week', 'bounds': [6, 1]}])
fig.update_xaxes(title_text = "日期", row = 2, col = 1)
# 設y軸標題
fig.update_yaxes(title_text = "股價", row = 1, col = 1)
# 設圖標及圖長寬
fig.update_layout(
title_text = "2021/09 - 2021/10 台積電(2330) K線圖",
width = 800,
height = 400
)
fig.update(layout_xaxis_rangeslider_visible = False)
fig.show()
#下載大盤指數歷史資料
def get_stock_market_price(start_year, start_month, end_year, end_month):
start_date = str(date(start_year, start_month, 1))
end_date = str(date(end_year, end_month, 1))
month_list = pd.date_range(start_date, end_date, freq='MS').strftime("%Y%m%d").tolist()
# 下載大盤資料
df = pd.DataFrame()
for month in month_list:
url = "https://www.twse.com.tw/indicesReport/MI_5MINS_HIST?response=json&date=" + month
res = r.get(url)
stock_json = res.json()
stock_df = pd.DataFrame.from_dict(stock_json['data'])
df = df.append(stock_df, ignore_index = True)
# 資料轉型
for col in range(0, 5):
for row in range(df.shape[0]):
# 把"日期"從字串(string)換成時間(datetime),並將民國年換成西元年
if col == 0:
day = df.iloc[row,0].split('/')
df.iloc[row, 0] = datetime(int(day[0]) + 1911, int(day[1]), int(day[2]))
# 把"開盤價", "最高價", "最低價", "收盤價"帶有逗號的字串(string)換成浮點數(float)
elif col != 0:
df.iloc[row, col] = float(df.iloc[row,col].replace(',', ''))
# 把日期從字串(string)換成時間(datetime),並將民國年換成西元年
df.columns = ['日期', '開盤價', '最高價', '最低價', '收盤價']
return df
twse = get_stock_market_price(start_year = 2021,
start_month = 9,
end_year = 2021,
end_month = 10)
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | |
---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 |
#下載大盤成交歷史資料
def get_stock_market_volumn(start_year, start_month, end_year, end_month):
start_date = str(date(start_year, start_month, 1))
end_date = str(date(end_year, end_month, 1))
month_list = pd.date_range(start_date, end_date, freq='MS').strftime("%Y%m%d").tolist()
# 下載大盤資料
df = pd.DataFrame()
for month in month_list:
url = "https://www.twse.com.tw/exchangeReport/FMTQIK?response=json&date=" + month
res = r.get(url)
stock_json = res.json()
stock_df = pd.DataFrame.from_dict(stock_json['data'])
df = df.append(stock_df, ignore_index = True)
# 資料轉型
for col in range(0, 5):
for row in range(df.shape[0]):
# 把"日期"從字串(string)換成時間(datetime),並將民國年換成西元年
if col == 0:
day = df.iloc[row,0].split('/')
df.iloc[row, 0] = datetime(int(day[0]) + 1911, int(day[1]), int(day[2]))
# 把"成交股數", "成交金額", "成交筆數", "發行量加權股價指數"帶有逗號的字串(string)換成浮點數(float)
elif col != 0:
df.iloc[row, col] = float(df.iloc[row,col].replace(',', ''))
# 把日期從字串(string)換成時間(datetime),並將民國年換成西元年
df.columns = ['日期', '成交股數', '成交金額', '成交筆數', '發行量加權股價指數', '漲跌點數']
return df
twse_vol = get_stock_market_volumn(start_year = 2021,
start_month = 9,
end_year = 2021,
end_month = 10)
twse_vol
日期 | 成交股數 | 成交金額 | 成交筆數 | 發行量加權股價指數 | 漲跌點數 | |
---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 7417302954.0 | 365348125049.0 | 2348742.0 | 17473.99 | -16.30 |
1 | 2021-09-02 00:00:00 | 7363528319.0 | 354658918739.0 | 2367548.0 | 17319.76 | -154.23 |
2 | 2021-09-03 00:00:00 | 7417361078.0 | 366252454304.0 | 2309735.0 | 17516.92 | 197.16 |
3 | 2021-09-06 00:00:00 | 8125514641.0 | 380807908396.0 | 2551101.0 | 17495.3 | -21.62 |
4 | 2021-09-07 00:00:00 | 6809125095.0 | 337806586675.0 | 2159870.0 | 17428.87 | -66.43 |
5 | 2021-09-08 00:00:00 | 6716726562.0 | 325685541312.0 | 2204512.0 | 17270.49 | -158.38 |
6 | 2021-09-09 00:00:00 | 5177907252.0 | 246914721428.0 | 1644144.0 | 17304.33 | 33.84 |
7 | 2021-09-10 00:00:00 | 5898868907.0 | 268550691237.0 | 1765201.0 | 17474.57 | 170.24 |
8 | 2021-09-13 00:00:00 | 5932740401.0 | 260743193791.0 | 1837328.0 | 17446.31 | -28.26 |
9 | 2021-09-14 00:00:00 | 5669147686.0 | 261864808382.0 | 1863142.0 | 17434.9 | -11.41 |
10 | 2021-09-15 00:00:00 | 5732189159.0 | 269826160399.0 | 1935541.0 | 17354.0 | -80.90 |
11 | 2021-09-16 00:00:00 | 5750136284.0 | 242577208525.0 | 1872875.0 | 17278.7 | -75.30 |
12 | 2021-09-17 00:00:00 | 6567681323.0 | 316458676884.0 | 1948384.0 | 17276.79 | -1.91 |
13 | 2021-09-22 00:00:00 | 7253281012.0 | 298478639255.0 | 2353514.0 | 16925.82 | -350.97 |
14 | 2021-09-23 00:00:00 | 5730646954.0 | 253840587758.0 | 1822103.0 | 17078.22 | 152.40 |
15 | 2021-09-24 00:00:00 | 6101942091.0 | 278917064315.0 | 1952491.0 | 17260.19 | 181.97 |
16 | 2021-09-27 00:00:00 | 6662794535.0 | 300599358406.0 | 2033202.0 | 17313.77 | 53.58 |
17 | 2021-09-28 00:00:00 | 5800849379.0 | 259664385112.0 | 1851311.0 | 17181.44 | -132.33 |
18 | 2021-09-29 00:00:00 | 7361396609.0 | 328878394450.0 | 2606470.0 | 16855.46 | -325.98 |
19 | 2021-09-30 00:00:00 | 6340103760.0 | 278193188293.0 | 1977101.0 | 16934.77 | 79.31 |
20 | 2021-10-01 00:00:00 | 7878762913.0 | 345101126468.0 | 2836222.0 | 16570.89 | -363.88 |
# 設定子圖
fig = make_subplots(rows = 2,
cols = 1,
shared_xaxes = True,
vertical_spacing = 0.05,
row_width=[0.2, 0.7])
# 畫K線圖
fig.add_trace(go.Candlestick(x = twse['日期'],
open = twse['開盤價'],
high = twse['最高價'],
low = twse['最低價'],
close = twse['收盤價'],
increasing_line_color = 'red',
decreasing_line_color = 'green',
name = 'K線圖'),
row = 1,
col = 1)
# 畫成交量長條圖
fig.add_trace(go.Bar(x = twse_vol['日期'],
y = twse_vol['成交金額'],
showlegend = False,
name = '成交金額'),
row = 2,
col = 1)
# 設x軸標題
fig.update_xaxes(rangebreaks = [{ 'pattern': 'day of week', 'bounds': [6, 1]}])
fig.update_xaxes(title_text = "日期", row = 2, col = 1)
# 設y軸標題
fig.update_yaxes(title_text = "指數", row = 1, col = 1)
# 設圖標及圖長寬
fig.update_layout(
title_text = "2021/09 - 2021/10 大盤K線圖",
width = 800,
height = 400
)
fig.update(layout_xaxis_rangeslider_visible = False)
fig.show()
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | |
---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 |
twse['成交金額'] = twse_vol['成交金額']
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | 成交金額 | |
---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 | 365348125049.0 |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 | 354658918739.0 |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 | 366252454304.0 |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 | 380807908396.0 |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 | 337806586675.0 |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 | 325685541312.0 |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 | 246914721428.0 |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 | 268550691237.0 |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 | 260743193791.0 |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 | 261864808382.0 |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 | 269826160399.0 |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 | 242577208525.0 |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 | 316458676884.0 |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 | 298478639255.0 |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 | 253840587758.0 |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 | 278917064315.0 |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 | 300599358406.0 |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 | 259664385112.0 |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 | 328878394450.0 |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 | 278193188293.0 |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 | 345101126468.0 |
twse["成交金額增減"] = twse['成交金額'].diff()
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | 成交金額 | 成交金額增減 | |
---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 | 365348125049.0 | NaN |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 | 354658918739.0 | -10689206310.0 |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 | 366252454304.0 | 11593535565.0 |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 | 380807908396.0 | 14555454092.0 |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 | 337806586675.0 | -43001321721.0 |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 | 325685541312.0 | -12121045363.0 |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 | 246914721428.0 | -78770819884.0 |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 | 268550691237.0 | 21635969809.0 |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 | 260743193791.0 | -7807497446.0 |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 | 261864808382.0 | 1121614591.0 |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 | 269826160399.0 | 7961352017.0 |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 | 242577208525.0 | -27248951874.0 |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 | 316458676884.0 | 73881468359.0 |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 | 298478639255.0 | -17980037629.0 |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 | 253840587758.0 | -44638051497.0 |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 | 278917064315.0 | 25076476557.0 |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 | 300599358406.0 | 21682294091.0 |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 | 259664385112.0 | -40934973294.0 |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 | 328878394450.0 | 69214009338.0 |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 | 278193188293.0 | -50685206157.0 |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 | 345101126468.0 | 66907938175.0 |
twse["成交金額顏色"] = np.where(twse["成交金額增減"] >= 0, 'red', 'green')
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | 成交金額 | 成交金額增減 | 成交金額顏色 | |
---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 | 365348125049.0 | NaN | green |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 | 354658918739.0 | -10689206310.0 | green |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 | 366252454304.0 | 11593535565.0 | red |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 | 380807908396.0 | 14555454092.0 | red |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 | 337806586675.0 | -43001321721.0 | green |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 | 325685541312.0 | -12121045363.0 | green |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 | 246914721428.0 | -78770819884.0 | green |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 | 268550691237.0 | 21635969809.0 | red |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 | 260743193791.0 | -7807497446.0 | green |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 | 261864808382.0 | 1121614591.0 | red |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 | 269826160399.0 | 7961352017.0 | red |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 | 242577208525.0 | -27248951874.0 | green |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 | 316458676884.0 | 73881468359.0 | red |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 | 298478639255.0 | -17980037629.0 | green |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 | 253840587758.0 | -44638051497.0 | green |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 | 278917064315.0 | 25076476557.0 | red |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 | 300599358406.0 | 21682294091.0 | red |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 | 259664385112.0 | -40934973294.0 | green |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 | 328878394450.0 | 69214009338.0 | red |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 | 278193188293.0 | -50685206157.0 | green |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 | 345101126468.0 | 66907938175.0 | red |
# 第一筆資料沒有成交金額增減,所以當收盤價>=開盤價標示紅色,收盤價<開盤價標示綠色
if twse.iloc[0, 4] - twse.iloc[0, 1] >= 0:
twse.iloc[0, 7] = 'red'
else:
twse.iloc[0, 7] = 'green'
twse
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 | 成交金額 | 成交金額增減 | 成交金額顏色 | |
---|---|---|---|---|---|---|---|---|
0 | 2021-09-01 00:00:00 | 17463.8 | 17503.93 | 17415.52 | 17473.99 | 365348125049.0 | NaN | red |
1 | 2021-09-02 00:00:00 | 17455.92 | 17523.16 | 17319.76 | 17319.76 | 354658918739.0 | -10689206310.0 | green |
2 | 2021-09-03 00:00:00 | 17380.52 | 17540.78 | 17380.52 | 17516.92 | 366252454304.0 | 11593535565.0 | red |
3 | 2021-09-06 00:00:00 | 17534.05 | 17633.67 | 17461.07 | 17495.3 | 380807908396.0 | 14555454092.0 | red |
4 | 2021-09-07 00:00:00 | 17534.38 | 17559.21 | 17388.37 | 17428.87 | 337806586675.0 | -43001321721.0 | green |
5 | 2021-09-08 00:00:00 | 17411.53 | 17447.58 | 17167.08 | 17270.49 | 325685541312.0 | -12121045363.0 | green |
6 | 2021-09-09 00:00:00 | 17175.04 | 17319.09 | 17122.95 | 17304.33 | 246914721428.0 | -78770819884.0 | green |
7 | 2021-09-10 00:00:00 | 17270.28 | 17474.57 | 17270.28 | 17474.57 | 268550691237.0 | 21635969809.0 | red |
8 | 2021-09-13 00:00:00 | 17452.16 | 17482.57 | 17387.57 | 17446.31 | 260743193791.0 | -7807497446.0 | green |
9 | 2021-09-14 00:00:00 | 17463.78 | 17529.47 | 17424.54 | 17434.9 | 261864808382.0 | 1121614591.0 | red |
10 | 2021-09-15 00:00:00 | 17434.02 | 17439.75 | 17316.52 | 17354.0 | 269826160399.0 | 7961352017.0 | red |
11 | 2021-09-16 00:00:00 | 17332.34 | 17411.37 | 17254.1 | 17278.7 | 242577208525.0 | -27248951874.0 | green |
12 | 2021-09-17 00:00:00 | 17279.29 | 17408.71 | 17235.45 | 17276.79 | 316458676884.0 | 73881468359.0 | red |
13 | 2021-09-22 00:00:00 | 17196.79 | 17196.79 | 16838.58 | 16925.82 | 298478639255.0 | -17980037629.0 | green |
14 | 2021-09-23 00:00:00 | 16998.07 | 17145.25 | 16998.07 | 17078.22 | 253840587758.0 | -44638051497.0 | green |
15 | 2021-09-24 00:00:00 | 17130.74 | 17273.59 | 17130.74 | 17260.19 | 278917064315.0 | 25076476557.0 | red |
16 | 2021-09-27 00:00:00 | 17278.11 | 17335.99 | 17235.68 | 17313.77 | 300599358406.0 | 21682294091.0 | red |
17 | 2021-09-28 00:00:00 | 17286.89 | 17286.89 | 17113.47 | 17181.44 | 259664385112.0 | -40934973294.0 | green |
18 | 2021-09-29 00:00:00 | 17127.86 | 17127.86 | 16801.78 | 16855.46 | 328878394450.0 | 69214009338.0 | red |
19 | 2021-09-30 00:00:00 | 16886.57 | 16994.21 | 16767.2 | 16934.77 | 278193188293.0 | -50685206157.0 | green |
20 | 2021-10-01 00:00:00 | 16883.0 | 16883.0 | 16503.74 | 16570.89 | 345101126468.0 | 66907938175.0 | red |
# 設定子圖
fig = make_subplots(rows = 2,
cols = 1,
shared_xaxes = True,
vertical_spacing = 0.05,
row_width=[0.2, 0.7])
# 畫K線圖
fig.add_trace(go.Candlestick(x = twse['日期'],
open = twse['開盤價'],
high = twse['最高價'],
low = twse['最低價'],
close = twse['收盤價'],
increasing_line_color = 'red',
decreasing_line_color = 'green',
name = 'K線圖'),
row = 1,
col = 1)
# 畫成交量長條圖
fig.add_trace(go.Bar(x = twse['日期'],
y = twse['成交金額'],
showlegend = False,
marker_color = twse['成交金額顏色'],
name = '成交金額'),
row = 2,
col = 1)
# 設x軸標題
fig.update_xaxes(rangebreaks = [{ 'pattern': 'day of week', 'bounds': [6, 1]}])
fig.update_xaxes(title_text = "日期", row = 2, col = 1)
# 設y軸標題
fig.update_yaxes(title_text = "指數", row = 1, col = 1)
# 設圖標及圖長寬
fig.update_layout(
title_text = "2021/09 - 2021/10 大盤K線圖",
width = 800,
height = 400
)
fig.update(layout_xaxis_rangeslider_visible = False)
fig.show()