私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA% P! v! t) y: d# F, s6 g7 c: I
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。4 r+ {, c0 j/ P4 c: g4 ]; ?; O
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
( E3 T0 x& H. p2 [以下是制定这些规则的代码。% B' C5 c& `( V$ w8 L) [
//--- Indicator ATR(1) with EMA(8) used for the stop level...1 ?5 k5 k, \# S+ q+ j" m0 I
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ w+ @$ [; j& c" xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 D/ `* b8 r' Y' f//--- Define a variable that indicates that we have a deal...
3 c. c% c# {7 Gbool tem_tick = false;+ L; k5 Z9 b+ X# }& W. ]
//--- An auxiliary variable for opening a position
* ~& E7 X) S$ F9 _#include<Trade/Trade.mqh>
8 x+ y1 p9 d7 Y% e& ~* e#include<Trade/SymbolInfo.mqh>
4 N7 g  B0 ?  v4 kCTrade negocios;: ]+ ]$ E$ K* P! X3 x8 t
CSymbolInfo info;
6 {0 b1 ?; ~+ P6 P4 k8 L# q& u: [//--- Define in OnInit() the use of the timer every second
1 U# N7 J# J3 B6 y+ j! ~//--- and start CTrade& a( t+ E. N; F4 Q, u7 A
int OnInit()
9 }( J3 b0 k: @{
  ^& F2 w: U- i- }! m- U//--- Set the fill type to keep a pending order
7 o! B4 Z. n. Z( K' K3 ~0 K//--- until it is fully filled7 m; L0 H; k& m& T! r
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
* H3 `, N* v4 q7 t//--- Leave the fixed deviation at it is not used on B3 exchange
' O4 N2 x' K$ W5 l* E% I  x2 Mnegocios.SetDeviationInPoints(5);
, l; w( \' w  ?" y! f# `- a3 a) F//--- Define the symbol in CSymbolInfo...
" c  ?* J0 u6 R; Tinfo.Name(_Symbol);# S. Y% {; f5 U# u3 i) D
//--- Set the timer...; s( i; \7 ?4 p1 Z
EventSetTimer(1);
: Z  ?  W. ^6 Z! t//--- Set the base of the random number to have equal tests...1 c. _0 P, `- Y1 V! d1 l
MathSrand(0xDEAD);4 B/ U1 i6 U1 ]. p+ J# @
return(INIT_SUCCEEDED);
$ ?0 E% I, D5 T! o}) _, U1 Y) r# \' J& r  k) U; E
//--- Since we set a timer, we need to destroy it in OnDeInit().
5 V0 h- |- C" wvoid OnDeinit(const int reason)
% S8 |8 t( n# j/ E8 Y+ E6 Q{) d- n! a0 h9 h) m3 ?) s0 I
EventKillTimer();/ i& c( K, g# V: f! g! }/ m  b
}) e, _. V" f7 ~( n- d
//--- The OnTick function only informs us that we have a new deal& C  a. F! Z. q/ d: a
void OnTick()* Y7 I) T0 @( W( }' _8 r
{. D' g1 ?( a6 }  o0 K2 {
tem_tick = true;: x$ p8 }% B) a& k; x; W
}& k2 {+ _* n! M6 l
//+------------------------------------------------------------------+
$ E0 d7 a7 ^. K1 c9 U//| Expert Advisor main function                                     |) e! l/ o2 H( j- N
//+------------------------------------------------------------------+
- `9 V9 `6 \- C" dvoid OnTimer()2 T8 \. W0 [. c& @, q$ r
{0 L! l/ S" y2 j8 j% S' x
MqlRates cotacao[];9 T! b- w; N9 b& V% S" f" e
return ;
4 ]: Y# ^* [0 r. Q9 K7 E  _if (negocios_autorizados == false) // are we outside the trading window?
% L9 b6 _0 v% c. @( B& Zreturn ;
, i, X7 j/ M* o# J* F( Q//--- We are in the trading window, try to open a new position!
: C, X* ]& ^% C+ S7 O8 _( Uint sorteio = MathRand();
9 k3 C  j1 ~9 e  G//--- Entry rule 1.1" i! n% F# F  [6 H
if(sorteio == 0 || sorteio == 32767)
1 e  d# T1 c8 @2 Lreturn ;
8 b, H- J' A. x6 nif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
6 S0 o0 }  C* ]3 H{! p9 s+ n5 W: c7 d" ]+ m9 @" ]* n
negocios.Buy(info.LotsMin(), _Symbol);
7 M6 u0 o6 o! ?  \! L}5 n3 U. \1 |. I' b% y7 r* C
else // Draw rule 1.3 -- odd number - Sell
  ^/ W4 e- B+ S) A& i; L( Q{- w% I5 {8 b" _# b6 r7 f5 `
negocios.Sell(info.LotsMin(), _Symbol);
% |- S5 M8 U; l}
% l' W' b/ z% ^" {1 [}
; T/ E6 [4 W8 o$ a//--- Check if we have a new candlestick...
% w5 r" i# M4 o: a* Fbool tem_vela_nova(const MqlRates &rate)
' ]) c7 m" C0 l& f# }( w( v  s+ Q{
* l- Y" o& p5 @/ `# S{
0 F. I" j" c. ?7 C. }ret = true;
1 I  f1 F# C& D" Dclose_positions = false;- E7 L4 c: N3 D6 D+ G
}3 X; v" V: \( D/ f8 U
else
1 G) P% G! P: [4 K{% M: d1 z# D+ `2 M
if(mdt.hour == 16). r9 [: D  g. H! t
close_positions = (mdt.min >= 30);$ ^" Y# c8 R0 C  z9 r
}
' @* t# ?. H4 W* S0 ?}. x7 P' T7 m! |! ]
return ret;/ W6 V* V0 T- _+ \( ]
}( M; h4 a( K1 x7 V% L) a; F
//---# A8 v, O* S. L
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
4 W6 |0 C+ _1 k: o* g7 f/ l: L{
/ D" K5 C5 `! G& @$ H) G: Hif(PositionsTotal()) // Is there a position?. y& V4 Z4 ~# d# j2 {
{2 B! H) D; h4 Y- `) l
double offset[1] = { 0 };
1 W+ {# C3 B3 s/ u! z- Wif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?  |" G& L" U' v; Y& }
&& PositionSelect(_Symbol))  // Select the existing position!8 _% Q" ?. {! v, A' n1 {
{
$ v9 F2 H7 @( B- u' E  R0 TENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
6 J7 ?/ u9 Z5 {: v, `( f# udouble SL = PositionGetDouble(POSITION_SL);, K6 f% r) T1 I" ^$ A, S  q
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));& {8 l7 x1 T0 G' P% K4 Y
if(tipo == POSITION_TYPE_BUY)3 B- n7 R* r7 N/ D/ Q7 w
{
' M: L( r- U) A5 P* R/ _5 J0 Kif (cotacoes[1].high > cotacoes[0].high)
) q1 d; X" v; j% o0 H9 f! L{  c5 V7 }( h; n) p  l. v  N+ B- X
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];) U. X) w! H! M: u3 d8 b/ d" V
info.NormalizePrice(sl);& i: G. o" @. ^. B, q3 y0 _8 n
if (sl > SL)/ e; z7 w1 d* w" D. t
{
4 @  F& H  x% W6 V1 w/ l2 e8 D$ Inegocios.PositionModify(_Symbol, sl, TP);2 X5 C  M' c( ^4 ^0 |' E& x
}
* e3 W- v; ~1 f% M* _}' Q( S6 _6 ]# K8 K: ?- [1 T, P% g
}% }* R. `4 }" D% y; R9 ^/ j, q1 B
else // tipo == POSITION_TYPE_SELL/ L: V4 [' w% P- \8 j; I
{
) T0 W9 H3 J- T7 ]0 N) C' r& Kif (cotacoes[1].low < cotacoes[0].low)
. A- P/ t1 }8 q! v" v/ W' X7 n{" Z) o1 c* z" ?2 B' h- v* k
return true;
$ \, [) n7 _" G9 A: p% V' L. I5 k}. H5 Z- c' [: b9 S- T) p
// there was no position
7 I8 U7 g6 Q: k- Nreturn false;0 q0 ?& F" f2 X" \$ c
}
$ p: K6 G( p6 l* R# d; @% z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。/ q) D* [3 S: E' K! z* O0 ]
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-21 12:06 , Processed in 0.390959 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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