到目前已知如何取得盤後資訊(日 K)及取得 Tick 資料,但卻缺乏常用的分 K 與分 MA 指標,而相關歷史資料卻又難以取得,因此如何從 Tick 換算分 K 與分 MA 就是必須掌握的技能。
import pandas
# 讀取 2019-10-01 大型台指期日盤 Tick 檔
TXF_TICKS = pandas.read_csv('TXFJ9-20191001.csv')
# 去掉第一行 08:45 開盤前訊號
TXF_TICKS = TXF_TICKS[1:]
# 重新命名欄位
TXF_TICKS = TXF_TICKS.rename(
columns={
'成交時間': 'time',
'成交價位': 'price'
}
)
# 只保留成交時間與成交價位二欄資料
TXF_TICKS = TXF_TICKS.filter([
'time',
'price'
], axis=1)
# 將成交時間欄位資料按格式轉換為 datetime 資料
TXF_TICKS['time'] = pandas.to_datetime(TXF_TICKS['time'], format='%Y%m%d%H%M%S%f')
# 設定資料以成交時間欄位為序列索引
TXF_TICKS = TXF_TICKS.set_index('time')
# 以 1 分鐘為單位,進行開高低收重新取樣
TXF_1MINK = TXF_TICKS['price'].resample('1MIN').ohlc()
print('------------------- 1MIN K --------------------')
print(TXF_1MINK)
# 以 5 分鐘為單位,進行開高低收重新取樣
TXF_5MINK = TXF_TICKS['price'].resample('5MIN').ohlc()
print('------------------- 5MIN K --------------------')
print(TXF_5MINK)
# 以 1 分鐘為單位,進行區間均值計算
TXF_1MINMA = TXF_TICKS['price'].rolling('1MIN').mean()
print('------------------- 1MIN MA --------------------')
print(TXF_1MINMA)
# 以 5 分鐘為單位,進行區間均值計算
TXF_5MINMA = TXF_TICKS['price'].rolling('5MIN').mean()
print('------------------- 5MIN MA --------------------')
print(TXF_5MINMA)
------------------- 1MIN K --------------------
open high low close
time
2019-10-01 08:45:00 10856 10887 10856 10881
2019-10-01 08:46:00 10882 10912 10881 10891
2019-10-01 08:47:00 10890 10894 10888 10889
2019-10-01 08:48:00 10890 10893 10888 10889
2019-10-01 08:49:00 10890 10892 10888 10888
... ... ... ... ...
2019-10-01 13:40:00 10955 10956 10953 10955
2019-10-01 13:41:00 10954 10956 10954 10955
2019-10-01 13:42:00 10954 10956 10953 10953
2019-10-01 13:43:00 10954 10954 10950 10953
2019-10-01 13:44:00 10952 10954 10952 10954
[300 rows x 4 columns]
------------------- 5MIN K --------------------
open high low close
time
2019-10-01 08:45:00 10856 10912 10856 10888
2019-10-01 08:50:00 10888 10894 10886 10891
2019-10-01 08:55:00 10891 10892 10886 10891
2019-10-01 09:00:00 10890 10902 10884 10896
2019-10-01 09:05:00 10896 10905 10896 10898
2019-10-01 09:10:00 10898 10901 10888 10896
2019-10-01 09:15:00 10895 10922 10895 10916
2019-10-01 09:20:00 10917 10932 10915 10930
2019-10-01 09:25:00 10931 10935 10928 10932
2019-10-01 09:30:00 10932 10937 10931 10933
2019-10-01 09:35:00 10934 10936 10929 10932
2019-10-01 09:40:00 10933 10934 10928 10932
2019-10-01 09:45:00 10932 10943 10932 10940
2019-10-01 09:50:00 10939 10946 10938 10946
2019-10-01 09:55:00 10945 10946 10941 10943
2019-10-01 10:00:00 10944 10950 10943 10949
2019-10-01 10:05:00 10949 10950 10946 10949
2019-10-01 10:10:00 10949 10950 10938 10939
2019-10-01 10:15:00 10938 10943 10937 10942
2019-10-01 10:20:00 10942 10944 10941 10943
2019-10-01 10:25:00 10943 10944 10941 10943
2019-10-01 10:30:00 10943 10943 10936 10940
2019-10-01 10:35:00 10939 10942 10938 10940
2019-10-01 10:40:00 10940 10946 10939 10946
2019-10-01 10:45:00 10945 10947 10941 10942
2019-10-01 10:50:00 10942 10946 10942 10944
2019-10-01 10:55:00 10943 10947 10941 10944
2019-10-01 11:00:00 10945 10948 10940 10942
2019-10-01 11:05:00 10942 10943 10938 10940
2019-10-01 11:10:00 10940 10944 10939 10943
2019-10-01 11:15:00 10943 10944 10937 10938
2019-10-01 11:20:00 10939 10942 10936 10940
2019-10-01 11:25:00 10940 10943 10940 10943
2019-10-01 11:30:00 10943 10948 10942 10946
2019-10-01 11:35:00 10946 10948 10945 10945
2019-10-01 11:40:00 10946 10948 10945 10946
2019-10-01 11:45:00 10946 10949 10946 10947
2019-10-01 11:50:00 10947 10948 10943 10945
2019-10-01 11:55:00 10945 10945 10942 10944
2019-10-01 12:00:00 10944 10947 10944 10946
2019-10-01 12:05:00 10946 10948 10945 10946
2019-10-01 12:10:00 10946 10946 10942 10943
2019-10-01 12:15:00 10943 10944 10940 10941
2019-10-01 12:20:00 10941 10946 10941 10945
2019-10-01 12:25:00 10944 10946 10944 10946
2019-10-01 12:30:00 10946 10948 10945 10947
2019-10-01 12:35:00 10948 10948 10944 10946
2019-10-01 12:40:00 10946 10946 10943 10944
2019-10-01 12:45:00 10943 10946 10943 10945
2019-10-01 12:50:00 10945 10947 10944 10944
2019-10-01 12:55:00 10944 10954 10943 10951
2019-10-01 13:00:00 10951 10954 10949 10953
2019-10-01 13:05:00 10953 10958 10952 10957
2019-10-01 13:10:00 10957 10957 10952 10952
2019-10-01 13:15:00 10953 10957 10951 10956
2019-10-01 13:20:00 10956 10960 10954 10958
2019-10-01 13:25:00 10959 10962 10957 10960
2019-10-01 13:30:00 10960 10961 10957 10959
2019-10-01 13:35:00 10960 10961 10954 10955
2019-10-01 13:40:00 10955 10956 10950 10954
------------------- 1MIN MA --------------------
time
2019-10-01 08:45:00.057 10856.000000
2019-10-01 08:45:00.063 10856.000000
2019-10-01 08:45:00.065 10856.000000
2019-10-01 08:45:00.068 10856.000000
2019-10-01 08:45:00.073 10856.000000
...
2019-10-01 13:44:59.147 10953.238908
2019-10-01 13:44:59.257 10953.241497
2019-10-01 13:44:59.656 10953.240678
2019-10-01 13:44:59.791 10953.243243
2019-10-01 13:44:59.825 10953.245791
Name: price, Length: 32924, dtype: float64
------------------- 5MIN MA --------------------
time
2019-10-01 08:45:00.057 10856.000000
2019-10-01 08:45:00.063 10856.000000
2019-10-01 08:45:00.065 10856.000000
2019-10-01 08:45:00.068 10856.000000
2019-10-01 08:45:00.073 10856.000000
...
2019-10-01 13:44:59.147 10953.433503
2019-10-01 13:44:59.257 10953.434077
2019-10-01 13:44:59.656 10953.433637
2019-10-01 13:44:59.791 10953.434211
2019-10-01 13:44:59.825 10953.434783
Name: price, Length: 32924, dtype: float64
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_1MINK = TXF_TICKS['price'].resample('1MIN').ohlc()
print('------------------- 1MIN K --------------------')
print(TXF_1MINK)
TXF_5MINK = TXF_TICKS['price'].resample('5MIN').ohlc()
print('------------------- 5MIN K --------------------')
print(TXF_5MINK)
TXF_1MINMA = TXF_TICKS['price'].rolling('1MIN').mean()
print('------------------- 1MIN MA --------------------')
print(TXF_1MINMA)
TXF_5MINMA = TXF_TICKS['price'].rolling('5MIN').mean()
print('------------------- 5MIN MA --------------------')
print(TXF_5MINMA)
# 將
figure = plotly.graph_objects.Figure(
data=[
plotly.graph_objects.Candlestick(
x=TXF_1MINK.index,
open=TXF_1MINK['open'],
high=TXF_1MINK['high'],
low=TXF_1MINK['low'],
close=TXF_1MINK['close'],
name='1MIN K',
),
plotly.graph_objects.Candlestick(
x=TXF_5MINK.index,
open=TXF_5MINK['open'],
high=TXF_5MINK['high'],
low=TXF_5MINK['low'],
close=TXF_5MINK['close'],
name='5MIN K',
),
plotly.graph_objects.Scatter(
x=TXF_1MINMA.index,
y=TXF_1MINMA,
name='1MIN MA',
mode='lines',
line=plotly.graph_objects.scatter.Line(
color='#E58B6B'
)
),
plotly.graph_objects.Scatter(
x=TXF_5MINMA.index,
y=TXF_5MINMA,
name='5MIN MA',
mode='lines',
line=plotly.graph_objects.scatter.Line(
color='#6B99E5'
)
)
],
# 設定 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說可以!!
請問程式中的"TXFJ9-20191001.csv"有現成的檔案嗎?
應該是 LINE 上聯絡我的那位朋友,對吧
是的.感謝