启动交易模型,并构建 EA
2 k6 ~1 X6 O5 N8 a在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( S, D( p4 x7 E为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。4 C6 {% I! F9 y0 h L( I
以下是制定这些规则的代码。1 D( h' f _. h
//--- Indicator ATR(1) with EMA(8) used for the stop level.../ `& U4 y3 J: ~) W _
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);# L& E1 w$ q* t- O, e/ v2 L
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 { R8 O/ {3 o+ A* n; m//--- Define a variable that indicates that we have a deal..., r; ]( |0 s8 |& O) q% U+ E; }
bool tem_tick = false;, ]4 v" Y* B$ Y( q7 }
//--- An auxiliary variable for opening a position+ L* w% _- A& g
#include<Trade/Trade.mqh>% B! W. N. V8 j
#include<Trade/SymbolInfo.mqh>8 e) t: p3 s. E, O5 c' ?$ o$ t
CTrade negocios;( i! ?# t7 }( D8 s) B6 X
CSymbolInfo info;& q, F2 R8 B3 r% P/ ?5 y
//--- Define in OnInit() the use of the timer every second
7 G# T, L8 W5 C6 b7 y2 S//--- and start CTrade% L: U+ H# |; G2 F& ^3 R! P& ?: \
int OnInit()
0 V' h3 B# o/ z+ P; r{+ d+ `' Q1 I. s8 `
//--- Set the fill type to keep a pending order# W! I# o9 F2 p8 \$ X* k! d
//--- until it is fully filled
" T4 @: V+ U. }5 s, w* Jnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
# l* t. d @+ U! X* n+ `. F, H//--- Leave the fixed deviation at it is not used on B3 exchange
- g" ^4 a: R: r) P7 N' T" F* F7 vnegocios.SetDeviationInPoints(5);0 k) s7 N; h2 {
//--- Define the symbol in CSymbolInfo...
+ X! k2 L7 u K Y9 Winfo.Name(_Symbol);. u/ N* b; y$ r) v
//--- Set the timer...3 G, f' y% t$ g
EventSetTimer(1);
1 U- ]2 V% M+ }& e- u//--- Set the base of the random number to have equal tests...# G f! A! H0 a8 a4 d
MathSrand(0xDEAD);( q( a7 N4 d2 r/ m& ?- ~3 w2 s
return(INIT_SUCCEEDED);
) M3 k5 B" @1 i0 x}
6 G/ J s3 b4 b6 s# U//--- Since we set a timer, we need to destroy it in OnDeInit().
* M0 \' t* _2 ]2 s1 {void OnDeinit(const int reason)
8 X( u5 P! V3 A{$ Q+ |0 [/ r1 W+ A
EventKillTimer();# }+ K( r. D( a" }4 h4 z; S
}
7 k4 K% k6 N1 V! A9 F- \+ d//--- The OnTick function only informs us that we have a new deal
" ~" E! |3 z8 U6 Mvoid OnTick()
" x# U( K2 w6 n% H" g{
' j8 {) S" a: u, p W7 r9 Mtem_tick = true;
. G2 q) Q" v5 j3 |5 ], l}
( s% z- I M2 r. j7 v" @7 q//+------------------------------------------------------------------+% q' z7 y+ X8 s/ R2 I* m
//| Expert Advisor main function |' A6 b Y, C1 {7 N# F+ r7 l
//+------------------------------------------------------------------+
2 W- J; E6 s; p' X7 hvoid OnTimer()8 R6 K1 z0 t( {. j1 X% ?
{
2 Z1 M& S* s( W% ]$ rMqlRates cotacao[];
; s$ c; H9 q0 V8 @return ;! _5 n" v( \- f. M8 D6 f
if (negocios_autorizados == false) // are we outside the trading window?
. ?4 g$ {# f$ o0 @% Nreturn ;, S2 M$ _ r& |
//--- We are in the trading window, try to open a new position!$ L4 N, O+ O/ m
int sorteio = MathRand();7 u1 \4 \- K7 ^: r2 m
//--- Entry rule 1.1, P4 I9 g! [' }+ g% S" w
if(sorteio == 0 || sorteio == 32767)% F) ~6 s, `# Q4 E( x* h$ e
return ;' j: g c' i* h9 Z1 j: Q" _
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy: h; u! j; G5 S% H0 y# N
{0 C. L" w- N" P
negocios.Buy(info.LotsMin(), _Symbol);
0 B8 ?2 ]0 j7 F( ]8 x- R; M2 j y}7 r# ]3 a3 i1 y6 d9 a( M& i% V
else // Draw rule 1.3 -- odd number - Sell' R8 U; }4 C4 M [* b
{
7 x' ^. d- q5 jnegocios.Sell(info.LotsMin(), _Symbol);
* Z& a% U; T/ T9 I- n7 m0 w6 P8 [+ `7 j}5 D2 b2 O6 X& a
}
# }/ D' H$ r+ C//--- Check if we have a new candlestick...
2 s' k4 t6 h- |7 p- \1 b( J7 J0 cbool tem_vela_nova(const MqlRates &rate)% v8 i5 g/ T3 u6 e. L6 V$ F/ v
{/ m. ^: v8 A. K P h5 ~
{2 q( r9 R7 n* m2 ]
ret = true;
9 I3 K: P# l6 u* L wclose_positions = false;9 ~. z* A1 a% c! v) H+ x
}
6 k& Q, R/ x& m, ?3 ]' W0 _$ m Z" Helse
) T$ `1 V# H5 }; r/ i* E1 B) D& \{! t/ C* S' O2 I. D2 a- }3 w
if(mdt.hour == 16)
7 D0 Q! k. V+ J1 r9 l# Jclose_positions = (mdt.min >= 30);
0 a7 `' }0 X4 G}
! c& k0 Y! Y0 U6 ]9 O}' D: Y8 q ?% A2 b! g- M: g1 z
return ret;; y& f/ H- G; I1 b. k& I; R
}* I% X* E c! F0 E
//---$ f/ G6 v+ q! ]- G+ R$ l
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 z @7 s4 f3 ]% O4 A{
) ^" b, d. D6 ?; N8 q- cif(PositionsTotal()) // Is there a position?( R7 j( t$ y. L2 g) I( |
{. I7 Z, S/ B, G3 n, S
double offset[1] = { 0 };
1 h+ z4 u8 Z8 P" E/ J0 tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' J; S7 x7 D6 I7 h
&& PositionSelect(_Symbol)) // Select the existing position! m$ `3 O7 G' l3 j U3 L
{
- ?9 x5 X0 y) E/ b" d, O4 K" |ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! S' T9 v1 V( V( wdouble SL = PositionGetDouble(POSITION_SL);
9 H- }2 w, Y" r" ]double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# d/ X# j6 D8 X7 k
if(tipo == POSITION_TYPE_BUY)
1 ~/ E5 {8 k& {: @{+ i: ?# h) ]: K% m
if (cotacoes[1].high > cotacoes[0].high)
0 }- P4 I2 P2 o, ~9 T{
" l+ L0 C7 H- ]. X( n6 mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ T$ V- J) z4 X/ F# C3 K; k( Q& E {info.NormalizePrice(sl);! Y; V5 w. c3 g6 j3 c2 _- Q
if (sl > SL)
9 p% b, L- a. Z' y3 z7 I% L! p{2 H2 o- K/ u9 M) ]7 T' R
negocios.PositionModify(_Symbol, sl, TP);1 K5 m+ j0 t0 l# U
}
; d: L5 i) W- O* _" Y- {}! b& K5 t- X4 ?% ^: v; G: z
}
: N* U, q; U; P/ `: yelse // tipo == POSITION_TYPE_SELL) I$ ?4 f V" V: O
{
9 u0 V$ J M x# \7 @: U" ^if (cotacoes[1].low < cotacoes[0].low)
9 _& l) X0 x# @8 O) P0 r- l{' r" h& b4 E) @. R
return true;
4 g: X) x( d% M3 t+ p}
5 {$ t6 f8 @$ k' m// there was no position; p: S7 a- P e( F* Q# X4 @9 N8 K
return false;5 C' w6 U3 |% {# [3 p! F
}1 X" c5 F: R/ s% F
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 z. U* P! W# n到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |