如何快速定義及應用K線型態來寫腳本?

By | 2020-02-04

這兩天盤的波動較大,非經濟因素帶來的衝擊,對一些長期趨勢型的分析工具可能比較容易失真,透過1到數根K棒的型態分析,再搭配籌碼及基本面因素的選股方式,變成短期因應之道,但坊間的選股工具,一般都是K線型態歸一類,籌碼與基本面分析的歸另一類,較少組合起來選股的工具,於是有XS的User反映能否把K線型態內建成函數,以利於跟其他選股條件組合使用,今天就來回應這個需求

首先,我寫了一個樣本函數如下:

1bKpatterm型態函數
2
3settotalbar(5);
4condition2 = (minlist(open,close)-Low) > absvalue(open-close)*3;
5condition3 = minlist(open, close) > low* (100 + 2)/100;
6
7if trueall( condition2 and condition3, 3)
8then bkpatterm="三長下影線";
9
10{[檔名:] 紅三兵
11[說明:] 連續三根上漲實體K棒
12}
13
14condition1= ( close - open ) >(high -low) * 0.75;
15//狀況1:實體上漲K棒
16condition4= ( close[1] - open[1] ) >(high[1] -low[1]) * 0.75;
17//狀況2:前一根也是實體上漲K棒
18condition5= ( close[2] - open[2] ) >(high[2] -low[2]) * 0.75;
19//狀況3:前前根也是實體上漲K棒
20condition6= close > close[1];
21//狀況4: 上漲
22condition7= close[1] > close[2];//狀況5:上漲
23{結果判斷}
24IF
25condition1
26and condition4
27and condition5
28and condition6
29and condition7
30THEN bkpatterm="紅三兵";
31
32condition8= ( open[2] - close[2] ) >(high[2] -low[2]) * 0.75;
33//狀況1:實體下跌K棒
34condition9= ( close[1] - open[1] ) >(high[1] -low[1]) * 0.75;
35//狀況2:實體上漲K棒
36condition10= high[1] < high[2] and low[1] > low[2];
37//狀況3:前期內包於前前期
38condition11=( close - open ) > 0.75 *(high -low);
39//狀況4:當期實體上漲K棒
40condition12=close > open[2];
41//狀況5:現價突破前前期開盤價
42IF condition8
43and condition9
44and condition10
45and condition11
46and condition12
47THEN bkpatterm="內困三日翻紅";
48
49condition13=open = High and close < open ;//狀況1: 開高收低留黑棒
50condition14=(high -low) > 2 *(high[1]-low[1]) ;//狀況2: 波動倍增
51condition15=(close-low)> (open-close) *2 ;//狀況3: 下影線為實體兩倍以上
52
53IF condition13
54and condition14
55and condition15
56THEN bkpatterm="吊人線";
57
58condition16=(open[1] - close[1] ) >(high[1] -low[1])*0.75;
59//狀況1:前期出黑K棒
60condition17=( close - open ) >(high -low) * 0.75;
61//狀況2:當期紅棒
62condition18=high > high[1];
63//狀況3:高過昨高
64condition19=open<low[1];
65//狀況4:開低破昨低
66IF condition16
67and condition17
68and condition18
69and condition19
70THEN bkpatterm="多頭吞噬";
71
72{
73[檔名:] 多頭執帶
74[說明:] 開在最低點一路走高收在最高點附近的K棒
75}
76condition20=close>open;
77condition21=(Close-Open)>(high-low)*0.9;
78condition22=Close>Close[1]+high[1]-low[1];
79IF condition20
80and condition21
81and condition22
82THEN bkpatterm="多頭執帶";
83
84{
85[檔名:] 多頭母子
86[說明:] 前期收長黑K棒 今期開高小幅收紅不過昨高
87}
88
89condition23=( open[1] - close[1] ) >(high[1] -low[1])*0.75;
90//狀況1:前期出長黑K棒
91condition24=close[3]-close[2]<close[2]-close[1];
92//狀況2:前期呈波動放大下跌
93condition25=( close - open ) >(high -low) * 0.75;
94//狀況3:當期紅棒
95condition26=high < high[1];
96//狀況4:高不過昨高
97condition27=low>low[1];
98//狀況5:低不破昨低
99
100IF
101condition23
102and condition24
103and condition25
104and condition26
105and condition27
106THEN bkpatterm="多頭母子";
107
108{
109[檔名:] 多頭遭遇
110[說明:] 前期收黑K棒 當期開低走高紅棒嘗試反攻昨收
111}
112
113condition28= (open[1] - close[1] ) >(high[1] -low[1]) * 0.75;
114//狀況1:前期出黑K棒
115condition29= close[1] < close[2];
116//狀況2:前期收跌
117condition30= ( close - open ) >(high -low) * 0.75;
118//狀況3:當期收紅K棒
119condition31= open < close[1] and close < close[1];
120//狀況4:開低且收跌
121condition32= low < low[1];//狀況5:破前期低點
122{結果判斷}
123IF
124condition28
125and condition29
126and condition30
127and condition31
128and condition32
129THEN bkpatterm="多頭遭遇";

有了這個函數,我就可以把型態跟其他選股條件組合在一起,成為一個新的選股策略。

舉個例子,K線型態上有個叫多頭母子的型態,其樣式如下圖

我寫了一個選股策略如下

這當中除了本益比小於12及近一日三大法人合計買超這兩個條件是系統內建的之外,另外加了一個腳本型的選股條件: 低本益比股大跌後出現多頭母子,我寫的腳本如下:

1if bkpatterm="多頭母子"
2and close*1.4<close[90]
3and close*1.2<close[30]
4then ret=1;

這個腳本是去尋找過去九十天跌了四成,過去三十天跌了兩成的股票,然後它的K線型態符合多頭母子。

如果拿這腳本跟上述條件組合而成的策略去選股,回測過去兩年,出場點設為20天之後,回測報告如下

過去兩年共出現八次抄底的機會,每次都能獲利出場,我把回測的時間接長到五年及十年,勝率也都有六成以上,平均報酬率也都還可以。

以上是bkpatterm這個函數的介紹及應用,這個函數目前包括的K線型態還不夠多,我會找時間把各種型態如W底,M頭等等的都補齊,未來大家直接引用即可。