2023年5月11日 星期四

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

EasyTrader TestNo 013

參考文章 MACD指標的另一種策略元素

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

//依比例值計算 MACD
// ************ Macd *****************
vars:uDif(0),uMacd(0),uOsc(0),MacdLen(0) ;
MacdLen = MaxList(LenA1,LenB1) ;
if MacdLen > 12 then MacdLen = 12 ;
uDif = Macd((High+Low+Close*2)/4,IntPortion(MacdLen*1.3),MacdLen*3) ;
uMacd = Xaverage(uDif,MacdLen) ;
uOsc = uDif-uMacd ;

//計算近60 根K棒高低點
// ************Fix Length High or Low *****************
vars:FixLenH(High),FixLenL(Low),FixLenHL(0),FixLenHPB(0),FixLenLPB(0),FixLen33(0),FixLen67(0) ;
FixLenH = Highest(High,60) ;
FixLenL = Lowest(Low,60) ;
FixLenHL = FixLenH - FixLenL ;
FixLen33 = FixLenL+FixLenHL*0.33 ;
FixLen67 = FixLenL+FixLenHL*0.67 ;
FixLenHPB = _BarsLast(High = FixLenH) ;
FixLenLPB = _BarsLast(Low = FixLenL) ;

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

//計算近幾日高低點
// ************ for Day High/Low box *****************
Vars:DayLong(5),DayShort(5),DH(0),DL(0),HLDay(0),DCount(0) ;
DH = 近日高點;
DL = 近日低點 ;

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

// ********** Main Strategy *********
if TimeOK then begin

//柱狀圖高度漸增且大於某數值 則收盤價突破近60根K棒高低區間三分之二價格做多
if MP <> 1 and uOsc > uOsc[1] and uOsc > RatioL-10 and Close < FixLen67 then
Buy ("LE_UOsc6T") next bar at FixLen67 stop ;

//柱狀圖高度漸減且小於某數值 則收盤價跌破近60根K棒高低區間三分之一價格做空
if MP <> -1 and uOsc < uOsc[1] and uOsc < RatioS-10 and Close > FixLen33 then
SellShort ("SE_UOsc6T") next bar at FixLen33 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 sellshort ("WrongEntryL12") next bar at Close stop ;
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buytocover ("WrongEntryS12") next bar at Close stop ;

// ************* rev N Week trade Exit *************
//空單自近週高點跌落一固定比例後 再次突破該比例價位反手做多
//多單自近週低點上升一固定比例後 再次跌破該比例價位反手做空

// ************* rev N Day trade Exit *************
//空單自近日高點跌落一固定比例後 再次突破該比例價位反手做多
//多單自近日低點上升一固定比例後 再次跌破該比例價位反手做空

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






沒有留言:

張貼留言