2014年5月29日 星期四

波動叢聚(Volatility Cluster)之交易策略 [程式碼]

EasyTrader ArtNo 158
在金融市場裡,波動率/價格/與交易規模常常會出現一些叢聚現象(clustering),例如從主觀看盤的即時資料跳動中,期指的數字跳動在 2, 5 ,8三個位置是一個很容易出現價格來回振盪的中心,而從收盤價最後一個數字來看,0,5是最常出現的數字,若以末2位數字來看則以 80,40,50,90出現的次數最多, 若從 K棒振幅來看, 如同 Larry Williams 所提價差循環一般 ,大價差出現開始總會出現較多的大價差在一起的現象 ( 趨勢出現) ,而小價差開始群聚時,那一段期間的 K棒的振幅都不大,如圖


程式碼
input:EntryType(1),ExitType(1),TradeProfit(0.05),TradeStopLoss(0.015),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 ;

策略的核心如下
Inputs: Deviations(2), Length(18),BarLength(3);
Variables: CloseToClose(0), CTCDeviations(0), RangeDeviations(0), BigVolatility(False);

{Setup calculations}
{ 計算相鄰兩根 K棒收盤價的價差絕對值 與 N 個標準差界限}
CloseToClose = AbsValue(Close - Close[1]);
CTCDeviations = Average(CloseToClose, Length) + StdDev(CloseToClose, Length) * Deviations;

{ 計算同樣期間內 K棒振幅的 N 個標準差界限}
RangeDeviations = Average(Range, Length) + StdDev(Range, Length) * Deviations;

{ 當K棒價差 或振幅超過上下界限 }
BigVolatility = CloseToClose > CTCDeviations[1] OR Range > RangeDeviations[1];

{Entry Orders}
{ 條件成立時以收盤價 +/- 近 M根平均振幅作進場點位 }

If BigVolatility then Begin
Buy next bar at Close + Average(Range, BarLength)[1] Stop;
Sell next bar at Close - Average(Range, BarLength)[1] 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
   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;

   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;

{ Exit at first Profitable Open }
if ExitType = 6 then Begin
   Inputs: BaseProfit(0), Delay(0);
  Variables: NextBarOpen(0);
   NextBarOpen = Open of next bar;
   If BarsSinceEntry > NBarL Then Begin
      If MP = 1 AND NextBarOpen > EntryPrice + (BaseProfit / BigPointValue) Then
      ExitLong ("Long Profit") next bar at Market;
   end;

   If BarsSinceEntry > NBarS Then Begin
      If MP = -1 AND NextBarOpen < EntryPrice - (BaseProfit / BigPointValue) Then
      ExitShort ("Short Profit") next bar at Market;
   End;
END;

{ ATR Volatility Stop }

if ExitType = 7 then Begin
   Inputs: VolatilityATRs(2), ATRLength(10);
   Variable: ATRVal(0);
   ATRVal = AvgTrueRange(ATRLength) * VolatilityATRs;
   If MP = 1 Then ExitLong Next Bar at EntryPrice - ATRVal Stop;
   If MP = -1 Then ExitShort Next Bar at EntryPrice + ATRVal Stop;
End;

if IsBalanceDay then setExitonClose ;

台指期 日K / 60 分K 留倉測試 2004/5/25~2014/5/22 交易成本 1200
下表藍色底為 日K測試 , 白色底為 60 分K , 最後一欄為程式碼中不同的出場方式
Type A 不加任何出場條件 , 每組作 200次隨機參數回測 [這些測試只花了約 90分鐘]
Type B ~ Type H 分別對應 ExitType 1 ~ 7 ,並挑出各組績效前2名放入下表中







1.日K表現比 60分K 好
2.PF 值都相對的高
3.若是操作股票,應該也是不錯的策略元素

MagicQS075

沒有留言:

張貼留言