启动交易模型,并构建 EA
u9 K" {- C+ o# T3 k2 u在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。$ j2 m& I7 j! W8 V2 F, U
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ d2 \% ^* [& G以下是制定这些规则的代码。
, s5 s- }( ^. i//--- Indicator ATR(1) with EMA(8) used for the stop level...* u5 J( Q Y4 q2 M" x) c, ]. Y
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
9 G1 K- @1 H1 Q4 |" f/ V/ vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);* m1 a3 \$ |& {2 n3 E; b5 [- ]
//--- Define a variable that indicates that we have a deal...0 F) j- @) ^4 R; R4 {% H9 ]2 J: a
bool tem_tick = false;
( G% X# ~; B% K# x6 g. I& F. U//--- An auxiliary variable for opening a position. n, D5 i/ j T) i8 S
#include<Trade/Trade.mqh>5 J6 l* ~+ s( J5 f8 B/ x/ r
#include<Trade/SymbolInfo.mqh>+ i* ~+ {! j4 J5 F; |
CTrade negocios;8 j! ^, Y+ q+ B: [
CSymbolInfo info;
1 N9 ]8 q7 m6 h& z0 [, w//--- Define in OnInit() the use of the timer every second ]) }' }$ d: \; [# N
//--- and start CTrade
2 o% [7 o8 Y& m6 F8 z# ~int OnInit()" l# _7 e! I+ Q! b
{
: ~" Q7 M' g7 P* d% _//--- Set the fill type to keep a pending order
- ~$ [) {- S h# @& R( W//--- until it is fully filled* [- R1 S$ Y5 l' M( G7 {; L/ h( b
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! j- T# u- @: c- O6 R
//--- Leave the fixed deviation at it is not used on B3 exchange
& r" d/ r$ k9 z4 ?, s8 V4 }negocios.SetDeviationInPoints(5);4 a3 C& }* B. S& D: E7 N6 F" L
//--- Define the symbol in CSymbolInfo...3 {' g" ^5 ]5 B5 p5 `. {8 R
info.Name(_Symbol);
+ J2 `" [) [- \, B//--- Set the timer...; e7 W1 \' D& e/ F$ L- T' [
EventSetTimer(1);
4 `. A, `6 ?& b' r. j//--- Set the base of the random number to have equal tests...
, x# b* k8 X) G2 v& T9 t6 [MathSrand(0xDEAD);
* O2 ~" x! ? m7 p& treturn(INIT_SUCCEEDED);0 T9 h, ?3 Y- s+ t, X& L
}% P, L: d5 ~# a, V* y8 |
//--- Since we set a timer, we need to destroy it in OnDeInit().( l0 m0 {' ^$ r( ?2 b; \
void OnDeinit(const int reason)
7 N4 V4 q5 s( U8 W{
6 }; s1 _ h7 `: j& oEventKillTimer();$ {; d1 n& h) m6 v8 T! A
}
" X4 j1 [, N* G# R/ m//--- The OnTick function only informs us that we have a new deal5 k" b* c' V6 x+ B) T
void OnTick()
" q! N+ q* V! p{
, m" ?( t6 D+ W0 Ftem_tick = true;
( I. h `; t: O}! [/ u! h1 w0 l7 V
//+------------------------------------------------------------------+
( y- i6 f) a$ e5 u- B# s0 y+ x//| Expert Advisor main function |2 t# y& k" w$ r! j8 W
//+------------------------------------------------------------------+
. q) C; L7 w& z$ ]4 m6 ]+ hvoid OnTimer()
* p7 ^. u1 v- L g3 n) c( M# ~! o; D{
1 t! N( _/ ^+ r/ B) o6 I) NMqlRates cotacao[];
+ h- t. J5 t. Lreturn ;3 G) `; i4 I+ r4 `- a
if (negocios_autorizados == false) // are we outside the trading window?3 `" ^2 Y( k9 q3 a# E2 r/ M, `
return ;/ e4 K- O4 F4 O" B1 Z) z/ g/ g
//--- We are in the trading window, try to open a new position!/ c: b M; @2 L
int sorteio = MathRand();
4 | U7 L# `( f5 l4 w6 S//--- Entry rule 1.1
0 Y5 y5 ~) U( K( r& Lif(sorteio == 0 || sorteio == 32767)1 F2 _' T9 c: ~/ [, E6 M0 l6 ?- M& x
return ;, q& J9 l3 F0 K' g2 F
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy) X4 v0 a+ ~- ^! P' n: h/ q3 b/ i6 ], y
{( N: O; Z. K8 g3 K- H5 A9 Q* {
negocios.Buy(info.LotsMin(), _Symbol);8 b% f5 m1 X- `4 |
}; D1 _% e( h" g* d3 L$ T" ]/ e
else // Draw rule 1.3 -- odd number - Sell
4 ?- g2 h2 g( ~' i{
& {5 X8 [6 X6 _3 R8 l# W! Y& Qnegocios.Sell(info.LotsMin(), _Symbol);) A0 R1 n- [% T' c7 Y- ]
}
! N( y0 I6 Y: ]* S7 F8 _! ^- }6 d}. D5 k4 {) K/ h6 _) E9 [
//--- Check if we have a new candlestick...& C5 l$ w5 E; c0 a
bool tem_vela_nova(const MqlRates &rate)9 p4 P3 v% T- o
{$ R1 P4 W! T |" t/ E
{0 s( K8 }9 X5 x* A" l# W' U( ^
ret = true;
4 ~2 T, P1 g8 c$ Vclose_positions = false;) a, V& b/ h* g r$ z+ f+ P# b
}
( H0 ^1 T) l8 ?& {3 m7 aelse' Z- Z! w& b8 r5 q# {1 Y* i2 Q
{
9 `1 C( o3 u0 R, N; |% q0 `if(mdt.hour == 16)
) @( V/ } y& [4 _close_positions = (mdt.min >= 30);
9 V6 }* W$ l/ x, ]: ^5 c} d/ M% Q/ D- g* O" O/ L
}( h4 N9 L3 W; b$ e' h. k
return ret;
1 z0 p1 b' Q( D2 v C) q8 W}% ]* b8 w! i, J
//---) l- n) B1 p7 E |) v1 [) w
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
# M/ ~" I5 H' l4 [{
% M& u, `9 \1 z/ dif(PositionsTotal()) // Is there a position?; Z5 \% {$ T& [+ K, t1 x
{' `$ p4 U& t2 ^: {8 p) v
double offset[1] = { 0 };6 Y( B/ ]& q$ u0 J! p: T
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 b: Y! R v1 H8 _. ]&& PositionSelect(_Symbol)) // Select the existing position!
@ Z9 H3 I# |+ q) R& J( N- V5 Q{: b4 o* K' j! X
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
9 L4 @/ o$ x, j$ j; H$ w1 t8 hdouble SL = PositionGetDouble(POSITION_SL);; o! {" _7 K/ J* u1 k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! s3 g Z; ^( }& w5 {
if(tipo == POSITION_TYPE_BUY)- U1 \% m0 m3 ?8 z! |& S
{ l+ A/ G, h) U$ a6 W6 F
if (cotacoes[1].high > cotacoes[0].high)
U8 s) E4 F& @{
/ v+ x. G. o0 R: mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
, G6 Z+ ^& f) V4 \info.NormalizePrice(sl);
, S4 o& y7 w1 P0 w% B: Uif (sl > SL)
' p9 ?7 U& H& o% Z4 ]{2 w: C" g7 ]# S* Y/ y
negocios.PositionModify(_Symbol, sl, TP);7 c6 r2 h& ^5 n K" e: t
}
% S& @7 a6 B8 o! n2 v}
) h, ]7 s0 e5 ?" N4 l# |8 s}
" t7 B& A1 T, q2 E7 h; _ [4 oelse // tipo == POSITION_TYPE_SELL
3 p. i) C4 ]- D$ _6 W& C' Y; W{2 C7 ^1 D" `& r% Z: q3 i
if (cotacoes[1].low < cotacoes[0].low)! _7 N Z5 R; m
{
1 M1 m r* z$ T/ Q' E5 E: }8 zreturn true;
) X5 @, t+ h3 B; G+ k+ O: B}% G% Q0 |7 L! |' d) b" P
// there was no position
U' G6 |$ }1 [return false;6 l8 b) J: U; b: g$ W- p2 a! M' w3 W
}' `9 C2 E' o2 l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ H6 v7 ?9 ^! X
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |