2023年5月1日 星期一

台指期全時段交易回測篇 [12]

EasyTrader TestNo 012

參考文章 艾達透視指標 Elder Ray 交易模型

// Public Variable
vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100),FilterA(0),FilterB(0) ;
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false),EntryExitCond(false),SelectNo(0) ;

//****************** Basic Setup ****************************************
MP = MarketPosition ;
if MP <> 0 then Begin
PF = EntryPrice*TradeProfit ;
PL = EntryPrice*TradeStopLoss ;
end else begin
PF = AvgPrice*TradeProfit ;
PL = AvgPrice*TradeStopLoss ;
end ;

// 依據目前指數來設定最大停利與停損點數
if Close > 10000 then begin
BasePF = BasePF+IntPortion((Close-10000)/1000)*30 ;
BasePF = MinList(BasePF,450) ;
BasePL = BasePL+IntPortion((Close-10000)/1000)*15 ;
BasePL = MinList(BasePL,225) ;
end ;

PF = MinList(PF,BasePF) ;
PL = MinList(PL,BasePL)*iff(CurrentContracts=1,1,0.67) ;

//計算近期高低點
// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;

WH = 近週高點 ;
WL =  近週低點 ;

if BarNumber = 1 then begin
Buy this bar on Close ;
Sell this bar on Close ;
end ;

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130)) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = BarsSinceExit(1) > MinList(LenA1,LenB1) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(OpenD(0),OpenD(1),OpenD(2))+Range*0.5 ;
ShortPrice = HighM(0)-MaxList(TrueRange*RatioL/10,6) ;

// ********** Main Strategy *********

Vars:HighOsc(0),LowOsc(0),CloseAvg(0),BuySetup1(false),SellSetup1(false),BuySetup2(false),SellSetup2(false) ;

//計算高低點擺盪值
HighOsc = High - Xaverage(High,BarPass) ;
LowOsc = Low - Xaverage(Low,BarPass) ;
CloseAvg = Xaverage(Close,MaxList(LenA1,LenB1)) ;

//設定多空進場初始條件
BuySetup1 = CloseAvg > CloseAvg[1] and Close > CloseAvg and LowOsc < -LowBand ;
SellSetup1 = CloseAvg < CloseAvg[1] and Close < CloseAvg and HighOsc > HighBand ;
BuySetup2 = HighOsc > HighBand and LowOsc > HighBand and HighOsc > LowOsc ;
SellSetup2 = HighOsc < -LowBand and LowOsc < -LowBand and HighOsc < LowOsc ;

if TimeOK then begin

//符合進場條件2 且收盤價突破設定價格做多
if MP <> 1 and BuySetup2 and Close < BuyPrice then
Buy ("LE_HLosc2T") next bar at BuyPrice stop ;

//符合進場條件2 且收盤價跌破設定價格做空
if MP <> -1 and SellSetup2 and Close > ShortPrice then
SellShort ("SE_HLosc2T") next bar at ShortPrice stop ;
end ;

if TimeOK then begin

/符合進場條件2 且 符合第二組濾網 則突破近週高點做多
if MP <> 1 and BuySetup2 and _MagicQF001(2) > 0 then
Buy ("LE_HLosc7T") next bar at WH stop ;

/符合進場條件2 且 符合第二組濾網 則跌破近週低點做空
if MP <> -1 and SellSetup2 and _MagicQF001(2) < 0 then
SellShort ("SE_HLosc7T") next bar at WL stop ;
end ;


//基本停利停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("PL2_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;

//進場後部位虧損大於預期則做反手單
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS8") next bar at Close stop ;

// ************* Rev high/low Swing *************

// 多單跌破盤整區下緣 反手做空
if MP > 0 and MRO(Close Cross over (Highest(High[1],34)+Lowest(Low[1],34))*0.5+6,34,4) > -1 then Sellshort ("rLX_FixL") next bar at Lowest(Low[1],34)-TrueRange stop;

// 空單突破盤整區上緣 反手做多
if MP < 0 and MRO(Close Cross under (Highest(High[1],34)+Lowest(Low[1],34))*0.5-6,34,4) > -1 then Buy ("rSX_FixH") next bar at Highest(High[1],34)+TrueRange stop;


//結算日出場
if _IsSettlementDay and time >= CalcTime(1300,-1*BarInterVal) and Time <= 1330 then begin
if MP > 0 then Sell ("LX_Bal") next bar at market ;
if MP < 0 then BuyToCover ("SX_Bal") next bar at market ;
end;

台指期 留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200





沒有留言:

張貼留言