2023年4月15日 星期六

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

EasyTrader TestNo 011

參考文章 開盤價與收盤價的慣性 - QStick 指標
// 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) ;

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

// ************Fix Length High or Low *****************
//計算近60根K高低點區間
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) ;

// ************ for week High/Low box *****************
WH = 近週高點;
WL = 近週低點;

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

// ************ Ichimoku Sanjin Syytem *****************
計算一目均衡表價位
Vars: ShortTerm(9),MidTerm(26),LongTerm(52);
Vars: ConvertLine(0),BaseLine(0),AheadLine1(0),AheadLine2(0),BehindLine(0),HighCloud(0),LowCloud(0),Cloud(0) ;
Vars: BaseCond(false),ConvertCond(false),C_over_B(0),C_Under_B(0),H_Over_L(0),H_Under_L(0),Thick(100) ;

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 ;
BaseLine = (highest(high,MidTerm) + Lowest(Low,MidTerm))/2 ;
AheadLine1 = (ConvertLine + BaseLine)/2 ;
AheadLine2 = (highest(high,LongTerm) + Lowest(Low,LongTerm))/2 ;
BehindLine = Close ;
C_over_B = _BarsLast(ConvertLine cross over BaseLine) ;
C_Under_B = _BarsLast(ConvertLine cross under BaseLine) ;

HighCloud = AheadLine1[MidTerm] ;
LowCloud = AheadLine2[MidTerm] ;
Cloud = AbsValue(HighCloud-LowCloud) ;
H_Over_L = _BarsLast(HighCloud cross over LowCloud) ;
H_under_L = _BarsLast(HighCloud cross under LowCloud) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighCloud ;
ShortPrice = WL+TrueRange ;

// ********** Main Strategy *********
計算近期 Qstick 值以及紅黑K數量
Vars:CandleBarNo(0),SumBar(0),QStick(0),RedK(0),BlackK(0),CandleHL(0);

CandleBarNo = MaxList(IntPortion(BarPass*1.6),25) ;
CandleHL = Highest(High,CandleBarNo)-Lowest(Low,CandleBarNo) ;
SumBar = Summation(Close-Open,CandleBarNo) ;
if CandleBarNo <> 0 then Qstick = SumBar/CandleBarNo ;
RedK = Countif(Close > Open and Close > Close[1],CandleBarNo) ;
BlackK = Countif(Close < Open and Close < Close[1],CandleBarNo) ;

if TimeOK then begin
//收盤價大於轉換線且紅K數量大於黑K數量 突破設定價格做多
if MP <> 1 and Close > ConvertLine and RedK > BlackK and Close < BuyPrice then
Buy ("LE_QStick6T") next bar at BuyPrice stop ;
//收盤價小於轉換線且紅K數量小於黑K數量 跌破設定價格做空
if MP <> -1 and Close < ConvertLine and RedK < BlackK and Close > ShortPrice then
SellShort ("SE_QStick6T") next bar at ShortPrice stop ;
end ;


if TimeOK then begin
//轉換線大於雲層上緣且紅K數量大於黑K數量且QStick大於0 則突破近日高點最多
if MP <> 1 and ConvertLine > MaxList(HighCloud,LowCloud) and RedK > BlackK and QStick > 0 then
Buy ("LE_QStick8T") next bar at DH stop ;
//轉換線小於雲層下緣且紅K數量小於黑K數量且QStick小於0 則跌破近日低點最多
if MP <> -1 and ConvertLine < MinList(HighCloud,LowCloud) and RedK < BlackK and QStick < 0 then
SellShort ("SE_QStick8T") next bar at DL 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 MaxPositionprofit/currentcontracts < 5000)
then Buy ("WrongEntryS7") next bar at Close stop ;

// ************* N Day trade Exit *************

//空單部位自近日高點回落至特定距離後 再次往上突破該價位則平倉
if MP < 0 and Close < DH-(HLday*FracA*0.1) then
buytocover ("LX_DTrade1") next bar at DH-(HLday*FracA*0.1)+Range Stop ;
//多單部位自近日低點上升至特定距離後 再次往下跌破該價位則平倉
if MP > 0 and Close > DL+(HLday*FracB*0.1) then
Sell ("SX_DTrade1") next bar at DL+(HLday*FracB*0.1)-Range Stop ;

/空單部位且OSC柱狀體上升大於特定值且仍低於區間價格2/3 則突破該價格時平倉
if MP < 0 and uOsc > uOsc[1] and uOsc > RatioL-10 and Close < FixLen67 then
buytocover ("LX_UOsc1") next bar at FixLen67 stop ;

/多單部位且OSC柱狀體下降小於特定值且仍高於區間價格1/3 則跌破該價格時平倉
if MP > 0 and uOsc < uOsc[1] and uOsc < RatioS-10 and Close > FixLen33 then
Sell ("SX_UOsc1") next bar at FixLen33 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





沒有留言:

張貼留言