启动交易模型,并构建 EA* i; T* f3 w$ c1 X/ N( ?
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
2 U( x# N6 B( r0 S6 j/ P6 w, R/ I为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 V9 n. C, y% u
以下是制定这些规则的代码。, a, B. i6 X; b4 p
//--- Indicator ATR(1) with EMA(8) used for the stop level...
% B, v" r! V6 V5 A Zint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
) o8 y' @! U; e) sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
, Y( z; t! ]* p9 n y2 h. x//--- Define a variable that indicates that we have a deal...
" d3 H+ R* D0 {5 a5 n" P( j) lbool tem_tick = false;& E. ^$ A9 ~; U0 g
//--- An auxiliary variable for opening a position
! q& J' x9 B" @, t! Y/ C! Z#include<Trade/Trade.mqh>* e0 ]9 T; x! `/ |0 E: y! N
#include<Trade/SymbolInfo.mqh>
/ {+ Y2 r6 v+ T/ C) UCTrade negocios;- Y+ W1 L0 P% X
CSymbolInfo info;
) W, E# {5 s F//--- Define in OnInit() the use of the timer every second
: }( [$ ~: x- D% P+ T/ p! u# h//--- and start CTrade$ m0 y9 z! ~% O
int OnInit()6 L7 D# u0 B1 E
{
1 ^& N) G( x& A8 |8 U//--- Set the fill type to keep a pending order
]+ R! P. ^ m//--- until it is fully filled) T/ @3 C6 R; |
negocios.SetTypeFilling(ORDER_FILLING_RETURN);& r3 Y' b4 c3 ~2 J
//--- Leave the fixed deviation at it is not used on B3 exchange
: T" ?8 j* L' d7 |5 t/ r0 V% ynegocios.SetDeviationInPoints(5);
( s0 P) C6 M K) o% f! ?//--- Define the symbol in CSymbolInfo...
9 P4 P+ p& `1 [ ]9 @info.Name(_Symbol);
1 C8 Z9 ]+ e5 M//--- Set the timer...5 p. O' o, n7 N% `) b
EventSetTimer(1);
0 X9 G; o6 B; X9 x" t8 F//--- Set the base of the random number to have equal tests...
0 R* M3 t8 W0 G2 Q QMathSrand(0xDEAD);
3 ?7 J- s% }4 z m t9 ?; y+ Hreturn(INIT_SUCCEEDED);
& G3 u: {1 e& G# c9 H6 g}5 Y& T+ @8 O/ B
//--- Since we set a timer, we need to destroy it in OnDeInit().: Z9 P, p% Q3 `
void OnDeinit(const int reason)4 _$ Z1 |& U% x1 B' F
{
$ c" l$ p B: `+ u$ F f& i, }- HEventKillTimer();( O$ \" G( N \
}
) {7 [/ L: A) [8 y: |+ j1 ?//--- The OnTick function only informs us that we have a new deal; e% P$ Y/ [4 S
void OnTick()( n O e2 d* |
{
0 i2 G' j9 u% H+ T1 Gtem_tick = true;: A0 N) L d$ w7 z& Q+ d6 `
}* Y8 T# z/ {% ]5 w* }! K
//+------------------------------------------------------------------+* {* g: }! f6 p2 p c) j
//| Expert Advisor main function |
$ | Y, V% l, f" x% d- A! ?, I//+------------------------------------------------------------------+
$ V# u4 r" z" v, I7 |8 I, E5 avoid OnTimer()/ Q. y3 V: k( N% `( ?5 E: B& e5 q
{
( b/ @( V4 h6 q4 UMqlRates cotacao[];
; ` i1 i6 l" ~- J! R7 y0 Ureturn ;
3 a" P6 W$ ^% ]5 q) }if (negocios_autorizados == false) // are we outside the trading window?
4 [# k: I/ h4 e& v$ oreturn ;+ ?5 \7 O" Z+ C; \: m+ i m
//--- We are in the trading window, try to open a new position!
. n. D3 w {+ N. Yint sorteio = MathRand();% q/ C: R1 ~/ [: p X s
//--- Entry rule 1.17 M1 |9 V, \. A* Z$ c2 p" [
if(sorteio == 0 || sorteio == 32767)- g) _( K8 o% h; N. ^' f8 h
return ;
' ?: z* d- z' eif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy' E5 Z" ]0 e4 x6 ~) Y
{
5 q* A" U) I' a6 E/ F9 o/ znegocios.Buy(info.LotsMin(), _Symbol);
/ {* F- Y6 B& ^- D- C3 {}' s; l. ]* u2 h' s; ^
else // Draw rule 1.3 -- odd number - Sell; _6 Z' w5 D6 W- m2 Y0 Q ^
{
" _% x* X0 g+ N" G$ C) Wnegocios.Sell(info.LotsMin(), _Symbol);) D3 p$ k5 k: J4 \
}
, E9 m1 F* Q! H" V}
7 j; D E* n6 H: d7 D4 G+ }//--- Check if we have a new candlestick...
x6 x; [' g9 J. r% tbool tem_vela_nova(const MqlRates &rate)
+ p7 H l* J, a{
& ?8 O) u- e, I{# i1 }! v; n" _! {
ret = true;
a, B2 O# m( aclose_positions = false;' J, l! Z: h2 q0 s' J0 _
}
, j$ h" `0 p9 {* U4 Q1 ?else
5 p; Y( Y, w5 }{
! Z( e @6 N. C& I; hif(mdt.hour == 16). Y% w6 ^( x6 o- b2 @! e
close_positions = (mdt.min >= 30);
) Y; j/ Y+ f, g7 j' f4 Q) d0 Z# U}
+ a+ W( x/ O- C9 H}
W$ L2 _2 d, {# Wreturn ret;% h8 |5 ^; N% Z! W
}7 O, \1 r$ s( q* Y- f8 r4 o" I
//---' q9 v! |( H# ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]), \6 J: Y. x7 w. Q6 y
{
! W4 ]. G: U# P. Qif(PositionsTotal()) // Is there a position?5 j$ o8 D: _% H) h1 e$ z& E5 a
{' C; i/ F6 j% m
double offset[1] = { 0 };
0 {/ ~7 h0 E6 O/ y* W/ @) I$ h& Aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# q$ R/ C" |' N/ H n+ f
&& PositionSelect(_Symbol)) // Select the existing position!: B9 o! }1 \! {* ?6 `
{
4 [- R- |- h! G" i! W; `* U% sENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 h* S( v* e3 R0 D9 ^9 \6 Hdouble SL = PositionGetDouble(POSITION_SL);
7 m1 ]; m8 M1 Z& j& m' D/ ]/ ~4 Zdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
! O! k; w' U+ E# \if(tipo == POSITION_TYPE_BUY), e4 }5 q4 I, S% d' @$ ]
{2 w& u" o- Z* b0 q, Y- x
if (cotacoes[1].high > cotacoes[0].high)
, U x7 M1 g4 l9 q2 h# z{ a2 @" V! U9 R
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 P8 Q# m, ?2 l+ y) D
info.NormalizePrice(sl);
8 w, c: {! c4 h/ @if (sl > SL), G* _1 |! d# e/ R
{7 D' o) t, T0 O* X; B- J; L
negocios.PositionModify(_Symbol, sl, TP);
( E' j2 ^8 Q& v% c; G" Q4 s! W}: s- t2 V, Q$ t0 A
}$ ~! A/ ^6 v3 G3 a# D. |' C4 ]
}4 e8 L9 [; R" E0 [$ m& }# N
else // tipo == POSITION_TYPE_SELL
0 \3 t" v7 X/ T" A) g/ P{ R; v# j' ~' W
if (cotacoes[1].low < cotacoes[0].low)
8 f. y/ O% a9 M, I( j, z{
' j4 F- X Z6 R' Zreturn true;
: j! ]* z4 ^# M+ b}
6 f, o W3 q |$ E4 j// there was no position/ o' S5 \+ x' u4 w1 j7 |: |
return false;; _6 `2 C+ g& B) N9 _% |! X) E
}8 o/ A- h3 I2 M r
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 x# e" D X' S, N* p" |: t5 Z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |