2014年7月16日 星期三

外期 - 恒生指數期貨當沖與波段交易策略 [程式碼]

EasyTrader ArtNo 179
恒生指數期貨及期權,是香港恒生指數的股票指數期貨投資產品,同時是香港交易所衍生產品市場內最主要的產品,由恒生指數期貨(簡稱恆指期貨)及恒生指數期權(簡稱恆指期權)組成,而期權可再細分為認購期權及認售期權兩類。

歷史:恒生指數期貨於1986年5月推出,而恒生指數期權則於1993年3月推出。2000年10月9日及2002年11月18日,香港交易所先後推出小型恒生指數期貨合約及小型恒生指數期權,金額均為原合約之兩成。

規則:每張恒生指數期貨合約價值等於恆生指數之點數乘以50港元(例如當恒生指數為20,000點時,恒生指數期貨合約價值為一百萬港元),每一個點數的升跌代表合約價值50港元的升跌。
恒生指數期貨的交易月份分別為現月(又稱即月)、下月及最近的兩個季月;短期期權的合約月份是現月、下兩個月及之後的三個季月;長期期權則是之後的五個6月及12月合約月份。

結算當月最後第二個股市交易日是為最後交易日,因為結算價以當日每5分鐘報價的平均價為準,所以被稱為「期指結算日」。實際的結算工作則於最後交易日之翌日進行。

交易時段
由9:15-12:00及 下午1:00-4:15,由2013年4月8日起,加設期貨夜市,由下午5時至晚上11時。從2014年11月3日起,夜市期貨收市時間延至晚上11時45分。



有了商品基本資料,接下來作好時間週期,交易成本設定,還有輸入績效/MDD/交易次數的期望值,看看人工智慧策略產生器幫我們輸出的程式碼是什麼樣的組合囉 !

10 min K 當沖程式碼
{ ***** System Code Start here *****}

{ Public inputs }
Inputs:LuckyNet(0),IntraDay(0), TradeStopLoss(0.031),TradeProfit(0.039) ;
Inputs:TradeInday(1),Time1SW(1),Time1B(945),Time1E(1200) ;
Inputs:Time2SW(0),Time2B(1300),Time2E(1430),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(1) ;
inputs:Frac_LMM(1.81),NBar_LM(2),Frac_SMM(5.25),NBar_SM(8) ;

{ Public Variables }
Vars:Cond_LE(false),Cond_SE(false),CondNet_L(false),CondNet_S(false),
UBuy(0),USell(0),BuyStop(0),SellStop(0),BuyStopA(0),SellStopA(0),
NewBuyStopA(0),NewSellStopA(0),PL(0),PS(0),LastTradeDay(false) ;
Vars:TimeOK1(true),TimeOK2(true),TimeOK3(true),PosSW(true) ;

{ BuyMode Setup }
inputs:Bar_L1(36),NBar_LE(7),Frac_LE(0.78) ;

{ SellMode Setup }
inputs:Bar_S1(9),NBar_SE(7),Frac_SE(4.44) ;

{ Exit Long Position Setup }

{ Exit Short Position Setup }
inputs:TRLen_S(4),TRPct_S(91),Ratio_TS(0.77),NBar_SX(99) ;

{ BuyMode Variable Setup }
Vars:LE_ATR(0) ;

{ SellMode Variable Setup }
Vars:SE_ATR(0) ;

{ Exit Long Position Variable Setup }
Vars:LMM_ATR(0) ;

{ Exit Short Position Variable Setup }
Vars:TS_ATR(0),Trigger_TSA(false) ;
Vars:SMM_ATR(0) ;

{ ***** LastTradeDay ***** }
LastTradeDay = _MagicQS268_LTD ;

{ initial profit and loss }
if MarketPosition = 0 then begin
   PL = AvgPrice*TradeProfit ;
   PS = AvgPrice*TradeStopLoss ;
end ;

{ ATR calculate for code }
LMM_ATR = AvgTrueRange(NBar_LM);
LE_ATR = AvgTrueRange(NBar_LE);
SMM_ATR = AvgTrueRange(NBar_SM);
SE_ATR = AvgTrueRange(NBar_SE);
TS_ATR = AvgTrueRange(TRLen_S);

