私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
% N; U& h2 C. u; j* U* l在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。" L5 o1 W5 h3 `& F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 K. w' q+ U$ M# n+ N6 e) e& e
以下是制定这些规则的代码。6 K3 a  S7 p5 w" Z+ }
//--- Indicator ATR(1) with EMA(8) used for the stop level...
' b3 ^, R& J; `8 U( @int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. v5 z. C  Y6 i% k* J
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 Y3 ^* ]/ a" I, _" x- i8 a
//--- Define a variable that indicates that we have a deal...2 O( r( y7 z# H8 o, Q; R
bool tem_tick = false;
) F/ f  A( z) o7 p+ g3 [//--- An auxiliary variable for opening a position5 w2 O6 l7 I9 y0 Y' D
#include<Trade/Trade.mqh>, O2 t8 \9 |9 t& s; o
#include<Trade/SymbolInfo.mqh>, N1 x/ a" |7 y: b2 _
CTrade negocios;+ q. f& K" K/ Q1 X3 s' c1 D- k
CSymbolInfo info;
4 _% d9 ?; f3 u% }" g4 [. J//--- Define in OnInit() the use of the timer every second8 l$ U4 y* S' b! e/ V
//--- and start CTrade) n9 h! ~6 h. [. W9 K6 R1 @
int OnInit()
0 {; V; [. n. I{$ a& V5 C2 |& b( ~# J) D  H9 Q
//--- Set the fill type to keep a pending order$ E" `9 ]' T: [: `6 W
//--- until it is fully filled) _* D  e/ E% W6 S' s
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
" l/ N/ R  K& ~& y//--- Leave the fixed deviation at it is not used on B3 exchange7 l7 }5 b" D( m5 `
negocios.SetDeviationInPoints(5);
: S- a' V0 c/ G% j! l9 K//--- Define the symbol in CSymbolInfo...
3 e% W, b: E3 @. L, P. Qinfo.Name(_Symbol);
: @. {' P$ t7 @7 _" Q/ w5 Y//--- Set the timer...' Q( r4 ?0 G$ P$ e
EventSetTimer(1);2 C5 S/ A8 c8 Y, h+ x
//--- Set the base of the random number to have equal tests...
( D7 b/ v+ I; f! K$ C, aMathSrand(0xDEAD);
8 o" u) k/ v4 A& J. Nreturn(INIT_SUCCEEDED);
* k+ G# M# M) o% n+ L6 L7 o3 X4 e}, d% Y) I' _( A5 }
//--- Since we set a timer, we need to destroy it in OnDeInit().
' S0 t9 d+ M4 \; |void OnDeinit(const int reason)
; h) F+ u9 K- \6 X# R  ?{
+ r7 P3 f- q& S5 Q1 m& KEventKillTimer();
( [6 E1 c$ `: a* |0 b7 d) b}! A% }  u" ?% b7 K
//--- The OnTick function only informs us that we have a new deal
3 h* h  I- W2 z+ G, v4 z( Bvoid OnTick()0 m5 q  p+ Y" m  J& ?0 f
{6 o1 P* U8 s: g: {* |5 N
tem_tick = true;
5 X/ J7 b6 `2 l4 f}$ }4 ]& V8 E/ r, M" R
//+------------------------------------------------------------------+
9 A8 Z/ a* ~$ k- F( |2 x//| Expert Advisor main function                                     |) h% t; w. w9 d4 |5 s* n* V% B( S
//+------------------------------------------------------------------+
- Z1 s! j6 h2 m$ \4 l1 v& fvoid OnTimer()6 _$ ^$ S# C( T
{
* A5 r' \- h& j& IMqlRates cotacao[];
( I2 d  n/ {. o! C5 K5 e' J) Breturn ;
# ~0 J2 [8 G% t* A% Tif (negocios_autorizados == false) // are we outside the trading window?: D! u2 X7 w5 _8 ~; j% }' I0 J8 c
return ;
+ ?3 {* ]) C# k2 v  F8 T//--- We are in the trading window, try to open a new position!
. Y. G* ~! C/ rint sorteio = MathRand();
2 r! C4 m9 r! `//--- Entry rule 1.1
1 J# {6 E. G; X1 kif(sorteio == 0 || sorteio == 32767)
" g' [0 v  A; F; `& |+ m% freturn ;
$ c# E6 k* k, l$ N+ ]& d  Cif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 ~. w' Z( V+ b) n! I' e/ V% K0 J
{
# U* c0 w2 {  ]- }negocios.Buy(info.LotsMin(), _Symbol);
) o9 j' B% A* x( s! M' @}% _/ E0 p$ ]7 ^
else // Draw rule 1.3 -- odd number - Sell* I  b6 s' u# I8 g1 j
{' q9 P! v/ H& s4 D9 U
negocios.Sell(info.LotsMin(), _Symbol);* T8 y) e4 o5 d: k# H+ W: b' x
}* u5 m1 N, _& t
}9 k8 @1 [2 x  E' f$ A# {- p
//--- Check if we have a new candlestick...
. v3 L2 g0 n$ C8 }4 R+ Sbool tem_vela_nova(const MqlRates &rate)! t) w  v* e! O3 r' Z
{
) b! H5 {- z  y  X4 P1 ~& B{( @4 C5 l! b6 U7 S: b! ]; S" S  [
ret = true;; |5 ~  p; ~; F9 w, s3 A3 X
close_positions = false;2 c0 [( h, n8 g6 n0 w2 U
}  V( S! N& h9 o% a0 y# i
else3 s9 g! t( o5 Z( m7 p, H1 P1 ?
{
* }, K4 ?: K% Z/ m) mif(mdt.hour == 16). s# ~6 ~, s; _) _: |9 Y
close_positions = (mdt.min >= 30);
0 b  w, v- P$ Z- ^0 K; a, b+ P}
2 F/ P5 d; y) B8 ]1 z}# v- }" _4 m' D) \! N$ r
return ret;
7 M: n( v% q( y8 B}$ Y  R; |& l2 B2 y
//---
7 W' D$ z$ l7 hbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. |4 L% `4 U4 x/ j1 j{
5 I8 w# }& O+ _& F3 hif(PositionsTotal()) // Is there a position?
9 {0 h0 [  D$ x' x' `+ n{
$ n# _6 I; i" I: udouble offset[1] = { 0 };" N" n* T+ [' a& d6 Q' X7 n0 {
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& N+ k" R. r, g: P, ]2 v&& PositionSelect(_Symbol))  // Select the existing position!
  C% L" I3 \: j" f7 d; D, e{2 U$ S; @: [3 o0 N9 P. J" t
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
( u9 O: T; Z# p& Q2 M2 Ndouble SL = PositionGetDouble(POSITION_SL);
& b. k0 P4 ]6 Adouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
# Z: Z9 L$ V) f+ T0 h" Xif(tipo == POSITION_TYPE_BUY)
8 ?9 K: q: H* Y% I7 p) M{
6 |7 p# t: J9 Q& ~if (cotacoes[1].high > cotacoes[0].high)2 m6 }: C4 C: k( C( a# }# k5 R
{
& g' ]: a3 f; a. u" ]* U& ~9 bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
3 ~* {" |/ h8 E' Kinfo.NormalizePrice(sl);
" o6 K  \; i5 p% g% z7 bif (sl > SL)% [- ]" d& |6 Q% U* ^; R
{
  }) k# C$ i. J8 wnegocios.PositionModify(_Symbol, sl, TP);7 q' m  h; P/ V% e6 a
}
! y5 r( t7 G' G: c: {}
* w2 f5 T' x" L7 z  ~0 f; p}# _: _0 n" s" k) j( U. b6 p$ ~
else // tipo == POSITION_TYPE_SELL
$ Y' \4 i3 }* O9 N. |5 `{# J/ M$ H% p) P; {1 y% h' J
if (cotacoes[1].low < cotacoes[0].low)' ]! F" y4 S  A4 `
{) Q+ F( E3 [' J* B: h+ i- I
return true;
# J! D6 M: q8 j9 ?& ]! s+ V}# _+ P& d# |+ {! W4 s9 {6 Q  w
// there was no position: A" F# i; Q' W9 @  e; u1 A8 B
return false;
9 d3 @. g/ H) u& x. j& C}2 J  L5 j3 O; s
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
0 {; w/ q9 y% M7 G) {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-23 05:50 , Processed in 0.440282 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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