2016年1月21日 星期四

開發商品的交易系統 - 基礎篇 [58]

EasyTrader ArtNo 284
本篇文章主要是利用隨機指標(KD)交叉與 MACD 交叉來尋找作為交易機會的進場點。

MACD的英文全名為Moving Average Convergence and Divergence,其原理仍是利用快速與慢速兩條指數平滑移動平均線,計算兩者間的差離值(DIF)再利用差離值與差離值平均值(DEM)的聚合與分散的現象,藉以研判股市或個股的買進或賣出時機,而利用 OSC = DIF-DEM 的柱狀圖在零軸上下來協助判斷多空的動能,找出股價真正趨勢方向。
KD隨機指標 (StochasticLine),為喬治.藍恩(George Lane)發表,是相當實用的技術分析方法,隨機指標融合了相對強弱指標RSI 超買超賣現象及移動平均線的觀念.本質上是一個隨機波動的觀念,可協助掌握短中期行情走勢。K值與D值介於0至100間,80以上為超買區,20以下為超賣區,50為多空均衡點。

當隨機指標上升而超過上方的基準線,這代表行情進入超買狀態。超買意味著過高,可能拉回。當隨機指標下降而超過下方的基準線,這代表行情進入超賣狀態。超賣意味著過低,可能回升。

技術指標MACD and KD @ 奇正

兩種指標同時運用是否也能幫助我們找到好的進場點呢? 一起來試試看 !

{系統參數與變數} 
input:EntryType(0),ExitType(0);
inputs:NBarL(5),NBarS(5),TradeProfit(0.035),TradeStopLoss(0.035),ATRs_L(8),ATRs_S(8.);
vars:IsBalanceDay(False),MP(0),PF(0),PL(0);
inputs:KDLen1(9),KDLen2(9),FastLen(12),Ratio(2),HB(50),LB(50),HighBar(8),LowBar(11) ;
vars:SlowLen(0),AvgLen(0),Kvalue1(0),Dvalue1(0),Kvalue2(0),Dvalue2(0),Dif(0),Dem(0),Histgram(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 ;

{ 利用變數來調整 MACD 參數間的變化以取代 MACD 原始參數 12,26,9 }
SlowLen = IntPortion(FastLen * Ratio) ;
AvgLen = IntPortion(SlowLen/3) ;

Dif = Macd(Close,FastLen,SlowLen) ;
Dem = Xaverage(Dif,AvgLen) ;
Histgram = Dif-Dem ;

{ 計算多空不同的KD值 }
Kvalue1 = SlowK(KDLen1) ;
DValue1 = SlowD(KDLen1) ;
Kvalue2 = SlowK(KDLen2) ;
DValue2 = SlowD(KDLen2) ;

{ 設立KD 交叉條件變數 }
Condition1 = KValue1 Cross over DValue1 ;
Condition2 = KValue2 Cross under DValue2 ;

{ 設立柱狀圖穿越零軸條件變數 }
Condition3 = Histgram Cross over 0 ;
Condition4 = Histgram Cross under 0 ;

{ 因為 KD 交叉 與 MACD 柱圖穿越零軸不容易在同一根K棒中發生 
所以利用 Countif 函數來檢查兩者在 N根K棒中之內皆有發生即可}

{ 作多 : N根K棒期間出現 KD金叉 且 D 值小於界線 且 柱圖向上穿越零軸 }if MP <> 1 and Countif(Condition1 and Dvalue1 < LB,NBarL) > 0 and Countif(Condition3,NBarL)> 0 then Buy next bar at Highest(High,HighBar) stop ;

{ 作空 : N根K棒期間出現 KD死叉 且 D 值大於界線 且 柱圖向下穿越零軸 }
if MP <> -1 and Countif(Condition2 and Dvalue2 > HB,NBarS) > 0 and Countif(Condition4,NBarS)> 0 then Sell next bar at Lowest(Low,LowBar) stop;

{ 以下為常用出場方式 }
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 or date = 1150224 then setExitonClose ;
台指期 30 min K 多空留倉 交易週期 2005/6/30~ 2015/6/30 交易成本 1200
MagicQS192

2 則留言:

  1. 这个策略作商品是亏的。

    回覆刪除
  2. 程序代码主要是提供策略元素的概念与想法,并将参数调整后的绩效贴出来,实际应用参数要请读者自行测试

    回覆刪除