2024年7月6日 星期六

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

EasyTrader TestNo 040

 SuperTrend(超級趨勢指標)
 Supertrend是一個相當好用的技術指標,它是採用Average True Range(ATR)為基礎的Trailing Stop(移動止損)指標。該指標有兩個參數-ATR period(預設10)與Multiplier(預設3)。當Supertrend的指標線出現在股價下方,代表上升趨勢。它是上升趨勢中最高點往下算3個ATR,該線只向上畫,若股價跌破指標線,則反轉成下跌趨勢,Supertrend的指標線變成在股價上方。

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

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

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

//計算近期區間價格
// ************ High/Low data *****************
BarPass = MaxList(HBar,LBar) ;
HLRange = Highest(High,BarPass)-Lowest(Low,BarPass) ;
if MP <> 0 then WinPoint = MaxPositionProfit/BigPointValue else WinPoint = 0 ;
HBarPos = HighestBar(High,HBar) ;
LBarPos = LowestBar(Low,LBar) ;
if MP <> MP[1] and MP > 0 then ExitL = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then ExitH = EntryPrice+PL ;

//計算自訂義MACD價格
// ************ Macd *****************
vars:vDif(0),vMacd(0),vOsc(0),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) ;

//計算一目均衡表區間價格
// ************ 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 ;

HighCloud = AheadLine1[MidTerm] ;
LowCloud = AheadLine2[MidTerm] ;
Cloud = AbsValue(HighCloud-LowCloud) ;

CondL = Close > HighCloud and HighCloud > LowCloud ;
CondS = Close < HighCloud and HighCloud < LowCloud ;

//計算波動變化
//* Volatility Cluster Setup
Vars: Deviations(2), Length(18),BarLength(3);
Vars: CloseToClose(0), CTCDeviations(0), RangeDeviations(0), BigVolatility(False);

Length = MaxList(LenA1,LenB1) ;

{Setup calculations}
CloseToClose = AbsValue(Close - Close[1]);
CTCDeviations = Average(CloseToClose, Length) + StdDev(CloseToClose, Length) * Deviations;
RangeDeviations = Average(Range, Length) + StdDev(Range, Length) * Deviations;
BigVolatility = CloseToClose > CTCDeviations[1] OR Range > RangeDeviations[1];

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

// ************* Time Set Up *************
TimeOK = ((time >= 0900 and time <= 1300)) ;

//選擇進場時機
// ************* Entry/Exit Condition Set Up *************
vars:MoneyLoss(false),LossCount(false),MoneyProfit(false),ProfitCount(false),ProfitCond(false),LossCond(false),ExitLen(0) ;

EntryExitCond = BarsSinceExit(1) > MinList(LenA1,LenB1) ;

//***************** BuyPrice & ShortPrice Setup *****************
BuyPrice = HighD(0)+MaxList(TrueRange*RatioL/10,6)  ;
ShortPrice = HighD(0)-OpenD(0)*RatioS/10000  ;

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

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

//TrendDir > 0 多方趨勢   TrendDir < 0 空方趨勢
TrendDir = 多空方向判斷;

   if EntryExitCond then begin
//多方趨勢 且MACD的DIF值大於0 則收盤價突破設定價位做多
      if MP <> 1 and TrendDir > 0 and uDif > 0 and Close < BuyPrice then 
         Buy ("LE_Super1") next bar at BuyPrice Stop ;
//空方趨勢 且MACD的DIF值小於0 則收盤價突破設定價位做多
      if MP <> -1 and TrendDir < 0 and uDif < 0 and Close > ShortPrice then 
         SellShort ("SE_Super1") next bar at ShortPrice Stop ;
   end ;

   if EntryExitCond then begin
//多方趨勢 且收盤價拉回一段時間 並大於近3日區間價2/3 則收盤價突破設定價位做多
      if MP <> 1 and TrendDir > 0 and Close > Day67 and DayHPB > LenB1 and Close < BuyPrice then 
         Buy ("LE_Super6") next bar at BuyPrice Stop ;
//空方趨勢 且收盤價反彈一段時間 並小於近3日區間價1/3 則收盤價突破設定價位做空
      if MP <> -1 and TrendDir < 0 and Close < Day33 and DayLPB > LenB1 and Close > ShortPrice then  SellShort ("SE_Super6") next bar at ShortPrice Stop ;
   end ;

//基本停利+移動停損出場
// ************* Base Exit *************
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at HighSinceEntry-MinList(PL,PF) stop ;
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) next bar at EntryPrice+PF limit ;
if MP < 0 then BuytoCover ("PL2_"+NumtoStr(PL,0)) next bar at LowSinceEntry+MinList(PL,PF)*0.75 stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;

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

// ************* General Exit *************
// 持倉部位 依進場時間長短及不同獲利程度 平倉出場

   if WinPoint < PL and BarsSinceEntry < BarPass then begin 
if MP > 0 and Close > (EntryPrice-PL) then Sell ("LX_GE0") next bar EntryPrice-PL stop
else if MP < 0 and Close < (EntryPrice+PL) then BuytoCover ("SX_GE0") next bar EntryPrice+PL stop ; 
   end else 
   if WinPoint < PL and BarsSinceEntry >= BarPass then begin 
if MP > 0  and Close > maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) then 
Sell ("LX_GE1") next bar maxlist(EntryPrice-PL ,Lowest(Low,BarPass)) stop
else if MP < 0 and Close < minlist(EntryPrice+PL ,Highest(High,BarPass)) then 
   BuytoCover ("SX_GE1") next bar minlist(EntryPrice+PL ,Highest(High,BarPass)) stop ; 
   end ; 

//持有空單 且波動變大 且收盤價在紅色雲層上 突破設定價位 反手做多
   if MP < 0 and BigVolatility and CondL and Close < BuyPrice then 
      Buy ("rSX_Cluster10") next bar at BuyPrice stop;
//持有多單 且波動變大 且收盤價在藍色雲層下 跌破設定價位 反手做空
   if MP > 0 and BigVolatility and CondS and Close > ShortPrice then 
      SellShort ("rLX_Cluster10") 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 ~ 2022/12/31 交易成本 1200








沒有留言:

張貼留言