2024年10月13日 星期日

小道瓊(YM)全時段交易回測篇 [03]

EasyTrader YM_Test 03
參考文章 特別的均線評分策略 - 移動平均匯合

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

Condition98 = 多方交易濾網 ;
Condition99 = 空方交易濾網 ;

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

//計算均線值
// ************* BreskThrough 3 Line *************
vars:Avg7(0),Avg13(0),Avg21(0),Avg60(0) ;
vars:Max3L(0),Min3L(0);

Avg7 = Average(Close,7) ;
Avg13 = Average(Close,13) ;
Avg21 = Average(Close,21) ;
Avg60 = Average(Close,60) ;
Max3L = MaxList(Avg7,Avg13,Avg21) ;
Min3L = MinList(Avg7,Avg13,Avg21) ;

//計算KDJ值
// ************ KD *****************
vars:vKA(0),vKB(0),vDA(0),vDB(0),vJA(0),vJB(0),DivSlowKCondA(false),DivSlowKCondB(false) ;
vKA = SlowK(LenA2) ;
vDA = SlowD(LenA2) ;
vJA = 3*vDA-2*vKA ;
vKB = FastK(LenB2) ;
vDB = FastD(LenB2) ;
vJB = 3*vDB-2*vKB ;

//計算一目均衡表數值
// ************ 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) ;
vars: CondL(false),CondS(false) ;

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) ;
CondL = Close > HighCloud and HighCloud > LowCloud ;
CondS = Close < HighCloud and HighCloud < LowCloud ;

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

// ************* Time Set Up *************
TimeOK = (time >= 1900 or time <= 0700) ;

// ************* Entry/Exit Condition Set Up *************
vars:MoneyLoss(false),LossCount(false),MoneyProfit(false),ProfitCount(false),ProfitCond(false),LossCond(false),ExitLen(0) ;
if Totaltrades > 6 then begin
    MoneyLoss = positionprofit(1)+positionprofit(2) < -1*(Close*0.01)*Bigpointvalue ;
    LossCount = positionprofit(1) < 0 and positionprofit(2) < 0 and positionprofit(3) < 0 ;
    LossCond = MoneyLoss or LossCount ;
    MoneyProfit = positionprofit(1)+positionprofit(2) > (Close*0.02)*Bigpointvalue ;
    ProfitCount = positionprofit(1) > 0 and positionprofit(2) > 0 and positionprofit(3) > 0 ;
    ProfitCond = MoneyProfit or ProfitCount ;
end else begin
    LossCond = true ;
    ProfitCond = true ;
end ;

ExitLen = iff(ProfitCond,1080/BarInterval,iff(LossCond,2160/BarInterval,MaxList(LenA1,LenB1))) ;

EntryExitCond = BarsSinceExit(1) > ExitLen ;


//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(1)+AvgTrueRange(LenA1)*FracA ;
ShortPrice = OpenW(0)-MaxList(TrueRange*RatioL/10,6*jumpPoint) ;

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

//計算移動平均匯合分數值 MACS
// ******** MACS Entry ***********
vars:Crossup(0),CrossDn(0),LineScore(0),FastLen(0),SlowLenA(0),SlowLenB(0),UpScore(0),DnScore(0),LengthA(1);
CrossUp = 0 ;
CrossDn = 0 ;
LengthA = 1 ;
for LengthA = 1 to 20 Begin
    FastLen = LengthA ;
    SlowLenA = IntPortion(FastLen * FracA)+1 ;
    SlowLenB = IntPortion(FastLen * FracB)+1 ;
    if Average(Close,FastLen) > Average(Close ,SlowLenA) then Begin
        Crossup = Crossup + 1 ;
        UpScore = CrossUp * 5 ;
    end;
    if Average(Close,FastLen) < Average(Close ,SlowLenB) then Begin
        CrossDn = CrossDn + 1 ;
        DnScore = CrossDn * 5 ;
    end;
end;

if TimeOK then begin
//據前次出場一段時間後 且MACS分數大於設定值 則收盤價突破設定價位作多
    if Condition98  and MP <> 1 and BarsSinceExit(1) > Hbar*0.5
        and UpScore > HighBand*4 and Close < BuyPrice then
        Buy ("LE_MACST") next bar at BuyPrice stop ;

//據前次出場一段時間後 且MACS分數小於設定值 則收盤價跌破設定價位作空
    if Condition99  and MP <> -1 and BarsSinceExit(1) > LBar*0.5
        and DnScore < LowBand*4 and Close > ShortPrice then
        Sellshort ("SE_MACST") next bar at ShortPrice stop ;
end ;

// 計算KD值
// ******** Slow KD Entry ***********
vars:SKbuy(false),SKshort(false) ;
SKbuy = vKA > vDA and vKA > 55;
SKshort = vKB < vDB and vKB < 45 ;

if EntryExitCond then begin
// K值多方趨勢 且最低價上升 則收盤價突破設定價位作多
    if Condition98   and MP <> 1 and SKbuy and Low > Low[LenA1*0.5] and Close <  BuyPrice then Buy ("LE_SK") next bar at BuyPrice stop;
// K值空方趨勢 且最高價下降 則收盤價跌破設定價位作空
    if Condition99  and  MP <> -1 and SKshort and High < High[LenB1*0.5] and Close >  ShortPrice then Sellshort ("SE_SK") next bar at ShortPrice stop;
end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
if Mod(SE01,3) = 0 then begin
if MP > 0 then begin
if WinPoint <= 2500 then Sell ("0Trail_PL2_"+NumtoStr(Absvalue(EntryPrice-(HighSinceEntry- MinList((PL+PF)*0.5,PL))),0)) All Contracts next bar at HighSinceEntryMinList((PL+PF)*0.5,PL) stop
else Sell ("0Trail_PL3_"+NumtoStr(Absvalue(winpoint*0.5),0))
All Contracts next bar at HighSinceEntry-winpoint*0.5 stop ;
end ;
if MP < 0 then begin
if WinPoint <= 2500 then BuytoCover ("0Trail_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
else BuytoCover ("0Trail_SL3_"+NumtoStr(Absvalue(winpoint*0.5),0))
All Contracts next bar at LowSinceEntry+winpoint*0.5 stop ;
end ;
end ;

//多單部位獲利不如預期 反手做空
if MP > 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 200)
then Sellshort ("WrongEntryL3") next bar at Close stop ;

//空單部位獲利不如預期 反手做多
if MP < 0 and (BarsSinceEntry >= 300/Barinterval and MaxPositionprofit/currentcontracts < 200)
then Buy ("WrongEntryS3") next bar at Close stop ;

//持有空單 且收盤價上穿60均線 且收盤價在雲層之上 突破K棒高點 反手做多
if MP < 0 and Close Cross over Avg60 and HighCloud > LowCloud and Close > HighCloud then
    Buy ("rSX_MoKu4") next bar at High Stop ;

//持有多單 且收盤價下穿60均線 且收盤價在雲層之下 突破K棒高點 反手做空
if MP > 0 and Close Cross under Avg60 and HighCloud < LowCloud and Close < HighCloud then
    SellShort ("rLX_MoKu4") next bar at Low Stop ;

小道瓊 留倉 交易週期 2018/1/1 ~ 2024/5/31 交易成本 USD 30









沒有留言:

張貼留言