iT邦幫忙

2023 iThome 鐵人賽

DAY 18
0
AI & Data

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

Day18 趨勢、位階、型態(2)

  • 分享至 

  • xImage
  •  

今天與明天都在討論多空之間的過程,主要有兩個目的:1、介紹常見技術分析的通用名詞,2、介紹多空上漲下跌之間產生的各種現象。

由多翻空的過程

起跌→ 反彈/盤整→續跌→止跌→落底→打底/築底成底→短線翻多→中線翻多→長線翻多

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

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

一、空頭起跌

起跌就是最高檔的三個高點

  • 第一個:波段最高點
  • 第二個:空頭下跌後反彈再續跌的點位
  • 第三個:頭部型態或盤整區間跌破的點位

其中第一個高點只能後來驗證,當下並不知道。接下來用人工方式標註三個點的數據,程式碼如下所示:

### 人工方式標示空頭起跌 ###
start_fall_points_len = len(np.array(df_k_line['Close']))
start_fall_points = np.array([np.nan]*start_fall_points_len)
# 起跌就是最高檔的三個高點:
# 第一個:波段最高點
idx = date_to_index(df_k_line,'2008-05-19')
start_fall_points[idx] = df_k_line.iloc[idx]['Close']
# 第二個:空頭下跌後反彈再續跌的點位
idx = date_to_index(df_k_line,'2008-06-06')
start_fall_points[idx] = df_k_line.iloc[idx]['Close']
# 設定頸線
neckline_price = df_k_line.loc['2008-04-01']['Close']
neckline_start_date = '2008-03-21'
neckline_end_date = '2008-06-18'
# 第三個:頭部型態或盤整區間跌破的點位
idx = date_to_index(df_k_line,'2008-06-10')
start_fall_points[idx] = neckline_price

程式執行結果如下:
Imgur

二、空頭反彈

空頭下跌中的上漲就是反彈,程式碼如下所示:

### 空頭反彈 ###

