COCO研究院

 找回密碼
 註冊
搜索
查看: 4143|回復: 0

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

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

參考文章 台指期貨的潛規則(II) - 平均振幅比例進場
// Public Variable

vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100) ;
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(0.0),ExtNum02(16.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(50.0),ShortMode(49.0),LE01(4.0),SE01(2.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) ;

// ************* Time Set Up *************
if LE01 = 4 then TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130) or time <= 0300) ;

//***************** BuyPrice & ShortPrice Setup *****************
if BuyMode = 50 then BuyPrice = HighW(1) ;
if ShortMode = 49 then ShortPrice = (OpenW(0)+CloseW(1))*0.5 ;

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

if EntNum01 = 2 or EntNum02 = 2 then begin
if TimeOK then begin

當振幅大於近期平均振幅且最低價大於近期收盤最高價 則在下根開盤價+平均振幅比例做多
if MP <> 1 and Range > Average(Range[1],LenA1) and Low > Highest(Close[1],LenA1) then
Buy ("MQS03_L1T") next bar at Open next Bar + Average(Range,LenA1)*FracA Stop;

當振幅大於近期平均振幅且最高價小於近期收盤最低價 則在下根開盤價-平均振幅比例做空
if MP <> -1 and Range > Average(Range[1],LenB1) and High < Lowest(Close[1],LenB1) then
SellShort ("MQS03_S1T") next bar at Open next Bar - Average(Range,LenB1)*FracB Stop;
end ;
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 = 2 then begin
if MP > 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then Sell ("WrongEntryL2") next bar at Close stop ;
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
then buytocover ("WrongEntryS2") next bar at Close stop ;
end ;

// ************* Rev Saita Range Exit *************

參考文章 賽塔支撐/壓力策略
vars:vSupport(0),vResistance(99999),LXtemp(0),SXtemp(0);

if ExtNum01 = 16 or ExtNum02 = 16 then Begin
if MP <> MP[1] and MP > 0 then vSupport = EntryPrice-PL ;
if MP <> MP[1] and MP < 0 then vResistance = EntryPrice+PL ;
if Close Cross over Average(High,HBar) then vSupport = Lowest(Close,HBar) ;
if Close Cross under Average(Low,LBar) then vResistance = Highest(Close,LBar) ;

LXtemp = MinList(EntryPrice-PL,vSupport-Range) ;
If MP > 0 and BarsSinceEntry > 0 and Close > LXtemp then SellShort ("rLX_Saita") Next Bar at LXtemp Stop ;

SXtemp = MaxList(EntryPrice+PL,vResistance+Range) ;
If MP < 0 and BarsSinceEntry > 0 and Close < SXtemp then Buy ("rSX_Saita") Next Bar at SXtemp 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;

台指期 22 分K 留倉 交易週期 2018/1/1 ~ 2022/12/31 交易成本 1200
MagicTest03_1.png
MagicTest03_2.png
MagicTest03_3.png
MagicTest03_4.png
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

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

GMT+8, 24-12-26 01:21

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

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