2014年7月7日 星期一

外期 - 印度指數期貨交易策略 [程式碼]

EasyTrader ArtNo 175
寫了一段時間有關台指期的交易策略 , 近期看到蠻多外期商品相關的介紹,因此假日也先從這些文章作一些功課 , 首先上場的是友版 Ray's Blog 介紹的國外期貨商品開發: 印度指數期貨(SGX) ,內容很清楚也提供了歷史資料下載 ,還有一段簡潔的交易策略 ! 就請讀者自行參照


本篇測試主要的交易時段在 QM的設定為 11:45 ~18:15 ,因此商品的 K棒圖表顯示區間也是只顯示這段時間的 K棒

以下的程式碼由人工智慧策略產生器自動輸出
測試程式碼( 30 分K)
{ ***** System Code Start here *****}
{ Public inputs }
Inputs:LuckyNet(0),IntraDay(1), TradeStopLoss(0.051),TradeProfit(0.018) ;
Inputs:TradeInday(0),Time1SW(1),Time1B(1245),Time1E(1745) ;
Inputs:Time2SW(0),Time2B(900),Time2E(1300),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(1) ;
inputs:Frac_LMM(4.87),NBar_LM(11),Frac_SMM(3.03),NBar_SM(18) ;

{ 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(4) ;

{ SellMode Setup }

{ Exit Long Position Setup }
inputs:NBar_LX(90) ;

{ Exit Short Position Setup }
inputs:TRLen_S(19),TRPct_S(45),Ratio_TS(3.64) ;

{ BuyMode Variable Setup }

{ SellMode Variable Setup }

{ 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_IN ;

{ 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);
SMM_ATR = AvgTrueRange(NBar_SM);
TS_ATR = AvgTrueRange(TRLen_S);

{ Entry and Exit prices }
UBuy = Highest(High,Bar_L1) ;
USell = Open ;

{ Long and Short Entry Condition Setup }
Cond_LE = DayofMonth(Date) > 21 ;
Cond_SE = MOD(DayofMonth(Date),5) = 2 ;

{ 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) ;
if Time2SW = 0 then TimeOK2 = true else TimeOK2 = (time >= Time2B and time <= Time2E) ;
if Time3SW = 0 then TimeOK3 = true else TimeOK3 = (time >= Time3B and time <= Time3E) ;

if Time1SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK1 ;
   Cond_SE = Cond_SE and TimeOK1 ;
end ;

if Time2SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK2 ;
   Cond_SE = Cond_SE and TimeOK2 ;
end ;

if Time3SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK3 ;
   Cond_SE = Cond_SE and TimeOK3 ;
end ;

{ 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) ;
   if BarsSinceEntry >= NBar_LX then Sell next bar at Market ;
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 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 ************}

印度指數期貨30分K 留倉 2008/3/24 ~ 2013/12/31 交易成本 8 美元 ,圖表績效單位 美元





測試程式碼(60 分K)
{ ***** System Code Start here *****}
{ Public inputs }
Inputs:LuckyNet(0),IntraDay(1), TradeStopLoss(0.014),TradeProfit(0.045) ;
Inputs:TradeInday(0),Time1SW(1),Time1B(1245),Time1E(1745) ;
Inputs:Time2SW(0),Time2B(900),Time2E(1300),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(0) ;
inputs:Frac_LMM(5.24),NBar_LM(5),Frac_SMM(3.72),NBar_SM(19) ;

{ 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(14),Frac_LE(3.17) ;

{ SellMode Setup }
inputs:Bar_S1(29),NBar_SE(11),Frac_SE(2.99) ;

{ Exit Long Position Setup }
{ Exit Short Position Setup }
inputs:NBar_SX(65),Bar_S2(40) ;

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

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

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

{ Exit Short Position Variable Setup }
Vars:SMM_ATR(0) ;

{ ***** LastTradeDay ***** }
LastTradeDay = _MagicQS268_LTD_IN ;

{ 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);

{ Entry and Exit prices }
UBuy = CloseD(1) + Frac_LE * LE_ATR ;
USell = Average(Low, Bar_S1) - Frac_SE * SE_ATR ;

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

{ check Whether louckNet use or not}
CondNet_L = true ;
CondNet_S = true ;

{ 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) ;
if Time2SW = 0 then TimeOK2 = true else TimeOK2 = (time >= Time2B and time <= Time2E) ;
if Time3SW = 0 then TimeOK3 = true else TimeOK3 = (time >= Time3B and time <= Time3E) ;

if Time1SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK1 ;
   Cond_SE = Cond_SE and TimeOK1 ;
end ;

if Time2SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK2 ;
   Cond_SE = Cond_SE and TimeOK2 ;
end ;

if Time3SW = 1 then Begin
   Cond_LE = Cond_LE and TimeOK3 ;
   Cond_SE = Cond_SE and TimeOK3 ;
end ;

{ 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;
   Sell next bar at BuyStop 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;
   end;
   if BarsSinceEntry >= NBar_SX then  BuytoCover next bar at Highest(High,Bar_S2) stop ;
end;

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

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

印度指數期貨60分K 留倉 2008/3/24 ~ 2013/12/31 交易成本 8 美元 ,圖表績效單位 美元





有 2014的歷史資料讀者可自行做樣本外測試
{ function : _MagicQS268_LTD_IN }
Vars:LastTradeDay(false) ;
if DAYofMonth(Date) > 22 and DAYofMonth(Date) < 30 and DAYofWeek(Date)= 4
then LastTradeDay = True else LastTradeDay =False ;
_MagicQS268_LTD_IN = LastTradeDay ;

沒有留言:

張貼留言