雞尾酒策略雷達的函數化

By | 2017-12-11

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

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

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

2017121001

共有

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

2.股價突破籌碼沈澱區

3.投信強買發動

4.爆量剛起漲

5.突破糾結均線

6.低PB股的逆襲

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

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

我寫的函數腳本如下:

var:count(0);
count=0;

condition1=false;
condition2=false;
condition3=false;
condition4=false;
condition5=false;
condition6=false;
condition7=false;

//跌不下去的高殖利率股

 
condition8 = L = Lowest(L,20);
condition9 = H = Highest(H,20);
 
if GetSymbolField("tse.tw","收盤價")
>average(GetSymbolField("tse.tw","收盤價"),60)
//大盤處於多頭市場
then begin 
 if condition9
 //股價創區間以來高點
 and TrueAll(Condition8=false,20)
 //這段區間都未破底
 and close<close[19]*1.05
 //區間股價漲幅不大
 then condition1=true;
end;

if barslast(condition1=true)<3
then count=count+1;

//股價突破籌碼沈澱區

condition10=false; 

if v[1] > 0 then value1 = (v[1] - GetField("當沖張數")[1])/v[1];
//實質成交比例
 
if value1[1] >0 then value2 = 100*value1/value1[1]-100;
//實質成交比例日變動百分比
value3 = standarddev(value2,5,1);
//實質成交比例日變動百分比的不同天期標準差
value4 = standarddev(value2,10,1);
value5 = standarddev(value2,20,1);
if value3 = lowest(value3 ,20) and
 value4 = lowest(value4 ,20) and
 value5 = lowest(value5 ,20) 
//現在各不同天期的成交比例日變動百分比標準差都處在期間最低點
then condition10=true;
 
// 成交量判斷
 

if tselsindex(10,6)=1
and condition10 
and close crosses over highest(high[1],10)
then condition2=true;
if barslast(condition2=true)<3
then count=count+1;

//投信強買發動

variable: SumForce(0), SumTotalVolume(0),Kprice(0), Kdate(0);
SumForce = Summation(GetField("投信買賣超")[1], 3);
sumTotalVolume = Summation(Volume[1], 3);

if SumForce > SumTotalVolume * 15/100 And Average(Volume[1], 5) >= 1000 then 
begin
 Kprice =highest(avgprice[1],3);
 Kdate = date[1];
end; 

Condition11 = C crosses above Kprice and datediff(date, kdate) <= 60; 
Condition12 = Average(Volume[1], 5) >= 1000;
Condition13 = Volume > Average(Volume[1], 5) * 1.2;
Condition14 = C > C[1];
condition15 = tselsindex(10,6)=1;
if Condition1 And Condition2 And Condition3 And Condition4 And condition5
then condition3=true;
if barslast(condition3=true)<3
then count=count+1;


//爆量剛起漲

 
Condition16 = H=highest(H,60);
 //今日最高創區間最高價
 
Condition17 = V=highest(v,60);
 //今日成交量創區間最大量
 
Condition18 = highest(H,60) < lowest(L,60)*(1 + 14*0.01);
 //今日最高價距離區間最低價漲幅尚不大
 
if Condition1 And Condition2 And Condition3
and tselsindex(10,6)=1
then condition4=true;
if barslast(condition4=true)<3
then count=count+1;

//突破糾結均線
variable: Shortaverage(0),Midaverage(0),Longaverage(0);
variable: AvgHLp(0),AvgH(0),AvgL(0);

//透過Z的時間安排來決定現在用的是那一根Bar的資料 
variable: Z(0);
if currenttime > 180000 
or currenttime < 083000 then 
 Z =0 
else 
 Z=1;

Shortaverage = average(close,5);
Midaverage = average(close,10);
Longaverage = average(close,20);
AvgH = maxlist(Shortaverage,Midaverage,Longaverage);
AvgL = minlist(Shortaverage,Midaverage,Longaverage);

if AvgL > 0 then AvgHLp = 100*AvgH/AvgL -100;

condition19 = trueAll(AvgHLp < 5,20);
condition20 = V > average(V[1],20)*(1+25/100) ;
condition21 = C > AvgH *(1.02) and H > highest(H[1],20);
condition22 = average(volume[1], 5) >= 1000; 
condition23 = tselsindex(10,6)[Z]=1;

if condition19
and condition20 
and condition21 
and condition22
and condition23
then condition5=true;
if barslast(condition5=true)<3
then count=count+1;

//低PB股的逆襲

if GetSymbolField("tse.tw","收盤價") > average(GetSymbolField("tse.tw","收盤價"),10)
then begin
 if close<12
 and H = highest(H,20)
 and close<lowest(low,20)*1.07
 and highest(h,40)>close*1.1
 then
 condition6=true;
end;
if barslast(condition6=true)<3
then count=count+1;

//即將鎖第一根漲停的中小型股
 
Condition24 = Close >= GetField("uplimit") * (1 - 1/100);
Condition25 = TrueAll(Close < GetField("uplimit"), 20);
Condition26 = Average(Volume, 5) >= 1000;
Condition27 = Close > Highest(High[1], 20);
Condition28 = Volume >= Highest(Volume[1], 20);
Condition29 = GetSymbolField("tse.tw","收盤價") > average(GetSymbolField("tse.tw","收盤價"),10); 
if Condition24 And Condition25 And Condition26 And Condition27 And Condition28 and condition29
then condition7=true;
if barslast(condition7=true)<3
then count=count+1;
entrycount=count;

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

if entrycount cross over 1 then ret=1;

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

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

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