启动交易模型,并构建 EA o$ E, l5 X! z8 R1 y: ~+ e1 C
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" A0 y, t4 s# Y% y( f为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 t7 p6 n- t' t( u
以下是制定这些规则的代码。' A$ G$ x4 V- E
//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 q, ~/ b( n6 eint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);7 Q9 P) t8 |8 O) i- m, c+ Y
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 t' i" K+ P2 t& |" K1 g x+ m/ n//--- Define a variable that indicates that we have a deal...
! s/ _, h( M l& Nbool tem_tick = false;
& S) ^+ T( K: Q2 B6 k//--- An auxiliary variable for opening a position
5 j/ s+ Z, n/ S0 U! n#include<Trade/Trade.mqh>: h: q" x* o- ~! m6 B. \* p* U( L
#include<Trade/SymbolInfo.mqh>% @# w7 d# L! s: r2 _5 i7 O
CTrade negocios;
' h8 K4 R0 |( l, ]! }/ `CSymbolInfo info;4 R3 ~0 H) Y9 k/ ?
//--- Define in OnInit() the use of the timer every second
0 I7 U. Z' O' h4 k: }6 h//--- and start CTrade+ t5 U+ H) C0 I+ p9 ]6 {
int OnInit() }. l8 ? D% [. T x4 L& n E8 C
{( N: P1 _# M, Y9 i0 D& b
//--- Set the fill type to keep a pending order: z2 o( O5 L% z5 T+ {8 y/ a% u
//--- until it is fully filled3 F; w* X" Y) H8 o) Z$ J2 H
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
( Y. K: h& C j$ q6 K$ U8 M//--- Leave the fixed deviation at it is not used on B3 exchange+ S6 P V4 K1 P7 z5 U0 _7 M+ ]
negocios.SetDeviationInPoints(5);
7 V. q2 K$ K* b4 }//--- Define the symbol in CSymbolInfo...
# o) U' f( ^% L& h) L: u/ oinfo.Name(_Symbol);
$ ]* M$ J f2 z5 w2 F+ v2 G* u//--- Set the timer...4 s( L% Q2 ^+ M& E/ b2 P- f2 l6 u( ]
EventSetTimer(1);
# C2 T, x# r( t. n* j//--- Set the base of the random number to have equal tests...
6 q2 Z& E. `8 Z8 c& }6 g" e2 yMathSrand(0xDEAD);4 N5 }7 \+ C. O& ^' b, I
return(INIT_SUCCEEDED);. O) T9 E9 x# D/ j' }. h3 g' j3 c
}
0 H$ O# h4 t" S0 {7 Z/ x//--- Since we set a timer, we need to destroy it in OnDeInit().
1 J; l. w7 w9 d3 D/ z$ J" K0 Vvoid OnDeinit(const int reason)% W: \6 R2 {1 C' J$ d
{: |) _, }' t7 N+ Y4 |" \1 L
EventKillTimer();, h8 N5 p1 W# } [; w v! \( o* d
}+ R& S% C* a. X" a- K+ p5 O
//--- The OnTick function only informs us that we have a new deal
. o7 S, `: u* }- ~& R. hvoid OnTick()4 F+ S* q8 K- p
{0 M4 ^% \' g' h9 B
tem_tick = true;
5 [. f9 {5 @. g) Z! ^}$ }5 D* U: g3 U" a& Y! D
//+------------------------------------------------------------------+1 l% Y* t& V, x6 F2 ?+ L* S4 x, V4 k
//| Expert Advisor main function |4 G. @2 M5 g5 \+ i5 _
//+------------------------------------------------------------------+0 @, F' w' C) ] i. I
void OnTimer()
3 p0 E; v" I' M; o{$ Z% t+ a4 m0 m
MqlRates cotacao[];
9 C" J7 V! I" c8 e& P) I& o4 Wreturn ;
$ [% _' Y* Y* w; o3 x( fif (negocios_autorizados == false) // are we outside the trading window?
- l) N! p$ x& B# ]return ;1 n0 m- Y- s' @( S2 M
//--- We are in the trading window, try to open a new position!$ |. x7 {8 c( j( ^3 b' i
int sorteio = MathRand();' s, Q- O$ o# H8 y
//--- Entry rule 1.1
' l2 ?1 l/ ~5 n" Kif(sorteio == 0 || sorteio == 32767)
+ j; w. V* Q1 oreturn ;, U* [& I6 C! }
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy( C0 m' x- x/ B2 @
{
3 x6 t2 U9 c, v e* w- dnegocios.Buy(info.LotsMin(), _Symbol);1 V3 f) @! |& s$ n; E! @3 l0 u2 |4 F
}) z" P9 _9 a" {# h) _! \7 {8 W
else // Draw rule 1.3 -- odd number - Sell
/ ^+ ]0 b8 g8 ?' t{8 ?/ T' k/ t& R5 }- A1 Z
negocios.Sell(info.LotsMin(), _Symbol);
: D5 ~, E' }. V* w$ Z9 `}- @) _: [7 F3 W
}3 \) _6 W: }3 f# @7 j, S
//--- Check if we have a new candlestick...
' p, `1 e3 Z/ b/ k4 ^$ gbool tem_vela_nova(const MqlRates &rate)
3 ?; _- E3 F' ]{
- B' z. w& O, {{$ e. V8 r: A! W
ret = true;
* `% H" S `$ V; D9 @ Jclose_positions = false;
( r+ N$ l( Y, b' V}
# F5 E! x- f" Y- P3 kelse( t% J( h0 i' {) g6 {% b4 d1 g/ @
{# Q& _$ o3 Z! C( d6 m7 K
if(mdt.hour == 16)% e5 P* ?! S* j7 K; Q& W# h
close_positions = (mdt.min >= 30);
: L$ f2 d8 i$ A6 I; T}) B* f& n+ m. u' ]
}
1 A/ Y, t7 P: z/ b$ H. V" T8 ~0 }return ret;
# h8 J3 F2 d2 o0 R}. |" L t. Y! f; Q1 O7 O3 \
//---
- G4 j3 S2 q T% V7 }; m H" Mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
0 {7 }& T s, X1 D, P& h- C( c& [8 @1 S{
" |& P3 S* X# K0 i8 e: f& Jif(PositionsTotal()) // Is there a position?0 ] K9 D) L5 h' `) d8 Z: d0 L
{( K) Q. A e4 M$ n, X$ o7 t
double offset[1] = { 0 };
$ Z' B. _! |! D& b1 ]1 aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?( p) O8 E- R( y9 \6 @
&& PositionSelect(_Symbol)) // Select the existing position!. O6 K0 ^6 F6 X7 m
{8 J; ?$ [+ A @$ j( H
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE); G: Y+ M( M# z1 m3 F: u9 L
double SL = PositionGetDouble(POSITION_SL);; l. s5 u4 E( X7 u. ~8 z
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
v, `7 g0 h$ n$ X% Kif(tipo == POSITION_TYPE_BUY)
% c- d7 z' M+ x5 r# b( j{8 }6 z! S6 K1 E7 s; r+ a; L
if (cotacoes[1].high > cotacoes[0].high)
" b/ C$ R! H/ O{5 n8 y8 {3 }& }: I2 ~
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: d# Z6 a7 d \4 v! _; c( q
info.NormalizePrice(sl);
( {7 E+ p8 B- ^$ Oif (sl > SL)
3 v) a; w5 e, y5 ]% {{
2 }% w% {; i- W2 cnegocios.PositionModify(_Symbol, sl, TP);
) o4 C% j* {( ]: J}& f1 H, o7 W7 L# j. l% @* N& C0 n
}: ?1 O! c! D, A, \$ w4 `9 R
}
# e+ t# ~5 G& X( v! y! c8 ^else // tipo == POSITION_TYPE_SELL, y4 v* t! O0 h$ I3 V
{
9 D% @$ L0 _! x; K7 I m. Nif (cotacoes[1].low < cotacoes[0].low)
0 [9 ]/ E- x2 {! e{6 W1 [ G v G8 \
return true;
- [* Q* |) w$ J0 n9 g6 a4 Q}" I# F; r! a: L0 o6 J( Q4 H
// there was no position
$ i* Z! ]* i* ~. T/ }return false;; A! F# z# c% o+ p
}3 r0 A4 w' [1 X- o
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 Q/ _& A3 V* F) ^, h! Y8 b: o d+ v$ t4 J; c到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |