启动交易模型,并构建 EA" b/ e: n1 S3 m( e5 @
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
: h+ E- N$ x' N+ j+ o为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, F5 y" J+ \. R7 t0 F# `以下是制定这些规则的代码。
& p l1 O' i) B//--- Indicator ATR(1) with EMA(8) used for the stop level...( U Q, D) X) z2 P
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. k+ O s D4 P, @int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);& V* V/ g" p1 A& ?, b$ [6 g l
//--- Define a variable that indicates that we have a deal...' a% N ?+ |. p8 b; T) d2 u( c
bool tem_tick = false;6 f1 ~, a6 N1 M9 s
//--- An auxiliary variable for opening a position
& P8 h: G+ I$ [7 Y8 s5 \#include<Trade/Trade.mqh>
. W- d) F) x3 ~#include<Trade/SymbolInfo.mqh>
( ^0 w8 T+ P) y5 u7 CCTrade negocios;& H. ]" W7 u2 R8 Q# J1 v
CSymbolInfo info;9 ~! u$ C3 }2 ?+ f
//--- Define in OnInit() the use of the timer every second
) G& t3 O1 g) i5 O+ E# _4 ^//--- and start CTrade
9 l# s) Q; q# c' N+ c- m- zint OnInit()( [) H0 a/ p4 \. D) H9 ~+ t! C
{- ]! u% I- m3 \) H, E" z+ z( v4 ?
//--- Set the fill type to keep a pending order
* a$ o& f1 G# Z' V# k: R+ @//--- until it is fully filled" c* p x* g/ S! }, a4 i/ D8 N
negocios.SetTypeFilling(ORDER_FILLING_RETURN);) o/ I+ e- v2 z5 |) I6 r
//--- Leave the fixed deviation at it is not used on B3 exchange+ @; L5 D9 v( f, R
negocios.SetDeviationInPoints(5);
( R. J7 J+ y, |: V2 J O/ m7 |5 Z//--- Define the symbol in CSymbolInfo..." L3 C- Y5 T- k$ k7 P9 D. \8 m
info.Name(_Symbol);, k! m/ Q/ o0 O8 B8 v2 I
//--- Set the timer...
. O1 H% e% d$ U- y( d- vEventSetTimer(1);- }; K \3 q2 ~
//--- Set the base of the random number to have equal tests...
* m; C7 w7 d+ ]) E8 yMathSrand(0xDEAD);
- X! `. m& F1 f! kreturn(INIT_SUCCEEDED);0 p! q2 }6 _* O0 Y* N
}
9 Y( Q' ? F8 U+ w9 Q//--- Since we set a timer, we need to destroy it in OnDeInit().
! S0 }# E; f1 r; w4 T; `* O, P4 I( ivoid OnDeinit(const int reason)
K; _& o2 k+ k7 W- E{
' s+ g- i' z. W7 f% ~3 @EventKillTimer();5 C8 u- y; u- ]8 P( s
}. p- C+ K1 n5 c
//--- The OnTick function only informs us that we have a new deal0 m! f( [' q% V8 z8 D+ y- {% U; D
void OnTick()
/ ^9 i2 g! P# Y" e# s9 `' w, U{, O3 o- k) @( [. q- |$ Q
tem_tick = true;
7 r, I) z2 V, b. v( ]}) d$ c, y v3 I o" y+ {. L
//+------------------------------------------------------------------+7 K! j' _8 X% W& S' K2 K7 Z R3 u
//| Expert Advisor main function |; u6 u8 M( ?- v: c) O" j" j
//+------------------------------------------------------------------+/ \9 l9 P7 `7 x8 [8 {% _
void OnTimer()
, w E3 t6 v' z{- s Y) ^& F6 v. |, t3 m
MqlRates cotacao[];3 U' J& \; w) l4 O. R, [7 O: M
return ; u0 t; n2 A1 h+ _5 e) r( j
if (negocios_autorizados == false) // are we outside the trading window?
7 A9 b. N" x! Dreturn ;( G6 p8 m9 A6 A U/ B- E
//--- We are in the trading window, try to open a new position!
+ n4 o, x9 y2 f) v2 Sint sorteio = MathRand();0 _5 h& u7 L& z( V. m9 S. `; w$ r
//--- Entry rule 1.1
/ o. M$ I. w/ V+ |if(sorteio == 0 || sorteio == 32767)9 [& Q! ^' y' W8 p2 T' A# U
return ;
9 c& n0 k- U2 }3 eif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy
; ?5 J! ]& @) E& }# y{
/ L/ S8 D7 D2 g7 C: K5 f9 ynegocios.Buy(info.LotsMin(), _Symbol);* W, B! G( @, k1 N
}' @' }, k( w" i( u3 c; w
else // Draw rule 1.3 -- odd number - Sell1 M# \# k! [7 E
{' _+ J3 @+ l+ y
negocios.Sell(info.LotsMin(), _Symbol);
( p2 Y" D$ R$ \+ M- a}
6 H( i9 D" N" Z- ^: s}
0 u" c3 l) Q/ h; t//--- Check if we have a new candlestick...
/ ?& K7 U8 _; h; M9 `' i2 Nbool tem_vela_nova(const MqlRates &rate)
2 u+ f( Y, z* K' ~; F! o5 g. m{
) P" a: ?! k K$ i{
- z# D, |- X& a+ Kret = true;
) u/ a/ w, ]9 c8 `2 a1 Q6 C9 iclose_positions = false;# A- H7 H" u0 j7 `/ c
}% a5 g6 u0 v9 m: q
else
( D$ K! `! V* [% N7 N6 ]. W{
3 }% o1 a" @* v% H2 hif(mdt.hour == 16)
7 Z: D1 @( m0 ?close_positions = (mdt.min >= 30);
0 \, V- ~9 A! z# D}
( {# G+ f6 d" _2 L$ S Z+ x}
8 }; ]. o2 `( M1 r5 @return ret;
! E, n g1 e4 Q+ `: Z}. |$ ?# c, A* ~9 _6 B4 a) z+ O0 f
//---) K/ h3 O/ c0 q. k7 P) n$ |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 c) \$ N8 @9 J7 I
{
+ m& a4 z- \. K% gif(PositionsTotal()) // Is there a position?
1 \5 w) N' B: @" a6 B% \9 p{7 r6 @1 l* E2 [7 L
double offset[1] = { 0 };
1 }3 ?1 ^3 f1 }# O' Aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% _* o" k6 e3 q, S) n&& PositionSelect(_Symbol)) // Select the existing position!
3 J' @4 l0 k, R# q5 [7 a7 o, i{( |9 _0 U: r+ T a
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);( Y$ m% K' w) J$ d. a
double SL = PositionGetDouble(POSITION_SL);4 D2 Q" i8 y& x
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 z. ^4 w. t5 y. @( Cif(tipo == POSITION_TYPE_BUY)' b0 K( G; Y$ {5 V$ I+ j, x
{
$ Z( z/ L3 m& m5 }* c: gif (cotacoes[1].high > cotacoes[0].high)! x# \# `/ h- _
{
8 B. {, I! R: Ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& C' k7 E2 h# M/ J+ v) ainfo.NormalizePrice(sl);, p# n9 X8 N* N t, |
if (sl > SL)
$ t/ R0 |. H7 H d8 Z{
" ]. Y- o% _+ g2 [% h" Q5 L$ inegocios.PositionModify(_Symbol, sl, TP);
9 i% W* s2 f- w2 {4 U}
2 z% {5 q, G+ v0 C% f1 F7 @}& n4 Q% ] ^, M9 N
}3 c5 K7 S* Q# O, u5 l
else // tipo == POSITION_TYPE_SELL" ^8 D: K3 R: {
{7 N; Z! G' G N
if (cotacoes[1].low < cotacoes[0].low)0 O3 }3 J7 s/ I+ f0 A- y
{2 o& B5 B8 |! ^5 f/ D e7 p
return true;3 Y" a* a0 l9 l5 i
}
/ B4 L/ \' l: C+ t// there was no position0 M z5 O2 n e
return false;
4 V$ U3 Y N g }+ a0 q}
: Q3 _1 B; }6 g" x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% c) f, o: |$ d0 S- i到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |