2013年10月9日 星期三

紅買綠賣的交易策略 - 平均K線

EasyTrader ArtNo 017
讀者應記得在"變身的K棒"文章內提到了利用PlotPaintBar的功能可以讓K棒的顏色依據個人的獲利操作心法轉換成紅買綠賣的指標,近日在網路上看到一篇不同的平均K線計算方式,因此將幾個不同的計算方式作為交易策略的核心 ,並比較其差異(下圖為平均K線與傳統K線圖比較)



不同的平均K線的計算公式如下:(為了方便,就直接將筆者程式碼貼上)

1.近日網路上看到的,只有開盤及收盤價
if CurrentBar > 1 then
    NewOpen = ( ( Open[1] + High[1] + Low[1] + Close[1] ) / 4 + NewOpen[1] ) / 2
else NewOpen = Open ;

NewClose = (( Open + High + Low + Close ) / 4 + NewOpen + MaxList( High, NewOpen ) + Minlist( Low, NewOpen )) / 4 ;

2.標準的平均k線

MyClose = (Close + Open + High + Low )/4 ;
if CurrentBar > 1 then MyOpen = (MyOpen[1]+MyClose[1])/2 else MyOpen = Open;
MyHigh = High ;
MyLow = Low ;
NewOpen = MyOpen ;
NewClose = MyClose ;

3.書上看來的
RefOpen = Open ;
if CurrentBar > 1 then MyOpen =(RefOpen[1] + Close[1])/2 ;
MyClose = (Close + Open + High + Low )/4 ;
MyHigh = MaxList(High,MyOpen,MyClose) ;
MyLow = MinList(Low,MyOpen,MyClose) ;
NewOpen = MyOpen ;
NewClose = MyClose ;

4.自己編的(只有開盤及收盤價)NewOpen = (MaxList(Open,Close)+High)/2 ;
NewClose = (MinList(Open,Close)+Low)/2 ;

針對同一個進出場邏輯,將此四種不同計算方式作測試
基本設定: 5分K , 留倉策略 , 來回成本 1200 回測時間 2001~2013/8/30

結果如下

公式1




公式2(公式 3 結果雷同因為沒用到 High/Low)





公式4






從以上的結果只有公式1較佳,而從三個績效曲線看,不知道是否合成起來會比較好,因此修改了 NewClose的計算

公式 5 NewClose = 公式 1 , 2, 4的平均





未蒙其利先受其害,獲利沒增加,反而MDD增加, 是不是進場規則不合適 ? 動念之間改了個符號 , 哇! 怎會變化這麼大???





績效變好,MDD也下降,真是"柳暗花明又一村" ~~

範例程式碼
input: Type(1) ;
Vars:NewOpen(0),NewClose(0),MyClose(0),MyOpen(0),MyHigh(0),MyLow(0),RefOpen(0) ;
Vars:OpenA(0),CloseA(0),OpenB(0),CloseB(0),OpenC(0),CloseC(0),MaxClose(0),MinClose(0);

{公式1}
if Type = 1 then Begin
if CurrentBar > 1 then
    NewOpen = ( ( Open[1] + High[1] + Low[1] + Close[1] ) / 4 + NewOpen[1] ) / 2
else  NewOpen = Open ;

NewClose = (( Open + High + Low + Close ) / 4 + NewOpen + MaxList( High, NewOpen ) + Minlist( Low, NewOpen )) / 4 ;
end;

{公式2}
if Type = 2 then Begin
   MyClose = (Close + Open + High + Low )/4 ;
   if CurrentBar > 1 then MyOpen = (MyOpen[1]+MyClose[1])/2 else MyOpen = Open;
   MyHigh = High ;
   MyLow = Low ;
   NewOpen = MyOpen ;
   NewClose = MyClose ;
end;

{公式3}
if Type = 3 then Begin
   RefOpen = Open ;
   if CurrentBar > 1 then MyOpen =(RefOpen[1] + Close[1])/2 ;
   MyClose = (Close + Open + High + Low )/4 ;
   MyHigh = MaxList(High,MyOpen,MyClose) ;
   MyLow = MinList(Low,MyOpen,MyClose) ;
   NewOpen = MyOpen ;
   NewClose = MyClose ;
end;

{公式4}
if Type = 4 then Begin
   NewOpen = (MaxList(Open,Close)+High)/2 ;
   NewClose = (MinList(Open,Close)+Low)/2 ;
end;

