启动交易模型,并构建 EA
[) x$ c, ]) j S! b) k" ?' w在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。7 J9 w* P' D. B |* X& N
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 m5 @% t5 C# {' P* b: U, @
以下是制定这些规则的代码。
& }1 L! D3 C& }- |, G6 `//--- Indicator ATR(1) with EMA(8) used for the stop level...5 ?: _" v: y: ?7 W7 ~# f: f
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1); U0 o6 Z3 t1 ?
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
5 @, X$ V S2 T7 [# N0 X//--- Define a variable that indicates that we have a deal...
7 k7 m1 I4 d( M: \+ {bool tem_tick = false;6 u5 Y4 J+ ]! y! K6 d+ ]+ I
//--- An auxiliary variable for opening a position& M# P" f% \: @( x: e( C7 I9 E
#include<Trade/Trade.mqh>
+ N: p- |& K" P8 J( J* P( }. _#include<Trade/SymbolInfo.mqh>
# w$ f0 s: _# _* y$ I( PCTrade negocios;
4 _& G1 r. V% z( h5 P& cCSymbolInfo info;( F) Q! N4 z0 p: [! F4 K
//--- Define in OnInit() the use of the timer every second7 ^+ e0 ^4 e6 _1 \. |
//--- and start CTrade: }7 K X, I. j1 M+ y
int OnInit()
* w9 F, |1 N1 ^' T+ ?5 w{7 e- _+ R2 z, N4 ]
//--- Set the fill type to keep a pending order
4 M, }9 H* h2 ^- A5 a0 g//--- until it is fully filled
2 m" [ ]8 e* }4 U) F' p$ Tnegocios.SetTypeFilling(ORDER_FILLING_RETURN);; r0 Y5 u2 {) Q; H
//--- Leave the fixed deviation at it is not used on B3 exchange
0 g5 w" `. d' ~8 R# `2 lnegocios.SetDeviationInPoints(5);
8 g1 ?- }; f0 ~$ A//--- Define the symbol in CSymbolInfo...
3 W' P* F3 S9 o: S# j/ qinfo.Name(_Symbol);2 S. P5 s; V. a' s
//--- Set the timer...& g* t, u4 o& E, y2 m x7 P1 m6 k
EventSetTimer(1);
9 n' _5 Q/ z, C8 [7 z//--- Set the base of the random number to have equal tests..., M6 Y4 W4 H; A* O" n3 J
MathSrand(0xDEAD);
/ I |& _& s) w; @4 d2 yreturn(INIT_SUCCEEDED);
. ?8 b. b; b# |2 o& O}# I5 N$ g$ m! x1 B0 L, D5 a2 S
//--- Since we set a timer, we need to destroy it in OnDeInit().6 i$ [4 {7 V, N/ o7 c6 X
void OnDeinit(const int reason)" h' I) m* _. @ e( u
{
8 r% g% i k1 d( S1 r; gEventKillTimer();- `4 _ Z6 O0 v# E4 w' v: A
}' J8 X) x" }6 a1 ?$ j
//--- The OnTick function only informs us that we have a new deal
. z. t; o" a9 L' Q, I3 nvoid OnTick()
0 {$ i/ u. n, C{$ W+ ^' M$ [! t K5 e
tem_tick = true;
) w5 C4 p9 Y, R* k}3 P6 D" i, b u
//+------------------------------------------------------------------+
. g, F- H1 q0 S7 B! a- F/ m//| Expert Advisor main function |
# p% x0 B X& W: e% N//+------------------------------------------------------------------+
2 J/ w9 G' c- y3 }; \8 Mvoid OnTimer()8 y6 |. n) A0 Q% _% q9 e& [. x i
{
6 f: d: f. W5 q8 }4 n+ XMqlRates cotacao[];
( T' `+ J7 u" \0 d% n mreturn ;
* f5 C" ]- Z, C6 N6 R; hif (negocios_autorizados == false) // are we outside the trading window?
; [/ _, @& v& C0 \return ;3 Q5 j2 z& b# r* K' c2 o9 I
//--- We are in the trading window, try to open a new position!) w v, ]; P! o* d2 p
int sorteio = MathRand();
& }4 U0 H. h, }/ X- ]% D" u//--- Entry rule 1.1
- c9 Q% r8 O& B9 S2 C5 }if(sorteio == 0 || sorteio == 32767)
+ c* J* C' L d0 K, y6 |2 wreturn ;
9 t7 s6 a" z: z, oif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy: h0 i$ D- v7 W" \0 T( I% ?8 }
{
2 e& O1 M$ b6 J' ~% q( J- P) `negocios.Buy(info.LotsMin(), _Symbol);/ I3 l( ?8 J5 U! K* J
}
2 p4 \. c/ ]" Z' celse // Draw rule 1.3 -- odd number - Sell4 _; k6 x& \% k; Z* W( f( g
{2 @4 e1 j3 h& \* _
negocios.Sell(info.LotsMin(), _Symbol);
( Q% Y4 C1 p5 q}+ y$ `' T- k# ~8 a: o& \% k
}
9 ?. ^" ?2 L8 j3 T0 q; ^//--- Check if we have a new candlestick...
& J) `' l. E& m2 ^# U* S* xbool tem_vela_nova(const MqlRates &rate)
9 y5 W3 F, z' ~4 ^" p o6 N{
; c: b4 Y+ R: \{
5 K' t9 _; B1 R+ g4 Pret = true;
8 V! A ~; h3 C7 {. v5 q, Sclose_positions = false;. D% q3 d2 b3 n; t& W! a
}
: A% R9 Y. I% ]% q$ s$ Uelse
$ e( y" N) l, p5 m, H{
. L' P* L; q# ~1 E; l7 \if(mdt.hour == 16)
7 E. x; b4 d2 w5 [close_positions = (mdt.min >= 30);
! {8 A, j. l; o; n4 G5 D2 Z}1 J$ _5 @" d2 C, D
}
1 \: J5 r _. K# Areturn ret;
/ E7 U+ h1 J: K1 E+ P- C. d+ Q}2 ^ h! U* ^8 ]7 B% s, ~6 [5 ]
//---
1 |8 b# t- {) C6 {bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])" t2 H7 I0 m& W% L& P
{
6 k: \0 w# X. P9 d/ T4 ~0 X( W; [if(PositionsTotal()) // Is there a position?/ D- w) q$ Y; h9 X/ b0 D6 ~$ k& z
{5 t% e5 H& ?8 K, A# Q. U$ o4 u
double offset[1] = { 0 };
+ i7 `+ M, l4 C `9 l+ |if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 x3 e& Z% b8 k( r' k9 Z! }/ S" ^
&& PositionSelect(_Symbol)) // Select the existing position!
8 `5 \' P( A; m% j3 K{
4 ]7 W8 x2 L8 [# t% sENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 X( y+ g' ]. t4 k
double SL = PositionGetDouble(POSITION_SL);
9 ]' N- h8 W- @# L* Q" d" M7 Kdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; M. C$ y, i! oif(tipo == POSITION_TYPE_BUY)
2 a' E% Y$ M7 j{; X; j$ v0 `5 v3 O: P
if (cotacoes[1].high > cotacoes[0].high)
- R, t+ F9 V" E) {9 \9 u{1 i1 ~: k! ?% p8 D3 s- m1 m: e! g
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];/ g2 m: d* x: h0 U
info.NormalizePrice(sl);
- w0 `* \7 H$ `" Y0 g6 ~if (sl > SL): c, I: b- N7 k; p5 n# c+ K4 s7 p; ~* }
{! g, [9 |3 d- f7 R# _6 M8 d
negocios.PositionModify(_Symbol, sl, TP);; {' v5 v3 N: ?# G+ }
}
& x# [) D) @& R, g& q6 `9 P! J}
# {2 v5 ]6 s T E2 m}
/ n+ V5 g* t* }+ r+ Oelse // tipo == POSITION_TYPE_SELL
+ A/ T; k; k& O{
& \1 R9 |3 k: |6 W. s) Oif (cotacoes[1].low < cotacoes[0].low) H9 r5 ^ B0 @2 z
{4 ]2 K3 Q' _- `+ ~0 Z+ H
return true;: y) ]0 g4 V+ e% C2 u! v
} Y4 ~ w4 k+ b; R
// there was no position9 k: O; L! y8 o, s2 q
return false;
* f" l% s: B2 A" ?% S5 j}/ n- f9 _$ S. I# [) b
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 r. A/ D5 F, C' K4 @3 b+ D: b到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |