2014年4月10日 星期四

TS 內建真實區間波動進出場策略 [程式碼]

EasyTrader ArtNo 136
依據商品價格真實區間的波動作進場依據的策略,在很多網路上的資料都有介紹,本篇單純將 TS內建的 ATR進場與出場邏輯組合作一歷史回測參考
inputs: ExitType(1),TradeProfit(0.05),TradeStopLoss(0.03),NBarL(2),NBarS(2);
vars: IsBalanceDay(False),MP(0),PF(0),PL(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 ;

{ 內建 ATR 進場 }
{*******************************************************************
Description : Volatility Long Entry
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
Inputs: VtyPercent_LE(.75),BarLE(5);
If MarketPosition <> 1 Then Buy ("Vty_LE") Next Bar at Close + (VtyPercent_LE * AvgTrueRange(BarLE)) Stop;

{*******************************************************************
Description : Volatility Short Entry
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}

Inputs: VtyPercent_SE(.75),BarSE(5);
If MarketPosition <> -1 Then Sell ("Vty_SE") Next Bar at Close - (VtyPercent_SE * AvgTrueRange(BarSE)) Stop;

{ 內建 ATR 出場 }
if ExitType = 1 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;

{ 內建固定K棒離場 }
if ExitType = 2 then Begin

{*******************************************************************
Description : Fixed Bar Exit Long Exit
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
Inputs: Length_LX(5);
If BarsSinceEntry = Length_LX Then
ExitLong ("FxBr_LX") This Bar on Close;

{*******************************************************************
Description : Fixed Bar Exit (Short Exit)
Provided By : Omega Research, Inc. (c) Copyright 1999
********************************************************************}
Inputs: Length_SX(5);
If BarsSinceEntry = Length_SX Then
ExitShort ("FxBr_SX") This Bar on Close;

end;

if IsBalanceDay then setExitonClose ;

[台指期 日K 留倉 2004/3/31 ~ 2014/3/31 交易成本 1200 ]

ATR 追蹤出場





另一個為固定K棒數離場


基本上大的趨勢都能掌握住
MagicQS058

沒有留言:

張貼留言