{公式5}
if Type = 5 then Begin
    if CurrentBar > 1 then
       OpenA = ( ( Open[1] + High[1] + Low[1] + Close[1] ) / 4 + OpenA[1] ) / 2
    else OpenA = Open ;
    CloseA = (( Open + High + Low + Close ) / 4 + OpenA + MaxList( High, OpenA )
    + Minlist( Low, OpenA )) / 4 ;

    CloseB = (Close + Open + High + Low )/4 ;
    if CurrentBar > 1 then OpenB = (OpenB[1]+CloseB[1])/2 else OpenB = Open;

    MyHigh = High ;
    MyLow = Low ;
    NewOpen = OpenB ;
    NewClose = CloseB ;

    OpenC = (MaxList(Open,Close)+High)/2 ;
    CloseC = (MinList(Open,Close)+Low)/2 ;

    NewClose = (CloseA+CloseB+CloseC)/3 ;
    MinClose = MinList(CloseA,CloseB,CloseC);
    MaxClose = MaxList(CloseA,CloseB,CloseC);
end;

{交易策略}
inputs: TypPriceLen(5),NewCloseLen(8),StopLossRatio(25),BreakEvenRatio(15);
vars: MP(0),AvgCost(0),EntryCount(0),EntryHigh(0),EntryLow(0),StopLossLong(0),
StopLossShort(0),LongTrigger(0),ShortTrigger(0),AvgOpen(0),AVGTyp(0),AVGClose(0)
,LineCorss(-1),AvgMaxClose(0),AvgMinClose(0) ;

{ 變數設定 }
MP = MarketPosition ;
AvgCost = AvgEntryPrice ;
EntryCount = BarsSinceEntry ;

{ 進場後高低點 }
if MP <> 0 then begin{ in a postion }
    if EntryCount = 0 then begin{ entry bar }
       EntryHigh = High ;
       EntryLow = Low ;
    end else begin
       EntryHigh = MaxList( EntryHigh,High ) ;
       EntryLow = MinList( EntryLow,Low ) ;
    end ;
end ;

{停損設定}
StopLossLong = EntryPrice {AvgCost} * ( 1 - StopLossRatio * 0.01 ) ;
StopLossShort = EntryPrice {AvgCost} * ( 1 + StopLossRatio * 0.01 ) ;
LongTrigger = EntryPrice {AvgCost} * ( 1 + BreakEvenRatio * 0.01 ) ;
ShortTrigger = EntryPrice {AvgCost} * ( 1 - BreakEvenRatio * 0.01 ) ;

{ 計算幾個測試用變數 }
AVGTyp = XAverage( TypicalPrice,TypPriceLen) ;
AVGOpen = XAverage( NewOpen,TypPriceLen) ;
AVGClose = XAverage( NewClose, NewCloseLen ) ;
AvgMaxClose = XAverage( MaxClose, NewCloseLen ) ;
AvgMinClose = XAverage( MinClose, NewCloseLen ) ;

{進場規則設計}
if AVGTyp > AVGClose and Close > Open then LineCorss = 1
    else if AVGTyp < AVGClose and Close < Open then LineCorss = 0 ;

if CurrentBar > 1 then begin

{ 多單進場 }

   if OpenD(0) > CloseD(1) and Close > OpenD(0) and LineCorss = 1 and LineCorss[1] = 0
       then Buy ( "MQS01_L" ) next bar market ;

{ 空單進場 }

   if OpenD(0) < CloseD(1) and Close < OpenD(0) and LineCorss = 0 and LineCorss[1] = 1
       then Sell( "MQS01_S" ) next bar at market ;

{停損機制 }

   if MP = 1 then ExitLong ( "MQS01_LX" ) next bar at StopLossLong stop ;
   if MP = -1 then ExitShort( "MQS01_SX" ) next bar at StopLossShort stop ;

{ Breakeven exits }

   if MP = 1 and EntryHigh >= LongTrigger then
       ExitLong ( "MQS01_BEL" ) next bar at AvgCost stop ;
   if MP = -1 and EntryLow <= ShortTrigger then
      ExitShort ( "MQS01_BES" ) next bar at AvgCost stop ;

end ;



後記:
交易策略開發過程有時碰牆撞壁時,將概念轉個彎,就像連續劇內常聽到的台詞"代誌不是你所想的那樣",也會有意料不到的結果

(程式碼變更位置說明)
if AVGTyp < AVGClose and Close > Open then LineCorss = 1
   else if  AVGTyp > AVGClose and Close < Open then LineCorss = 0 ;

就是這個符號變動,好玩吧!!
MagicQS001

4 則留言: