2014年10月20日 星期一

●雙均線交易系統 [程式碼]

EasyTrader ArtNo 211
這套方法是由常用的移動平均穿越系統修改而來。單一移動平均線穿越系統的最大問題就是訊號反復。改進方法之一是利用兩條均線構建通道。
方法:兩條移動平均線,其中一條計算每天最高價的移動平均,另一條計算每天最低價的移動平均,兩者分別為通道的上限與下限。
交易訊號:唯有當收盤價高於通道上限才買進;唯有當收盤價低於通道下限才賣出。

除此之外,利用下列方法過濾交易訊號:今天的X天移動平均高於前一天的讀數,才接受買進訊號;今天的X天移動平均低於前一天的讀數,才接受賣出訊號。濾網使用的參數X,通常都設定等於移動平均長度(如採用20天的通道,則X=20)。

本案結合兩套系統,一個採用20天移動平均,另一個採用80天移動平均。唯有當兩套系統的訊號彼此一致,才接受進場訊號。至於出場訊號,如果今天的收盤價跌破80天期盤中低價的簡單移動平均,則結束多頭部位;如果今天的收盤價超過80天期盤中高價的簡單移動平均,則結束空頭部位。
~摘譯 [ QUANTITATIVE TRADING STRATEGIES]

此系統運用於期貨市場,績效很不錯有11年獲利,且夏普率、K比率都很高。股票市場稍差,但仍有7年獲利。
期貨市場:凈利:14,658,917 最大資金流失:-1,440,842 K比率:0.43 夏普率:0.95 突破相關:0.85 均線相關:0.81

測試程式碼
input:EntryType(3),ExitType(1) ;
input:NBarL(2),NBarS(2),TradeProfit(0.035),TradeStopLoss(0.035),ATRs_L(2),ATRs_S(2);
vars: IsBalanceDay(False),MP(0),PF(0),PL(0);

input:FastL(20),RatioL(4),FastS(20),RatioS(4) ;
Vars:SlowL(0),SlowS(0),AvgL1(0),AvgL2(0),AvgS1(0),AvgS2(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 ;

{ 計算長期均線 }
SlowL = IntPortion(FastL*RatioL) ;
SlowS = IntPortion(FastS*RatioS) ;

{ 雙均線高低點計算 }
AvgL1 = Average(High,FastL) ;
AvgL2 = Average(High,SlowL) ;
AvgS1 = Average(Low,FastS) ;
AvgS2 = Average(Low,SlowS) ;

{ 進場規則 + 動量濾網確認 }
Condition1 = Close > AvgL1 and Close > AvgL2 and Close > Close[FastL] ;
Condition2 = Close < AvgS1 and Close < AvgS2 and Close < Close[FastS] ;

{ 濾網 - 前一根K棒在均線通道下}
Condition3 = Close[1] < AvgL1 or Close[1] < AvgL2 ;
Condition4 = Close[1] > AvgS1 or Close[1] < AvgS2 ;

{ 原始策略進場規則 + 均線方向濾網確認 }
Condition5 = Close > AvgL1 and Close > AvgL2 and AvgL1 > AvgL1[1] ;
Condition6 = Close < AvgS1 and Close < AvgS2 and AvgL1 < AvgL1[1] ;


if EntryType = 1 then begin
if Close[1] < AvgL1 and Condition1 then Buy next bar at Market ;
if Close[1] > AvgS1 and Condition2 then Sell next bar at Market ;
end;

if EntryType = 2 then begin
if Close[1] < AvgL1 and Condition5 then Buy next bar at Market ;
if Close[1] > AvgS1 and Condition6 then Sell next bar at Market ;
end;

if EntryType = 3 then begin
if AvgL1 < AvgL2 and Condition1 and Condition3 then Buy next bar at Market ;
if AvgS1 > AvgS2 and Condition2 and Condition4 then Sell next bar at Market ;
end;

{ 原始出場規則 }
if ExitType = 6 then Begin
ExitLong next bar at SlowL stop ;
ExitShort next bar at SlowS stop ;
end;

{ 常用出場規則 }
if ExitType = 1 then SetStopLoss(PL * BigPointValue) ;

if ExitType = 2 then Begin
SetStopLoss(PL * BigPointValue) ;
setProfitTarget(PF * BigPointValue) ;
end;

if ExitType = 3 then Begin
if MP > 0 and BarsSinceEntry = NBarL then ExitLong next bar at Market ;
if MP < 0 and BarsSinceEntry = NBarS then ExitShort next bar at Market ;
end;

if ExitType = 4 then Begin
SetStopLoss(PL * BigPointValue) ;
setProfitTarget(PF * BigPointValue) ;
if MP > 0 and BarsSinceEntry = NBarL then {Sell } ExitLong next bar at Market ;
if MP < 0 and BarsSinceEntry = NBarS then {Buy} ExitShort next bar at Market ;
end;

if IsBalanceDay then setExitonClose ;
台指期 60 min K 多空留倉 交易週期 2004/8/31~ 2014/8/29 交易成本 1200


台指期 60 min K 多空留倉 交易週期 2004/8/31~ 2014/8/29 交易成本 1200

6 留言:

匿名 提到...

站長您好:不好意思 我是程式交易初學者可以請您幫忙把10/20 雙均線交易系統 的程式碼 修改成 multicharts 使用嗎?還有 指標碼 謝謝您 感謝

匿名 提到...

不好意思 與站長連絡 傳不出去 敝姓王

WEN 提到...

TS的Code改成MC很簡單的,大概只要1分鐘以內,請自行google一下囉。

匿名 提到...

謝謝,改好了!

匿名 提到...

站長您好:請問 Condition4 = Close[1] > AvgS1 or Close[1] < AvgS2 ;
是否該改為 Condition4 = Close[1] > AvgS1 or Close[1] > AvgS2 ;
謝謝您

匿名 提到...

請教不知此法是否適合日內短分k不留倉的當沖呢?

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------