iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0
AI & Data

用Python程式進行股票技術分析系列 第 19

Day19 趨勢、位階、型態(3)

  • 分享至 

  • xImage
  •  

經過昨日Day18的「由多翻空的過程」的程式實作,應該發現技術分析的主觀部份真的很依賴人的視覺以及經驗;跟著股票老師學這個課題的時候也是這樣,股票老師會給案例(兩張圖,多頭跟空頭;請參考我在6月2日的股票投資筆記);在老師說明完講義上的範例之後就得自行在案例上繪圖練習,因此沒有太多的文字描述可供參照。今天會接續著說明「由空翻多的過程」。

由空翻多的過程

起漲→回檔/盤整→續漲→止漲→盤頭→做頭起跌→短線翻空→中線翻空→長線翻空

程式實作:人工方式標註空翻多過程,以瞭解多空之間的各種現象及其特徵

以下將會介紹過程中各階段的特徵並進行程式實作,使用Day1所提的「主觀性質客觀化」的方式一(將在看盤軟體繪製的圖形與線段以人工方式轉換成數據)來將視覺觀察的現象轉換為客觀數據。

一、多頭確立

空頭落底後翻多的兩個工具:

  • 道氏理論:N字型的現象 低點越來越高,高點越來越高
  • 移動平均線理論 20日線站上且20日線上揚
    程式實作如下:
### 人工方式標示多頭確立 ###
# 方法一:道氏理論 N字型的現象:低點越來越高,高點越來越高
bulls_dow_points_len = len(np.array(df_k_line['Close']))
bulls_dow_points = np.array([np.nan]*bulls_dow_points_len)
idx = date_to_index(df_k_line,'2019-08-06')
bulls_dow_points[idx] = df_k_line.iloc[idx]['Open']
idx = date_to_index(df_k_line,'2019-08-26')
bulls_dow_points[idx] = df_k_line.iloc[idx]['Close']
idx = date_to_index(df_k_line,'2019-08-08')
bulls_dow_points[idx] = df_k_line.iloc[idx]['Close']
idx = date_to_index(df_k_line,'2019-08-22')
bulls_dow_points[idx] = df_k_line.iloc[idx]['Open']
# 方法二:移動平均線理論 20日線站上且20日線上揚
bulls_ma20_points_len = len(np.array(df_k_line['Close']))
bulls_ma20_points = np.array([np.nan]*bulls_ma20_points_len)
idx = date_to_index(df_k_line,'2019-08-30')
bulls_ma20_points[idx] = df_k_line.iloc[idx]['Open']

程式執行結果如下:
Imgur

二、多頭起漲

起漲就是最低的三個點位

  • 第一個:波段最低點
  • 第二個:上漲回檔再續漲
  • 第三個:出現迷你底型或盤整區間突破
    其中第一個低點只能後來驗證,當下並不知道。接下來用人工方式標註三個點的數據,程式碼如下所示:
### 人工方式標示多頭起漲 ###
start_rise_points_len = len(np.array(df_k_line['Close']))
start_rise_points = np.array([np.nan]*start_rise_points_len)
# 起漲就是最低的三個點位:
# 第一個:最低點 
idx = date_to_index(df_k_line,'2019-08-06')
start_rise_points[idx] = df_k_line.iloc[idx]['Close']
# 第二個:上漲回檔再續漲
idx = date_to_index(df_k_line,'2019-08-30')
start_rise_points[idx] = df_k_line.iloc[idx]['Close']
# 第三個:出現迷你底型或盤整區間突破
idx = date_to_index(df_k_line,'2019-09-04')
start_rise_points[idx] = df_k_line.iloc[idx]['Close']

程式執行結果如下:
Imgur

三、多頭回檔

多頭格局中的拉回叫做回檔,程式碼如下所示:

### 多頭回檔 ###

range_lines = []
# 範圍一
corner1 = ('2019-08-22',df_k_line.loc['2019-08-22']['Open'])
corner2 = ('2019-08-28',df_k_line.loc['2019-08-22']['Open'])
corner3 = ('2019-08-22',df_k_line.loc['2019-08-27']['Close'])
corner4 = ('2019-08-28',df_k_line.loc['2019-08-27']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍二
corner1 = ('2019-11-06',df_k_line.loc['2019-11-06']['Open'])
corner2 = ('2019-11-15',df_k_line.loc['2019-11-06']['Open'])
corner3 = ('2019-11-06',df_k_line.loc['2019-11-11']['Close'])
corner4 = ('2019-11-15',df_k_line.loc['2019-11-11']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])


