启动交易模型,并构建 EA6 f+ \- L5 X/ G5 c# Y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。! A* G( m4 i6 f* t5 d; C8 u1 ]3 g
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' j* M% F; {# a7 ?
以下是制定这些规则的代码。2 h: A% o! _4 q# l) v: H5 Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...
5 |! s5 j- i. G Xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
0 O0 h( r/ k9 K7 L! s( |5 Nint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);1 Q1 b5 Q) H8 D
//--- Define a variable that indicates that we have a deal...
# w0 H+ B- Q3 e6 L6 rbool tem_tick = false;
$ u9 l' z" |7 C! h: {) B! ~9 t//--- An auxiliary variable for opening a position, _* {" k- e H& S7 q
#include<Trade/Trade.mqh>" P$ `, N: ?* ]% y* ?$ G& g
#include<Trade/SymbolInfo.mqh>
, v7 d0 W8 N" O' pCTrade negocios;0 a2 A- ~2 x* j7 I; x" d
CSymbolInfo info;2 e' N8 Z% [+ |3 q$ O- b
//--- Define in OnInit() the use of the timer every second0 C6 m, W+ v! ^8 a$ u( S: b1 E% O
//--- and start CTrade8 \1 L9 ? ]) d' E6 h# ]
int OnInit()# f" }1 n4 y4 M6 w0 B l. @* q+ Y0 I: V
{$ ]8 N) w4 l/ G3 I0 V
//--- Set the fill type to keep a pending order
% R$ R9 u; Q& i- L3 I* L1 u//--- until it is fully filled) g! p! d$ Q% U$ L& V
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
% n) J( \ c# Q' v/ l//--- Leave the fixed deviation at it is not used on B3 exchange
4 {- y9 A; d$ [+ p; q, g+ I( Inegocios.SetDeviationInPoints(5);
) i/ L0 s0 o+ A' w, W: Q//--- Define the symbol in CSymbolInfo...! n- Z3 Z+ g; `! Y; u
info.Name(_Symbol);' I. t6 \7 ]) m, e' J: S4 j4 R Q1 `! Q
//--- Set the timer...; W% t3 ?' B- @* x
EventSetTimer(1);
3 E9 s1 d) p6 X) W0 k//--- Set the base of the random number to have equal tests...- k. j1 I! y- B2 h$ j, @) T8 y [! ~
MathSrand(0xDEAD); D4 Q7 X3 h$ W2 i
return(INIT_SUCCEEDED);6 Y& k: Q( _5 A5 u. b: |8 P
}; w% |6 a* B6 M7 z. i
//--- Since we set a timer, we need to destroy it in OnDeInit().- ?/ e9 j1 k- C. o" j3 o
void OnDeinit(const int reason)
Y% _- G0 N$ t9 Z; q/ A) I# b( e{6 a7 r: r" ? t1 E" M
EventKillTimer();
8 O2 n7 G# S- P7 V# G6 k}0 Y% {- x3 ?( e, @/ O% P* B' s0 P
//--- The OnTick function only informs us that we have a new deal
6 M8 O7 d8 I! V" f. g$ c- H9 i+ jvoid OnTick()
- _6 N! M; P7 W& j0 w0 a{
+ o& d5 ?9 i; e- P ztem_tick = true;
0 S/ G4 P \8 K0 z7 }9 x' x5 P1 J}: c. G! x. D" H {
//+------------------------------------------------------------------+4 X) C' }1 K4 E: g
//| Expert Advisor main function |9 c4 H3 X3 C& \' L5 q( p9 T
//+------------------------------------------------------------------+
) x! G2 C) z7 R# T- q yvoid OnTimer()9 R# ^( N2 o* V+ \- O3 z
{
- F$ l) I7 W: ?$ B- I) gMqlRates cotacao[];
7 m5 \% q5 D8 G; B" K) u2 u& \return ;7 t/ e9 y% C1 f# n3 }
if (negocios_autorizados == false) // are we outside the trading window?0 U" Z; ~/ L3 M; x8 f
return ;/ g* Y$ i' }9 L2 j0 E- Y% V# j( I" Z
//--- We are in the trading window, try to open a new position!3 _1 D; x& |7 D5 }2 W
int sorteio = MathRand();
7 I) x* N: I: a$ i+ v9 {: V" ]7 [//--- Entry rule 1.1
% }$ Y+ o- ~8 [1 jif(sorteio == 0 || sorteio == 32767)" X T' p& `; T2 z3 ~2 Q6 l
return ;
1 _% \6 b# C! @$ k6 rif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy
! c7 v: f6 N7 \0 C{2 l' p B! }8 D+ }: J
negocios.Buy(info.LotsMin(), _Symbol);! w1 k. f7 R2 R2 M% I: @
}
/ K% |+ m! b8 Yelse // Draw rule 1.3 -- odd number - Sell
- c" }- c. j$ e8 ?: g2 z, M{$ H: `$ ^- }" l+ ~
negocios.Sell(info.LotsMin(), _Symbol);7 L T5 W. H( w" `7 L8 M
}! i& H; \' a! K' \ k
}
2 v2 p# u% r+ s% Q4 A- L//--- Check if we have a new candlestick...8 I# P; L% S0 Y, X4 \
bool tem_vela_nova(const MqlRates &rate)
( M. b+ ~+ ^* E! W4 Q% N{
9 ~8 v x2 C) |8 t{
( G8 j: T; N$ n O& n& |ret = true;6 D% q) l2 R5 q/ j7 g) T
close_positions = false;1 W$ J8 j1 b9 `1 O8 V
}
: l# ]4 A1 ~. G4 ^( }; ?6 a6 Eelse
" D, q8 s2 ]1 v( `8 R' a{9 P' q+ h8 z# G6 D
if(mdt.hour == 16)
* a4 C. ~9 H/ n- nclose_positions = (mdt.min >= 30);
4 j7 ?! b9 J! h- O1 j' m}
$ f; b, g8 k3 E( L) q- F: l* P. V5 O}9 M: c* C; q& D' n5 w P @6 b
return ret;
6 M9 v. B, |7 x2 V5 O2 S}
6 |$ N9 p p4 ?5 Y) I L- R/ R5 o% i//---1 E- X& g" L- _4 _/ M8 o* L% w
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]). @- d( J" H0 v( V `. H
{# R; s9 a( N$ w" q# w7 A
if(PositionsTotal()) // Is there a position?4 ]$ ^6 `4 w# o
{) ?+ ~1 R( D$ o4 V, X; B- G, l+ X
double offset[1] = { 0 };. c0 c" H" g1 j8 s' ~( ~
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?) t% m a# X4 d1 y" k1 P
&& PositionSelect(_Symbol)) // Select the existing position!
6 I+ l! n5 }; j) R3 h0 ~{( D& T0 \# {5 Q5 K/ y% o" V# L
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; O2 ]9 Q+ c: edouble SL = PositionGetDouble(POSITION_SL);
3 K5 x/ ^) R+ z& E/ odouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));/ V7 h$ s2 y6 p1 ?
if(tipo == POSITION_TYPE_BUY)
& i, B7 e } C' M9 R7 Q& g: P. z{
/ ?3 x r" J1 I6 X0 k. K6 [if (cotacoes[1].high > cotacoes[0].high)& D% q* @, B& c( j
{
; g2 `) ~& Y- H3 k# [double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];, W, U( Z; v9 H) C2 [% p Z+ P
info.NormalizePrice(sl);
" w5 {- _, H5 [: Qif (sl > SL)
4 F G3 ^+ B6 o' I( G3 k& ^{
: V+ Y7 I# D& B) d3 mnegocios.PositionModify(_Symbol, sl, TP);" z' h: O' H: N
}# T4 I5 F4 O. T9 J
}5 S3 I ~; {1 L8 r
}
% }$ L# F; \; ?' yelse // tipo == POSITION_TYPE_SELL
* e4 B! p. R4 m0 @4 l{* x0 d6 @0 {3 \' s
if (cotacoes[1].low < cotacoes[0].low)
( n0 L0 n0 H; [1 G5 A{
) l/ P! r: z' K& Ireturn true;7 T7 {# x& U' s+ i
}
' e* i/ G' E( l" A) _% z# L// there was no position
: P$ I# {9 d! f' o9 t; Mreturn false;' O9 t5 E/ \* G9 N3 B8 o. f- p8 p! v
}
* E! ?# Y* p% ?' l3 {$ D4 Z2 o我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
i( i' Y$ D7 G) _7 A) A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |