2024年9月23日 星期一

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

EasyTrader NQ_Test  02

參考   國外知名交易系統   R-Breaker 及 內建高低點轉折函數應用
// 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,2500/BigPointValue) ;
PL = MinList(PL,1000/BigPointValue) ;

//計算 R-Break 區間價格
// ******** R-Break Entry ***********
vars:f1(0.4),f2(0.1),f3(0.1),xdiv(4);
var:SellshortCheck(0),BuyCheck(0),RevSellshort(0),RevBuy(0),TrendBuy(0),TrendSellshort(0),div(0),Rangefilter(false);

f1 = FracA/6 ;
f2 = FracB/6 ;
f3 = RatioL/6 ;
xdiv = RatioS ;

div=maxlist(xdiv,1) ;

if date>date[1] then begin
{ Calculate Ref Price box }
SellshortCheck=HighD(1)+f1*(CloseD(1)-LowD(1));
RevSellshort=((1+f2)/2)*(HighD(1)+CloseD(1))-(f2)*LowD(1);
RevBuy=((1+f2)/2)*(LowD(1)+CloseD(1))-(f2)*HighD(1);
BuyCheck=LowD(1)-f1*(HighD(1)-CloseD(1));
TrendBuy=SellshortCheck+f3*(SellshortCheck-BuyCheck);
TrendSellshort=BuyCheck-f3*(SellshortCheck-BuyCheck);
Rangefilter=HighD(1)-LowD(1)>=MaxList((Highband+LowBand)*div,80*jumpPoint);
end;

// ************* Time Set Up *************
TimeOK = (time >= 0900 or time <= 2100) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = High+Range*0.5 ;
hortPrice = LowD(0)+OpenD(0)*RatioS/10000 ;

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

//判斷做多做空條件是否成立
// ******** Pivot Entry ***********
vars:LowCondA(false),HighCondA(false),LowCondB(false),HighCondB(false),LeftLen(5),RightLen(5) ;

LeftLen = Maxlist(5,LenA1) ;
RightLen = MaxList(5,LenB1) ;
LowCondA = PivotLowVS(1,Low,LeftLen,RightLen,RightLen+1 ) <> -1 ;
HighCondA = PivotHighVS(1,High,LeftLen,RightLen,RightLen+1 ) <> -1 ;

if TimeOK then begin
// 轉折低條件出現 則收盤價突破設定價位做多
   if MP <> 1 and LowCondA and Close < BuyPrice then
       Buy ("LE_PivotAT") next bar at BuyPrice stop;

// 轉折高條件出現 則收盤價破設定價位做空
   if  MP <> -1 and HighCondA and Close > ShortPrice then
       SellShort ("SE_PivotAT") next bar at ShortPrice stop;
end ;

//基本停利/移動停損出場
// ************* Base Exit *************
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;

if MP > 0 then Sell ("1Trail_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 ("1LPF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice+PF limit ;

if MP < 0 then BuytoCover ("1Trail_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 ("1SPF1_"+NumtoStr(PF,0)) All Contracts next bar at EntryPrice-PF limit ;

//多單部位獲利小於預期 平倉出場
if MP > 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 200)
then sell ("WrongEntryL15") next bar at Close stop ;

//多單部位虧損大於預期 反手做空
if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -700)
then sellshort ("WrongEntryL16") next bar at Close stop ;


// ************* Rev R-Break System Exit *************
// 持有空單 且收盤價大於 R-Break 買價 則突破K棒高點反手做多
if MP < 0 and Close > TrendBuy then Buy ("rSX_RBreak") next bar at High stop;

// 持有多單 且收盤價小於 R-Break 賣出價 則跌破K棒低點反手做空
if MP > 0 and Close < TrendSellshort then Sellshort ("rLX_RBreak") next bar at Low stop;


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






沒有留言:

張貼留言