2024年8月13日 星期二

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

EasyTrader YM_Test  01

布林線是由上限和下限兩條線構成的路徑型指標。當股價向上突破上限時視為超買,有回檔調整的可能,發出賣出信號;當股價向下突破下限時視為超賣,有反彈的要求,發出買入信號。

布林通道要完整運用突破,有四個基本元素:
1.布林通道上緣及下緣(Bollinger Band)
2.布林通道的寬度(Band Width)
3.過去一段時間停留在通道的位置(B%) 。
4.成交量放大(Volume)
這個交易策略是利用 1 & 2的元素去發想的
// 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) ;

// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;

//計算RSI值
// ************ RSI *****************
vars:vRSIA(0),vRSIB(0),DivRSICondA(false),DivRSICondB(false) ;
vRSIA = RSI(Close,LenA1) ;
vRSIB = RSI(Close,LenB1) ;

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

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = Maxlist(OpenD(0),OpenD(1),OpenD(2))+Range*0.5 ;
ShortPrice = HighW(0)-MaxList(TrueRange*RatioL/10,6*jumpPoint) ;

TimeOK = (time >= 2300 or time <= 1500) ;

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

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

// ********** Main Strategy *********
// ********** Entry Method
// ******** BB band Entry ***********
//計算布林通道區間值
vars:BBUP(0),BBDN(0),BBRange(0),BBLen(0),DataBase(Close),ShrinkDn(false) ;
DataBase = StdDev(Close,20)*4 ;
   
BBUP = BollingerBand(DataBase,BBLen,2) ; 
BBDN = BollingerBand(DataBase,BBLen,-2) ;
BBRange = BBUP-BBDN ;

ShrinkUp = Countif(BBUP > BBUP[1],2) = 2 and Countif(BBUP < BBUP[1],4) = 2 ;
ShrinkDn = Countif(BBDn < BBDn[1],2) = 2 and Countif(BBDn > BBDn[1],4) = 2 ;
Shrink = ShrinkUp or ShrinkDn ;

if EntryExitCond then begin
//BBand 指標線收縮後放大 且收盤價大於軌道中值 則收盤價突破設定價位做多
   if  Condition98 and MP <> 1 and Shrink and Close > Average(Close,BBLen) and Close < BuyPrice then  Buy ("LE_Shrink") next bar at BuyPrice stop ;

//BBand 指標線收縮後放大 且收盤價小於軌道中值 則收盤價破設定價位做空
   if Condition99 and MP <> -1 and Shrink and Close < Average(Close,BBLen) and Close > ShortPrice then SellShort ("SE_Shrink") next bar at ShortPrice stop ;
   end ;
end ;

if TimeOK then begin
//RSI 大於70 且近期K棒多數 RSI值大於45 則收盤價突破設定價位做多
      if Condition98 and MP <> 1 and vRSIA > 70 and Countif(vRSIA>45,LenA1) > LenA1*0.7 
         and Close < BuyPrice then Buy ("LE_RSIt") next bar at BuyPrice stop;

//RSI 小於30 且近期K棒多數 RSI值小於55 則收盤價跌破設定價位做空
      if Condition99 and MP <> -1 and vRSIB < 30 and Countif(vRSIB<55,LenB1) > LenB1*0.7 
         and Close > ShortPrice then Sellshort ("SE_RSIt") next bar at ShortPrice stop;
end ;

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

if Mod(SE01,3) = 1 then begin
   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 ;
end ;

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

//空單部位虧損大於預期 平倉出場
  if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -700)
      then buytocover ("WrongEntryS12") next bar at Close stop ;

// ************* Rev EntryPrice PullBack *************
//持有多單 且進場一段時間 拉回跌破設定價位 反手做空
   if MP > 0 and BarsSinceEntry > NBarL then 
      SellShort ("rLX_EntPricePB") next bar at EntryPrice - MaxList(FracA*AvgTrueRange(BarPass),100*jumpPoint) stop;

//持有空單 且進場一段時間 反彈突破設定價位 反手做多
   if MP < 0 and BarsSinceEntry > NBarS then 
      Buy ("rSX_EntPricePB") next bar at EntryPrice + MaxList(FracB*AvgTrueRange(BarPass),100*jumpPoint) stop;


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







2 則留言:

  1. Condition98 = 多方交易濾網
    Condition99 = 空方交易濾網

    請問這兩個濾網的語法哪裡可參考呢?

    回覆刪除
    回覆
    1. 這是從 Larry williams 短線交易秘訣 書內的交易日的概念發想而來
      也可以參加 MultiCharts策略開發北中南巡迴研討會進一步了解

      刪除