# 範圍三
corner1 = ('2019-11-27',df_k_line.loc['2019-11-27']['Close'])
corner2 = ('2019-12-04',df_k_line.loc['2019-11-27']['Close'])
corner3 = ('2019-11-27',df_k_line.loc['2019-12-03']['Open'])
corner4 = ('2019-12-04',df_k_line.loc['2019-12-03']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍四
corner1 = ('2020-01-02',df_k_line.loc['2020-01-02']['Close'])
corner2 = ('2020-01-09',df_k_line.loc['2020-01-02']['Close'])
corner3 = ('2020-01-02',df_k_line.loc['2020-01-09']['Open'])
corner4 = ('2020-01-09',df_k_line.loc['2020-01-09']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

程式執行結果如下:
Imgur

四、多頭續漲

程式碼如下所示:

### 多頭續漲 ###

range_lines = []
# 範圍一
corner1 = ('2019-08-15',df_k_line.loc['2019-08-15']['Close'])
corner2 = ('2019-08-22',df_k_line.loc['2019-08-15']['Close'])
corner3 = ('2019-08-15',df_k_line.loc['2019-08-22']['Open'])
corner4 = ('2019-08-22',df_k_line.loc['2019-08-22']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍二
corner1 = ('2019-08-26',df_k_line.loc['2019-08-26']['Close'])
corner2 = ('2019-09-19',df_k_line.loc['2019-08-26']['Close'])
corner3 = ('2019-08-26',df_k_line.loc['2019-09-19']['Open'])
corner4 = ('2019-09-19',df_k_line.loc['2019-09-19']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍三
corner1 = ('2019-10-03',df_k_line.loc['2019-10-03']['Open'])
corner2 = ('2019-11-06',df_k_line.loc['2019-10-03']['Open'])
corner3 = ('2019-10-03',df_k_line.loc['2019-11-06']['Close'])
corner4 = ('2019-11-06',df_k_line.loc['2019-11-06']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍四
corner1 = ('2019-11-13',df_k_line.loc['2019-11-14']['Close'])
corner2 = ('2019-11-20',df_k_line.loc['2019-11-14']['Close'])
corner3 = ('2019-11-13',df_k_line.loc['2019-11-19']['Close'])
corner4 = ('2019-11-20',df_k_line.loc['2019-11-19']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍五
corner1 = ('2019-12-03',df_k_line.loc['2019-12-03']['Open'])
corner2 = ('2019-12-18',df_k_line.loc['2019-12-03']['Open'])
corner3 = ('2019-12-03',df_k_line.loc['2019-12-18']['Close'])
corner4 = ('2019-12-18',df_k_line.loc['2019-12-18']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

程式執行結果如下:
Imgur

五、多頭盤整

程式碼如下所示:

### 多頭盤整 ###

range_lines = []
# 範圍一
corner1 = ('2019-09-12',df_k_line.loc['2019-10-01']['Close'])
corner2 = ('2019-10-08',df_k_line.loc['2019-10-01']['Close'])
corner3 = ('2019-09-12',df_k_line.loc['2019-09-16']['Open'])
corner4 = ('2019-10-08',df_k_line.loc['2019-09-16']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍二
corner1 = ('2019-11-05',df_k_line.loc['2019-12-09']['Close'])
corner2 = ('2019-12-11',df_k_line.loc['2019-12-09']['Close'])
corner3 = ('2019-11-05',df_k_line.loc['2019-11-11']['Close'])
corner4 = ('2019-12-11',df_k_line.loc['2019-11-11']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍三
corner1 = ('2019-12-13',df_k_line.loc['2020-01-03']['Open'])
corner2 = ('2020-01-30',df_k_line.loc['2020-01-03']['Open'])
corner3 = ('2019-12-13',df_k_line.loc['2020-01-07']['Close'])
corner4 = ('2020-01-30',df_k_line.loc['2020-01-07']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

程式執行結果如下:
Imgur

六、多頭止漲

漲到最高點就是多頭止漲,程式碼如下所示:

### 人工方式標示多頭止漲 ###
stop_rise_points_len = len(np.array(df_k_line['Close']))
stop_rise_points = np.array([np.nan]*stop_rise_points_len)

# 最高點
idx = date_to_index(df_k_line,'2020-01-03')
stop_rise_points[idx] = df_k_line.iloc[idx]['Open']

程式執行結果如下:
Imgur

七、多頭盤頭與空頭做頭

盤頭跌破就是做頭,程式碼如下所示:

### 多頭盤頭 與 空頭做頭 ####

range_lines = []
# 頭部範圍
corner1 = ('2019-12-13',df_k_line.loc['2020-01-03']['Open'])
corner2 = ('2020-01-30',df_k_line.loc['2020-01-03']['Open'])
corner3 = ('2019-12-13',df_k_line.loc['2020-01-07']['Close'])
corner4 = ('2020-01-30',df_k_line.loc['2020-01-07']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 跌破點
breakthrough_points_len = len(np.array(df_k_line['Close']))
breakthrough_points = np.array([np.nan]*breakthrough_points_len)
breakthrough_points[date_to_index(df_k_line,'2020-01-30')] = df_k_line.loc['2020-01-07']['Close']

程式執行結果如下:
Imgur
接下來又循環地回到昨天Day18的「空頭起跌」。
完整的程式碼請參照「第十九天:趨勢、位階、型態(3).ipynb」。


上一篇
Day18 趨勢、位階、型態(2)
下一篇
Day20 移動平均線的扣抵
系列文
用Python程式進行股票技術分析30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言