私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
% p: _9 Y6 ]/ ]& Q: `$ `在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
: z1 _3 p; z! P$ A: G7 |5 J为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。: x- e5 P/ O8 x- J+ C6 E  k
以下是制定这些规则的代码。
- D1 y5 t& w* R- z//--- Indicator ATR(1) with EMA(8) used for the stop level...1 M3 R4 F" T2 x, j, m% R+ U/ t5 q5 A
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 }( d5 c* s" m
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
  D; O" \! H: e2 d/ m( M$ L//--- Define a variable that indicates that we have a deal...
" |' i* R& u2 i: ^, k) Sbool tem_tick = false;7 o& i' Z7 c: Y3 _
//--- An auxiliary variable for opening a position
: j6 b( P5 A/ v& p# P#include<Trade/Trade.mqh>
5 ^1 q* A' J* _) e#include<Trade/SymbolInfo.mqh>
+ v; L, X* x$ \CTrade negocios;- {3 m3 @, a& x$ k
CSymbolInfo info;
3 c  q) s6 k: H1 b0 G$ N& K4 a/ G//--- Define in OnInit() the use of the timer every second
& D, J5 u4 E) l) @# m2 B//--- and start CTrade
3 e5 O: c2 g9 V- t1 V% a; ?2 @) Kint OnInit()1 e6 R0 F  x: X+ E& Z# k5 ]
{
0 O  P2 e8 K( Q* k$ f8 c//--- Set the fill type to keep a pending order
( F$ p( p. d7 ~# F9 F  C//--- until it is fully filled
) K) |. i& m9 `+ C% `) dnegocios.SetTypeFilling(ORDER_FILLING_RETURN);3 a) {8 a4 ~3 r; z7 r
//--- Leave the fixed deviation at it is not used on B3 exchange: {. @5 E, L* N& m2 T* k2 K' W3 T
negocios.SetDeviationInPoints(5);+ |0 B! g: g; `
//--- Define the symbol in CSymbolInfo...! c( o3 X; ?3 A6 H$ G' R0 t" S
info.Name(_Symbol);- c' p, A4 ^( \! F; _% G
//--- Set the timer...: \+ r( u" L0 M- m
EventSetTimer(1);
. w, f# H$ t1 E9 P- w//--- Set the base of the random number to have equal tests...6 D& g' @4 A# }
MathSrand(0xDEAD);& m. ~6 ~$ o: V% J9 y6 ^
return(INIT_SUCCEEDED);
1 B2 e) ~. s  M, J) V+ @, ^, z}
) T" F- A+ S1 F//--- Since we set a timer, we need to destroy it in OnDeInit().
7 p4 c% S9 G( Ivoid OnDeinit(const int reason)& t" s* y3 z4 W: P& Q+ a% |
{
: e% a$ m0 X- M4 ?0 V4 C4 rEventKillTimer();
$ r2 o. }) c4 l: K4 G}) W  D2 _4 y0 v% }
//--- The OnTick function only informs us that we have a new deal
9 z+ `, U! l- ~$ ]2 p8 \void OnTick()* r; ^3 w1 j8 j1 s/ y8 S+ P6 _9 V% t
{
3 u: d: Y5 J4 A4 f# etem_tick = true;
# O2 U: m: y+ z5 \6 O& u% A! C}
. x) ~( [! m) S//+------------------------------------------------------------------+# D3 e, Y: C/ i
//| Expert Advisor main function                                     |& @5 j8 }  O1 y6 h# S# p$ V% [% c. Q
//+------------------------------------------------------------------+
/ [6 u6 Y' T: m  _2 d. bvoid OnTimer()
+ o8 J+ t" N* @: ]1 B{
. M9 [" }$ j& p" h5 CMqlRates cotacao[];" a4 \; {# {- B
return ;
4 b6 M, \+ B7 g$ n, |: c& Mif (negocios_autorizados == false) // are we outside the trading window?4 c; r  G# E8 V6 ]* u% ]7 i9 s0 Q$ S
return ;% f5 u% l( A) H& j8 M3 O# ^; Z: z
//--- We are in the trading window, try to open a new position!. v$ `, {2 E" C% i
int sorteio = MathRand();; Z  V. l- h3 G) p/ m: E
//--- Entry rule 1.1' H5 H2 s/ k3 j8 D: Z$ R- z
if(sorteio == 0 || sorteio == 32767)
4 Z+ B2 ]6 D$ K$ K1 i& vreturn ;4 y: @" E7 ^1 u8 [3 a7 y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' x& t* Z1 d# a, C{+ r) `% b! K6 V
negocios.Buy(info.LotsMin(), _Symbol);
& T2 c/ ]- x# H: |: v5 |}# J+ a8 o: \  d. j  D) a
else // Draw rule 1.3 -- odd number - Sell
$ [" J% V" w7 Z# R7 i{
) e' C$ f  A; j2 t) ]negocios.Sell(info.LotsMin(), _Symbol);. s: {6 K+ n9 |! ]3 e) X  x
}* t! ]- z- j' C: X6 C0 d
}% k# r1 {3 b8 z2 ^
//--- Check if we have a new candlestick...
" g* \: n+ w3 gbool tem_vela_nova(const MqlRates &rate)
9 s( o% N2 }; f. _( {" f{$ ?+ D! }" S8 Q1 O3 a
{
7 k- b4 x! O' s- Z# Jret = true;  U: L" M; Q& Q2 |
close_positions = false;
8 \0 d5 i5 B9 p}( K1 k4 i" N4 a
else& r+ x, K/ ]+ F8 V
{
/ A( {/ s4 K: P8 m7 V7 }  U/ F1 Sif(mdt.hour == 16)
" h) f( y4 B1 E# s. bclose_positions = (mdt.min >= 30);
7 k- ]# L" s$ Q# T1 T# a, b}+ X! _! R. T+ E8 e' u# ?
}
$ ^' Y: L" H( T* R  zreturn ret;
( p! Y4 M3 J  a- b, E7 c}
* g4 k' a% m$ t* P" q//---& D9 [$ d5 J9 y4 y0 Y0 ~6 \2 k; U
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. ~- @- e# |& _7 t: R{. [( n# x5 g4 p- y
if(PositionsTotal()) // Is there a position?8 G$ Q  s: W8 G
{* r& @0 g% M* i; E- F# t% o! s
double offset[1] = { 0 };
7 L. k) L* {$ Y5 e+ F5 mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?$ _0 y6 o+ }/ T6 e$ y: A+ `
&& PositionSelect(_Symbol))  // Select the existing position!
. x& r* W! F6 t# f) z2 g{% I  y2 J/ q/ o9 U8 s  p6 x
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);  G0 k% b& S, U& U: z
double SL = PositionGetDouble(POSITION_SL);
' G- c1 Y6 x" ~, I4 u+ idouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 z3 V- ~2 y8 R! Sif(tipo == POSITION_TYPE_BUY)% P6 ~' C7 \+ s9 a1 M
{
# }! S( `' y; z$ P# T' Yif (cotacoes[1].high > cotacoes[0].high)
2 h- L8 c: L' N! q, ?% p# y; n2 C{
. @, S. k, p0 B# O( R, Vdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
, E, U& O% w& x+ ]/ {info.NormalizePrice(sl);
3 Z" H) D2 y1 eif (sl > SL)+ Q9 l8 ?7 j8 Y5 k
{
* E' n% X( z4 @, P7 m- @; m  Jnegocios.PositionModify(_Symbol, sl, TP);
0 H2 p) S) w1 }2 ^0 }* F2 A) g- [}; u1 l# e9 ]9 c' D& ~
}6 r1 e# P1 P; j- z6 [
}
$ r+ `8 v% h% @7 O1 f5 Zelse // tipo == POSITION_TYPE_SELL
" h* B" [' A9 e8 Z7 g- s4 s{" `# n0 B" S5 }
if (cotacoes[1].low < cotacoes[0].low). ], r' k3 {2 \" Y; Z+ y5 o
{
) Q/ Z7 F7 m) Y% Z7 Ureturn true;
9 E& y% K( Y5 P; t- L( n}
+ |0 v  N/ X. ?8 X7 G/ e3 {4 a% `// there was no position+ C$ j( H( D" x1 h1 r4 X
return false;
! [+ p; F$ O  {7 l! D% m}% [- c1 n1 j% ^% O! F9 e2 m3 v
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 Z1 d9 c9 S4 y0 n到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-14 16:36 , Processed in 0.871846 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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