range_lines = []
# 範圍一
corner1 = ('2008-07-18',df_k_line.loc['2008-07-23']['Close'])
corner2 = ('2008-07-23',df_k_line.loc['2008-07-23']['Close'])
corner3 = ('2008-07-18',df_k_line.loc['2008-07-18']['Close'])
corner4 = ('2008-07-23',df_k_line.loc['2008-07-18']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

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

# 範圍三
corner1 = ('2008-09-18',df_k_line.loc['2008-09-24']['Close'])
corner2 = ('2008-09-24',df_k_line.loc['2008-09-24']['Close'])
corner3 = ('2008-09-18',df_k_line.loc['2008-09-18']['Open'])
corner4 = ('2008-09-24',df_k_line.loc['2008-09-18']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍四
corner1 = ('2008-10-28',df_k_line.loc['2008-11-05']['Open'])
corner2 = ('2008-11-05',df_k_line.loc['2008-11-05']['Open'])
corner3 = ('2008-10-28',df_k_line.loc['2008-10-28']['Open'])
corner4 = ('2008-11-05',df_k_line.loc['2008-10-28']['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 = ('2008-05-20',df_k_line.loc['2008-05-20']['Open'])
corner2 = ('2008-07-16',df_k_line.loc['2008-05-20']['Open'])
corner3 = ('2008-05-20',df_k_line.loc['2008-07-16']['Close'])
corner4 = ('2008-07-16',df_k_line.loc['2008-07-16']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍二
corner1 = ('2008-07-24',df_k_line.loc['2008-07-24']['Close'])
corner2 = ('2008-08-05',df_k_line.loc['2008-07-24']['Close'])
corner3 = ('2008-07-24',df_k_line.loc['2008-08-05']['Close'])
corner4 = ('2008-08-05',df_k_line.loc['2008-08-05']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍三
corner1 = ('2008-08-14',df_k_line.loc['2008-08-15']['Open'])
corner2 = ('2008-08-20',df_k_line.loc['2008-08-15']['Open'])
corner3 = ('2008-08-14',df_k_line.loc['2008-08-19']['Open'])
corner4 = ('2008-08-20',df_k_line.loc['2008-08-19']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍四
corner1 = ('2008-08-29',df_k_line.loc['2008-08-29']['Open'])
corner2 = ('2008-09-18',df_k_line.loc['2008-08-29']['Open'])
corner3 = ('2008-08-29',df_k_line.loc['2008-09-18']['Open'])
corner4 = ('2008-09-18',df_k_line.loc['2008-09-18']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍五
corner1 = ('2008-09-25',df_k_line.loc['2008-09-25']['Open'])
corner2 = ('2008-10-28',df_k_line.loc['2008-09-25']['Open'])
corner3 = ('2008-09-25',df_k_line.loc['2008-10-28']['Open'])
corner4 = ('2008-10-28',df_k_line.loc['2008-10-28']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍六
corner1 = ('2008-11-03',df_k_line.loc['2008-11-03']['Close'])
corner2 = ('2008-11-21',df_k_line.loc['2008-11-03']['Close'])
corner3 = ('2008-11-03',df_k_line.loc['2008-11-21']['Open'])
corner4 = ('2008-11-21',df_k_line.loc['2008-11-21']['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 = ('2008-07-07',df_k_line.loc['2008-08-15']['Open'])
corner2 = ('2008-09-02',df_k_line.loc['2008-08-15']['Open'])
corner3 = ('2008-07-07',df_k_line.loc['2008-07-16']['Close'])
corner4 = ('2008-09-02',df_k_line.loc['2008-07-16']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍二
corner1 = ('2008-09-15',df_k_line.loc['2008-09-23']['Close'])
corner2 = ('2008-10-03',df_k_line.loc['2008-09-23']['Close'])
corner3 = ('2008-09-15',df_k_line.loc['2008-09-30']['Open'])
corner4 = ('2008-10-03',df_k_line.loc['2008-09-30']['Open'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

# 範圍三
corner1 = ('2008-10-20',df_k_line.loc['2008-10-21']['Open'])
corner2 = ('2008-11-17',df_k_line.loc['2008-10-21']['Open'])
corner3 = ('2008-10-20',df_k_line.loc['2008-10-29']['Close'])
corner4 = ('2008-11-17',df_k_line.loc['2008-10-29']['Close'])
range_lines.append([corner1,corner2])
range_lines.append([corner1,corner3])
range_lines.append([corner3,corner4])
range_lines.append([corner4,corner2])

程式執行結果如下:
Imgur

五、空頭止跌/落底

也就是波段最低點,這也是事後才能知道。程式碼如下所示:

### 人工方式標示空頭落底 ###
stop_fall_points_len = len(np.array(df_k_line['Close']))
stop_fall_points = np.array([np.nan]*stop_fall_points_len)

# 最低點
idx = date_to_index(df_k_line,'2008-11-21')
stop_fall_points[idx] = df_k_line.iloc[idx]['Open']

程式執行結果如下:
Imgur

六、空頭打底/築底 與 空頭成底

在區間內盤整為打底或築底,突破後為成底。程式碼如下所示:

### 空頭打底/築底 與 空頭成底 ###

range_lines = []
# 底部範圍
corner1 = ('2019-08-02',df_k_line.loc['2019-08-02']['Close'])
corner2 = ('2019-09-04',df_k_line.loc['2019-08-02']['Close'])
corner3 = ('2019-08-02',df_k_line.loc['2019-08-06']['Open'])
corner4 = ('2019-09-04',df_k_line.loc['2019-08-06']['Open'])
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,'2019-08-30')] = df_k_line.loc['2019-08-02']['Close']

程式執行結果如下:
Imgur
有些時候會遇到有第二層底,就會是一個很大的底形;且通常最多就兩層底。也有時候會遇到不會有底部型態,出現一根爆量下引線就上去了。

完整的程式碼請參照「第十八天:趨勢、位階、型態(2).ipynb」。


上一篇
Day17 趨勢、位階、型態(1)
下一篇
Day19 趨勢、位階、型態(3)
系列文
用Python程式進行股票技術分析30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言