启动交易模型,并构建 EA
2 K% C, X. ~( t) \7 h" E在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' j b9 Z! {2 u$ x
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。1 z6 w1 |8 \" A4 g4 l+ O
以下是制定这些规则的代码。
0 }( ^* | N t- R" l$ ]2 x: m# F" t//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 b9 M3 K# g6 Z4 O' [int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& E8 [; \" G8 d2 G
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);* ` ], k6 t( H5 q0 s( c
//--- Define a variable that indicates that we have a deal...
9 o" M2 x, E W+ h" K7 m5 V4 Bbool tem_tick = false;
- z, x1 O6 _! i//--- An auxiliary variable for opening a position2 A9 b5 a0 x d& ]7 M2 s i* n# I
#include<Trade/Trade.mqh>! m5 z. |+ ]( a p8 l8 O( d- e
#include<Trade/SymbolInfo.mqh>
) d% _4 Z! ^5 n& T$ o) L' iCTrade negocios;9 H+ T; I; g' N- q: ~7 h4 N7 K
CSymbolInfo info;; g; a& d5 P7 c- s: d" D9 Q
//--- Define in OnInit() the use of the timer every second
6 t' D2 j6 V. X; x: g2 e7 s# L) U//--- and start CTrade
* k/ U. T8 ?9 V2 R, i* ~1 Yint OnInit()) n. z" X' P; f% ^& |, D! ` l" q
{( u9 Q9 |% `. `( R4 @3 w- Q5 L# D8 G
//--- Set the fill type to keep a pending order
) ? j% \9 d$ `' j/ k//--- until it is fully filled4 F( C- k- k7 z
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
" H1 L: M% W! _; q; h) r& _//--- Leave the fixed deviation at it is not used on B3 exchange8 l& \ E' j8 t
negocios.SetDeviationInPoints(5);
+ e) ]6 a* i8 f- ^//--- Define the symbol in CSymbolInfo...
9 R; {5 W+ x p! I% S- Linfo.Name(_Symbol);* J: C) J' ^* ]% g; B; v2 i' N4 p
//--- Set the timer...
3 s j. N3 ]! `" x: S" |EventSetTimer(1);' a/ F8 B9 D" C9 ~; ?9 y3 w
//--- Set the base of the random number to have equal tests...
$ _: s+ z- W5 v/ B( s t8 tMathSrand(0xDEAD);3 A% g6 x: @3 O9 ^& J% T& M
return(INIT_SUCCEEDED);
; s, A/ q7 p: h; \. f5 l. t- Y}
( L3 s8 z( a3 d, h4 A3 r! Q//--- Since we set a timer, we need to destroy it in OnDeInit().
4 I5 v4 D( {* o5 P) f2 tvoid OnDeinit(const int reason)* Q1 l1 V) ] P8 n) x
{- p3 n$ S8 n" e! W. k7 ?3 |7 X$ r \
EventKillTimer();
" H* x8 ^( n- z; Z}
; ^+ r& g5 L5 g( P//--- The OnTick function only informs us that we have a new deal
( d1 H6 U* g8 W& F1 J# y" Tvoid OnTick()* t# D' i2 {+ m0 F
{
7 h9 Y$ J4 A) v2 x+ c1 atem_tick = true;7 x1 p8 X/ {* E
}: i' M: k+ O- u9 Z
//+------------------------------------------------------------------+
0 n6 f/ q. E0 l: `" `9 w; W$ J, K//| Expert Advisor main function |) r# g$ {, A' K' B$ y; i
//+------------------------------------------------------------------+. \ l! t7 f" y) D4 q, P
void OnTimer()( L$ E/ a3 z) A P! S
{, m6 ]6 r: R! D
MqlRates cotacao[];
% L4 H8 a5 Z) Y# u0 b9 |return ;
" J( a/ M, G5 {' C" f! Sif (negocios_autorizados == false) // are we outside the trading window?
1 C A* p, |) Y; C! Q9 G; o$ Vreturn ;
3 z% H7 a$ J" B: n5 O//--- We are in the trading window, try to open a new position!
" z- [8 @8 G3 B3 t1 s/ Yint sorteio = MathRand();( ^: R( C& r! h- |9 T* h, E/ Y5 n6 P
//--- Entry rule 1.10 ^6 y+ F' w2 @ D( A; b
if(sorteio == 0 || sorteio == 32767)& l+ Q6 T* m& q' i8 F, T) ^
return ;6 A' ]. F+ e q/ X
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy5 F4 m( Y% e* k9 p8 a# {
{
8 R: z4 R( j0 M# o8 G8 nnegocios.Buy(info.LotsMin(), _Symbol);5 D: k6 e5 v! `+ l" |9 l$ v
}1 c. o0 m, E1 Y% U, B: X
else // Draw rule 1.3 -- odd number - Sell9 y2 B8 u/ ~! p$ l7 L
{5 C: l3 Q! A/ u' L) V* o
negocios.Sell(info.LotsMin(), _Symbol);" k* Y$ L5 }+ z1 M
}( _* {; h s; Q/ b6 r. i3 g! W
}0 C: A, ~+ [) J. f% N% \
//--- Check if we have a new candlestick...- e0 n* X0 W2 I/ @8 l# b9 j
bool tem_vela_nova(const MqlRates &rate)
8 t$ V* @1 r0 ]) d, P{5 N3 z7 h& z& p5 r
{) q) H' k7 y: u# A" g
ret = true;/ O% B1 H- T4 ~9 V8 G. `! ^* R
close_positions = false;9 _& J0 v' |8 ?- C; o& B/ n
}8 U: W; T* n' n: `% ]/ U
else3 }5 v* C5 j6 P
{9 y9 F& y" `1 ?
if(mdt.hour == 16)( G* E9 x8 |7 l1 Z, }, F5 J
close_positions = (mdt.min >= 30);2 z: n/ e# J t7 V
}
6 E9 g. Q! t# Z, C5 P: h}* h5 N6 M- h9 U! Q2 W( G! g0 t
return ret;6 M, a! p) |- N& E! C
}0 Y, F! R9 B. `4 x
//---
9 E. a: Y, }* _* o! U" jbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])1 f- t+ f \2 j/ `
{6 V5 |) ^; E! X' A- \" W+ t! X
if(PositionsTotal()) // Is there a position?
, g$ l4 p, ^- _/ I V{ j8 {/ k! Z; f) V' K
double offset[1] = { 0 };$ r0 a+ S1 c/ I
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
6 W* X% m. ` o4 S: y&& PositionSelect(_Symbol)) // Select the existing position!/ \- @& ]! g9 }+ I
{
1 t2 P1 \( ]% C- @4 t) JENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 f" I$ s, w) c- d' f: E1 J8 e' c! O
double SL = PositionGetDouble(POSITION_SL);+ k' L: q% N2 q9 ~+ E$ m
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 t% @2 c2 R5 D0 }8 j; fif(tipo == POSITION_TYPE_BUY)
# O: x$ o6 F5 _5 K{% f2 u, z1 u. m6 w/ Y5 u
if (cotacoes[1].high > cotacoes[0].high), v# i( h F" W7 N
{2 z* }* w" _3 ]8 X6 F0 B
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];9 D5 }4 G( X' I o$ [) j8 _7 P; \
info.NormalizePrice(sl);* a5 c4 p, {" w3 |5 t; i. D; _1 r7 n
if (sl > SL)2 A8 z7 X" r! _( p! ~* f
{
$ I" B8 n' m* G) h# ~: G% k, x) m. mnegocios.PositionModify(_Symbol, sl, TP);) w3 y+ B, ?8 ~) r3 O: W0 [
}
0 U5 K8 Q# p9 D+ G5 |/ u' I}
* M. f# V& x" n}
0 ^ h) c. k7 A) R5 N/ eelse // tipo == POSITION_TYPE_SELL3 ?6 N# s* S2 d
{
0 I( t% K- ~3 fif (cotacoes[1].low < cotacoes[0].low): J; i. `8 X; Q/ j' w. Q0 }. M
{ q- }# M/ K- F: _4 R
return true;
@2 o+ M, C* V- S# t}
2 |& @1 O7 I f1 y' K& [* @) [// there was no position
. [+ D2 N8 F7 ?return false;8 {1 ~) O9 k2 W1 A% d) w" @
}
# r* M, w& ]8 l4 E/ e我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ \2 c! B R+ X; r' d F6 X/ w到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |