雞尾酒策略雷達的函數化

By | 2017-12-11

因著雲端策略中心愈來愈普及於券商的下單系統,開始出現一種需求,希望可以綜合多個交易策略來考量,只有在特定幾個交易策略近N天內出現多個以上的交易訊號時才出現進場訊號。 這個需求PM應該是有收到,但考慮到 XQ一年才換三次版,且前面已經排了不少需求,所以我想從腳本下手,找出一個方法可以綜合多個腳本考量後再出訊號。

我想舉雲端策略中心裡的例子來跟大家說明這個方法

假設雲端策略裡長線作多的策略裡,我訂閱的策略如下表

2017121001

共有

1.跌不下去的高殖利率股

2.股價突破籌碼沈澱區

3.投信強買發動

4.爆量剛起漲

5.突破糾結均線

6.低PB股的逆襲

7.即將鎖第一根漲停的中小型股

一共七個策略,或希望如果這個七個腳本有超過兩個在三天內都有出訊號時,系統再通知我,我的作法是寫一個函數來計算近三天這七個腳本共出現多少個訊號。

我寫的函數腳本如下:

1var:count(0);
2count=0;
3
4condition1=false;
5condition2=false;
6condition3=false;
7condition4=false;
8condition5=false;
9condition6=false;
10condition7=false;
11
12//跌不下去的高殖利率股
13
14 
15condition8 = L = Lowest(L,20);
16condition9 = H = Highest(H,20);
17 
18if GetSymbolField("tse.tw","收盤價")
19>average(GetSymbolField("tse.tw","收盤價"),60)
20//大盤處於多頭市場
21then begin 
22 if condition9
23 //股價創區間以來高點
24 and TrueAll(Condition8=false,20)
25 //這段區間都未破底
26 and close<close[19]*1.05
27 //區間股價漲幅不大
28 then condition1=true;
29end;
30
31if barslast(condition1=true)<3
32then count=count+1;
33
34//股價突破籌碼沈澱區
35
36condition10=false; 
37
38if v[1] > 0 then value1 = (v[1] - GetField("當沖張數")[1])/v[1];
39//實質成交比例
40 
41if value1[1] >0 then value2 = 100*value1/value1[1]-100;
42//實質成交比例日變動百分比
43value3 = standarddev(value2,5,1);
44//實質成交比例日變動百分比的不同天期標準差
45value4 = standarddev(value2,10,1);
46value5 = standarddev(value2,20,1);
47if value3 = lowest(value3 ,20) and
48 value4 = lowest(value4 ,20) and
49 value5 = lowest(value5 ,20) 
50//現在各不同天期的成交比例日變動百分比標準差都處在期間最低點
51then condition10=true;
52 
53// 成交量判斷
54 
55
56if tselsindex(10,6)=1
57and condition10 
58and close crosses over highest(high[1],10)
59then condition2=true;
60if barslast(condition2=true)<3
61then count=count+1;
62
63//投信強買發動
64
65variable: SumForce(0), SumTotalVolume(0),Kprice(0), Kdate(0);
66SumForce = Summation(GetField("投信買賣超")[1], 3);
67sumTotalVolume = Summation(Volume[1], 3);
68
69if SumForce > SumTotalVolume * 15/100 And Average(Volume[1], 5) >= 1000 then 
70begin
71 Kprice =highest(avgprice[1],3);
72 Kdate = date[1];
73end; 
74
75Condition11 = C crosses above Kprice and datediff(date, kdate) <= 60; 
76Condition12 = Average(Volume[1], 5) >= 1000;
77Condition13 = Volume > Average(Volume[1], 5) * 1.2;
78Condition14 = C > C[1];
79condition15 = tselsindex(10,6)=1;
80if Condition1 And Condition2 And Condition3 And Condition4 And condition5
81then condition3=true;
82if barslast(condition3=true)<3
83then count=count+1;
84
85
86//爆量剛起漲
87
88 
89Condition16 = H=highest(H,60);
90 //今日最高創區間最高價
91 
92Condition17 = V=highest(v,60);
93 //今日成交量創區間最大量
94 
95Condition18 = highest(H,60) < lowest(L,60)*(1 + 14*0.01);
96 //今日最高價距離區間最低價漲幅尚不大
97 
98if Condition1 And Condition2 And Condition3
99and tselsindex(10,6)=1
100then condition4=true;
101if barslast(condition4=true)<3
102then count=count+1;
103
104//突破糾結均線
105variable: Shortaverage(0),Midaverage(0),Longaverage(0);
106variable: AvgHLp(0),AvgH(0),AvgL(0);
107
108//透過Z的時間安排來決定現在用的是那一根Bar的資料 
109variable: Z(0);
110if currenttime > 180000 
111or currenttime < 083000 then 
112 Z =0 
113else 
114 Z=1;
115
116Shortaverage = average(close,5);
117Midaverage = average(close,10);
118Longaverage = average(close,20);
119AvgH = maxlist(Shortaverage,Midaverage,Longaverage);
120AvgL = minlist(Shortaverage,Midaverage,Longaverage);
121
122if AvgL > 0 then AvgHLp = 100*AvgH/AvgL -100;
123
124condition19 = trueAll(AvgHLp < 5,20);
125condition20 = V > average(V[1],20)*(1+25/100) ;
126condition21 = C > AvgH *(1.02) and H > highest(H[1],20);
127condition22 = average(volume[1], 5) >= 1000; 
128condition23 = tselsindex(10,6)[Z]=1;
129
130if condition19
131and condition20 
132and condition21 
133and condition22
134and condition23
135then condition5=true;
136if barslast(condition5=true)<3
137then count=count+1;
138
139//低PB股的逆襲
140
141if GetSymbolField("tse.tw","收盤價") > average(GetSymbolField("tse.tw","收盤價"),10)
142then begin
143 if close<12
144 and H = highest(H,20)
145 and close<lowest(low,20)*1.07
146 and highest(h,40)>close*1.1
147 then
148 condition6=true;
149end;
150if barslast(condition6=true)<3
151then count=count+1;
152
153//即將鎖第一根漲停的中小型股
154 
155Condition24 = Close >= GetField("uplimit") * (1 - 1/100);
156Condition25 = TrueAll(Close < GetField("uplimit"), 20);
157Condition26 = Average(Volume, 5) >= 1000;
158Condition27 = Close > Highest(High[1], 20);
159Condition28 = Volume >= Highest(Volume[1], 20);
160Condition29 = GetSymbolField("tse.tw","收盤價") > average(GetSymbolField("tse.tw","收盤價"),10); 
161if Condition24 And Condition25 And Condition26 And Condition27 And Condition28 and condition29
162then condition7=true;
163if barslast(condition7=true)<3
164then count=count+1;
165entrycount=count;

這個腳本的作法是用計數器的寫法來計算每個腳本是否符合三天內出訊號的條件,用barslast這個函數來計算特定策略三天內是否出現訊號,最後完成這個函數,透過這個函數寫的警示腳本就可以變的很簡單

1if entrycount cross over 1 then ret=1;

有了這個函數之後,如果我們要找出這七個腳本在三天內不只出現一個訊號,一行就寫完了。

各位可以利用這樣的寫法,把同頻率的策略雷達合成一個腳本去跑,如果要當天同時都出訊號,就不要用barslast就可以。

透過這樣的方法,就可以達到綜合多個策略考量,當不只一個策略出訊號時才通知的效果。