2025年1月2日 星期四

小那斯達克(NQ)全時段交易回測篇 [04]

EasyTrader NQ_Test 04
透過週規則的概念加以衍生轉化為日區間及週區間的價格回調進場

平均真實波動幅度(ATR)是一種常見的技術分析指標,旨在衡量波動性。該指標最初由著名商品交易員、開發人員和分析師 Welles Wilder 開發,於 1978 年推出。ATR 提供一種定性方法,將商品的潛在波動性對應到具體的數字。波動性強的市場價格範圍較寬,而波動性較小的市場價格範圍較窄。波動率指標透過追蹤商品的波動程度。

// Public Variable
vars:MP(0),PF(0),PL(0),BuyPrice(0),ShortPrice(0),BasePF(600),BasePL(300) ;
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 ;

PF = MinList(PF,6000/BigPointValue) ;
PL = MinList(PL,1500/BigPointValue) ;

//計算進場後獲利點數
// ******** Long/Short win/loss point *********
vars:WinPoint_LE(0),LossPoint_LE(0),WinPoint_SE(0),LossPoint_SE(0),PF_Ref(800),SL_Ref(-800) ;
if barssinceentry > 0 then begin
   if MP > 0 then begin
      WinPoint_LE = HighSinceEntry_N - EntryPrice ;
      LossPoint_LE = LowSinceEntry_N - EntryPrice ;
   end ;
   if MP < 0 then begin
      WinPoint_SE = EntryPrice - LowSinceEntry_N ;
      LossPoint_SE = EntryPrice - HighSinceEntry_N ;
   end ;
end else begin
      WinPoint_LE = 0 ;
      LossPoint_LE = 0 ;
      WinPoint_LE = 0 ;
      LossPoint_LE = 0 ;
end ;

//計算近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) ;

//計算長短週期ATR 波動比
// **** AvgTrueRange Ratio Entry
vars:TRratioA(0),TRratioB(0),RA(0),RB(0) ;
RA = FracA+1 ;
RB = FracB+1 ;
if AvgTrueRange(IntPortion(LenA1*RA)) <> 0 then TRratioA = AvgTrueRange(LenA1)/AvgTrueRange(IntPortion(LenA1*RA)) ;
if AvgTrueRange(IntPortion(LenB1*RB)) <> 0 then TRratioB = AvgTrueRange(LenB1)/AvgTrueRange(IntPortion(LenB1*RB)) ;

//計算彈性模數 
//* Vidya Setup
vars:Vidya1(1),Vidya2(1) ;

Vidya1 = _SS_Vidya(LenA1,FracA) ;
Vidya2 = _SS_Vidya(LenB1,FracB) ;

//計算五日平均振幅
vars:Day5Range(0) ;
Day5Range = ((HighD(1)-LowD(1))+(HighD(2)-LowD(2))+(HighD(3)-LowD(3))+(HighD(4)-LowD(4))+(HighD(5)-LowD(5)))/5 ;

// ************* Time Set Up *************
TimeOK = (time >= 1100 or time <= 0300)

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(1) ;   
ShortPrice = OpenD(1)-Range*0.5 ;

// ********** Main Strategy *********
// ********** Entry Method

   if TimeOK then begin
//ATR 波動比 小於設定值 且3日高點拉回後 則收盤價突破設定價格 做多
   if MP <> 1 and TRratioA < RatioL and DayHPB < LenA1 and Close < BuyPrice then 
      Buy ("LE_ATR_2T") next bar at BuyPrice Stop ;

//ATR 波動比 大於設定值 且3日低點反彈後 則收盤價跌破設定價格 做空
   if MP <> -1 and TRratioB > RatioS and DayLPB > LenB1 and Close > ShortPrice then 
      SellShort ("SE_ATR_2T") next bar at ShortPrice Stop ;  

//近3根K棒彈性模數連升 且收盤價大於週區間價2/3 則收盤價突破設定價格 做多
      if MP <> 1 and Countif(Vidya1 > Vidya1[1],3) = 3 and Close > Week67 and WeekHPB > LenB1 and Close < BuyPrice then  Buy ("LE_Vidya7T") next bar at BuyPrice stop;

//近3根K棒彈性模數連降 且收盤價小於週區間價1/3 則收盤價跌破設定價格 做空
      if MP <> -1 and Countif(Vidya2 < Vidya2[1],3) = 3 and Close < Week33 and WeekLPB > LenB1 and Close > ShortPrice then 
         SellShort ("SE_Vidya7T") next bar at ShortPrice stop;
   end ;

//基本停利及移動停損
// ************* Base Exit *************
  if MP > 0 then begin
     Sell ("LX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice+PF+Day5RAnge limit ;
     if WinPoint_LE <= PL*1.5 then Sell ("LX_EPL"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop
        else if WinPoint_LE <= PF then Sell ("LX_H2PL_"+NumtoStr(PL*2,0)) next bar at HighSinceEntry_N-PL*2 stop 
           else if WinPoint_LE > PF then Sell ("LX_PF_"+NumtoStr(WinPoint_LE*0.5,0)) 
           next bar at HighSinceEntry_N-WinPoint_LE*0.5 stop ;
  end ;      

  if MP < 0 then begin
     BuytoCover ("SX_PF5D_"+NumtoStr(PF+Day5RAnge,0)) next bar at EntryPrice-PF-Day5RAnge limit ;
     if WinPoint_SE <= PL*1.5 then BuytoCover ("SX_EPL_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop 
        else if WinPoint_SE <= PF then BuytoCover ("SX_L2PL_"+NumtoStr(PL*2,0)) next bar at LowSinceEntry_N+PL*2 stop 
           else if WinPoint_SE > PF then BuytoCover ("SX_PF_"+NumtoStr(WinPoint_SE*0.5,0)) 
           next bar at LowSinceEntry_N+WinPoint_SE*0.5 stop ;
  end ;

小那斯達克 留倉 交易週期 2018/1/1 ~ 2024/10/31 交易成本 USD 60






沒有留言:

張貼留言