電子信箱 service [at] bituzi.com
幣圖誌首頁 facebook粉絲團 google plus google plus


開發商品的交易系統 - 基礎篇 [25] --葛蘭碧法則




著名的美國投資專家Joseph E.Granville(葛蘭碧,又譯為格蘭威爾)於20世紀中期提出移動平均線,簡稱均線。均線理論是當今應用最普遍的技術指標之一,它幫助交易者確認現有趨勢、判斷將出現的趨勢、發現過度延伸即將反轉的趨勢。均線周期越長的,穩定性較佳,不容易受到短期股價波動的影響,但延遲性越明顯;反之,均線周期較短的,穩定性較差,非常容易受到短期股價波動的影響,但延遲性較不明顯。

在金融市場最重要的原則是市場多頭時做多,空頭時做空,要是看錯市場趨勢,而不小心與市場為敵,是很難賺到錢的,因此,投資最重要的基礎,就是要能夠判斷市場多空,而且要與市場站在同一邊。通常價格趨勢形成時,在均線的排列上會出現多頭排列 ~短周期均線都在較長周期均線之上,是多頭攻勢力道很強的象徵;反之,較長周期均線都在短周期均線之上,即稱為空頭排列,是空頭跌勢力道很強的象徵。

什麼是黃金交叉
黃金交叉是指原本呈現空頭排列之短、中、長期平均線,長天期的技術平均線下降趨勢逐漸變緩,而短天期的技術平均線自底部翻升升上突破中、長期平均線,帶動中、長期平均線同步翻轉向上的情況。 這代表股價下跌趨勢已經停止,一波股價續漲可能性高,為買進信號。



一個簡單的三均線交叉策略
inputs:FastLen(10),MidLen(20),SlowLen(60) ;
inputs:TradeProfit(0.04),TradeStopLoss(0.04);
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);

MP = MarketPosition ;

if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3
then isBalanceDay = True else isBalanceDay =False ;

PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;

{ 買方環境建立 - 短期均線向上交叉中期均線與長期均線 }
Condition1 = Xaverage(Close,FastLen) Cross over Xaverage(Close,MidLen) ;
Condition2 = Xaverage(Close,FastLen) Cross over Xaverage(Close,SlowLen) ;

{ 賣方環境建立 - 短期均線向下交叉中期均線與長期均線 }
Condition3 = Xaverage(Close,FastLen) Cross under Xaverage(Close,MidLen) ;
Condition4 = Xaverage(Close,FastLen) Cross under Xaverage(Close,SlowLen) ;

{作多進場 - 短期均線同時向上突破中期與長期兩條均線}
if Condition1 and Condition2 then Buy next bar at market ;

{作多進場 - 短期均線同時向下跌破中期與長期兩條均線}
if Condition3 and Condition4 then Sell next bar at market ;

{多單在倉時 若出現短期均線跌破中期或長期任一均線則平倉出場}
If MP > 0 and (Condition3 or condition4 ) then ExitLong next bar at Lowest(Low,3) stop;

{空單在倉時 若出現短期均線突破中期或長期任一均線則平倉出場}
If MP < 0 and (Condition1 or condition2 ) then ExitShort next bar at Highest(High,3) stop;

if IsBalanceDay then setExitonClose ;

這裡我們會碰到兩個狀況 1.三線同時交叉的出現次數可能不多 2.若要作參數最佳化時,這三個參數的範圍要如何去設定

狀況一. 我們可以去控制均線交叉在幾根K棒內發生即可算是作多條件成立
a. 使用 MRO 函數 
if Condition2 and MRO(Condition1,N1,1) <> -1 then Buy next bar at market ;
這代表的意義是 Condition2 成立且 N1 根K棒期間內最近也出現過Condition1 成立的情形
if Condition4 and MRO(Condition3,N2,1) <> -1 then sell next bar at market ;
這代表的意義是 Condition4 成立且 N2 根K棒期間內最近也出現過Condition3 成立的情形

b. 使用 Countif 函數
if Condition2 and Countif(Condition1,N1) = 1 then Buy next bar at market ;
這代表的意義是 Condition2 成立且 N1 根K棒期間內最近只出現一次Condition1 成立的情形
if Condition4 and Countif(Condition3,N2) = 1 then Sell next bar at market ;
這代表的意義是 Condition4 成立且 N2根K棒期間內最近只出現一次Condition3 成立的情形

MRO 與 Countif 使用上的差異點是
MRO 可以檢查在 N根K棒期間內是否有出現最近一次,最近二次‧‧‧‧‧‧條件成立的狀況
Countif  是檢查N根K棒期間內條件成立的總發生次數