{ Entry and Exit prices }
UBuy = Highest(Close, Bar_L1) + Frac_LE * LE_ATR ;
USell = Highest(Low, Bar_S1) - Frac_SE * SE_ATR ;

{ Long and Short Entry Condition Setup }
Cond_LE = DayofWeek(Date) <> 7 ;
Cond_SE = MOD(DayofMonth(Date),8) = 3 ;

{ Combine Trade Number in day }
Cond_LE = Cond_LE and EntriesToday(date) <= TradeInDay ;
Cond_SE = Cond_SE and EntriesToday(date) <= TradeInDay ;

{ Combine Trade time zone in day }
if Time1SW = 0 then TimeOK1 = true else TimeOK1 = (time >= Time1B and time <= Time1E) ;

Cond_LE = Cond_LE and TimeOK1 ;
Cond_SE = Cond_SE and TimeOK1 ;

{ Check Position status for entry}
if PositionSW = 0 then PosSW = true else PosSW = (MarketPosition = 0) ;
Cond_LE = Cond_LE and PosSW ;
Cond_SE = Cond_SE and PosSW ;

{ Entry Long orders }
if Cond_LE then Buy next bar at UBuy Stop ;

{ Entry Short orders }
if Cond_SE then SellShort next bar at USell Stop ;

{ Exit orders, long trades }
If MarketPosition > 0 then begin
   PL = EntryPrice(0)* TradeProfit;
   PS = EntryPrice(0)* TradeStopLoss ;
   If BarsSinceEntry = 0 then begin
      BuyStop = EntryPrice - Frac_LMM * LMM_ATR;
   end;
   SetStopLoss(PS * BigPointValue) ;
end;

{ Exit orders, short trades }
If MarketPosition < 0 then begin
   PL = EntryPrice(0)* TradeProfit;
   PS = EntryPrice(0)* TradeStopLoss ;
   If BarsSinceEntry = 0 then begin
      SellStop = EntryPrice + Frac_SMM * SMM_ATR;
      SellStopA = 0 ;
      Trigger_TSA = false;
   end;

   If EntryPrice - Close > Ratio_TS * TS_ATR then Trigger_TSA = true;
   If Trigger_TSA then begin
       NewSellStopA = EntryPrice - TRPct_S * (EntryPrice - Close)/100.;
       SellStopA = MinList(SellStopA, NewSellStopA);
   end;

    if BarsSinceEntry >= NBar_SX then BuytoCover next bar at Market ;
    If Trigger_TSA then BuytoCover next bar at SellStopA stop;
end;

if IntraDay = 0 then SetExitonClose
else if IntraDay = 1 and LastTradeDay then SetExitonClose ;

{***************** End of Strategy ************}

恆生指數期貨 10 分K 當沖 交易週期 2008/01/01 ~ 2014/2/28 交易成本 港幣 300 元





45 min K 波段程式碼
{ ***** System Code Start here *****}

{ Public inputs }
Inputs:LuckyNet(0),IntraDay(2), TradeStopLoss(0.041),TradeProfit(0.028) ;
Inputs:TradeInday(1),Time1SW(1),Time1B(915),Time1E(1200) ;
Inputs:Time2SW(0),Time2B(1300),Time2E(1430),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(1) ;
inputs:Frac_LMM(2.32),NBar_LM(17),Frac_SMM(2.85),NBar_SM(7) ;

{ Public Variables }
Vars:Cond_LE(false),Cond_SE(false),CondNet_L(false),CondNet_S(false),
UBuy(0),USell(0),BuyStop(0),SellStop(0),BuyStopA(0),SellStopA(0),
NewBuyStopA(0),NewSellStopA(0),PL(0),PS(0),LastTradeDay(false) ;
Vars:TimeOK1(true),TimeOK2(true),TimeOK3(true),PosSW(true) ;

{ BuyMode Setup }
inputs:NBar_LE(1),Frac_LE(1.47) ;

{ SellMode Setup }

{ Exit Long Position Setup }
inputs:TRLen_L(1),TRPct_L(79),Ratio_TL(4.54) ;

{ Exit Short Position Setup }
inputs:TRLen_S(6),TRPct_S(86),Ratio_TS(3.19),NBar_SX(51) ;

