2014年8月21日 星期四

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

EasyTrader ArtNo 196
跳空缺口代表的意義是商品價格的突然波動,跳空位置可以是在前一日最高最低點以外,也可以是在前一日振幅以內,本篇介紹的交易策略是以第一根K棒的收盤價當作參考位置
我們的假設:
A.如果市場是在長期上漲的趨勢,當開盤價低於前一天的最低價,則趨勢的方向會將此跳空修正回到原來的軌道。
B.同樣的,如果市場是在長期下降趨勢,當開盤價高於前一天的最高價,市場趨勢會嘗試修正此跳空,讓它回歸到下降軌道中。
但是,在上述假設A的第一根K棒收盤價高於昨日高點(趨勢續漲),我們以此根K棒的最低點 -3 作為趨勢反轉的多方防禦線,若被跌破則轉多為空。
同樣的,假設B第一根K棒收盤價低於昨日低點(趨勢續跌),我們以此根K棒的最高點 +3 作為趨勢反轉的空方防禦線,若被突破則轉空為多。


那麼趨勢如何去定義 ? 這裡使用了兩條指數平均線的方向來考量
系統參數與變數宣告
inputs:FastLen(3),SlowRatio(2),RangeL(4),RangeS(4),PctL(3),PctS(3);
vars:FastAvg(0),SlowAvg(0),StopPrice(0),BullTrend(false),BearTrend(True),
SellonSwingHigh(false),BuyonSwingLow(false);

進場準備
{ 分別計算快速/慢速指數平均線,快慢的比例值以 SlowRatio來定義 }
FastAvg = XAverage(Close,FastLen)  ;
SlowAvg = Xaverage(Close,FastLen*SlowRatio) ;

{ 市場趨勢判斷 }
BullTrend = FastAvg >= SlowAvg  ; {牛市 }
BearTrend = FastAvg < SlowAvg  ; {熊市 }

作多進場邏輯
1. 牛市且開盤第一根收盤價低於昨日低點時下根K棒開盤價進場作多
if BullTrend and Close < LowD(1) then Buy next bar at Market  ;

2. 熊市開盤第一根收盤價仍低於昨日低點時突破第一K棒最高點 + 3進場作多(反轉)
if BearTrend and Close < LowD(1)  then BuyonSwingLow = true ;
if BuyonSwingLow then Buy ("RevLow") next bar at High+3  stop ;

作空進場邏輯
1. 熊市且開盤第一根收盤價高於昨日高點時下根K棒開盤價進場作空
if BearlTrend and Close > HighD(1) then Sell next bar at Market  ;

2. 牛市開盤第一根收盤價高於昨日高點時跌破第一K棒最低點 -3進場作空(反轉)
if BullTrend and Close > HighD(1)  then SellonSwingHigh = true ;
if SellonSwingHigh then Sell ("RevHigh") next bar at Low-3  stop ;

進場後取消原條件
if MP = -1 then SellOnSwingHigh = false ;
if MP = 1 then BuyonSwingLow = false ;

{ 原始出場條件 }
if ExitType = 0 then Begin
進場後馬上先建立停損價

多單以最低點價格減去 4根振幅的平均值作為停損初始值
if MP = 1 and MP[1] <> 1 then StopPrice = Low-Average(Range,RangeL) ;

空單以最高點價格加上 4根振幅的平均值作為停損初始值
if MP = -1 and MP[1] <> -1 then StopPrice = High+Average(Range,RangeS) ;

多單在倉時,每經過一根K棒 ,則調整停損位置
if MP = 1 then Begin
ExitLong ("LX") next bar at StopPrice stop ;
if PctL > 0 then StopPrice = StopPrice + (Low-StopPrice)/PctL ;
end;

空單在倉時,每經過一根K棒 ,則調整停損位置
if MP = -1 then Begin
ExitShort ("SX") next bar at StopPrice stop ;
if PctS > 0 then StopPrice = StopPrice - (StopPrice-High)/PctS ;
end;
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 ExitType = 5 then Begin
{*******************************************************************
Description : ATR Trailing Stop Long Exit
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
{Inputs: ATRs_L(3);}
Variables: PosHigh(0), ATRVal_L(0);
ATRVal_L = AvgTrueRange(10) * ATRs_L;
If BarsSinceEntry = 0 Then PosHigh = High;
If MarketPosition = 1 Then Begin
If High > PosHigh Then PosHigh = High;
ExitLong ("ATR") Next Bar at PosHigh - ATRVal_L Stop;
End else ExitLong ("ATR eb") Next bar at High - ATRVal_L Stop;

{*******************************************************************
Description : ATR Trailing Stop Short Exit
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
{Inputs: ATRs_S(3);}
Variables: PosLow(0), ATRVal_S(0);
ATRVal_S = AvgTrueRange(10) * ATRs_S;

If BarsSinceEntry = 0 Then PosLow = Low;

If MarketPosition = -1 Then Begin
If Low < PosLow Then PosLow = Low;
ExitShort ("ATR_1") Next Bar at PosLow + ATRVal_S Stop;
End else ExitShort ("ATR_1 eb") Next bar at Low + ATRVal_S Stop;
end;

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

結論:市場的趨勢變化不是一成不變的,順勢操作的過程中也要有趨勢反轉的準備,當反轉訊號出現後就能適時的進場
MagicQS104

沒有留言:

張貼留言