到目前已知如何取得盤後資訊(日 K)、Tick 資料、分 K 與分 MA,但還缺乏時 K、周 K 這二個指標,因此需要透過歷史資料快速計算取得。
import pandas
import plotly.graph_objects
TXF_TICKS = pandas.read_csv('TXFJ9-20191001.csv')
TXF_TICKS = TXF_TICKS[1:]
TXF_TICKS = TXF_TICKS.rename(
columns={
'成交時間': 'time',
'成交價位': 'price'
}
)
TXF_TICKS = TXF_TICKS.filter([
'time',
'price'
], axis=1)
TXF_TICKS['time'] = pandas.to_datetime(TXF_TICKS['time'], format='%Y%m%d%H%M%S%f')
TXF_TICKS = TXF_TICKS.set_index('time')
TXF_1HOURK = TXF_TICKS['price'].resample('1H').ohlc()
print('------------------- 1HOUR K -------------------')
print(TXF_1HOURK)
figure = plotly.graph_objects.Figure(
data=[
# 1HOUR K
plotly.graph_objects.Candlestick(
x=TXF_1HOURK.index,
open=TXF_1HOURK['open'],
high=TXF_1HOURK['high'],
low=TXF_1HOURK['low'],
close=TXF_1HOURK['close'],
name='1HOUR K',
)
],
# 設定 XY 顯示格式
layout=plotly.graph_objects.Layout(
xaxis=plotly.graph_objects.layout.XAxis(
tickformat='%Y-%m-%d %H:%M'
),
yaxis=plotly.graph_objects.layout.YAxis(
tickformat='.2f'
)
)
)
figure.show()
從個股每月各交易日盤後資訊獲得台泥(1101)2019-09 的各交易日盤後資訊。
日期 | 開盤價 | 最高價 | 最低價 | 收盤價 |
---|---|---|---|---|
2019-09-02 | 38.45 | 38.45 | 38.05 | 38.25 |
2019-09-03 | 38.25 | 38.45 | 38.25 | 38.35 |
2019-09-04 | 38.25 | 38.50 | 38.25 | 38.45 |
2019-09-05 | 38.50 | 39.00 | 38.50 | 39.00 |
2019-09-06 | 39.05 | 40.10 | 39.05 | 39.85 |
2019-09-09 | 40.00 | 40.20 | 39.80 | 40.00 |
2019-09-10 | 40.15 | 40.25 | 39.85 | 39.95 |
2019-09-11 | 40.15 | 40.15 | 39.75 | 40.00 |
2019-09-12 | 40.15 | 40.20 | 40.00 | 40.05 |
2019-09-16 | 40.05 | 40.25 | 39.90 | 40.10 |
2019-09-17 | 40.10 | 40.10 | 39.35 | 39.50 |
2019-09-18 | 39.60 | 40.10 | 39.60 | 39.80 |
2019-09-19 | 40.00 | 40.00 | 39.70 | 39.80 |
2019-09-20 | 39.80 | 40.20 | 39.65 | 40.20 |
2019-09-23 | 40.20 | 40.20 | 39.75 | 39.75 |
2019-09-24 | 39.90 | 39.95 | 39.60 | 39.80 |
2019-09-25 | 39.85 | 39.90 | 39.60 | 39.60 |
2019-09-26 | 39.85 | 40.00 | 39.65 | 39.90 |
2019-09-27 | 40.00 | 40.00 | 39.60 | 39.65 |
import pandas
import plotly.graph_objects
STOCK_1101_1DAYK = pandas.read_csv('1101-201909.csv')
STOCK_1101_1DAYK = STOCK_1101_1DAYK.rename(
columns={
'日期': 'date',
'開盤價': 'open',
'最高價': 'high',
'最低價': 'low',
'收盤價': 'close'
}
)
STOCK_1101_1DAYK['date'] = pandas.to_datetime(STOCK_1101_1DAYK['date'], format='%Y-%m-%d')
figure = plotly.graph_objects.Figure(
data=[
# 1DAY K
plotly.graph_objects.Candlestick(
x=STOCK_1101_1DAYK.index,
open=STOCK_1101_1DAYK['open'],
high=STOCK_1101_1DAYK['high'],
low=STOCK_1101_1DAYK['low'],
close=STOCK_1101_1DAYK['close'],
name='1DAY K',
)
],
# 設定 XY 顯示格式
layout=plotly.graph_objects.Layout(
xaxis=plotly.graph_objects.layout.XAxis(
tickformat='%Y-%m-%d %H:%M'
),
yaxis=plotly.graph_objects.layout.YAxis(
tickformat='.2f'
)
)
)
figure.show()
import pandas
import plotly.graph_objects
STOCK_1101_1DAYK = pandas.read_csv('1101-201909.csv')
STOCK_1101_1DAYK = STOCK_1101_1DAYK.rename(
columns={
'日期': 'date',
'開盤價': 'open',
'最高價': 'high',
'最低價': 'low',
'收盤價': 'close'
}
)
STOCK_1101_1DAYK['date'] = pandas.to_datetime(STOCK_1101_1DAYK['date'], format='%Y-%m-%d')
STOCK_1101_1DAYK = STOCK_1101_1DAYK.set_index('date')
STOCK_1101_1WEEKK = STOCK_1101_1DAYK['close'].resample('1W').ohlc()
print('------------------- 1WEEK K -------------------')
print(STOCK_1101_1WEEKK)
figure = plotly.graph_objects.Figure(
data=[
# 1WEEK K
plotly.graph_objects.Candlestick(
x=STOCK_1101_1WEEKK.index,
open=STOCK_1101_1WEEKK['open'],
high=STOCK_1101_1WEEKK['high'],
low=STOCK_1101_1WEEKK['low'],
close=STOCK_1101_1WEEKK['close'],
name='1WEEK K',
)
],
# 設定 XY 顯示格式
layout=plotly.graph_objects.Layout(
xaxis=plotly.graph_objects.layout.XAxis(
tickformat='%Y-%m-%d %H:%M'
),
yaxis=plotly.graph_objects.layout.YAxis(
tickformat='.2f'
)
)
)
figure.show()
團隊系列文:
CSScoke - 金魚都能懂的這個網頁畫面怎麼切 - 金魚都能懂了你還怕學不會嗎
Clarence - LINE bot 好好玩 30 天玩轉 LINE API
Hina Hina - 陣列大亂鬥
King Tzeng - IoT沒那麼難!新手用JavaScript入門做自己的玩具
Vita Ora - 好 Js 不學嗎 !? JavaScript 入門中的入門。
TaTaMo - 用Python開發的網頁不能放到Github上?Lektor說可以!!