私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
" \+ {% R# E* @. ]. H在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 b$ g' S4 ]% n5 X4 C& A! F
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 m/ r: I. ]; C& T/ ?' K0 Q$ Y
以下是制定这些规则的代码。
, `. U  A, V. j3 E//--- Indicator ATR(1) with EMA(8) used for the stop level...6 O# c/ C- |2 x9 ~
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
9 ?5 `/ Q9 \% Qint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# q% _( `! q# P4 K# ?//--- Define a variable that indicates that we have a deal...0 \0 s( {$ i: L; O0 s
bool tem_tick = false;0 X' ^8 W/ f# ]0 ]9 T
//--- An auxiliary variable for opening a position& R9 j; w6 H9 }9 C+ g
#include<Trade/Trade.mqh>' W) ]$ B5 q  W$ F
#include<Trade/SymbolInfo.mqh>
/ t5 N, u/ X- y: mCTrade negocios;
+ `' d) B" a8 j+ J' OCSymbolInfo info;
7 ?, \6 p7 {/ D% Y  P+ u//--- Define in OnInit() the use of the timer every second6 y3 _' W, e0 `/ Z
//--- and start CTrade5 @% Z4 h3 U" T$ R: H7 J
int OnInit()& l  C  f. T3 G- H
{
/ E0 M' J, g& M/ q//--- Set the fill type to keep a pending order$ p5 P2 y: j/ Q! `1 F$ f
//--- until it is fully filled
! z) r6 e& ^9 `3 a; Jnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
# j. n  `2 ~" B3 f5 @3 h. n//--- Leave the fixed deviation at it is not used on B3 exchange( c, z1 [9 ^  `8 q; N
negocios.SetDeviationInPoints(5);
- _3 R$ i9 X3 X1 J2 m( t+ Y) F//--- Define the symbol in CSymbolInfo...3 ]3 j  {: N" Y! I8 j& v- l
info.Name(_Symbol);
! g% [' P# O; c6 U9 k2 U% i* r//--- Set the timer.... a$ A3 ?9 n$ j/ B
EventSetTimer(1);/ G/ W, X8 I( F. F# c( u9 W. u
//--- Set the base of the random number to have equal tests...% P9 l8 N$ m3 r' K! h
MathSrand(0xDEAD);
$ `6 \9 [, S5 }  n2 {! u$ C, z; C0 K0 creturn(INIT_SUCCEEDED);- i' i9 V: U% ~! b6 \+ d; J
}
' @: `' j$ j) H1 W//--- Since we set a timer, we need to destroy it in OnDeInit().
9 R8 ]' q. m8 y& r% ?2 i  c" Svoid OnDeinit(const int reason)
8 j7 Z0 s+ F( _& M{
, i% A% B3 k& Z/ y1 p# \; m. e7 R, TEventKillTimer();
# r" F) K$ h) i5 w}
4 [+ o1 R7 c5 N0 c//--- The OnTick function only informs us that we have a new deal
! K* k; u% T  x9 F; Svoid OnTick()3 x% K! G0 v. d  E0 c
{
' {6 P6 H! {4 x; H) gtem_tick = true;
4 M- L4 f8 `9 I. v; D( Y}; |; X( t* Z/ g) h0 c/ F  T  ]
//+------------------------------------------------------------------+
! r# h! x; {' [/ H/ ~8 ^3 `) P//| Expert Advisor main function                                     |9 |- J+ Q# s: s: G. a
//+------------------------------------------------------------------+: q' H- `, j2 p5 I
void OnTimer()5 ]9 k: v, O0 s2 k, n$ y
{* @" c, P' C8 V& ]$ ]
MqlRates cotacao[];2 b2 O+ c2 ~' e/ L
return ;
# e1 `6 {! \, L! J( D" O3 Y% C( v. ]if (negocios_autorizados == false) // are we outside the trading window?
- n2 r% p8 g! `return ;
% @/ [; a, g8 D7 D//--- We are in the trading window, try to open a new position!: A' W) j) c% m" ]( \
int sorteio = MathRand();
, }. `0 D7 i# @: s' H/ T//--- Entry rule 1.1
" [1 k* C, ^9 ~2 o  jif(sorteio == 0 || sorteio == 32767)* H. e' v6 U8 x
return ;  c$ v, ]8 `$ C/ H+ u! k
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy* `; g* i* `) R
{# q9 N- S% A& X5 {4 {/ u
negocios.Buy(info.LotsMin(), _Symbol);
1 U; R! n: m6 U7 P  S}
. R( H( n& \( X0 e& Xelse // Draw rule 1.3 -- odd number - Sell
; }+ ]6 D% \& ^* ?  p: P6 i( [{
( @# e0 W8 Y! r2 z( Cnegocios.Sell(info.LotsMin(), _Symbol);8 n% O8 X% d# u
}: H  ~3 l, t; V$ U
}' k4 q! `' H2 k0 k8 c9 ?
//--- Check if we have a new candlestick...
7 w5 L) M% W% H' n/ O& Dbool tem_vela_nova(const MqlRates &rate)
  g, H/ B6 t" Z. N{: G( C/ P; ~& d& Y) g  M
{
" ]( ]  _( @. L1 C$ rret = true;
) ~3 g- ^$ g6 _8 s+ H1 o5 Jclose_positions = false;
- B7 {7 f; J+ B}
% K  m& x: ]4 S7 {else# D$ h1 H/ L$ f/ m! k
{# R1 X( N1 B. k9 F# l6 `2 |
if(mdt.hour == 16)/ H1 Q1 j) _) j( g4 h
close_positions = (mdt.min >= 30);
* Q, K/ C2 J0 T}
$ |$ y" e  J7 A" K) Y7 C}  T! m/ e: J6 r3 {+ ]- S% Z
return ret;
% l- t: r% E3 o; U/ E}
' j" e/ `& P  P( }+ Y& h; ?//---
2 E- s5 R7 b5 v8 bbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 J% l4 A+ {+ h8 d9 F0 x{
, S) Y9 x9 K5 O. o# Z9 J/ r, C# I1 Sif(PositionsTotal()) // Is there a position?
  w0 T) G* {! Z! p& M/ j1 T9 s{4 F. T9 C; |3 m. W7 F) t* a6 j8 H$ j. L* G
double offset[1] = { 0 };- x' i8 @. K9 s6 n( Z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, }% r; ^+ e# z$ e: M/ V3 W( y+ V+ o& {&& PositionSelect(_Symbol))  // Select the existing position!1 o* ~% F2 u5 z/ x& n1 |7 ^
{
  J; O+ M3 S. o, u' L0 [ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ K0 ?6 J0 X% j4 R4 P. m4 l
double SL = PositionGetDouble(POSITION_SL);! ^2 I' t- v2 S$ E- j( a; B; F9 G
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ r; X. t. k' Q  a. R+ f. uif(tipo == POSITION_TYPE_BUY)
# @+ B5 d6 i$ i; p5 y" A{
+ j  k" Q! c; b$ c. Pif (cotacoes[1].high > cotacoes[0].high)
6 ~% b# I( L, ?& Z6 w! h* o{
; A" j6 d6 d- r) Z! m1 ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
0 o, U2 G4 u% m  M1 q: h; n7 ?6 }info.NormalizePrice(sl);
* C+ D9 t4 p: Q7 ~; S/ {: Dif (sl > SL)
1 m* S! D& q& j$ a3 n* [( _! y{
6 }* |8 L( m. G& X+ ynegocios.PositionModify(_Symbol, sl, TP);
( j9 u& }9 h& G+ n# r}
# _4 S) Q5 }5 ]5 L# P6 u}6 V6 `# }. v* V$ v1 U/ {9 R
}
+ x) N+ z2 H& l5 ~else // tipo == POSITION_TYPE_SELL5 L3 ]6 {# s1 c" ^
{
- |, W! g1 D$ O- ]6 Gif (cotacoes[1].low < cotacoes[0].low)6 A* E  I' S5 R: R* [) ~% n6 @
{# s, t, S% D: B  Y( {. _8 d: }7 L
return true;
- d  Z: w+ c6 q' e2 a- z7 j  \}3 ~5 S9 n1 H  Z
// there was no position
$ a) e: n' G5 X4 m2 o$ z4 Ireturn false;
2 N2 M' @# o) f' M}
3 T- ^# y( d' D7 |6 ?  o' C我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
* o5 W* N5 v. W- V4 T到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-23 17:28 , Processed in 0.422696 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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