{ BuyMode Variable Setup }
Vars:LE_ATR(0) ;

{ SellMode Variable Setup }

{ Exit Long Position Variable Setup }
Vars:TL_ATR(0),Trigger_TLA(false) ;
Vars:LMM_ATR(0) ;

{ Exit Short Position Variable Setup }
Vars:TS_ATR(0),Trigger_TSA(false) ;
Vars:SMM_ATR(0) ;

{ ***** LastTradeDay ***** }

LastTradeDay = _MagicQS268_LTD ;

{ initial profit and loss }
if MarketPosition = 0 then begin
   PL = AvgPrice*TradeProfit ;
   PS = AvgPrice*TradeStopLoss ;
end ;

{ ATR calculate for code }
LMM_ATR = AvgTrueRange(NBar_LM);
LE_ATR = AvgTrueRange(NBar_LE);
TL_ATR = AvgTrueRange(TRLen_L);
SMM_ATR = AvgTrueRange(NBar_SM);
TS_ATR = AvgTrueRange(TRLen_S);

{ Entry and Exit prices }
UBuy = OpenD(0) + Frac_LE * LE_ATR ;
USell = Low ;

{ Long and Short Entry Condition Setup }
Cond_LE = MOD(DayofMonth(Date),2) = 1 ;
Cond_SE = DayofWeek(Date) <> 7 ;

{ Combine Trade Number in day }
Cond_LE = Cond_LE and EntriesToday(date) <= TradeInDay ;
Cond_SE = Cond_SE and EntriesToday(date) <= TradeInDay ;

{ Combine Trade time zone in day }
if Time1SW = 0 then TimeOK1 = true else TimeOK1 = (time >= Time1B and time <= Time1E) ;

Cond_LE = Cond_LE and TimeOK1 ;
Cond_SE = Cond_SE and TimeOK1 ;

{ Check Position status for entry}
if PositionSW = 0 then PosSW = true else PosSW = (MarketPosition = 0) ;
Cond_LE = Cond_LE and PosSW ;
Cond_SE = Cond_SE and PosSW ;

{ Entry Long orders }
if Cond_LE then Buy next bar at UBuy Stop ;

{ Entry Short orders }
if Cond_SE then SellShort next bar at USell Stop ;

{ Exit orders, long trades }
If MarketPosition > 0 then begin
   PL = EntryPrice(0)* TradeProfit;
   PS = EntryPrice(0)* TradeStopLoss ;
   If BarsSinceEntry = 0 then begin
      BuyStop = EntryPrice - Frac_LMM * LMM_ATR;
      BuyStopA = 0;
      Trigger_TLA = false;
   end;
   If Close - EntryPrice > Ratio_TL * TL_ATR then Trigger_TLA = true;

   If Trigger_TLA then begin
       NewBuyStopA = EntryPrice + TRPct_L * (Close - EntryPrice)/100.;
       BuyStopA = MaxList(BuyStopA, NewBuyStopA);
   end;

   If Trigger_TLA then Sell next bar at BuyStopA stop;
end;

{ Exit orders, short trades }

If MarketPosition < 0 then begin
   PL = EntryPrice(0)* TradeProfit;
   PS = EntryPrice(0)* TradeStopLoss ;
   If BarsSinceEntry = 0 then begin
       SellStop = EntryPrice + Frac_SMM * SMM_ATR;
       SellStopA = 0 ;
       Trigger_TSA = false;
  end;

   If EntryPrice - Close > Ratio_TS * TS_ATR then  Trigger_TSA = true;
   If Trigger_TSA then begin 
       NewSellStopA = EntryPrice - TRPct_S * (EntryPrice - Close)/100.;
       SellStopA = MinList(SellStopA, NewSellStopA);
   end;

   if BarsSinceEntry >= NBar_SX then BuytoCover next bar at Market ;
   BuytoCover next bar at SellStop stop;
   If Trigger_TSA then BuytoCover next bar at SellStopA stop;
end;

if IntraDay = 0 then SetExitonClose
else if IntraDay = 1 and LastTradeDay then SetExitonClose ;

{***************** End of Strategy ************}

恆生指數期貨 45 分K留倉 交易週期 2008/01/01 ~ 2014/2/28 交易成本 港幣 300 元





沒有留言:

張貼留言