私募

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

期货量化软件:赫兹量化中为智能系统制定品质因数

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA2 a! q) Y# C6 `; b+ a9 M0 S
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
4 j2 l5 }( a) f2 S' ]8 X% z为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
5 I* F1 F% D/ _0 x: Y以下是制定这些规则的代码。; ]/ d5 @. Z2 H6 }% H# ~4 E* r3 V" K
//--- Indicator ATR(1) with EMA(8) used for the stop level...
  m1 E) D4 E  J+ o: |int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
8 w' v& g: N! Q& t: i$ lint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
' O' |: P8 @5 w1 j//--- Define a variable that indicates that we have a deal...1 j+ @+ e, V% W6 |4 h: K: r9 R& x
bool tem_tick = false;4 M5 ]- Q$ x- ?& Z: s" p2 F; |# {4 v
//--- An auxiliary variable for opening a position3 G8 C6 I7 m3 Q# @0 O' [5 K5 F% `
#include<Trade/Trade.mqh>' K7 P' O% M& @
#include<Trade/SymbolInfo.mqh>
7 L+ W% C- C2 J' }" `CTrade negocios;' {3 j4 h3 G9 f0 Q& X+ Y
CSymbolInfo info;
5 {' S, c$ v' y//--- Define in OnInit() the use of the timer every second6 j* G+ d# H, e7 r; I  i
//--- and start CTrade
" I, a9 v4 [7 a1 r/ Y0 sint OnInit(), U# {0 h1 B3 s- f
{
2 D' e+ j$ W% v' I//--- Set the fill type to keep a pending order! P% }9 s1 l$ c  Q3 c% V: ?  p
//--- until it is fully filled
3 q! Y* b+ k- |$ E% |$ h. hnegocios.SetTypeFilling(ORDER_FILLING_RETURN);4 U1 s, |5 _* K' r
//--- Leave the fixed deviation at it is not used on B3 exchange
! Q" ]- S$ w2 [) b  i/ g* gnegocios.SetDeviationInPoints(5);! g- j% v  ^% ^7 q
//--- Define the symbol in CSymbolInfo...
& m6 n7 V+ [/ Ainfo.Name(_Symbol);0 T5 B- t7 n8 {, w2 ^' O3 s" R
//--- Set the timer...7 l. f$ p; c* w: f/ ]
EventSetTimer(1);# T  g8 j; ?7 X$ q# U5 m. g
//--- Set the base of the random number to have equal tests...) }: b6 E* ~7 _: ?$ V( S" @  w
MathSrand(0xDEAD);
' ~! }! K" \5 c9 B/ _return(INIT_SUCCEEDED);, f8 u/ ~" u. V4 }: v8 ?7 `
}
5 W3 g" W" ^( L9 _; B& U//--- Since we set a timer, we need to destroy it in OnDeInit().
& A+ ]0 R! z! A$ ~void OnDeinit(const int reason)6 W& f  l5 ~& W, q* e$ ]
{  G/ R7 Z/ m/ G2 z) U- A
EventKillTimer();
2 }- G  Y( j9 b1 O4 \}
2 P" s: h6 |# S" R! @% k8 o//--- The OnTick function only informs us that we have a new deal1 h0 q! A' g$ t& n% S% s+ N
void OnTick()
) m; m6 h7 c  e5 i& p{
: M$ h7 |5 D4 N/ x' b  Ktem_tick = true;
! k) ^" o* g) H# v  Y5 Y+ n% W! t}* l! {) m& u$ \- A
//+------------------------------------------------------------------+
9 R8 B2 @1 v  M, c8 U% h//| Expert Advisor main function                                     |7 `* s8 T8 o5 X9 q1 E! \
//+------------------------------------------------------------------+* p  D: h' H$ v( Z( ]) e
void OnTimer()* |& g( D6 V/ ]5 r  g) v
{
3 W  M- z3 S2 T0 |0 o& d8 fMqlRates cotacao[];! K* x$ A& ?4 \# }
return ;9 U( M$ U/ Q) F7 h2 d6 b- S
if (negocios_autorizados == false) // are we outside the trading window?
/ l, E. T( `! g5 J9 ereturn ;5 N6 t0 `9 ~3 r) A
//--- We are in the trading window, try to open a new position!
8 S4 a5 V4 p- t+ Z: H! z1 w" rint sorteio = MathRand();
$ ^4 ~+ i; S+ o0 F3 O8 U+ t2 j" D//--- Entry rule 1.1  K) n6 ]' K/ K
if(sorteio == 0 || sorteio == 32767)
" b  s2 W+ h  R* z& oreturn ;
6 R9 E/ g+ e. m- f* i9 ^5 w) Eif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; S0 f0 c1 s' q; b1 W% r{
& L0 q+ U3 i2 E1 N0 c! anegocios.Buy(info.LotsMin(), _Symbol);) s3 @; J1 ^# Y+ i: d! o0 K
}
( ?# d6 e" Y9 W6 d) [' Delse // Draw rule 1.3 -- odd number - Sell7 _$ ~! t4 J! p; S' {3 f9 C+ G# R
{
6 C/ b( v, g- I5 m3 V! N( Ynegocios.Sell(info.LotsMin(), _Symbol);
9 J( m6 C) v  s! I}! I# D3 I  A1 `9 a# Q* S: s
}, M3 I( G2 {9 a- V% K8 v
//--- Check if we have a new candlestick...
( D  T9 K  b! N' M4 Fbool tem_vela_nova(const MqlRates &rate)
5 a1 _  r2 @) h2 W( ?{/ I- _8 W8 j' z, z, i  v5 T# ?! R  V
{: S1 \: G0 ?: U
ret = true;
2 G. ^/ p4 p0 ?$ ?/ Zclose_positions = false;
0 k+ j# S) ~* _* f; F}
6 @. |* N# J  S3 V5 {else4 ^4 V4 b7 ^. V6 |
{
7 O1 ^& Q- y5 [0 C2 rif(mdt.hour == 16)
1 K; V: }* |, n; P" _9 U! y6 iclose_positions = (mdt.min >= 30);3 r: x! S5 O" U, M" }6 l; ~' D) }
}
9 C+ D4 j# o; E2 Z* ^+ N# W4 k}7 E( U  c2 y" ^
return ret;) p4 t" m# C- {( B' p
}
- C; V% ]! J; D, L: [+ Y& \2 S//---9 C. L0 u/ a0 ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 Q# i8 K- D8 w2 M, J( N; b6 e5 [5 C1 E{& J& I; \5 u3 H- j( V
if(PositionsTotal()) // Is there a position?  }6 k/ s. ?0 y% {1 z: D
{. W, N. u$ W* P" d& b8 v9 g1 H2 A/ [
double offset[1] = { 0 };1 l0 Z1 f8 A' X/ J+ U0 i) q: t6 m/ C2 F
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?9 e0 o* Z2 K, Z
&& PositionSelect(_Symbol))  // Select the existing position!1 Z4 B4 }) q2 U1 G+ t- f+ J
{
: q+ B4 [2 y2 j7 G' r" QENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
8 P1 k  X$ p4 {* W# B0 Z& ddouble SL = PositionGetDouble(POSITION_SL);( {# |- G" p& J. H
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, _; D7 o  y# L$ z+ m
if(tipo == POSITION_TYPE_BUY)
+ {" z, Y3 B  E' A3 _$ }{
9 K$ H0 l0 z& c3 yif (cotacoes[1].high > cotacoes[0].high)0 r2 q" w5 m2 p, F: y  V
{
( [& b9 ^. f0 ]double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];9 ]0 S$ T: D+ Y
info.NormalizePrice(sl);
! N) g3 a8 d* |' Nif (sl > SL)9 d8 v. W0 x2 t2 @! ~/ G
{: ?5 Q5 x7 l: n9 H" h9 t: f& V; {
negocios.PositionModify(_Symbol, sl, TP);
1 k  F# Y3 M) g, t  M. v6 s}
2 T# D3 ~# h0 Z" K# }}+ i9 W- _: N* b3 {. y/ m" l
}
/ H. J+ s8 G) r  l) g8 T$ z# Felse // tipo == POSITION_TYPE_SELL
, f! H: @6 y: J8 Q{( I' s1 F" h! C9 L; ]
if (cotacoes[1].low < cotacoes[0].low)
6 b& R7 V! l" X4 b# a/ u{/ t# Q% _7 ~( f
return true;7 {- _) \" j! w. O+ G% r# e- B; z2 e
}1 S- E: A( T. T; I* E- _( L
// there was no position
0 k7 c$ b8 A' v& Ureturn false;' E& D( q% M, c/ d6 \
}
* c4 B! C: I0 q% E我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。5 P3 O4 V! m# X  |- {" r
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-1-24 18:23 , Processed in 0.394354 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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