2023年2月8日 星期三

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

EasyTrader TestNo 005

參考文章 
費氏數列 - 神秘的黃金比例
// 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 Week High or Low *****************
vars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
WeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
WeekHL = WeekH - WeekL ;

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

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1300)) ;

// ************* Entry/Exit Condition Set Up *************
EntryExitCond = True ;

//***************** BuyPrice & ShortPrice Setup *****************
 BuyPrice = (OpenW(0)+CloseW(1))*0.5 ;
ShortPrice = (Highest(High,Lbar)+Lowest(Low,Lbar))*0.5 ;

// ********** Main Strategy *********
vars: FabonaciA(0.618),FabonaciB(0.382) ;
Vars: WLEntry(0),WSEntry(0),HighPoint(0),LowPoint(0),StartLong(0),StartShort(0),Trigger(0) ; ;

//將費氏數列係數做優化 FabonaciA 需大於0.5 FabonaciB 需小於0.5
FabonaciA = MaxList(FracA/5,0.5) ;
FabonaciB = MinList(FracB/5,0.5) ;

//改變原文章內計算邏輯為以近三週高低點為基礎
WLEntry = WeekL+WeekHL*FabonaciA ;
WSEntry = WeekL+WeekHL*FabonaciB ;

{原文章內計算邏輯為近期高低點+費氏數列係數計算出多空進場點
vars: FabonaciFactor(0.618) ;
LongEntry = Highest(High,NBar)*FabonaciFactor + Lowest(Low,NBar)*(1-FabonaciFactor) ;
ShortEntry = Lowest(Low,NBar)*FabonaciFactor + Highest(High,NBar)*(1-FabonaciFactor) ;
}

HighPoint = Close + (Close - Close[1])*FabonaciA ;
LowPoint = Close - (Close - Close[1])*FabonaciB ;

StartLong = (HighPoint - Close)/(1+FabonaciA) + Close ;
StartShort = Close - (Close- LowPoint)/(1+FabonaciB) ;

Trigger = Xaverage(XAverage(StartLong,8)-XAverage(StartShort,8),2) ;

if TimeOK then begin
if MP <> 1 and _MagicQF001(1) > 0 and Trigger > HighBand-13 then Buy ("MQS05_L1T") next bar at WLEntry stop ;
if MP <> -1 and _MagicQF001(1) < 0 and Trigger < LowBand-13 then SellShort ("MQS05_S1T") next bar at WSEntry stop ;
end ;

//基本停利停損出場
// ************* Base Exit *************
if MP <> 0 and BarsSinceEntry >= 1 then begin
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 ;
end ;

//進場後部位損失大於預期則出場
if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then Sell ("WrongEntryL2") next bar at Close stop ;
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buytocover ("WrongEntryS2") next bar at Close 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;

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







沒有留言:

張貼留言