私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 C" T- g# j4 I, X
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( X" S. m% j2 G& \为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 ?( N9 x( M+ k' [! F# N
以下是制定这些规则的代码。7 ?! M% n, u, D  V2 L/ W* l: a7 U
//--- Indicator ATR(1) with EMA(8) used for the stop level..." M& e9 }5 @5 x, j
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 H; o' S+ l$ N) ^' Oint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 C' e. E$ L5 z5 `; I& s//--- Define a variable that indicates that we have a deal...
- o' y- N2 l* ^. |6 Ubool tem_tick = false;
6 l/ E5 U# y( C: @+ A//--- An auxiliary variable for opening a position
4 h) \4 G1 a5 w' u4 F) z0 {#include<Trade/Trade.mqh>5 T8 u% W3 c* p8 l8 D5 P- F! c% n
#include<Trade/SymbolInfo.mqh>
" J8 S4 ?2 O) p( G. _! f; O& |CTrade negocios;; S7 L" j% `" c8 Z
CSymbolInfo info;
# _5 @7 y: J2 g4 _9 M4 _+ T//--- Define in OnInit() the use of the timer every second
" Q* x, \+ F3 R//--- and start CTrade
* i8 C7 U) k! I# ?# w( ^int OnInit()
: ?) N+ y9 w; _9 J$ e; @' M{
% H9 _5 E6 Y" ~$ ^//--- Set the fill type to keep a pending order$ z) _! D4 r- t1 M/ f
//--- until it is fully filled
- \# h# u" D, r0 P8 ^negocios.SetTypeFilling(ORDER_FILLING_RETURN);" O0 ?: r3 M- ?$ S
//--- Leave the fixed deviation at it is not used on B3 exchange6 ], X$ |1 k) E1 A3 e0 O" T
negocios.SetDeviationInPoints(5);
  {) V( {$ B' b4 U& _' A//--- Define the symbol in CSymbolInfo...- p& |) y$ ^$ ~6 L) O
info.Name(_Symbol);( S6 k9 h9 x! }" L: y3 n
//--- Set the timer...
3 }! U: L# l* D* ?( Y/ I! A! _EventSetTimer(1);
( q; \" Q4 _$ i  B4 g% `: f' |! N//--- Set the base of the random number to have equal tests...
+ k2 ^5 `5 O4 O% Y: j/ r/ pMathSrand(0xDEAD);
1 L4 z: M+ O" N% B6 h; Creturn(INIT_SUCCEEDED);
( r; J% q% ~8 `2 X1 r}
4 S4 }8 o/ E* J2 Q* r' Z6 s0 R//--- Since we set a timer, we need to destroy it in OnDeInit().
: ~* Q6 @* G7 |+ i& pvoid OnDeinit(const int reason)1 t# `8 b% O9 p4 C& R5 T( X
{
5 p8 L- W- {9 H* R& N" xEventKillTimer();
2 a- Y5 I/ j9 v9 i6 t6 B# ]* w}7 k% u4 ]% F' a: J! i$ S7 `
//--- The OnTick function only informs us that we have a new deal5 O3 W& d' y% F9 s+ S& w
void OnTick()
) ?# W- e! Q  I5 O- J4 A{  s1 v3 C% Z6 X' i* ]  y% Y8 D
tem_tick = true;
6 w' N. |+ _7 ~8 [4 L8 C$ V}( U  Y: U3 L& B4 Y; n
//+------------------------------------------------------------------+
3 y6 u5 ^8 M+ F( W% T//| Expert Advisor main function                                     |. p: s# T7 f: G3 y3 g! j
//+------------------------------------------------------------------+
% W* B: z0 @; `% n: zvoid OnTimer()
: }6 B* e3 k, D1 l& e0 T' K{
# j* V3 C% O5 Q' S% |& ~* @* yMqlRates cotacao[];
) F' {/ b" I- V# m7 Rreturn ;
; g1 i7 y# I5 h1 Y& Iif (negocios_autorizados == false) // are we outside the trading window?% v8 u; m3 g2 U& a/ S) A
return ;: O" ~8 r: u2 S/ ~9 _% I- c) F
//--- We are in the trading window, try to open a new position!4 k9 U# O# y8 j9 r( `
int sorteio = MathRand();
4 I& f6 a8 |( X4 S8 I//--- Entry rule 1.1
# w6 m5 O0 H0 k% m! q1 p/ fif(sorteio == 0 || sorteio == 32767): Y& y' f' W' ^# B- ]
return ;
* c0 R7 C7 e. l3 ~: e' E. p9 y. oif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
; |- D" z: A& `% a{
) D% ]  m1 W; y6 `% Z/ bnegocios.Buy(info.LotsMin(), _Symbol);  [( e3 C9 U( |1 L( k
}
: O- x4 m+ B1 `% W, l1 Felse // Draw rule 1.3 -- odd number - Sell" c0 a+ i6 O: ~
{8 i' P7 D1 P' Y0 N  h  {* S0 E
negocios.Sell(info.LotsMin(), _Symbol);
, K+ E, n8 g; u# q3 q: N7 {5 i1 P- H9 R- X}6 W; O) L: c& C. G, ?
}
% i; D" n( J/ W& x//--- Check if we have a new candlestick..., P  D8 ^' H0 V' J) [
bool tem_vela_nova(const MqlRates &rate)
: s3 M8 w- ]( }" ?, g{
4 ]' h4 a5 d% q1 b( J* @8 F{
: a' |+ t4 @- B( {ret = true;
, H4 g& o; k0 K% S6 }8 O& [close_positions = false;7 z5 F; ^1 u) D7 N! r
}
4 o' H' n7 D2 }+ d  {else. E8 {( J1 z+ J2 Z6 Y& u! A: i4 Q
{
6 B/ {) h% d6 R; w* Qif(mdt.hour == 16)
9 o+ P/ y+ ~5 f# Xclose_positions = (mdt.min >= 30);
' V* ~- x- I, R( s}2 r2 O* n( {; f$ z1 N
}+ R; P( p$ p" |8 w& j" {
return ret;/ j, Z2 ~5 t. U8 p! w+ O* y
}
3 n% X% [* W  }1 U0 z( g! U0 \1 J//---4 R( `/ U. W1 z, l  g! q
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 w, c8 C0 u5 \; a{
. U  e9 `) X, A5 p' m3 |3 kif(PositionsTotal()) // Is there a position?
( |7 D: g2 i( \. H* h0 d{. w# q8 |- ~4 o2 w6 e1 @
double offset[1] = { 0 };- V: y9 I: s6 D$ Y, O6 G# M
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ N& c8 Q, y1 S5 i& r3 B. y* h&& PositionSelect(_Symbol))  // Select the existing position!
, h; k9 D! q7 _: {; o{
3 b' u9 N+ `4 P2 y1 jENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
6 V( t: n# ?( N" f6 Rdouble SL = PositionGetDouble(POSITION_SL);
7 M. x. N3 q$ R) ?  q3 L6 Gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# \, J2 B$ j7 {  P; |" j
if(tipo == POSITION_TYPE_BUY)
6 n9 c1 v/ @+ C1 `{1 t2 E1 G0 v9 m0 Q& O) j( d* Y3 A- N
if (cotacoes[1].high > cotacoes[0].high)6 T7 |1 V% k, a, X) b4 r
{
1 H  i7 M' }( P* r( idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];( e8 l: k0 m5 [5 V+ ^
info.NormalizePrice(sl);6 d: v/ a2 u/ ]( `
if (sl > SL)
. X" O4 F- p4 ^# ]5 X4 K# \{
$ a4 n3 f# y6 Z3 v. W: M- `negocios.PositionModify(_Symbol, sl, TP);# N/ S  `# T! A( ?( N" r
}* C, `7 Z+ W) k. p# P1 {7 a
}% x0 |. d( K6 ~% a
}6 Q$ h0 W* p% c8 }
else // tipo == POSITION_TYPE_SELL
! W: c% e6 m, d{( p/ E% ]) K1 `
if (cotacoes[1].low < cotacoes[0].low)
$ @5 W# |5 L2 a8 N- W{
6 M2 N+ g9 q5 o  G  x; Treturn true;
$ ?8 u" Z& ]. y6 L}
- ]: ?* K/ D# K( F# B- l8 ^! }% @// there was no position
, I' i  ]+ ]: b2 _& areturn false;
* O' d/ L0 n: Q  ~  _5 C}+ h4 r4 t7 ?& ~/ N" J# V
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 c- B4 s6 G9 `- S8 D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-2 05:43 , Processed in 1.181796 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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