启动交易模型,并构建 EA
' S8 [8 h; d; ?0 h" Q在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- ?0 p' E! L i- O/ t5 D为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' E8 P2 r9 _9 k( q' G
以下是制定这些规则的代码。
. _2 T% P/ ?. A% l& a# Y//--- Indicator ATR(1) with EMA(8) used for the stop level...
- B; q+ X7 p1 I! }$ ~ pint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 O& {* Z/ z# L Aint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);8 l; Z( B. \7 s: Q
//--- Define a variable that indicates that we have a deal... e, v7 n, R3 a& F
bool tem_tick = false;2 Z0 m& b; j/ L
//--- An auxiliary variable for opening a position
! t0 Q4 j: X# _" ^+ R#include<Trade/Trade.mqh>
0 L. y! J$ Y+ ?! |* D( e; J#include<Trade/SymbolInfo.mqh>' r) P8 H. f0 Y) M
CTrade negocios;
! y( S8 d- n4 f/ w- K7 W6 Z5 ^CSymbolInfo info;
' `! Q& S# H8 `' i: b; |% S- n9 b//--- Define in OnInit() the use of the timer every second, p- k+ B6 K# g3 k( y' k3 _
//--- and start CTrade
w- V& w5 ~, I- j9 v' q# xint OnInit()+ g" f d7 f5 Z3 P
{
9 Q ?. \- s8 \3 ?" G6 n//--- Set the fill type to keep a pending order7 k* `% A! Z3 @0 [
//--- until it is fully filled
0 T: p: P; K: a; ^negocios.SetTypeFilling(ORDER_FILLING_RETURN);( t8 s; x0 J$ Q) u5 ?, r& x
//--- Leave the fixed deviation at it is not used on B3 exchange
e/ F& X5 F! _! S5 [$ ~0 Q; dnegocios.SetDeviationInPoints(5);& k g* [% k2 y/ M
//--- Define the symbol in CSymbolInfo...
" i) f1 M& Z; M7 A+ vinfo.Name(_Symbol);$ P; I1 ]9 K( r. |$ a- `3 V
//--- Set the timer...
! _# s/ P- ?+ s% [7 VEventSetTimer(1);+ O8 ]! n! Y" a0 ?) r5 I' E
//--- Set the base of the random number to have equal tests...
: \9 Z- w- b% G }+ U9 i1 UMathSrand(0xDEAD);: |7 d) n7 Y! q& e7 S2 I
return(INIT_SUCCEEDED);
- h' m& P! n; f7 G w0 |% t3 o1 l9 [}
3 O$ A: m7 s# D4 C5 _- I. p) B//--- Since we set a timer, we need to destroy it in OnDeInit().
: k- w" ^8 I$ w6 C) I' Nvoid OnDeinit(const int reason)
9 \, f, [& W# n{
L' d7 D: \( Z- m2 cEventKillTimer();
! C1 a" t8 h1 u, A- Z4 ^9 k}
& A6 a- m: ]+ D//--- The OnTick function only informs us that we have a new deal
3 b- [8 ]: }& i; _void OnTick()- T9 f9 n8 e) v8 y$ D
{2 g v7 d7 B9 F7 n' E
tem_tick = true;
. E+ H& o( [2 q: y}+ E" }$ v" x/ r7 ^7 r% o
//+------------------------------------------------------------------+
7 O% [; O0 L, x: X* w% {//| Expert Advisor main function |
3 m" S' q( B S$ \1 B) D//+------------------------------------------------------------------+1 _! E/ f* a/ r
void OnTimer()6 r' [" k5 p9 S2 h; W5 d \2 ^6 R
{
* n/ \2 D: z* e6 V' y/ ~* YMqlRates cotacao[];
* _5 B2 a* w. v1 U' B: R5 M+ Jreturn ;- {% }9 G7 Y2 p ]* I9 p
if (negocios_autorizados == false) // are we outside the trading window?( Z, D# z( D/ J( C
return ;) B8 [1 ^# B& G5 u
//--- We are in the trading window, try to open a new position! H" v0 V0 N: i3 F) P4 q& l
int sorteio = MathRand();
" Q5 k7 v4 ?/ q% ]5 S//--- Entry rule 1.1
" S9 m' `- X# U# y( Y1 q* @if(sorteio == 0 || sorteio == 32767)! C0 P6 U; J: d& j3 N$ F4 ^' m. I
return ;
- K0 N* z0 M! z4 tif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy2 _ \+ F& h. j
{
1 H* W2 ^$ Z' d3 x* n" |4 W8 ~, hnegocios.Buy(info.LotsMin(), _Symbol);
* D) d R" p; ~' V}( [7 _% f m! m+ H/ |' s( E
else // Draw rule 1.3 -- odd number - Sell2 O: g$ D4 c. B, B4 T
{
# ^4 Z. {' G$ i2 r" tnegocios.Sell(info.LotsMin(), _Symbol);2 ^6 U! C, E" j1 I
}
8 z" m. n& b" y! y( r5 F! u}
- c3 j! Q4 I& H) x2 ]- A//--- Check if we have a new candlestick...
! F% N2 I6 S4 G5 Obool tem_vela_nova(const MqlRates &rate)
4 a3 b5 I0 J* | R# w{
; f0 i5 v) V: [1 Z% s9 i6 @: i{' b$ e: P% i4 Q- s) ]6 V
ret = true;- G1 T% X4 W) ]; s
close_positions = false;
) Y3 M/ B: E; y, V1 u6 D}0 Z3 Z8 y' @2 \4 `; }5 f
else- e8 T$ k1 l+ Q+ j3 v4 r
{/ P" f2 m6 q0 _( i, T; h
if(mdt.hour == 16)( m$ z* u9 {) H' e$ ~0 Q; X4 o
close_positions = (mdt.min >= 30);
6 `, v4 t D; m+ ]8 a6 y' ~% h}; f6 H( I. c8 \9 Z& W
} R1 J4 N0 L. v$ s% t
return ret;
9 {8 k: Y& Q3 T" A$ w5 v}- Z& Y9 }) \; H" }" t2 ]" D. S; ~
//---
, t0 h) B `% J# l5 J$ lbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]). R N$ R# y8 g# A' c, `9 B1 |
{
5 }$ l _% ~, S) Y/ C, O" k/ E& T* C" ?" ~if(PositionsTotal()) // Is there a position? a$ t' o6 J$ c9 x/ J/ \& ^
{
. P8 Y" o4 x& r' z) [& {" W8 tdouble offset[1] = { 0 };
0 S9 x! c; f; w9 r9 Q. Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?0 S( k3 s6 h' H7 N
&& PositionSelect(_Symbol)) // Select the existing position!; j2 P( S" D+ t& L/ ^& ~
{
1 P# n7 `6 K; F6 YENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE); f4 V* D S* _3 t2 R
double SL = PositionGetDouble(POSITION_SL);3 I9 Q# \& w7 Q
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
?) [; W2 ]* `; kif(tipo == POSITION_TYPE_BUY)3 d6 T# E0 ^' J Z2 z, Y
{
8 {! n: Y2 C4 Bif (cotacoes[1].high > cotacoes[0].high)" M- ? l) I% d) ]# g
{ r: }7 b, {7 P: P% m1 T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: [* }: V1 y0 u; {6 F1 z- ?. O
info.NormalizePrice(sl);3 f+ N5 m* ?/ S" V2 F% L5 K5 L
if (sl > SL)
" H% F/ n: o b, l* \{* m% N9 |, K6 V, _9 W; {
negocios.PositionModify(_Symbol, sl, TP);/ x& H& m+ u# H1 U, Z" c4 Q
}7 }8 D2 z9 o- B! Y5 E P
}
( V, f4 ^% f7 j% |7 b}
4 x' H' V9 |& X8 ?else // tipo == POSITION_TYPE_SELL% \2 ]/ x* S' N; K& c
{5 D+ I: @% f/ y" |5 d; {) G
if (cotacoes[1].low < cotacoes[0].low)$ M( ?+ t$ a! V: d$ `+ Z- l
{8 k9 y7 p2 m) j: C
return true;1 |% }, W5 h' n+ _/ I
}
2 V' x! J' A1 c4 k. T W// there was no position
5 d1 @! U9 f* i0 j1 g4 @- M greturn false;7 h( {- u" C$ A- |8 i
}1 _7 u, @8 V+ a; m* \4 G; H
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
* m4 Z/ L. W l* N到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |