2014年7月9日 星期三

●滬深300股指期貨當沖與留倉策略 [程式碼]

EasyTrader ArtNo 176
滬深300指數是根據流動性和市值規模從滬深兩市中選取300只A股股票作為成份股,其樣本空間為剔除如下股票後的A股股票:上市時間不足一個季度的股票(大市值股票可以有例外)、暫停上市股票、經營狀況異常或最近財務出現嚴重虧損的股票、市場價格波動異常明顯受操縱的股票、其他經專家委員會認為應剔除的股票。
網路找到一些相關文章請參閱
  1. 投資組合》多策略之一,策略評估與比較 by Ape(林培勝)
  2. 滬深300當沖交易策略 (附回測報表下載) by 期謀
  3. 滬深300(IF300)當沖策略與一些想法. by Ray's Blog
滬深300股指期貨合約
合約標的:滬深300指數
合約乘數:每點300元
報價單位:指數點
最小變動價位:0.2點
合約月份:當月、下月及隨後兩個季月
交易時間:上午:9:15-11:30,下午:13:00- 15:15
最後交易日交易時間:上午:9:15-11:30 下午:13:00-15:00
每日價格最大波動限制:上一個交易日 結算價的±10%
最低交易保證金:合約價值的12%
最後交易日:合約到期月份的第三個周五 遇法定假日順延
交割日期:同最後交易日
交割方式:現金交割
交易代碼:IF
上市交易所:中國金融期貨交易所

當沖測試程式碼
{ ***** System Code Start here *****}
{ Public inputs }
Inputs:LuckyNet(0),IntraDay(0), TradeStopLoss(0.011),TradeProfit(0.016) ;
Inputs:TradeInday(0),Time1SW(1),Time1B(945),Time1E(1100) ;
Inputs:Time2SW(1),Time2B(1330),Time2E(1430),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(1) ;
inputs:Frac_LMM(4.26),NBar_LM(3),Frac_SMM(3.60),NBar_SM(11) ;

{ 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(50),NBar_LE(7),Frac_LE(1.40) ;

{ SellMode Setup }
inputs:Bar_S1(4),NBar_SE(19),Frac_SE(0.86) ;

{ Exit Long Position Setup }
inputs:TRLen_L(4),TRPct_L(32),Ratio_TL(4.01) ;

{ Exit Short Position Setup }
inputs:NBar_SX(30),Bar_S2(32) ;

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

{ 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);
SE_ATR = AvgTrueRange(NBar_SE);

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

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

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

{ 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 Time3SW = 1 then Begin
Cond_LE = Cond_LE and (TimeOK1 or TimeOK2 or TimeOK3) ;
Cond_SE = Cond_SE and (TimeOK1 or TimeOK2 or TimeOK3) ;
end ;
if Time2SW = 1 then Begin
Cond_LE = Cond_LE and (TimeOK1 or TimeOK2) ;
Cond_SE = Cond_SE and (TimeOK1 or TimeOK2) ;
end ;
if Time1SW = 1 then Begin
Cond_LE = Cond_LE and TimeOK1 ;
Cond_SE = Cond_SE and TimeOK1 ;
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;
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;
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 ************}
滬深指數期貨 15分K 當沖 2010~ 2014 交易成本 200 RMB ,圖表績效單位 RMB



留倉測試程式碼
{ ***** System Code Start here *****}
{ Public inputs }
Inputs:LuckyNet(0),IntraDay(1), TradeStopLoss(0.041),TradeProfit(0.019) ;
Inputs:TradeInday(0),Time1SW(1),Time1B(945),Time1E(1100) ;
Inputs:Time2SW(1),Time2B(1330),Time2E(1430),Time3SW(0) ;
Inputs:Time3B(900),Time3E(1300),PositionSW(0) ;
inputs:Frac_LMM(4.79),NBar_LM(18),Frac_SMM(2.16),NBar_SM(13) ;

{ 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 }
{ SellMode Setup }
inputs:NBar_SE(19),Frac_SE(2.46) ;

{ Exit Long Position Setup }
{ Exit Short Position Setup }
inputs:TRLen_S(9),TRPct_S(42),Ratio_TS(3.19) ;

{ BuyMode Variable Setup }
{ 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_CH ;

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

{ Entry and Exit prices }
UBuy = LowD(0) ;
USell = OpenD(0) - Frac_SE * SE_ATR ;

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

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

{ 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 Time3SW = 1 then Begin
Cond_LE = Cond_LE and (TimeOK1 or TimeOK2 or TimeOK3) ;
Cond_SE = Cond_SE and (TimeOK1 or TimeOK2 or TimeOK3) ;
end ;

if Time2SW = 1 then Begin
Cond_LE = Cond_LE and (TimeOK1 or TimeOK2) ;
Cond_SE = Cond_SE and (TimeOK1 or TimeOK2) ;
end ;

if Time1SW = 1 then Begin
Cond_LE = Cond_LE and TimeOK1 ;
Cond_SE = Cond_SE and TimeOK1 ;
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) ;
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 ************}
滬深指數期貨 15分K 留倉 2010~ 2014 交易成本 200 RMB ,圖表績效單位 RMB


0 留言:

張貼留言

如果有私人問題想請教,請透過網站右方『與站長聯絡』之表單,謝謝!

----------------------------------------------------------------------------------------------------
網站聲明(Disclaimer)
本教學網站內所提供之程式碼(包括函數、指標、訊號)屬開放程式碼,用意在於讓使用者學習程式語法之撰寫,使用者可以任意修改語法內容並調整參數。本網站所有之內容(包括文章、影片、歷史紀錄、程式碼、教材)限用於個人學習使用,請勿轉寄、濫用,嚴禁私自串接帳戶交易。
-------------------------------------------------------------------------------------------------