2023年3月11日 星期六

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

EasyTrader TestNo 008

參考文章 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) ;

// ************* Time Set Up *************
TimeOK = True ;

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

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

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

// ********** Main Strategy *********
{ for Larry Day High/Low box }
vars:TempA(0),TempB(0),Energy(0) ;

//1.當日低點與前三日高點價差(動能)
TempA = AbsValue(HighD(3)-LowD(0)) ;

//2.前一日高點與三日前低點價差 
TempB = AbsValue(HighD(1)-LowD(3)) ;

//兩者取其大作為波動範圍的參考
Energy = MaxList(TempA,TempB) ;

if TimeOK then begin

//在下根K棒開盤價加上波動範圍比例進場做多
if MP <> 1  then Buy ("MQS14_LE4T") next bar at Open next bar + MaxList(EnErgy*FracA/6,21) stop ;

//在下根K棒開盤價減去波動範圍比例進場做空
if MP <> -1  then SellShort ("MQS14_SE4T") next bar at Open next bar - MaxList(EnErgy*FracB/6,34) 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 Sell ("WrongEntryL13") next bar at Close stop ;
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS14") next bar at Close stop ;

// ************* Rev Lower Low & Higher High Exit *************
vars:BaseBar(5),var01(0),var02(0),Var03(0),Var04(0) ;

if EntriesToday(Date) = 0 then Begin

//持有多單,當收盤價小於均線,跌破最近兩日最低價均值一段距離後則反手做空
if MP > 0 then Begin
var01 = (LowD(1)+LowD(0))*0.5-AvgTrueRange(BaseBar)*1.618 ;
var02 = LowSinceEntry ;
if Close < xAverage(Close,LenA2) and Close > var01 then
SellShort ("rLX_HL1") next bar at var01 stop ;

{ if HBarPos<=BaseBar and Countif(Low<=Low[1],BaseBar)> BaseBar*0.5 and Close > var02 then
SellShort ("rLX_HL2") next bar at var02 stop ; }
end ;

//持有空單,當收盤價大於均線,突破最近兩日最高價均值一段距離後則反手做多
if MP < 0 then Begin
var03 = (HighD(1)+HighD(0))*0.5+AvgTrueRange(BaseBar)*1.618 ;
var04 = HighSinceEntry ;
if Close > xAverage(Close,LenB2) and Close < Var03 then
Buy ("rSX_HL1") next bar at Var03 stop ;

//持有空單,近低點反彈連續過高,反手做多 
if LBarPos<=BaseBar and Countif(High>=High[1],BaseBar)> BaseBar*0.5 and Close < Var04 then
Buy ("rSX_HL2") next bar at Var04 stop ;
end ;
end ;


// ************* Rev EntryPrice PullBack *************

//多單進場一段時間 收盤價低於進場價格一段距離後 反手做空
if MP > 0 and BarsSinceEntry > NBarL then
SellShort ("rLX_EntPricePB") next bar at EntryPrice - MaxList(FracA*AvgTrueRange(BarPass),100) stop;

//空單進場一段時間 收盤價高於進場價格一段距離後 反手做多
if MP < 0 and BarsSinceEntry > NBarS then
Buy ("rSX_EntPricePB") next bar at EntryPrice + MaxList(FracB*AvgTrueRange(BarPass),100) 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




沒有留言:

張貼留言