私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA$ i. |8 P- }& w
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
% f4 U9 S6 X5 I. ~0 D- W为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
5 g9 x, N) D# i+ \0 C( r以下是制定这些规则的代码。& |/ u% ^4 i# e- B8 m
//--- Indicator ATR(1) with EMA(8) used for the stop level...
' G+ T3 R  m& Y- k2 K" bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
4 W) f' R. o& Z4 r% Sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 M! j- N+ N; }) r
//--- Define a variable that indicates that we have a deal...
4 v, l" W3 q6 z+ I! r- dbool tem_tick = false;; j  j1 I$ E0 p' d, V( c+ N
//--- An auxiliary variable for opening a position
$ W4 X" q3 D3 ^" n#include<Trade/Trade.mqh>
) A0 p. y- h& e, `#include<Trade/SymbolInfo.mqh>$ m5 r# _5 t  a  v9 F5 c
CTrade negocios;9 u8 c$ w) p8 W
CSymbolInfo info;
% D. k3 t2 ?% m//--- Define in OnInit() the use of the timer every second1 j9 c6 y  R( }& W1 U' M9 s
//--- and start CTrade
5 K4 R* F, s$ eint OnInit()
+ K7 W' g! z% i  I$ j% u! ^{
/ t; n7 N! ^0 N0 a1 J* A+ y//--- Set the fill type to keep a pending order/ @2 i/ C' ]( t5 K
//--- until it is fully filled
; Z6 l8 ^0 p0 w. B$ P% Snegocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 _- _0 w+ l  O0 _; d0 w' p- G//--- Leave the fixed deviation at it is not used on B3 exchange
! g6 x3 c- F. Q+ G4 `- r) gnegocios.SetDeviationInPoints(5);$ V8 w: |, i6 x+ }3 U
//--- Define the symbol in CSymbolInfo...
: b* A6 c9 L  _4 N* _* S7 N4 hinfo.Name(_Symbol);1 G7 U" i9 R$ n  M/ i! f6 O
//--- Set the timer...
4 K6 f- r. E6 T( I9 `9 }EventSetTimer(1);! M  P: ^( Y  h6 f: K, H
//--- Set the base of the random number to have equal tests...! l) @! @2 F3 T) `, `2 _
MathSrand(0xDEAD);
" ?& h! o, G' v! }- H+ _+ Kreturn(INIT_SUCCEEDED);1 t1 g& n# Q+ P
}9 |2 S; A9 d& R7 ], }& o  {
//--- Since we set a timer, we need to destroy it in OnDeInit().
3 T; X. F. c( e7 F- Avoid OnDeinit(const int reason)
5 J# ^% r# }0 p5 T' M{
2 P( i6 ]/ q' sEventKillTimer();: p% u5 q+ w3 s* n; a
}& ^- [+ n6 M$ g0 K: g2 ^$ r
//--- The OnTick function only informs us that we have a new deal
  [8 H) ]2 @/ x3 b7 f9 Lvoid OnTick()
9 h; Z7 }" M' V2 p# z! n{1 t- {. j# p" B" N
tem_tick = true;
: Y! X. I. O' {* X) e1 u}
, |+ B% I( `0 a- L% T7 s% h//+------------------------------------------------------------------+! q4 E/ k1 Z+ g6 M! H
//| Expert Advisor main function                                     |
# \. ?  d: J* E1 t$ I. s//+------------------------------------------------------------------+! K1 T2 K7 L5 A! C, Z
void OnTimer()
# M$ C+ a  X$ p8 J" N+ R$ \{% O, f) O3 Y$ [- y6 n) r1 H
MqlRates cotacao[];
5 D! P# m1 b. L2 Wreturn ;6 {; b6 J4 N- d
if (negocios_autorizados == false) // are we outside the trading window?
& k4 O$ |: A( e$ O! |; S; U& `return ;* r# w6 Y9 J! t$ ?( ~2 r
//--- We are in the trading window, try to open a new position!
. A) V( i$ l. X! j. g, p& `- t4 tint sorteio = MathRand();, q  z0 ^; r, [
//--- Entry rule 1.1; I4 M6 q1 W* f8 P% X9 w5 i
if(sorteio == 0 || sorteio == 32767)
2 s) Y( u' R! r2 w. b* Z9 K' ^return ;( {( Q# a% w/ ?; d% ?+ K
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy7 X- ^0 U$ |$ [2 X* H1 ]' q
{2 h/ |" U3 A' h* t4 |6 p2 E
negocios.Buy(info.LotsMin(), _Symbol);
1 f" T1 I8 \+ @1 e% w$ G5 X' S- @}
4 G, F, h6 [# H% d2 ^& n% ?. B& Xelse // Draw rule 1.3 -- odd number - Sell
8 \' _; Z. I! A' t# a. f0 Q" `{
' v" p; J6 [2 T3 c* u+ Q% pnegocios.Sell(info.LotsMin(), _Symbol);
; r$ n( M0 g4 U# F. H0 M( Q: ^}
+ v1 l' F, u2 I}1 @# C) K5 A' b) X6 G& @7 J
//--- Check if we have a new candlestick...( W& f$ m+ r7 ^$ B  }/ e
bool tem_vela_nova(const MqlRates &rate)! N5 ^' V6 W  r1 m
{, P2 G* Q; z& O
{9 T) ~2 T7 ?, q
ret = true;( A( ]  V  u3 B
close_positions = false;4 \0 p% x, M& \0 t% J! ^% c$ e0 t8 f
}
  ]9 N* \0 L2 w& ]5 g) E2 Nelse
' S: X" I. a2 G0 ~{" ?; X& G! M5 @
if(mdt.hour == 16)" l4 T7 L3 y& w' V
close_positions = (mdt.min >= 30);
) f. P5 l3 y& O0 @7 ?0 m0 d}
# H6 O& I# Y. O6 X8 \& r* H}9 P7 S2 t5 ~8 m( o! ]
return ret;
! Y, |$ c: L- z}4 P. O6 p4 L: Z5 j; v* N( I
//---
: ~# e0 f: [' M- Vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& U7 v. y8 d; i, p{
$ \  ~+ p6 @" D  F) u' b' uif(PositionsTotal()) // Is there a position?
, s. x9 ]+ b3 b) {{
0 ?* Z3 B$ q& p9 h% x0 b3 P5 h7 ddouble offset[1] = { 0 };$ i" G4 D+ ^7 F9 A
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?$ u& x1 e' ?2 J- L. E; R' B! t' x3 w
&& PositionSelect(_Symbol))  // Select the existing position!
. f2 `( M4 F1 X, U2 ?+ D{
6 z( `5 l. Y% o. u& yENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 ^1 [7 ?1 y( B
double SL = PositionGetDouble(POSITION_SL);
% }% q& g5 H; N3 l. Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
3 H, }9 [  ?4 P% M' V6 L% b/ oif(tipo == POSITION_TYPE_BUY)
* c: p; G; O8 m! {! z{7 q$ \& }$ R0 d- u
if (cotacoes[1].high > cotacoes[0].high)
; f3 h  p& n3 Z- J4 P! f  W$ c6 [{
( U( z+ m+ q+ X. K- Vdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 Q& y, o) F- F" U. S" l4 Z$ C2 Minfo.NormalizePrice(sl);
% n2 a/ @% O9 qif (sl > SL)9 Y4 X, w' @" t! L* d" k2 e8 B  V
{
  f1 {" M$ y4 c( snegocios.PositionModify(_Symbol, sl, TP);, \/ J. c6 `7 ]0 h  b
}
0 s& N8 P/ v  d- [* `+ @+ v8 `, o8 B}3 E% b: c, V2 I+ U
}
! s6 h0 r* \. ]! R3 z- Eelse // tipo == POSITION_TYPE_SELL
! G1 T8 k0 \' C2 \, Q7 W; H/ l8 e' Y( i{
  n0 [3 n1 V, Y/ G+ U: Xif (cotacoes[1].low < cotacoes[0].low)
9 V+ o) u; ^* F2 ^/ t" b$ ~{4 M5 e& c4 u( y% W! A+ s
return true;5 Y0 `5 X1 I+ I$ V. ]
}+ N! d) K( K0 h4 {
// there was no position
3 m! E  b. o2 I1 A/ J3 h, Qreturn false;$ j* O1 m) a5 s* g* L2 b7 n8 U: S
}
4 W( t6 o/ p. w  R6 U我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。) _7 L* K7 p- A
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-8 09:16 , Processed in 1.342974 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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