COCO研究院

 找回密碼
 註冊
搜索
查看: 10989|回復: 1

[範例程式碼] 台指期全時段交易回測篇 [4]

[複製鏈接]
發表於 23-2-28 08:22 | 顯示全部樓層 |閱讀模式

參考文章 台指期貨的潛規則(III) - 漲跌密碼
// 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) ;

vars:EntNum01(2.0),EntNum02(0.0),ExtNum01(22.0),ExtNum02(32.0),LenA1(20.0),LenB1(20.0),FracA(2.50),FracB(2.50),HBar(20.0),LBar(20.0),TradeProfit(0.025),TradeStopLoss(0.015),NBarL(20.0),NBarS(20.0),LenA2(20.0),LenB2(20.0),RatioL(2.50),RatioS(2.50),HighBand(20.0),LowBand(20.0),BuyMode(18.0),ShortMode(35.0),LE01(2.0),SE01(12.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)*iff(CurrentContracts=1,1,0.67) ;

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

//計算布林通道上下界線
// ************ BollingerBand *****************
vars:BBUP(0),BBDN(0),BBRange(0),BBLen(0) ;
BBLen = MaxList(LenA2,LenB2) ;
BBUP = BollingerBand(Close,BBLen,2) ;
BBDN = BollingerBand(Close,BBLen,-2) ;
BBRange = BBUP-BBDN ;

//計算 CDP 相關數值
// ************ CDP *****************
vars:CDP(0),AH(0),NH(0),NL(0),AL(0),CDPrange(0),CrossA(false),CrossB(false),TH1(0),TL1(0),TH2(0),TL2(0) ;
if Date <> Date[1] then begin
CDP = (HighD(1)+LowD(1)+2*CloseD(1))/4;
AH = CDP + (HighD(1) - LowD(1));
NH = CDP*2 - LowD(1);
NL = 2*CDP - HighD(1);
AL = CDP - (HighD(1) - LowD(1));
end ;
CDPrange = HighD(1) - LowD(1) ;

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

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

//***************** BuyPrice & ShortPrice Setup *****************
if BuyMode = 18 then BuyPrice = Maxlist(OpenD(0),OpenD(1),OpenD(2))+Range*0.5 ;
if ShortMode = 35 then ShortPrice = BBDn-TrueRange ;

// ********** Main Strategy *********

Vars:CloseGap(0),AvgGapTrend(0),Bias(0),PriceDir(0);

{Cumulative price change}
CloseGap = Summation(Close-Close[1],BarPass) ;
AvgGapTrend = Average(CloseGap,MinList(LenA1,LenB1)) ;

以紅黑K及近6根K棒最高/最低收盤價差距做濾網
Condition3 = (Highest(Close,6)-Lowest(Close,6)) > AvgTrueRange(6) and Close > Open ;
Condition4 = (Highest(Close,6)-Lowest(Close,6)) > AvgTrueRange(6) and Close < Open ;

PriceDir = Average(AbsValue(AvgGapTrend-CloseGap),MaxList(LenA1,LenB1)) ;

if PriceDir <> 0 then Bias = (AvgGapTrend/PriceDir)*5 ;

if EntNum01 = 2 or EntNum02 = 2 then begin
if MP <> 1 and _MagicQF001(1) > 0 and Condition3 and Bias > HighBand then Buy ("MQS04_L1T") next bar Highest(High,6) stop ;
if MP <> -1 and _MagicQF001(1) < 0 and Condition4 and Bias < -Lowband then SellShort ("MQS04_S1T") next bar Lowest(Low,6) stop ;
end ;

// ************* Base Exit *************
if MP <> 0 and BarsSinceEntry >= 1 then begin
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at EntryPrice-PL 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 EntryPrice+PL stop ;
if MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;
end ;

//進場後空單部位損失大於預期則做反手單
if SE01 = 12 then begin
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buy ("WrongEntryS8") next bar at Close stop ;
end ;

// CDP 的 AH/AL 價格跌破/突破做反手單
// ************* Rev CDP exit *************
if ExtNum01 = 22 or ExtNum02 = 22 then begin
if MP > 0 and CDPrange > MaxList(89,PF*0.382) and Close > AH then
SellShort ("rLX_CDP") next bar at AH-Minlist(Range,HBarPos*MinMove/PriceScale) stop ;
if MP < 0 and CDPrange > MaxList(89,PF*0.382) and Close < AL then
Buy ("rSX_CDP") next bar at AL+Minlist(Range,LBarPos*MinMove/PriceScale) stop ;
end ;

// ************* Rev no more high or low *************
if ExtNum01 = 32 or ExtNum02 = 32 then begin

// 近六根高低點區間大於100且多單連續破低 則反手單進場
if MP > 0 and countif(Low < Low[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
SellShort ("rLX_6Low") next bar at Lowest(Low,LBar) stop;

// 近六根高低點區間大於100且空單連續過高 則反手單進場
if MP < 0 and countif(High > High[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
Buy ("rSX_6High") next bar at Highest(High,HBar) stop;
end ;

//結算日出場
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;

台指期 11 分K 留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200

MagicTest04_4.png
MagicTest04_3.png
MagicTest04_2.png
MagicTest04_1.png
發表於 23-3-21 22:03 | 顯示全部樓層
感謝分享文章,謝謝
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-12-26 00:41

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 |