如果我們希望兩種往上交叉的狀況之間,不要有向下交叉的情形發生
Condition5 = Countif(Condition1 , N1) >= 1 and Countif(Condition2 , N1) >= 1 ;
Condition6 = Countif(Condition3 , N1) = 0 and Countif(Condition4 , N1) = 0 ;

如果我們希望兩種往下交叉的狀況之間,不要有向上交叉的情形發生
Condition7 = Countif(Condition3 , N2) >= 1 and Countif(Condition4 , N2) >= 1 ;
Condition8 = Countif(Condition1 , N2) = 0 and Countif(Condition2 , N2) = 0 ;

運用MRO 或 Countif 的概念就可以讓我們控制兩組金叉之間的距離作為交易控制項

狀況二. 短中長的均線參數怎麼應用在最佳化設定
我通常使用的方式會先觀察原策略想法中參數的對應關係,以本例而言 中期均線是短期均線的兩倍,長期均線是中期均線的 3倍,此時我們在系統參數與變數的設定可以改用以下方式
inputs: FastLen(10) ;
vars: SlowLen(0),MidLen(0) ;
MidLen = FastLen * 2;
SlowLen = MidLen * 3 ;
當 FastLen = 5 時 ,MidLen 就等於 5*2 = 10 ,SlowLen 就等於 10 * 3 = 30

若是我們希望有多一點變化時
inputs: FastLen(10),R1(2),R2(3);
vars: SlowLen(0),MidLen(0) ;
MidLen = IntPortion(FastLen*R1)+1 ;
SlowLen = IntPortion(MidLen*R2)+1 ;

綜合上述方式,我自己在測試過程的程式碼如下
input:EntryType(1) ;
inputs:NBarL(55),NBarS(35),TradeProfit(0.04),TradeStopLoss(0.04);
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);
inputs: FastLen(10),R1(1.2),R2(1.2),N1(2),N2(2);
vars: SlowLen(0),MidLen(0) ;

MP = MarketPosition ;

if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3 then isBalanceDay = True else isBalanceDay =False ;

PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;

MidLen = IntPortion(FastLen*R1)+1 ;
SlowLen = IntPortion(MidLen*R2)+1 ;

{Calculation of the necessary values, and assignment to variables}
Condition1 = Xaverage(Close,FastLen) Cross over Xaverage(Close,MidLen) ;
Condition2 = Xaverage(Close,FastLen) Cross over Xaverage(Close,SlowLen) ;
Condition3 = Xaverage(Close,FastLen) Cross under Xaverage(Close,MidLen) ;
Condition4 = Xaverage(Close,FastLen) Cross under Xaverage(Close,SlowLen) ;

Condition5 = Countif(Condition1 , N1) >= 1 and Countif(Condition2 , N1) >= 1 ;
Condition6 = Countif(Condition3 , N1) = 0 and Countif(Condition4 , N1) = 0 ;
Condition7 = Countif(Condition3 , N2) >= 1 and Countif(Condition4 , N2) >= 1 ;
Condition8 = Countif(Condition1 , N2) = 0 and Countif(Condition2 , N2) = 0 ;

if EntryType = 1 then Begin
if Condition5 and Condition6 then Buy next bar at market ;
if Condition7 and Condition8 then sell next bar at market ;
end;

if EntryType = 2 then Begin
if Condition2 and MRO(Condition1,N1,1) <> -1 then Buy next bar at market ;
if Condition4 and MRO(Condition3,N2,1) <> -1 then sell next bar at market ;
end;

if EntryType = 3 then Begin
if Condition2 and Countif(Condition1,N1) = 1 then Buy next bar at market ;
if Condition4 and Countif(Condition3,N2) = 1 then Sell next bar at market ;
end;

if EntryType = 4 then Begin
if Condition1 and Condition2 then Buy next bar at market ;
if Condition3 and Condition4 then Sell next bar at market ;
end;

If MP > 0 and (Condition3 or condition4 ) then ExitLong next bar at Lowest(Low,3) stop;
If MP < 0 and (Condition1 or condition2 ) then ExitShort next bar at Highest(High,3) stop;

if IsBalanceDay then setExitonClose ;
台指期 60 min K 留倉 交易期間 2004/11/1 ~ 2014/10/31 交易成本 1200

台指期 15 min K 留倉 交易期間 2004/11/1 ~ 2014/10/31 交易成本 1200

短、中、長週期均線彼此間交叉的順序,以及來回糾結過程中會影響的因子 都可以透過本篇內容的 MRO/Countif 兩個內建函數來作檢查,透過此方式應用,可以讓策略的邏輯更具彈性。

0 意見: