私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
. K- l7 [3 j" J+ y- D, \在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。$ j) m8 x7 _. @* Y* t. b) x3 A* q% }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
+ B. y, J$ R; k5 l; g2 G! n以下是制定这些规则的代码。
, ?  k! f: z  _. w0 k! b//--- Indicator ATR(1) with EMA(8) used for the stop level...! N" D  {8 G7 ?/ [2 O* g* s; J
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 B# e' ]2 y9 G4 F% `( Cint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);! q# u' D" c' j' V
//--- Define a variable that indicates that we have a deal...
2 ?) y; v+ A3 o4 V2 Rbool tem_tick = false;& i" c, |- o* K
//--- An auxiliary variable for opening a position- x4 ^; E( d% t
#include<Trade/Trade.mqh>
6 v: Y2 {5 k8 j  m4 X#include<Trade/SymbolInfo.mqh>- a6 u5 v; t% p1 w, t3 r
CTrade negocios;# g( w1 x: ]4 ]" v/ |
CSymbolInfo info;
0 x9 \+ L6 ?$ I//--- Define in OnInit() the use of the timer every second6 L* K$ ?! G9 i/ H0 p2 l$ ?  y3 @
//--- and start CTrade
  i; Y* F/ a! z4 ~0 aint OnInit()" K, `" l2 q$ B) |9 p
{7 g$ g( V4 L+ {! r4 n3 U
//--- Set the fill type to keep a pending order
5 F9 X; w+ }* r+ p' Y: N- A7 Q; f//--- until it is fully filled
  u( m% l4 k4 F' V7 P- J+ ~negocios.SetTypeFilling(ORDER_FILLING_RETURN);+ U" _+ F& g+ Y; ~
//--- Leave the fixed deviation at it is not used on B3 exchange2 p" V$ r, K& H, w! q/ u3 q2 r
negocios.SetDeviationInPoints(5);
" J; E4 g$ M* z7 g//--- Define the symbol in CSymbolInfo...
5 f" @9 a+ b& h+ kinfo.Name(_Symbol);% q" e0 _7 j$ I0 y  a$ Q/ e
//--- Set the timer...9 Z# h+ r. S: `# M2 Y$ g$ v
EventSetTimer(1);
+ |, n5 ]' l3 D3 V3 l0 Y4 @//--- Set the base of the random number to have equal tests...0 b' m2 \, m7 m8 Y5 Q+ f( x. O
MathSrand(0xDEAD);- Q/ v0 \- @8 C- }4 f. s" E" ?  _6 m. l
return(INIT_SUCCEEDED);  O8 G. m. u& r3 z! {$ t
}% n+ u: X* K  b1 g7 n
//--- Since we set a timer, we need to destroy it in OnDeInit().
( [3 }) M3 ?  L: A) l! O2 b( xvoid OnDeinit(const int reason)
  o% @2 u9 W  u# W! u{! o5 ?! S; u# v9 f9 p- k3 R
EventKillTimer();9 F. G/ U  I! a: P- t
}! [( h* o+ P5 u( G4 ?7 f8 b  A3 |
//--- The OnTick function only informs us that we have a new deal
% S7 i* T, m; Q4 _! Ivoid OnTick()
* ~* q9 Z; e3 q; v/ s8 ~{
" Q6 K/ n% C0 X# T$ X" Dtem_tick = true;
1 F3 B3 U7 [5 O9 M}
0 n4 p. U! f# B$ J1 W//+------------------------------------------------------------------+
! w. b6 t, q7 |//| Expert Advisor main function                                     |
/ {$ o, T8 K# s8 K: y//+------------------------------------------------------------------+
6 e( r: _! X% I9 _9 ivoid OnTimer()
$ D! W2 ?1 P( ]{1 k" y' O; h0 Y  ~( ~; [
MqlRates cotacao[];
/ W9 x) }: G5 M! z% _4 i& _return ;9 R. J* i( ~( ^$ D! B1 T9 t
if (negocios_autorizados == false) // are we outside the trading window?' ?0 d0 K4 n( W8 B$ O6 R" x& G
return ;
4 ^( b: b7 k) V/ k//--- We are in the trading window, try to open a new position!
9 v# d. U( O+ I0 x3 U' Dint sorteio = MathRand();
5 d3 j" M& w* ?4 N9 T9 O& |$ q6 M+ n//--- Entry rule 1.1+ D5 `! m  u1 k  D3 q7 G& z
if(sorteio == 0 || sorteio == 32767)7 N0 `7 g- t! N6 T: D1 j" y
return ;' b' l% L" @1 O5 _- ?
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
/ m0 K  J# u& V{
& G* a. e' P- e6 d/ x/ y2 T2 L( Mnegocios.Buy(info.LotsMin(), _Symbol);) D2 C1 k, I5 I) ~
}
) K6 e+ b0 P; D, Selse // Draw rule 1.3 -- odd number - Sell3 H2 J/ `3 ^. z# P  I3 g/ t
{
9 g! ^+ ]( U3 R$ \negocios.Sell(info.LotsMin(), _Symbol);% A8 s7 ]: c6 B+ w, ]  ^1 S
}
* f1 G4 E! E9 v" F}
4 d  _: P1 N% H5 `8 Q5 G  x//--- Check if we have a new candlestick...+ j$ a) N9 D1 F2 H: z! U9 I. T
bool tem_vela_nova(const MqlRates &rate)
  L0 w8 {, }. }" S! Q! X3 \5 A{
% E7 |6 _  s0 _0 X4 r{
( Z; r# P. |$ V1 Uret = true;
; E0 B! T9 `$ n1 d' X1 N8 @* }close_positions = false;+ B9 _1 g# N8 P9 K2 K' U
}
+ [" K- l; l# `1 i& Aelse
7 Z8 n7 z, z6 F# g: }{8 l9 v5 A! e$ ^7 e! U
if(mdt.hour == 16)
3 h" }8 p% M/ u/ Zclose_positions = (mdt.min >= 30);. h! ~+ A9 i  X4 }9 g
}
* |4 X5 C9 @4 W7 v# c4 G}
! B& y! l7 r6 D$ e; vreturn ret;
8 o) ^4 v, E2 f9 W( _, u! [/ H}
9 C6 v% W# m" v//---
. z5 }/ \2 ]3 Q/ B5 W' Obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 `- N- Q& a" f' q% z+ m{
) q7 X: h* i9 I9 Q, a2 Zif(PositionsTotal()) // Is there a position?+ K3 ]* x; y* r0 H
{) Y2 _' c5 g& z% Z; F) `# {% ~$ g
double offset[1] = { 0 };
$ _  V! a* M& p+ I/ N" Pif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
: x, R3 y1 b/ ?" X, W&& PositionSelect(_Symbol))  // Select the existing position!$ i% _; a( A+ _6 A
{. L- x* `& ?8 `% C3 Q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; P$ O. X6 E* Q- k: u5 Hdouble SL = PositionGetDouble(POSITION_SL);1 Y( U& J4 C6 \% Z& ~+ [& V% i
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ C1 k8 J2 y5 C9 ?% s+ eif(tipo == POSITION_TYPE_BUY)
9 O% |' v  z1 V) S! D0 W" W) D4 V{
' w$ E: w. W6 k0 F7 v4 y/ K* fif (cotacoes[1].high > cotacoes[0].high)% p, m! g3 C: l" V6 ^) ]
{
: \8 u4 f' z3 e7 O5 U+ q( A* `double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];" v# i! A$ M$ [+ t" Q- J% ~
info.NormalizePrice(sl);
5 j' t3 {: v& R6 _( g; Hif (sl > SL)
9 m! `! ^1 p7 `, a8 J2 b' O{2 k# j5 C/ k+ k" A
negocios.PositionModify(_Symbol, sl, TP);
% Q6 W. g2 r4 P5 v& o+ k0 r6 ]( ~}3 Y0 ]2 \9 M( ~9 m* A
}
" \6 h+ V( _9 y  f}0 x% {7 Z3 H  L9 u( W) G& [
else // tipo == POSITION_TYPE_SELL7 H( {  C6 g' q" [: W
{2 z7 D! H# b, [6 V. N( d/ T2 v1 i
if (cotacoes[1].low < cotacoes[0].low)6 T$ F  [- P- u$ c( ]9 U) J* c5 E5 _
{
& F  t5 y* m1 z% q7 c- V, n+ Xreturn true;4 b8 B' A% y9 e( A3 Y, ?: v
}
- Q- @) M' ^& P// there was no position6 Q% V9 r/ Q$ h. @
return false;
# Q+ y+ Y- f5 B$ f}
, P6 y4 [# t$ d* `! {# b$ M我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。6 C! }; P8 h& V' B6 |; h2 r7 ?
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-24 15:31 , Processed in 4.029325 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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