2024年8月2日 星期五

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

EasyTrader TestNo 042

什麼是交易回調?
回調是基本趨勢方向的暫時逆轉。例如,如果股票的價格呈現上漲趨勢,回檔是價格在繼續上漲趨勢之前暫時下跌回調交易背後的想法是在價格暫時下跌期間買入股票,並期望該股票在回檔後將繼續其上漲趨勢。

如果股票的價格呈現下跌趨勢,反彈是價格在繼續下跌趨勢之前暫時上漲回調交易背後的想法則是在價格暫時上漲期間做空股票,並期望該股票在反彈後將繼續其下跌趨勢。

如何利用回調進行交易?
當進行回調交易時,您首先需要確定股票何時處於回檔/反彈狀態。做到這一點的一種方法是使用技術指標。可以幫助您識別回調的技術指標的一個很好的例子是 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) ;
vars:jumpPoint(0) ;
jumpPoint = MinMove/PriceScale ;

//****************** 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*jumpPoint) ;
   BasePL = BasePL+IntPortion((Close-10000)/1000)*15 ;
   BasePL = MinList(BasePL,225*jumpPoint) ;
end ;

PF = MinList(PF,BasePF) ;
PL = MinList(PL,BasePL) ;

PF = MinList(PF,450*jumpPoint) ;
PL = MinList(PL,225*jumpPoint) ;

//計算自訂義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 ;

  //計算近3日區間價格
// ************3 Day High or Low *****************
vars:DayH(High),DayL(Low),DayHL(0),DayHPB(0),DayLPB(0),Day33(0),Day67(0) ;
DayH = MaxList(HighD(0),HighD(1),HighD(2)) ;
DayL = MinList(LowD(0),LowD(1),LowD(2)) ;
DayHL = DayH - DayL ;
Day33 = DayL+DayHL*0.33 ;
Day67 = DayL+DayHL*0.67 ;
DayHPB = _BarsLast(High = DayH) ;
DayLPB = _BarsLast(Low = DayL) ;

  //計算近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 ;
Week33 = WeekL+WeekHL*0.33 ;
Week67 = WeekL+WeekHL*0.67 ;
WeekHPB = _BarsLast(High = WeekH) ;
WeekLPB = _BarsLast(Low = WeekL) ;

  //計算近N週區間價格
// ************ for week High/Low box *****************
Vars:WeekLong(5),WeekShort(5),WH(0),WL(0),HLWeek(0),WCount(0),WBox33(0),WBox67(0) ;
WH = 近週高點 ;
WL = 近週低點;

//計算超級趨勢指標上下軌及多空方向
//* SuperTrend Setup
var:LenST(10),MultiRatio(3) ;
vars:BasePrice(0),UpBand(0),DnBand(0) ;
vars:TrendDir(0),flagDn(false),flagUp(false),TrendLine(0) ;
vars:TRbarSL(0),TRbarSS(0);

LenST = LenA1 ;
MultiRatio = FracA ;
BasePrice = (High+Low)/2 ;
UpBand = BasePrice+AvgTrueRange(LenST)*MultiRatio ;
DnBand = BasePrice-AvgTrueRange(LenST)*MultiRatio ;

TrendDir = iff(Close > UpBand[1],1,iff(Close < DnBand[1],-1,TrendDir)) ;
flagDn = TrendDir < 0 and TrendDir[1] > 0 ;
flagUp = TrendDir > 0 and TrendDir[1] < 0 ;

DnBand = iff(TrendDir > 0 and DnBand < DnBand[1],DnBand[1],DnBand) ;
UpBand = iff(TrendDir < 0 and UpBand > UpBand[1],UpBand[1],UpBand) ;

UpBand = iff(flagDn,BasePrice+AvgTrueRange(LenST)*MultiRatio,UpBand) ;
DnBand = iff(flagUp,BasePrice-AvgTrueRange(LenST)*MultiRatio,DnBand) ;

TrendLine = iff(TrendDir > 0,DnBand,iff(TrendDir < 0,UpBand,BasePrice)) ;
TRbarSL = _BarsLast(TrendDir cross over 0) ;
TRbarSS = _BarsLast(TrendDir cross under 0) ;

//**************************************************

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

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Highest(High,Hbar) ;
ShortPrice = WL ;

// ********** Main Strategy *********
// ********** Entry Method
   if EntryExitCond then begin
//收盤價在長均線之上 等MACD 指標回檔  過前根K棒高點 買進做多
      if MP <> 1 and uOsc < uOsc[1] and uDif < 0 and uDif < uDif[1] and Close > Average(Close,LenA1*20)  then Buy ("LE_Osc") next bar at High stop ;

//收盤價在長均線之下 等MACD 指標反彈  破前根K棒低點 賣出做空
      if MP <> -1 and uOsc > uOsc[1] and uDif > 0 and uDif > uDif[1] and Close < Average(Close,LenB1*20)  then Sellshort ("SE_Osc") next bar at Low Stop ;
   end ;

// *************  WeeK high or low Entry*************
   if EntryExitCond then begin
//近四根K棒收盤價在近三週區間價盤整 突破設定價位 買進做多
    if MP <> 1 and Countif(Close > WeekL+WeekHL*0.5 and Close < WeekL+WeekHL*0.67,4) = 4 and Close < BuyPrice then   Buy ("LE_Week067") next bar at BuyPrice stop;

//近四根K棒收盤價在近三週區間價盤整 跌破設定價位 賣出做空
    if MP <> -1 and Countif(Close < WeekL+WeekHL*0.5 and Close > WeekL+WeekHL*0.33,4) = 4 and Close > ShortPrice then
       sellshort ("SE_Week067") next bar at ShortPrice stop;
   end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("Trail_PL2_"+NumtoStr(Absvalue(EntryPrice-(HighSinceEntry-MinList((PL+PF)*0.5,PL))),0)) 
   All Contracts next bar at HighSinceEntry-MinList((PL+PF)*0.5,PL) stop ;   
if MP > 0 then Sell ("LPF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice+PF limit ;

if MP < 0 then BuytoCover ("Trail_SL2_"+NumtoStr(Absvalue(EntryPrice-(LowSinceEntry+MinList((PL+PF)*0.5,PL))),0)) 
   All Contracts next bar at LowSinceEntry+MinList((PL+PF)*0.5,PL) stop ;
if MP < 0 then BuytoCover ("SPF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice-PF limit ;

//多空單部位短沖出場
   if MP > 0 and (BarsSinceEntry > 0 and BarsSinceEntry <= 80/Barinterval and Close Cross over EntryPrice+40*jumpPoint)   then sell ("QuickLX1") next bar at Market ;
   if MP < 0 and (BarsSinceEntry > 0 and BarsSinceEntry <= 80/Barinterval and Close Cross under EntryPrice-40*jumpPoint)  then Buytocover ("QuickS1") next bar at Market ;

// SuperTrend Exit
//持有空單 且多方趨勢 近日高點拉回後 再突破設定價位 反手做多
   if MP < 0 and TrendDir > 0 and Close > Day67 and DayHPB > LenB1 and Close < BuyPrice then 
      buy ("rSX_Super6") next bar at BuyPrice Stop ;

//持有多單 且空方趨勢 近日低點反彈後 再跌破設定價位 反手做空
   if MP > 0 and TrendDir < 0 and Close < Day33 and DayLPB > LenB1 and Close > ShortPrice then 
      sellshort ("rLX_Super6") next bar at ShortPrice 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 ~ 2023/12/31 交易成本 1200





沒有留言:

張貼留言