2023年3月21日 星期二

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

 EasyTrader TestNo 009

參考文章 Larry Williams 短線交易秘訣
// 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 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;

//計算近3天高低點
// ************3 Day High or Low *****************
vars:DayH(High),DayL(Low),DayHL(0),DayHPB(0),DayLPB(0),Day33(0),Day67(0) ;
DayH = 近3日高點;
DayL = 近3日低點 ;

// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
AH = CDP + (HighD(1) - LowD(1));
NH = CDP*2 - LowD(1);
NL = 2*CDP - HighD(1);
AL = CDP - (HighD(1) - LowD(1));
end ;
CDPrange = HighD(1) - LowD(1) ;

Larry Williams 另一種價格波動策略(開盤價+ 前三日最大波動區間的百分比)

原文的邏輯為:
1.當日低點與前三日高點價差(動能)
2.前一日高點與三日前低點價差 ,兩者取其大作為波動範圍的參考,再乘上一百分比加上隔日開盤價就成為買賣訊號上下通道

這裡改為 N天的高點與M天低點的波動範圍作為買賣通道
{ for Nday Trading System }
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0) ;
{ for Day High/Low box }
DH = 近日高點;
DL = 近日低點 ;

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

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = EntriesToday(Date)=0 ;

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

if TimeOK then begin

//當收盤價大於3日高低點區間三分之二以上, 則突破開盤價加波動比例則進場做多
if MP <> 1 and Close > Day67 then Buy ("MQS14_LE5T") next bar at OpenD(0) + MaxList(HLDay*FracA/6,21) stop ;

//當收盤價小於3日高低點區間三分之一以下, 則跌破開盤價減波動比例則進場做
if MP <> -1 and Close < Day33 then SellShort ("MQS14_SE5T") next bar at OpenD(0) - MaxList(HLDay*FracB/6,34) stop ;
end ;


if EntryExitCond then begin
//當波動範圍小於250且自高點回落波動比例值後 再次突破則進場做多
if MP <> 1 and HLDay < 250 and Close < DH-(HLDay*FracA*0.1) then
Buy ("MQS14_LE10") next bar at DH-(HLDay*FracA*0.1)+Range Stop ;

//當波動範圍小於250且自低點反彈波動比例值後 再次跌破則進場做
if MP <> -1 and HLDay < 250 and Close > DL+(HLDay*FracB*0.1) then
SellShort ("MQS14_SE10") next bar at DL+(HLDay*FracB*0.1)-Range 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 MaxPositionprofit/currentcontracts < 5000)
then buytocover ("WrongEntryS13") next bar at Close stop ;
if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then sell ("WrongEntryL14") next bar at Close stop ;

// ************* Rev CDP exit ************

//當CDP範圍大於一定範圍且收盤價大於CDP上限值 則再跌落上限後平倉
if MP > 0 and CDPrange > MaxList(89,PF*0.382) and Close > AH then
sell ("rLX_CDP") next bar at AH-Minlist(Range,HBarPos) stop ;

//當CDP範圍大於一定範圍且收盤價小於CDP下限值 則再反彈過下限後反手做多
if MP < 0 and CDPrange > MaxList(89,PF*0.382) and Close < AL then
buy ("rSX_CDP") next bar at AL+Minlist(Range,LBarPos) stop ;


// ************* FixTime Exit *************
vars:FT033(0),Condition33(false),FT050(0),Condition50(false),FT067(0),Condition67(false) ;
FT033 = IntPortion((1200/Barinterval)*0.33) ;
FT050 = IntPortion((1200/Barinterval)*0.50) ;
FT067 = IntPortion((1200/Barinterval)*0.67) ;

Condition33 = (time = CalcTime(0000,FT033*BarInterVal)) ;
Condition50 = (time = CalcTime(0000,FT050*BarInterVal)) ;
Condition67 = (time = CalcTime(0000,FT067*BarInterVal)) ;

//每日固定時間若收盤價大於前日高點則空單平倉
if MP < 0 and Condition67 and Close > HighD(1) then buytocover ("SX_FT067_1") next bar at High stop;
//每日固定時間若收盤價小於前日低點則多單平倉
if MP > 0 and Condition33 and Close < LowD(1) then Sell ("LX_FT033_1") next bar at Low 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





沒有留言:

張貼留言