私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA7 g# U. ], O# }' D
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。: Z8 x: L; b- [4 v5 ~0 F, \
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 o) t1 }: P; @9 l& S- y6 u以下是制定这些规则的代码。9 ?0 X/ d; d& [4 y0 |
//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 a2 v& @! h7 \( X& yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, a- u) W1 m) S
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 |# R% u/ |  @  \' m" h//--- Define a variable that indicates that we have a deal...4 P' O- D: G! o7 W$ C& N* a. ^
bool tem_tick = false;
5 o; k* I( [( H1 Q//--- An auxiliary variable for opening a position
  g' {+ x1 h; l7 C+ W# x3 h7 C) W* e. C#include<Trade/Trade.mqh>
6 }( F/ e4 C' F* U% P, B" J8 i6 u" d#include<Trade/SymbolInfo.mqh>& F! I6 k8 k( N9 y1 r# |9 I
CTrade negocios;
  b3 W6 V% M' D: S: I" T/ qCSymbolInfo info;5 U" f2 K/ q$ g2 g  n2 A1 W, f* C
//--- Define in OnInit() the use of the timer every second1 V2 Q/ s0 T# e
//--- and start CTrade
  u* `$ x, U  m1 T8 G8 V- Iint OnInit()$ O, z$ v8 H9 K  v; s* K
{
2 b4 p& W. [. Q9 _//--- Set the fill type to keep a pending order2 f* z  M8 p: e! T
//--- until it is fully filled' ^' R; ?  }; F- b/ h. f
negocios.SetTypeFilling(ORDER_FILLING_RETURN);: A! ]3 F8 N7 K8 Y. X
//--- Leave the fixed deviation at it is not used on B3 exchange
$ W1 M1 g. q  n" Rnegocios.SetDeviationInPoints(5);
1 ~% F0 a: [" j7 g2 n7 Z2 [$ h//--- Define the symbol in CSymbolInfo...
1 @: W$ `" m2 Pinfo.Name(_Symbol);
( R& O8 v; u/ i% `//--- Set the timer...) {" O+ a1 e0 q% M' s" y" i; [
EventSetTimer(1);
3 D6 D* [" S) ?" S( m- \. E//--- Set the base of the random number to have equal tests...( X& [4 Y" b4 B0 C
MathSrand(0xDEAD);2 U  w8 e7 M$ P+ c) w% ~' b' B, [) @
return(INIT_SUCCEEDED);3 J* e% B) i/ M0 |! y. x) u% X# B1 H4 e
}& M5 g. G9 c: g! C; _: L, r
//--- Since we set a timer, we need to destroy it in OnDeInit()./ E' Y6 X8 |% E) F: Q. X
void OnDeinit(const int reason)
" t5 p: R4 i% Q* b4 ~8 r! A{
& y/ t( J8 F& l) j1 z5 M0 wEventKillTimer();6 P+ U9 v& X# s4 J
}6 b' a: O8 C' \" L0 i
//--- The OnTick function only informs us that we have a new deal
/ J- }( j* ]: q3 yvoid OnTick()" A* g7 Q% R3 H5 i
{
' }6 e6 J0 R+ D: s5 J  R5 T+ r- w1 H, U6 ttem_tick = true;
/ |# C8 c* g9 @' g}- v4 w4 ~- \/ B" }2 |3 p
//+------------------------------------------------------------------+
4 p; q4 T0 f2 I( i2 M5 d* N9 Z//| Expert Advisor main function                                     |
- o4 D! _/ r4 N5 `//+------------------------------------------------------------------+/ ^" L2 H( v  S. _
void OnTimer()
# b% p( |, W9 }/ e, _) M2 q{- B+ u. E! e3 d" B1 S& U7 R* P
MqlRates cotacao[];, {5 o( w, D% h" n9 L/ s+ l
return ;" n$ Z- i' n+ c9 b
if (negocios_autorizados == false) // are we outside the trading window?
$ C% P% \1 m% x/ Creturn ;* D. V5 g, I3 r  A
//--- We are in the trading window, try to open a new position!
. Y6 C6 H) R, Y; m) ~; n, nint sorteio = MathRand();% K" Y& t; y3 G: B: t
//--- Entry rule 1.1
8 C& R3 E( a: b& l- |4 |if(sorteio == 0 || sorteio == 32767)5 u! b  ~2 z+ e& \
return ;1 U7 D- l  u) Y( ]- O# r
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
/ s6 ?! G3 w0 D0 f4 N! G{
9 p- o0 H  m4 mnegocios.Buy(info.LotsMin(), _Symbol);9 z! {  O; f$ t/ G* Z- E
}
# _& T0 e6 ?% C4 U* yelse // Draw rule 1.3 -- odd number - Sell
. q% P0 [8 c( Q) \{
& G) j" N. ?# H# n9 ^7 gnegocios.Sell(info.LotsMin(), _Symbol);
, y6 T% X- V- q7 D+ U}
# s$ N' m  l6 e9 ~: ?& e! P}" y7 @* y3 A0 }/ L) r( W
//--- Check if we have a new candlestick..." ?+ ^8 B# {0 V; W* i# w
bool tem_vela_nova(const MqlRates &rate)$ @3 K* {2 k' w* S  h. P, F2 P
{2 X( h6 {2 Q& }; |* P! q7 C* Z
{
4 H: b6 m: N: _! [6 W3 aret = true;- Z  p. t( ]8 h+ y. x: C  I, Z
close_positions = false;
( C' ^7 z. |9 H" m" o1 V; H}
, A2 D4 f# C6 Y* Qelse, g# V4 ?1 ~+ P; ?7 A9 R
{8 A, f, A# q# ?4 `" j" Z
if(mdt.hour == 16)$ \; P7 K+ S+ \1 [2 w
close_positions = (mdt.min >= 30);
# ~! t+ |+ A( `9 e  n( |2 ]}
; a, n, k# h3 K4 z! T. p}) @$ g3 Y4 u! G2 n8 e
return ret;) d' t' i7 _( Z; L* \/ ]
}
; o8 n$ {# @# Y//---6 V$ y- N4 C# ^6 ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 z3 A/ Z0 H9 w; S* k2 ^
{
) H, c8 W4 P7 o. H: S# ], `# Jif(PositionsTotal()) // Is there a position?
: [: S& {( r% H! Y( P7 ^5 n( _* p6 ?3 s{& {$ c8 A0 V% m. T* U
double offset[1] = { 0 };3 X; M6 J* `: u
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# C7 u; @; G6 d+ b- r  E- x7 N&& PositionSelect(_Symbol))  // Select the existing position!
4 N1 A/ B! h3 W+ y6 a) p{
/ r$ j6 T0 b$ H! U0 b% C/ u% t$ TENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 V1 X. b% ?& x. g0 gdouble SL = PositionGetDouble(POSITION_SL);
3 W# R% G- P) Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ M* |/ [' U. Z: O9 {0 l6 Bif(tipo == POSITION_TYPE_BUY)' V/ z# O9 i, n2 o& F1 c& f0 O4 }& ?9 h4 M# D
{
$ E+ M: u; j- J* ?9 _- Fif (cotacoes[1].high > cotacoes[0].high)
/ V  o. r6 f* _7 g2 ]{# \5 {& v8 o: y3 ^
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
6 T7 C. n9 ~1 E+ B! N8 Sinfo.NormalizePrice(sl);  n/ S! O5 t- W
if (sl > SL)
- x/ g' C! T0 H{
& J$ C9 t: p2 s& I! S/ ~negocios.PositionModify(_Symbol, sl, TP);
/ {+ x' }5 E  ^* w6 H' _9 h" k$ T}& M% x8 }) m) r+ r0 C
}
1 ^6 F4 V+ ]6 x# ^3 \) ~}
: s! }; l9 O( L4 T: d( relse // tipo == POSITION_TYPE_SELL: C  F! z* Q$ L" Q1 U1 Y9 v3 V$ B
{
- {2 M! @5 C, L1 y: j, i8 B7 Wif (cotacoes[1].low < cotacoes[0].low)
" o( ^! |+ @3 }6 q, f8 `  q{
0 D4 J- X9 ]- H# P' T% e" sreturn true;8 ]' m) H- q4 i( @! C: O( `
}( A7 Z3 l9 x1 i& J
// there was no position. A1 k) D) {+ q$ k7 l$ S; i8 F% z1 h
return false;+ {" \5 S  S5 V7 e/ T; V6 }
}5 S" A' l$ e5 F* U# n2 u
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。! x% X$ m. B9 `2 r; h5 `5 Q
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-12 04:20 , Processed in 0.627147 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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