EasyTrader ArtNo 229
Perry Kaufman在他 1995年的著作 " Smarter Trading"首次提出了效率比值 (ER)。它是一種趨勢強度的衡量,用以調整市場的波動程度.它的計算方法如下Efficiency Ratio = direction/volatility
ER = (N 期間內價格總變化的絕對值)/ (N 期間內個別價格變化的絕對值)
如果個別價格變化都是正值 (或負值),那麼 ER 將等於 1.0,這代表了強勁的趨勢行情。然而,如果有正面和負面價格變動造成相互的抵消,代表公式中的分子將會縮小,ER 將會減少。ER 反映價格走勢的一致性。ER 的所有值將都介於 0.0 ~ 1.0
另外一種計算方式為
ER = (N 期間內價格總變化)/ (N 期間內個別價格變化的絕對值)
此時 ER值的變化範圍就會落在 -1.0 ~ 1.0 之間 , 分別代表漲勢與跌勢的方向 , 其中 0 代表無方向性的波動
指標程式碼
input:FastLen(10),UpBand(0.6),DnBand(0.6) ;
Vars:FastDiff(0),FastSignal(0),FastNoise(0),FastER(0) ;
FastDiff = AbsValue(Close - Close[1]) ;
IF CurrentBar > FastLen Then Begin
FastSignal = Close - Close[FastLen];
FastNoise = Summation(FastDiff, FastLen);
if FastNoise <> 0 then FastER = FastSignal / FastNoise;
End;
Plot1(FastER,"FastER") ;
Plot2(UpBand,"UB") ;
Plot3(-DnBand,"DB") ;
ER 值為 0 時表示大量的隨機運動與不明確的趨勢。Kaufman 的 ER 策略可用於在 15 分鐘的時間內或更高的時間框架,Kaufman對於較低的時間框架不建議使用!至於進場的時間點則是以 ER 值大於 0.6 時作多買進 ;ER 值小於 0.6 時作空賣出
注意: 不同的商品使用的參數可能不同
測試程式碼
input:EntryType(2),ExitType(5) ;
input:NBarL(28),NBarS(3),TradeProfit(0.035),TradeStopLoss(0.041),ATRs_L(12.6),ATRs_S(4.8);
vars:IsBalanceDay(False),MP(0),PF(0),PL(0),HLRange(100);
input:FastLen(10),Ratio(2),UpBand(0.6),DnBand(-0.6) ;
Vars:SlowLen(0),FastDiff(0),FastSignal(0),FastNoise(0),FastER(0),SlowDiff(0),SlowSignal(0),SlowNoise(0),SlowER(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 ;
{ 快速效率比計算 }
FastDiff = AbsValue(Close - Close[1]) ;
IF CurrentBar > FastLen Then Begin
FastSignal = Close - Close[FastLen];
FastNoise = Summation(FastDiff, FastLen);
if FastNoise <> 0 then FastER = FastSignal / FastNoise;
End;
{ 慢速效率比計算 }
SlowLen = IntPortion(FastLen*Ratio) ;
SlowDiff = AbsValue(Close - Close[1]) ;
IF CurrentBar > SlowLen Then Begin
SlowSignal = Close - Close[SlowLen];
SlowNoise = Summation(SlowDiff, SlowLen);
if SlowNoise <> 0 then SlowER = SlowSignal / SlowNoise;
End;
if DAYofMonth(Date) > 14 and DAYofMonth(Date) < 22 and DAYofWeek(Date)= 3 then isBalanceDay = True else isBalanceDay =False ;
PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;
{ 快速效率比計算 }
FastDiff = AbsValue(Close - Close[1]) ;
IF CurrentBar > FastLen Then Begin
FastSignal = Close - Close[FastLen];
FastNoise = Summation(FastDiff, FastLen);
if FastNoise <> 0 then FastER = FastSignal / FastNoise;
End;
{ 慢速效率比計算 }
SlowLen = IntPortion(FastLen*Ratio) ;
SlowDiff = AbsValue(Close - Close[1]) ;
IF CurrentBar > SlowLen Then Begin
SlowSignal = Close - Close[SlowLen];
SlowNoise = Summation(SlowDiff, SlowLen);
if SlowNoise <> 0 then SlowER = SlowSignal / SlowNoise;
End;
if EntryType = 1 then begin
if FastER Cross over UpBand then Buy next bar at Highest(High,3) stop ;
if FastER Cross under DnBand then Sell next bar at Lowest(Low,3) stop ;
end;
{ 第二種進場 - 快速比值與慢速比值交叉 - EasyTrader's Rule}
if EntryType = 2 then begin
if EntryType = 2 then begin
if FastER > UpBand and FastER Cross over SlowER then Buy next bar at Highest(High,3) stop ;
if FastER < DnBand and FastER Cross under SlowER then Sell next bar at Lowest(Low,3) stop ;
end;
if FastER < DnBand and FastER Cross under SlowER then Sell next bar at Lowest(Low,3) 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 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 ;
Kaufman's rule 讀者可以自行測試,以下圖表為第二種進場方式的測試
台指期 60 分K 多空留倉 交易週期 2004/11/1~ 2014/10/31 交易成本 1200
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 ;
Kaufman's rule 讀者可以自行測試,以下圖表為第二種進場方式的測試
台指期 60 分K 多空留倉 交易週期 2004/11/1~ 2014/10/31 交易成本 1200
MagicQS140
沒有留言:
張貼留言