2014年3月13日 星期四

常見技術指標 VS 標準差應用 [程式碼]

EasyTrader ArtNo 121
在前面幾篇舊文中我們了解到統計學上的標準差 ,也將之應用在法人籌碼的變化,本篇也將此想法再與常用技術指標 RSI , MACD , XMA , ATR 作一些結合,並回測 2004/1 以來的日K資料
MACD



測試程式碼 [ 台指期 日K 2004/1 ~2014/2 交易成本 1200 ]
Inputs: XL1(40.6),XS1(38.7),NBar_LE(7),Frac_LMM(1.17),NBar_LX(6),NBar_SX(11);
Var:ATRL(0);
Var:UBuy(0),BuyStop(0);
Var:VarL1(0),VarS1(0),Cond_LE(false),Cond_SE(false);
ATRL = Average(TrueRange, NBar_LE);

{ Entry price }
UBuy = High;
VarL1 = StdDev(Macd(Close,12,26),10);
VarS1 = StdDev(Macd(Close,12,26),10);

Cond_LE = VarL1 <= XL1;
Cond_SE = VarS1 >= XS1;

If Cond_LE then begin
   Buy next bar at UBuy stop;
end;

If Cond_SE then begin
   Sell next bar at market;
end;

If MarketPosition > 0 then begin
   If BarsSinceEntry = 0 then begin
      BuyStop = EntryPrice - Frac_LMM * ATRL;
   end;
   If BarsSinceEntry >= NBar_LX then ExitLong next bar at market;
   ExitLong next bar at BuyStop stop;
end;

If MarketPosition < 0 then begin
   If BarsSinceEntry >= NBar_SX then ExitShort next bar at market;
end;
-----------------------------------------------------------------------------------------------------------------------

RSI



測試程式碼 [ 台指期 日K 2004/1 ~2014/2 交易成本 1200 ]
Inputs: XL1(7.76),NBar_LE(8),Frac_LE(0.88),NBar_LX(4),Frac_LMM (1.26),
NBar_SE (10),Frac_SE (1.06),Frac_SMM (3.26),NBar_SX (5);

Var:ATRL(0),ATRS (0);
Var:UBuy(0),USell(0),BuyStop(0),SellSTOP(0);
Var:VarL1(0),Cond_LE(false),Cond_SE(false);

ATRL = Average(TrueRange, NBar_LE);
ATRS = Average(TrueRange, NBar_SE);

UBuy = Close + Frac_LE * ATRL;
USell = Open - Frac_SE * ATRS;

VarL1 = StdDev(RSI(Close,10),5);
Cond_LE = VarL1 < XL1;
Cond_SE = true;

If Cond_LE then begin
   Buy next bar at UBuy stop;
end;

If Cond_SE then begin
   Sell next bar at USell stop;
end;

If MarketPosition > 0 then begin
   If BarsSinceEntry = 0 then begin
      BuyStop = EntryPrice - Frac_LMM * ATRL;
   end;
   If BarsSinceEntry >= NBar_LX then ExitLong next bar at market;
   ExitLong next bar at BuyStop stop;
end;

If MarketPosition < 0 then begin
   If BarsSinceEntry = 0 then begin
      SellSTOP = EntryPrice + Frac_SMM * ATRS;
   end;
   If BarsSinceEntry >= NBar_SX then ExitShort next bar at market;
   ExitShort("ExStop-S") next bar at SellSTOP stop;
end;
----------------------------------------------------------------------------------------------------------------------

ATR



測試程式碼 [ 台指期 日K 2004/1 ~2014/2 交易成本 1200 ]
Inputs: XL1(9.7),NBar_LE(2),Frac_LMM(3.2),Ratio_TL(4.3),TRPct_L (61),
NBar_SE(19),Frac_SE(1.35),Ratio_TS(0.82),TRPct_S (82),Frac_SMM(4.58);

Var:ATRL(0),ATRS(0);
Var:USell(0),BuyStop(0),NewBuyStop(0),Trigger_TL(false),SellSTOP(0),NewSellSTOP(0),Trigger_TS(false);
Var:VarL1(0),Cond_LE(false),Cond_SE(false);

ATRL = Average(TrueRange, NBar_LE);
ATRS = Average(TrueRange, NBar_SE);

USell = Close - Frac_SE * ATRS;
VarL1 = StdDev(AvgTrueRange(5),5);
Cond_LE = VarL1 <= XL1;
Cond_SE = true;

If Cond_LE then begin
   Buy next bar at market;
end;

If Cond_SE then begin
   Sell next bar at USell stop;
end;

If MarketPosition > 0 then begin
   If BarsSinceEntry = 0 then begin
      BuyStop = EntryPrice - Frac_LMM * ATRL;
      Trigger_TL = false;
   end;

   If Close - EntryPrice > Ratio_TL * ATRL then Trigger_TL = true;
   If Trigger_TL then begin
       NewBuyStop = EntryPrice + TRPct_L * (Close - EntryPrice)/100.;
       BuyStop = MaxList(BuyStop, NewBuyStop);
   end;
   ExitLong next bar at BuyStop stop;
end;

If MarketPosition < 0 then begin
   If BarsSinceEntry = 0 then begin
       SellSTOP = EntryPrice + Frac_SMM * ATRS;
       Trigger_TS = false;
   end;
   If EntryPrice - Close > Ratio_TS * ATRS then Trigger_TS = true;
   If Trigger_TS then begin
       NewSellSTOP = EntryPrice - TRPct_S * (EntryPrice - Close)/100.;
       SellSTOP = MinList(SellSTOP, NewSellSTOP);
   end;
   ExitShort next bar at SellSTOP stop;
end;
----------------------------------------------------------------------------------------------------------------------
RSI+MACD



----------------------------------------------------------------------------------------------------------------------
XMA+MACD



舊指標加上新創意 ,也是能找到新策略的方向 !!
MagicQS043_1

沒有留言:

張貼留言