私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA+ v+ H6 w" K% ?: \- [4 R2 u! w
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。1 a- g$ s5 ]& ]: p
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 ^" ]  Y" G6 _. i9 \6 X
以下是制定这些规则的代码。
/ r3 g( e/ z/ X0 j0 C: M( J//--- Indicator ATR(1) with EMA(8) used for the stop level...
1 f6 q0 x( G) Lint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" J* b2 N! M9 a' O0 y, t) u! Z7 Vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
( L" p8 N2 X1 i  w! S+ n  ]//--- Define a variable that indicates that we have a deal...; B; Z( z& j4 O- K+ @' H
bool tem_tick = false;
# v3 m7 S3 h/ }' ~; O//--- An auxiliary variable for opening a position: i$ ^& h, e1 v/ I2 t
#include<Trade/Trade.mqh>* }) |) b1 V) Y# \  I9 n
#include<Trade/SymbolInfo.mqh>. M0 A+ l  D. L# ?
CTrade negocios;
( f" b+ s- f1 Q) E3 OCSymbolInfo info;
4 A2 X, P) m5 X! ~% S2 k8 B: H//--- Define in OnInit() the use of the timer every second
) Z" Z' V& p( e" w//--- and start CTrade, n. m9 [, B: z  `. x* D- Z, U2 A
int OnInit()
' _( ~5 r7 L" [9 c( p/ I! x$ K{! E8 L$ p. _' @* s) i* `
//--- Set the fill type to keep a pending order
: d# P1 S; i2 K' }& J8 W* U# a% c//--- until it is fully filled
0 D, _' |& H0 a) ]negocios.SetTypeFilling(ORDER_FILLING_RETURN);
: m; y$ G+ p& u3 t. t//--- Leave the fixed deviation at it is not used on B3 exchange' I# `7 {; [% t- {9 Z+ n
negocios.SetDeviationInPoints(5);
# g5 M( T2 t# e0 R//--- Define the symbol in CSymbolInfo...
6 ^( G$ J8 b7 f: Q/ binfo.Name(_Symbol);
" i- ~7 G  D  T8 i5 F; e//--- Set the timer...8 Q/ J& P  n2 b0 i9 Y3 E
EventSetTimer(1);( f+ a/ ~2 L3 S5 f
//--- Set the base of the random number to have equal tests...
7 @$ i; E; h3 W. v2 K. E$ r* kMathSrand(0xDEAD);. }/ i' _8 Z1 i
return(INIT_SUCCEEDED);! K$ v" ^8 J: y! m) H
}
; A2 Y5 r! v3 P, B//--- Since we set a timer, we need to destroy it in OnDeInit().
: O/ ?4 ], N/ ?( b# Z! k! f2 v4 }void OnDeinit(const int reason)
0 x/ _6 r& F+ O. y+ d* M% n{& `+ T$ t% z* z7 W
EventKillTimer();
6 \7 z$ l5 p; E2 ?! a- @0 W}1 [% P: \/ x3 o" B  R
//--- The OnTick function only informs us that we have a new deal
$ r/ p) ?' S4 d9 _% Rvoid OnTick()
6 b' @* |2 _% d" ~* i{5 a+ @6 E& D- L  l$ j* \
tem_tick = true;
. y) w" `4 y5 c4 Z; {( \}
# J* c7 x4 W, v8 z% n1 m" E  Z* X//+------------------------------------------------------------------+
9 V) N% Y" b) H" A# o//| Expert Advisor main function                                     |6 u" _# V) r. u9 P& F" f: m
//+------------------------------------------------------------------+
: B+ c8 C4 s+ b- h8 r4 Uvoid OnTimer()
1 E& G" y5 z$ v{5 I1 Q- O1 i0 j& |& E( w
MqlRates cotacao[];
$ D, l# G  |- Y; B- Y) creturn ;
, o: L$ n, s; {0 V4 N  z' c& V( K  Pif (negocios_autorizados == false) // are we outside the trading window?
% s6 ?% T' l. Z+ |7 H. `: }return ;
% z: f. K* l; `* m//--- We are in the trading window, try to open a new position!
. R: I. `9 |- k3 ^5 k5 lint sorteio = MathRand();/ ^2 Q) S: p. n  j
//--- Entry rule 1.1
, v( @: y6 z0 n" V3 Wif(sorteio == 0 || sorteio == 32767)  k/ n1 S" p* h5 d6 F- j# r! \
return ;$ Z+ ?, x, [$ d) E
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
4 G. `# _: @3 m/ N/ j( d{7 g( _+ x* H8 t0 ~) n; a
negocios.Buy(info.LotsMin(), _Symbol);
; x1 ?2 [' B4 ^3 @: y7 f}
$ P/ b  x; c, U6 Jelse // Draw rule 1.3 -- odd number - Sell( v! f6 L& I& N( Y, a) a/ w
{0 @1 [7 w: e( t( Q" X0 ~3 a
negocios.Sell(info.LotsMin(), _Symbol);  I% t8 Z, r, l8 n; l
}
! Z9 t' ]6 y+ A( |5 r- b}
4 @6 j  L# j$ d5 I- v3 R: v" h% z- ~//--- Check if we have a new candlestick...3 l) }% m  V) I; m2 H$ P
bool tem_vela_nova(const MqlRates &rate)& V; f! B# U  L( G! R5 c' a
{1 i# n- t/ P2 e# A# i
{
. _# z. H4 Z1 jret = true;
4 L: Y& k6 a5 x/ V9 K; ?. P7 ]1 Zclose_positions = false;
8 p- f+ {, T" Y5 a( {}
; }! l' Z: b5 |6 ^else
- X& D' B+ U, o" ?! ?& G# G{
% t7 t8 u1 H7 e" `- uif(mdt.hour == 16)7 b) e2 h( G, I( v8 D' B5 {2 Q
close_positions = (mdt.min >= 30);2 R9 n1 x" c9 m# F' P& K! g
}
/ r* _0 r5 T2 ^7 c) q}
5 }4 o7 x$ I; @3 \( n+ wreturn ret;( j6 Y$ P1 c  I5 a( y# a4 u
}, b2 F) n4 F4 ~* c% a
//---0 I5 V/ |0 }: o5 Z& j2 ]7 g
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( R: j. C6 ^: l5 s5 P+ y{8 J+ Q; h% i! J
if(PositionsTotal()) // Is there a position?, q7 N9 s& J" w/ F! m: i2 G
{
) J' y  H, Z4 F  d, g* Hdouble offset[1] = { 0 };4 ?* a) t# a6 v! r) V/ g
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 G% {' E9 T4 d9 U3 @) L( t
&& PositionSelect(_Symbol))  // Select the existing position!
  [" F" V7 u" |( W8 i; ^$ j/ H{
2 A. Q) K6 ~( J) AENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. d: Z2 N5 Z" D
double SL = PositionGetDouble(POSITION_SL);) ?( p; `1 _8 M5 A& g7 E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: H( Y7 w' ?- E0 _8 K" dif(tipo == POSITION_TYPE_BUY)/ l: e1 o) s2 K3 ]6 N
{% Z1 |9 \! p) z* M
if (cotacoes[1].high > cotacoes[0].high)7 t: {3 |$ R) w2 t( u8 N  Z' R
{
0 b4 S; ^* C, s" Ydouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 ~9 O) o5 {# R/ o; Q8 x! c! l
info.NormalizePrice(sl);4 S/ P$ V5 c2 L
if (sl > SL); G" N" o. s  F9 F4 [) x
{% E3 b1 x" w& c  B, s( {
negocios.PositionModify(_Symbol, sl, TP);
0 w' a0 {) s4 ~+ s- |" x}
- h% M- v8 y$ A6 ^3 A" t0 q}+ p* u' ~- @$ a4 B( _
}
) C7 H: W5 U) D! Q9 f: eelse // tipo == POSITION_TYPE_SELL4 R  ^$ E% O6 L
{1 [- \" @8 t+ |) l3 t+ o
if (cotacoes[1].low < cotacoes[0].low)
3 t0 q! W4 H& [7 r( u! p6 _" t{% d+ D1 o3 B' e  t' Z, v
return true;
  m7 g8 \: M1 ]+ b}0 I4 D/ r& c' u  [$ s
// there was no position
  \8 X8 z5 W& t% B- }8 W: Z/ Lreturn false;
" X+ Y0 [1 g+ o- a+ `}
& n- a: d% g( P# l我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
$ D5 l2 \2 k+ [; g- l到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-19 09:43 , Processed in 7.101502 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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