2024年2月10日 星期六

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

EasyTrader TestNo 031

參考文章 Larry Williams 終極指標 Part1

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

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

//計算不同均線價格
// ************* BreskThrough 3 Line *************
vars:Avg7(0),Avg13(0),Avg21(0),Avg60(0) ;
Avg7 = Average(Close,7) ;
Avg13 = Average(Close,13) ;
Avg21 = Average(Close,21) ;
Avg60 = Average(Close,60) ;

//計算一目均衡表價格
// ************ Ichimoku Sanjin Syytem *****************
Vars: ShortTerm(9),MidTerm(26),LongTerm(52);
Vars: ConvertLine(0) ;

ShortTerm = LenA1 ;
if ShortTerm <= 8 then ShortTerm = 8 
   else if ShortTerm >= 20 then ShortTerm = 20 ;
MidTerm = ShortTerm*3 ;
LongTerm = ShortTerm*6 ;

ConvertLine = (highest(high,ShortTerm) + Lowest(Low,ShortTerm))/2 ;

//判斷多空趨勢
//*********** Trend filter setup
vars:UpTrendNo(1),DnTrendNo(1),UpTrend_High(false),UpTrend_Low(false),DnTrend_High(false),DnTrend_Low(false) ;
vars:Trend4Long(false),Trend4Short(false) ;

UpTrendNo = Mod(LenA1,5)+1 ;
DnTrendNo = Mod(LenB1,5)+1 ;

if  UpTrendNo = 5 then Trend4Long = 多方趨勢 ;
if  DnTrendNo = 3 then Trend4Short = 空方趨勢 ;

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

if DataCompression > 1 then begin
   Condition1 = Close > Open and Open > Close[1] ;
   Condition2 = Close < Open and Open < Close[1] ;
end else begin
   Condition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;
   Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
end;

// ************* Time Set Up *************
TimeOK = ((time >= 1200 and time <= 2345)) or (time >= 0300 and time <= 0500 ) ;

//進場條件
// ************* Entry/Exit Condition Set Up *************
SelectNo = IntPortion(RatioL+0.6) ;
if SelectNo < 1 or SelectNo > 5 then SelectNo = 1 ;
if SelectNo = 5 then EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(HighD(0),HighD(1),HighD(2))-Range*0.5 ;
ShortPrice = ConvertLine ;

// ********** Main Strategy *********
// ********** Entry Method
//計算終極指標價格
Var:FastLenUO(7),MediumRatio(4.5),SlowRatio(2.5);
Var:ShiftL(5),ShiftS(4),HLRangeL(45),HLRangeS(55) ;
Var: TL(0),BP(0),TR(0), FastAvg(0),MediumAvg(0),SlowAvg(0),MediumLenUO(0),SlowLenUO(0);
Var: FastTRSum(0),MediumTRSum(0),SlowTRSum(0),MRatio(0),SRatio(0),UO(0) ;

TL = TrueLow ;
BP = Close - TL ;
TR = TrueHigh - TrueLow ;

FastLenUO = LenA1 ;
MediumRatio = FracA ;
SlowRatio = FracB ;

if MediumRatio < 2 then MRatio = 2 ;
if SlowRatio < 2 then SRatio = 2 ;

MRatio = MediumRatio ;
SRatio = SlowRatio ;

MediumLenUO = IntPortion(FastLenUO * MRatio) ;
SlowLenUO = IntPortion(MediumLenUO * SRatio) ;

FastTRSum = Summation(TR,FastLenUO) ;
MediumTRSum = Summation(TR, MediumLenUO) ;
SlowTRSum = Summation(TR, SlowLenUO) ;

if FastTRSum <> 0 then FastAvg = Summation(BP,FastLenUO)/FastTRSum ;
if MediumTRSum <> 0 then MediumAvg = Summation(BP,MediumLenUO)/MediumTRSum ;
if SlowTRSum <> 0 then SlowAvg = Summation(BP,SlowLenUO)/SlowTRSum ;

if (SRatio*MRatio+MRatio+1) > 0 then
UO = 100*((SRatio*MRatio*FastAvg)+(MRatio*MediumAvg)+SlowAvg)/(SRatio*MRatio+MRatio+1) ;

   if EntryExitCond then begin
//終極指標大於特定數值 且自近3日高點拉回後在區間價2/3以上 則收盤價突破設定價格做多
      if MP <> 1 and UO > HighBand*4 and Close > Day67 and DayHPB > LenB1 and Close < BuyPrice then  Buy ("LE_UO6") next bar at BuyPrice stop;

//終極指標小於特定數值 且自近3日低點反彈後在區間價1/3以下 則收盤價跌破設定價格做空
      if MP <> -1 and UO < LowBand*4 and Close < Day33 and DayLPB > LenB1 and Close > ShortPrice then  SellShort ("SE_UO6") next bar at ShortPrice stop;
   end ;


   if EntryExitCond then begin
/終極指標大於特定數值 且多方趨勢 則平盤上 收盤價突破設定價格做多
      if MP <> 1 and UO > HighBand*4 and Trend4Long and Condition1 and Close < BuyPrice then 
         Buy ("LE_UO9") next bar at BuyPrice stop;

/終極指標小於特定數值 且空方趨勢 則平盤下 收盤價跌破設定價格做空
      if MP <> -1 and UO < LowBand*4 and Trend4Short and Condition2 and Close > ShortPrice then 
         SellShort ("SE_UO9") next bar at ShortPrice stop;
   end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-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 LowSinceEntry+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 buytocover ("WrongEntryS6") next bar at Close stop ;

// ************* Rev BreskThrough 3 Line *************
vars:Max3L(0),Min3L(0);

Max3L = MaxList(Avg7,Avg13,Avg21) ;
Min3L = MinList(Avg7,Avg13,Avg21) ;

//持有多單 進場大於3根K棒 且近四根K收盤價大於短中期均線 則跌破短中期均線反手作空
    if MP > 0 and Countif(Close > Max3L,4) = 4 and BarsSinceEntry >= 3 then
       SellShort ("rLX_BK3L") next bar at Min3L stop;

//持有空單 進場大於3根K棒 且近四根K收盤價小於短中期均線 則突破短中期均線反手作多
    if MP < 0 and Countif(Close < Min3L,4) = 4 and BarsSinceEntry >= 3 then
       Buy ("rSX_BK3L") next bar at Max3L 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






2 則留言: