设为首页收藏本站[範例程式碼] 台指期全時段交易回測篇 [1]
开启辅助访问 切换到窄版

登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 233|回复: 0

[MultiCharts分析软件] [範例程式碼] 台指期全時段交易回測篇 [1]

[复制链接]
发表于 2023-2-18 11:19:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转外汇。

您需要 登录 才可以下载或查看,没有账号?立即注册

x

8 Z  e* b: n9 z% H& c5 u# R3 b, {
- v* a9 O6 R$ U9 P& U# \3 S測試以2018/01/01~2021/12/31的資料做為樣本內測試資料,2022/01/01~2022/12/31的資料做為樣本外的驗證,測試的方式將以部落格內舊文章的策略進場元素搭配一些策略產生器的出場或反手單元素做回測,當然按照慣例只會放預設參數,重點還是讓讀者自行做發想測試
0 ]4 Z# ^/ B/ D, J& E) ^  S9 EPS:因為是搭配策略產生器運作,程式碼內有些沒用到的參數就仍保留著!
( s; J" n4 R5 B9 [
4 M, Z: g+ O$ G// Public Variable% D( ?. y8 C' J5 O# |2 r% z1 e
vars:MP(0),PF(0),PL(0),DayLast(1345),NightLast(0500),BuyPrice(0),ShortPrice(0),BasePF(150),BasePL(100) ;) R0 a2 u+ B  X5 a1 C" X
vars:BarPass(5),HLRange(0),WinPoint(0),HBarPos(0),LBarPos(0),ExitH(0),ExitL(0),TimeOK(false) ;
/ S7 X( v' J6 i0 b
; V. \* F. |' I5 k
" r# `7 X: Z* q$ H2 s) M
vars:EntNum01(2.0),EntNum02(0.0),ExtNum01(31.0),ExtNum02(50.0),LenA1(20.0),LenB1(20.0),FracA(2.50),FracB(2.50),HBar(20.0),LBar(20.0),TradeProfit(0.020),TradeStopLoss(0.020),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(41.0),ShortMode(21.0),LE01(4.0),SE01(12.0) ;3 z6 a0 X, d: V3 n' Q$ p
7 q- r3 Q# P: \7 A+ a* F7 F5 Q

  w/ ~- Q6 P# \! \. \//****************** Basic Setup ****************************************0 C  J5 \' w5 V
8 v- \% b1 A" P% x
; A% D+ W# D0 ?. a: }
MP = MarketPosition ;0 K3 J6 B# j& [+ W
if MP <> 0 then Begin
7 Y0 K; F% B+ l; Z6 D* Q: lPF = EntryPrice*TradeProfit ;
. y, m7 v" I. ~( a" ?" x; pPL = EntryPrice*TradeStopLoss ;" I) _/ ]1 b+ R* r  ~' W
end else begin$ }/ t, z$ ?) L- w; H
PF = AvgPrice*TradeProfit ;
) w# i, h1 @/ E2 S# P# o/ f' i# e5 yPL = AvgPrice*TradeStopLoss ;
- C  N- l. E5 d( e  N0 }1 Dend ;# F4 t$ i3 u7 M3 r4 p
) b! [- x! m" A' T9 k4 @3 K

7 s% u- D8 F+ @; H& ]// 依據目前指數來設定最大停利與停損點數0 k2 V# D" R3 i9 ?9 j
if Close > 10000 then begin1 [# N4 x) ~+ c$ A/ E
BasePF = BasePF+IntPortion((Close-10000)/1000)*30 ;1 a% c8 E. T$ P
BasePF = MinList(BasePF,450) ;% g; i$ `; J9 |9 ~  P6 |
BasePL = BasePL+IntPortion((Close-10000)/1000)*15 ;" j0 a( ^+ R+ c  f6 x
BasePL = MinList(BasePL,225) ;5 `7 ?8 B5 H2 V6 `' g
end ;
! l: i1 @7 Y% X) L( X
9 c4 x9 ~% H( O+ A. F1 o/ @$ Y
# N$ n& l, K3 D" _. M' n* h" K' M0 ~
PF = MinList(PF,BasePF) ;  i% Z; b1 I; I6 ]) o$ c( \& N
PL = MinList(PL,BasePL)*iff(CurrentContracts=1,1,0.67) ;
5 m, T8 Y7 B  g7 ?0 q  ?; ]. p* X, x3 i: j" N: L
1 _; u' {; k: {3 e+ k  D( N5 B$ H# k
// ************3 Week High or Low *****************
2 ]+ n5 {: g0 P  O6 ?( tvars:WeekH(High),WeekL(Low),WeekHL(0),WeekHPB(0),WeekLPB(0),Week33(0),Week67(0) ;
; l, T; T" W. _8 jWeekH = MaxList(HighW(0),HighW(1),HighW(2)) ;- a. {8 E$ `2 |
WeekL = MinList(LowW(0),LowW(1),LowW(2)) ;
% S6 A4 |9 o1 N- _& {6 o7 [! A7 cWeekHL = WeekH - WeekL ;0 B8 ~; ^1 h* m  K
Week33 = WeekL+WeekHL*0.33 ;
9 v" i3 C0 H) f. p$ _4 dWeek67 = WeekL+WeekHL*0.67 ;( j: c% v, m/ |% A. }" Z1 w' {
WeekHPB = _BarsLast(High = WeekH) ;& v5 p* K! j. b7 E( ]
WeekLPB = _BarsLast(Low = WeekL) ;  z4 O5 L5 q2 b0 Y5 d, i

$ K9 d, K+ l  }) U, S
+ ~/ G8 }0 v( b3 A' L8 B8 Z
if DataCompression > 1 then begin
% g9 S' x1 h) _. V  u; H" JCondition1 = Close > Open and Open > Close[1] ;
0 J- @0 }! ~5 g% y  `" [! u2 J- lCondition2 = Close < Open and Open < Close[1] ;7 ?6 M0 O  [: @' Z
end else begin
$ |# W. O, p5 {' wCondition1 = Close > OpenD(0) and OpenD(0) > CloseD(1) ;9 O6 v9 P, D& h! b/ P: n2 I' d  m
Condition2 = Close < OpenD(0) and OpenD(0) < CloseD(1) ;
! n3 O% ^2 k  P9 Tend;
* H+ A6 Q, b$ s+ x6 y: f  E) [( b
( U* I: i) W/ b9 I5 w

  P/ |( v. u; B- t/ N4 p// ************* Time Set Up *************
# `: O8 A. g0 `if LE01 = 4 then TimeOK = ((time >= 0900 and time <= 1200) or (time >= 2130) or time <= 0300) ;& d5 E5 g; T, c

" @' W$ Z; u) n* p, p  ^. n* I6 b

0 I! G$ r$ \3 e// ********** Entry Method! F$ w+ O9 x. j' G4 \6 O' ~+ o, ^! ^/ d

/ D1 ~$ c8 U  T' z/ t) v: s& y. [1 R6 T
& U& o# l* @1 t" O' A' F4 ]5 @
Vars:OpenA(0),CloseA(0),OpenB(0),CloseB(0),NewClose(0),AVGTyp(0),AVGClose(0),LineCorss(-1),AvgMaxClose(0) ;
* g! ^+ g+ Y. w; L! C
/ K9 E% }; r7 z, W! C; Q( k% N
( d1 M; J+ \9 x, v$ h8 {
if CurrentBar > 1 then' t6 Z8 }* k8 J* x# r( P2 y
OpenA = (( Open[1] + High[1] + Low[1] + Close[1])/4 + OpenA[1])/2
5 k- B9 T: S- S* R- ~else OpenA = Open ;
7 s3 M, S; p0 \4 @$ o% q
8 J$ ]6 F' `/ e& P3 h# f

) [. Q& I( ^4 }& q' |& ACloseA = ((Open + High + Low + Close)/4 + OpenA + MaxList(High,OpenA) + Minlist(Low,OpenA ))/4 ;7 `. E( |) W2 h* o
CloseB = (Close + Open + High + Low )/4 ;! z, g# u3 H, X4 Q: u6 z5 O" D

! F: c0 F- _% Z7 P  s

) e( b6 E0 Z0 V4 F' c/ P# }* wif CurrentBar > 1 then OpenB = (OpenB[1]+CloseB[1])/2 else OpenB = Open ;, j  Y4 |0 s# y* R' i3 d6 ]2 m8 G
3 E' G( C' k( ?

: v8 v0 {" U' v; I$ h1 tNewClose = (CloseA+CloseB)/2 ;0 b/ B% h2 r3 J4 G/ V

0 C. u& V* r) m( `& }# R# ~
6 @# m* L4 ~# s
{ calculate averages of Typical Price and NewClose }
# |/ N) {/ N3 Q2 cAVGTyp = XAverage( TypicalPrice,LenA1) ;
) z# d( F  k. A1 q+ U% wAVGClose = XAverage( NewClose, LenB1 ) ;
' e# h% L5 j3 t% k2 n& ~% ]$ B7 W) ~5 g3 F) z7 N
8 e3 ~! ?  p# ?* y) J
if AVGTyp < AVGClose and Close > Open then LineCorss = 1- m7 X' Z+ ^# c4 ~, w
else if AVGTyp > AVGClose and Close < Open then LineCorss = 0 ;% \% v) |2 \; I2 g' F; L- L! {

# L* Y. n4 p, c. `  G/ a9 h% y
  {5 O7 K* a) [6 t
// 進場改用前週高低點做濾網% v  F' h9 e! m
if EntNum01 = 2 or EntNum02 = 2 then begin
9 d7 ~& ?( X, q" Gif TimeOK then begin' k& p7 W4 C; M& w/ s9 `: K
if MP <> 1 and Close > HighW(1) and LineCorss = 1 and LineCorss[1] = 0- L1 c  e; l/ H; ]
then Buy ( "MQS01_L1T" ) next bar at market ;
0 Z) Y5 f; m! j% uif MP <> -1 and Close < LowW(1) and LineCorss = 0 and LineCorss[1] = 1
( t5 v+ H; B7 i+ V; kthen SellShort ( "MQS01_S1T" ) next bar at market ;
4 i& Q' p1 p8 p5 ?end ;
& W; M, C0 v: N: [! u/ tend ;. ^) S, z' Z. ~4 {: t8 p
7 T+ r3 y, D/ A
5 C5 }5 a0 ~" [, |* f7 U+ b* v7 p
// ************* Base Exit *************
. ^2 C, ~1 f5 t0 ?  u7 `) lif MP <> 0 and BarsSinceEntry >= 1 then begin1 q, x0 q( U3 n! P8 ~2 H" J
if MP > 0 then Sell ("PL1_"+NumtoStr(PL,0)) next bar at EntryPrice-PL stop ;( s  A( L% T) ~3 l: u
if MP > 0 then Sell ("PF1_"+NumtoStr(PF,0)) next bar at EntryPrice+PF limit ;
5 l0 f; Z* b8 ?% t% _* Wif MP < 0 then BuytoCover ("PL2_"+NumtoStr(PL,0)) next bar at EntryPrice+PL stop ;
7 y( P9 H; l5 A7 T& iif MP < 0 then BuytoCover ("PF2_"+NumtoStr(PF,0)) next bar at EntryPrice-PF limit ;6 `( g2 C+ s' q0 x+ u8 M
end ;
7 ^* `1 ~2 P* y; t( t  O9 u1 Y& \

+ e: [0 w! @1 y* E4 eif SE01 = 12 then begin8 S, ?# h* s+ R$ i
if MP < 0 and (BarsSinceEntry <= 300/Barinterval and maxpositionloss/currentcontracts < -20000)
" C; T* i; R( S9 j! e) u1 wthen buy ("WrongEntryS8") next bar at Close stop ;
0 O9 \) j# F! m" kend ;
# @" i' q  U7 c% A9 r1 g8 C
/ Y" b' p! {3 y- q0 O2 `

( x0 e% s2 R1 r5 N// ************* no more high or low *************
) {8 p2 h+ ~  o$ |+ Eif ExtNum01 = 31 or ExtNum02 = 31 then begin; K4 I) D+ M) h  ^8 L
! p& b7 j+ M* E
+ a4 X2 q1 i" m6 @% E
// 近六根高低點區間大於100且多單連續破低 則平倉出場
0 E8 f9 |- R, C3 J: A$ g( I: fif MP > 0 and countif(Low < Low[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then1 M5 c  S$ e9 }9 ~: B8 G1 |. W
Sell ("LX_6Low") next bar at Lowest(Low,LBar) stop;& u' S9 j! l+ r4 o
5 T, K( {! P3 T0 b0 g/ r
- j- t+ f  C( x
// 近六根高低點區間大於100且空單連續過高 則平倉出場8 v5 q2 {6 w( B4 Z! ^2 E# Y
if MP < 0 and countif(High > High[1],6) >= 6 and Highest(High,6)-Lowest(Low,6) > 100 then
3 w* A7 @, [5 s- H; nBuytoCover ("SX_6High") next bar at Highest(High,HBar) stop;
7 U2 ?1 X3 |- u: y/ a. ]$ ]& p7 Q$ uend ;
* }8 E3 u# S0 c2 ~) l' o( x+ K# C  Q& C$ s$ L2 @) s
. T) G! X* y/ S: E( [, m' b# ^" G
// ************* Rev H/L PullBack turn *************- Z* Y$ @, d, B3 b- E( b
if ExtNum01 = 50 or ExtNum02 = 50 then begin
1 S% y; R# k; j
$ K0 N# u' V7 {$ G. D

$ O5 l! I+ _2 m3 \; |( R//近三週高點拉回 且破近三週低點 反手做空" V" e0 N, Z9 e3 }+ V0 W' K" E$ K$ ]
if MP > 0 and WeekHPB < LenA1 and Close > WeekL then
$ s2 X  |# x* E# k/ ISellShort ("rLX_WeekHPB") next bar at WeekL-Range stop;; E4 A7 B% r1 r: [
" F6 V& D- Z+ t8 r! f
% S3 b$ [5 \/ k  `6 k/ a
//近三週低點反彈 且過近三週高點 反手做多! h; O3 B! C  V1 i  g
if MP < 0 and WeekLPB < LenB1 and Close < WeekH then3 \4 n, S" a. O- U
Buy ("rSX_WeekLPB") next bar at WeekH+Range stop;) b* l$ H1 R2 \. s; [  @
end ;
: V  ~& a% o0 {3 A& k  H1 b+ s: q" R) e8 r' e

4 n- X3 N8 z3 t//結算日出場
& U1 X9 }" m) R6 k+ I; Y! Gif _IsSettlementDay and time >= CalcTime(1300,-1*BarInterVal) and Time <= 1330 then begin  r/ O7 |7 w1 ^3 @8 t9 a
if MP > 0 then Sell ("LX_Bal") next bar at market ;; u  D6 \/ N9 O. ]3 Z5 D' V# M
if MP < 0 then BuyToCover ("SX_Bal") next bar at market ;  S, R. {3 |# C5 J9 H
end;
5 ^& b3 ~: d, r: z2 \' @( Y$ r
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|骑士论坛 |网站地图

GMT+8, 2023-3-22 23:51 , Processed in 0.156319 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表