启动交易模型,并构建 EA
u* M9 m& c7 p' l4 b5 ^在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 E. |7 o" |( S$ c6 _; V$ t3 _为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。- [3 _5 ~# Y$ C2 M5 c$ K, A0 V1 e
以下是制定这些规则的代码。
" f, G$ t0 Z2 a5 y% P' ]: U$ K9 d//--- Indicator ATR(1) with EMA(8) used for the stop level...
5 }' v2 @1 a: r: x( c2 Eint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" X5 {& M' s( ?* Dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# M6 m- |. M0 s4 c+ q
//--- Define a variable that indicates that we have a deal...- d2 d/ s6 {- S) @
bool tem_tick = false;- x: p" B8 x8 c( e% ~3 L; E& x6 R+ S
//--- An auxiliary variable for opening a position
9 e, Y; U$ Y7 M4 x#include<Trade/Trade.mqh>
& k% |1 a. x x& x7 D8 I3 ]6 m2 I#include<Trade/SymbolInfo.mqh>
. V: L5 u7 S& S5 X! s8 M: TCTrade negocios;. R' {' l9 G8 O( U6 k, m
CSymbolInfo info;2 w j/ e" H6 r v Y; w
//--- Define in OnInit() the use of the timer every second
- \; x; i v7 b% v9 t//--- and start CTrade
8 ^$ u& y/ R: D: Qint OnInit(). ^( R1 \6 i, g
{
9 M% O: @# `: ~//--- Set the fill type to keep a pending order8 z1 l) y3 S: {8 o! T* T' R3 h
//--- until it is fully filled) x4 `7 U- J# Z7 h2 j8 [* B
negocios.SetTypeFilling(ORDER_FILLING_RETURN);( c7 Q( u" E) R9 Z, X- e
//--- Leave the fixed deviation at it is not used on B3 exchange
( |7 s& I6 v$ ~negocios.SetDeviationInPoints(5);8 e- _# _6 g" l( u5 G @
//--- Define the symbol in CSymbolInfo...
7 c$ N2 _, {/ K4 f/ u ginfo.Name(_Symbol);: f( n. |4 B8 x. K; c' A3 _
//--- Set the timer...; A. H0 M; ?% c
EventSetTimer(1);$ A9 R& N. c: _$ l2 ^: ]0 e
//--- Set the base of the random number to have equal tests...' Z) }0 [& ^0 d8 s
MathSrand(0xDEAD);7 F9 }. X: @# a# ?8 k, |( P
return(INIT_SUCCEEDED);1 | U- X N; R* W. m2 h/ F
}
8 c# h; p6 l0 Q3 S/ M//--- Since we set a timer, we need to destroy it in OnDeInit()., T% J8 C# X9 C3 o+ c- \$ G
void OnDeinit(const int reason)
. g: a9 y# }: m* Y" A{
$ U5 _$ i( Q5 o& ~% n/ {EventKillTimer();& X. o- M* M4 x
}
4 m. Q0 c( [4 q4 ]$ T//--- The OnTick function only informs us that we have a new deal& f w$ U9 o F" I
void OnTick()
% S/ }# }: O8 \8 {{
$ W9 W9 f2 C, Z% X8 Gtem_tick = true;- j. y. Q4 [5 x
}8 ~! A% T) S9 a v% e, {
//+------------------------------------------------------------------+* l7 N6 F" R* z, B
//| Expert Advisor main function |& c) \" y' Y1 q: s3 V5 u
//+------------------------------------------------------------------+% ^9 I! D2 R, R3 `( [1 I% B3 Q8 U
void OnTimer()
; H0 O6 C5 c- t( w0 C* b{
5 R( m# p j* E3 v1 G$ Z. DMqlRates cotacao[];4 G: ]: _& N% D
return ;
8 ~. r; V, j' jif (negocios_autorizados == false) // are we outside the trading window?
/ }, K! V2 M4 \% g' I, j, w# {return ;! q% I, v+ B8 h( M6 G
//--- We are in the trading window, try to open a new position!. b: M+ N* a! g8 \6 O7 Y3 d$ T( w( E
int sorteio = MathRand();, e- {4 w' @, O r7 x A
//--- Entry rule 1.1! X' d4 [& h$ p- W$ V+ N0 D, z
if(sorteio == 0 || sorteio == 32767)
+ Q8 q: ]+ ?+ h$ R& |- B b- m/ h$ Freturn ;6 e" p! y& b, I4 l/ M
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy9 A8 h6 y5 q- l! B( N5 N3 l& p$ w
{6 e4 M S4 }; M% d+ t. P t; u
negocios.Buy(info.LotsMin(), _Symbol);
]+ Y* t0 O2 |* U, Z}
1 ^! p6 H! M4 G' o! F0 Z7 F" Belse // Draw rule 1.3 -- odd number - Sell/ A" W0 |+ ^0 b
{
6 z5 I( V: N- P& ^; inegocios.Sell(info.LotsMin(), _Symbol);
# L# Q3 _7 m7 b, L! @4 O: X% V}: R8 }5 b- F0 O6 A& ^# M( i+ f
}
' }6 E2 v. m; g0 Z' ~//--- Check if we have a new candlestick...
" X) O& V3 r8 R* N" p' dbool tem_vela_nova(const MqlRates &rate)
( C8 U1 [$ _7 X7 q. ?/ b; s# E( Y{1 A8 [' e( R4 r$ U* G1 B- Y
{2 j5 c4 i, z) j$ A
ret = true;4 r* [, ~0 l8 x( m4 R7 y
close_positions = false; e2 T1 |6 N+ H: n" y
}( j& R$ H" T7 `) r! e( k- U4 q
else
- C3 p9 H9 Y3 i) t' l{
# Y- O" c n/ H2 m+ f8 \+ tif(mdt.hour == 16)7 E5 g% {' r" k1 G. ?
close_positions = (mdt.min >= 30);& [! m4 v# l' _ Y; _3 h
}
) L4 r: m' g- h# l* x8 a}
+ C# y# I% x5 v; U, vreturn ret;1 E' @8 R, B5 T* i' ]( D
}: R1 e7 S5 y% @+ ]& H. q
//---5 m; s" W0 ?$ b
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! M! z% A* w/ H; p: ^
{
# U# u& y$ T( @7 ]3 p& aif(PositionsTotal()) // Is there a position?. p. P9 W% z& k% U
{0 B+ H! S* m1 t) q. u
double offset[1] = { 0 };
% y" y7 s7 V4 i1 v$ Xif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; M0 f4 U% ^# R: {5 y0 u&& PositionSelect(_Symbol)) // Select the existing position!
# ~7 c7 t( m' f% L: j9 d2 t# | g{1 U1 b( S5 s# e& _5 ~3 k# B
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
) n7 h2 e' i5 |" d9 T# l3 jdouble SL = PositionGetDouble(POSITION_SL);; N( v V$ T8 f: h& M8 I% t
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
* p1 V* j0 _3 Gif(tipo == POSITION_TYPE_BUY)
9 D1 ^7 C- Q9 J2 ^3 s+ \8 N, H{ Z+ m6 p: a1 ~! s
if (cotacoes[1].high > cotacoes[0].high)+ j) x7 k+ X( c5 B
{1 x* P. Q7 M4 d1 ^
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 Y0 V! p! T& z) f# cinfo.NormalizePrice(sl);# W9 O8 o, I" [7 e% g+ ?
if (sl > SL)3 ^& }: x( @, T Q+ U7 W7 h1 V0 o
{/ O9 N& U+ u' j0 T% ~
negocios.PositionModify(_Symbol, sl, TP);
M" `& L9 v+ Z9 z% F}/ b- X- o+ i' T. u
}1 E! j6 m% e$ I2 ]1 [: n
}$ w+ K7 \! i. [" @
else // tipo == POSITION_TYPE_SELL
/ Z% f- P% Q$ t' g{) h' @: A2 E, d# s' O+ G
if (cotacoes[1].low < cotacoes[0].low): y5 c" K: G; \9 J& X5 W
{
' Y, ^) `% g7 d: {" L& S" u2 M) lreturn true;9 n8 A7 Y+ P/ C& d: ?7 l- d
}
: d, p3 x; C$ m* h7 W1 K// there was no position
% E7 `8 U: v% }/ a% X8 vreturn false;
4 C: |6 j0 \+ I0 D3 `1 c- c( Q}7 `2 n& L" L. E* l
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: q+ H! G, R$ d: }/ E, M& [到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |