启动交易模型,并构建 EA
' V& h( I3 Y" d/ m$ t在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# ~; x, r k; m! Y; M8 o
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
# V/ ]) B9 s! O6 k/ v以下是制定这些规则的代码。
# I3 J" U5 z5 P) Y6 y8 R* |//--- Indicator ATR(1) with EMA(8) used for the stop level...
/ e% ^* O6 n `) M3 s/ h" Sint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; Y; [5 z/ i/ z8 x- @int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
/ \ |. m4 A& P) O" R T//--- Define a variable that indicates that we have a deal...
0 s7 r9 ^9 n i& M! Abool tem_tick = false;
, N. _; G( b- B0 k//--- An auxiliary variable for opening a position
/ C# l' w! u' W* N. W9 o#include<Trade/Trade.mqh>5 m3 Y& b& I, V$ d- H- O- N6 l6 s
#include<Trade/SymbolInfo.mqh>% f; O* C( Q; P1 v5 ~2 m$ M
CTrade negocios;* c; b) P0 S/ |6 N
CSymbolInfo info;. W- D5 g1 Y: S
//--- Define in OnInit() the use of the timer every second
, G/ d, F; p, V( U( M//--- and start CTrade$ ^/ U+ `4 E! S, E8 D; m& ?# O, f
int OnInit(): V4 C; X- I4 }8 _
{
7 V1 I# u( L* e7 s- [* F0 U//--- Set the fill type to keep a pending order S5 H: I+ p1 a( q$ @7 _# l$ O
//--- until it is fully filled) ` i% V2 Q3 T& v; S; Y/ J
negocios.SetTypeFilling(ORDER_FILLING_RETURN);4 P! D& }- m/ Q
//--- Leave the fixed deviation at it is not used on B3 exchange. V2 R4 s1 F. V5 a1 z% E! i* j
negocios.SetDeviationInPoints(5);. [5 W! R/ y) r
//--- Define the symbol in CSymbolInfo...
' {; G! v% r3 H- @2 t) qinfo.Name(_Symbol);
- Z" P+ G) d& m6 Y* j6 U5 o# D//--- Set the timer...
- T9 t$ R* Q, j2 i$ b$ _EventSetTimer(1);- _" p/ D3 G0 R1 n0 p. J) S+ @
//--- Set the base of the random number to have equal tests...) n# h7 W4 ]( \6 @6 Y; m8 a
MathSrand(0xDEAD);
' c6 Y" Z' j5 lreturn(INIT_SUCCEEDED);
) M! W8 {4 k# j}
# D {& H4 J) N1 s9 A- L+ I9 T4 Y//--- Since we set a timer, we need to destroy it in OnDeInit().
, A7 M) O5 Z1 r+ Xvoid OnDeinit(const int reason)2 {' Y4 X: D1 `; {8 T
{; X( r# X3 T2 C
EventKillTimer();; }+ a4 s; p# ^& g
}# R4 B; K$ R, ]
//--- The OnTick function only informs us that we have a new deal
' T/ c% @: d; \void OnTick()
; N/ q3 [- n" `0 `( m2 K Y{2 q1 q5 c, ]# T5 G$ k
tem_tick = true;+ i W( c8 e3 d) \+ b
}
4 Z9 y. z1 K8 F" T//+------------------------------------------------------------------+
4 U# G# V7 o8 L* n* f, j' I: q5 e//| Expert Advisor main function | v+ n5 Q3 g0 ?
//+------------------------------------------------------------------+1 M5 F1 J# e# X2 N: D. U& N+ C
void OnTimer()
* b# ?! p3 }9 G; m/ G' N0 [{
( g8 y5 X0 T B, O# u, T8 nMqlRates cotacao[];1 ] T( D- L$ ~& A
return ;9 N: c: M: D/ Y" e% {& u
if (negocios_autorizados == false) // are we outside the trading window? a) P( |* P3 O3 Y0 I& V" l3 G
return ;
, T% u3 A7 I1 l% j# [//--- We are in the trading window, try to open a new position!5 D4 h& ~8 q. W
int sorteio = MathRand();8 ~. ?5 a, O% Q
//--- Entry rule 1.1 n4 {% R1 W# V" H
if(sorteio == 0 || sorteio == 32767)3 r; S; E) j1 u# _" ^% s% o
return ;
0 S) g. J$ ?8 a# cif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy D3 {! j- n. r1 x2 r% Z8 k
{
, o# W4 i% G/ H7 l: w5 p" P! p( Wnegocios.Buy(info.LotsMin(), _Symbol);
, ^0 ^: N0 j9 h: J}, j! u9 \0 w; R$ a3 a/ X1 M; M: C+ B
else // Draw rule 1.3 -- odd number - Sell
. [' u) o% c# t! j' c6 C3 x% j, |4 V{, P# J) w" P J- T+ i3 q0 J
negocios.Sell(info.LotsMin(), _Symbol);$ S2 Z9 o R: f
}
6 E" `) D% _1 W; T' T}" n) O7 x( J) v Y* D. g m, d) O
//--- Check if we have a new candlestick...3 R- p D P# \
bool tem_vela_nova(const MqlRates &rate)$ @/ p" E8 V* }2 G2 c
{* H2 |, w9 y. v, ^& j3 \. W
{
: I3 p4 y9 p( H2 ?/ g2 g. }0 Aret = true;5 q) W) \0 |, {5 P9 {1 _6 o0 K( h7 ?
close_positions = false;
. N1 `3 B" v1 t; U}
) N; B( ?8 K. Q" _$ delse. ?# A9 x: Y1 n! k
{8 |! D" X- C0 ^& J* `
if(mdt.hour == 16)
1 M! Q% y1 L+ F j2 t4 Aclose_positions = (mdt.min >= 30);$ W/ l8 m9 W. C1 V, o# s
}
# K% w: A' {1 w0 }}( _$ D" P- J7 R( [6 _
return ret;& R% [2 t% Z l; ^( B1 v/ d I
}5 r- _7 p- {" D7 y
//---
( e1 e% t# Q- {- B/ Ibool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- \% q+ [7 X, m) v0 g# L{3 E) v+ e0 F! j/ b$ t! I+ b
if(PositionsTotal()) // Is there a position?8 f# z% H5 H+ G3 p$ D, y, _
{
/ C0 w- L' u5 m. S# Y# W0 Z0 s$ Ldouble offset[1] = { 0 };
& t: N4 p; I, O& eif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, L; o& U% v8 K&& PositionSelect(_Symbol)) // Select the existing position!
5 [$ E5 t. c+ e0 V{- f# |! t' R7 U
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& g# f6 |( ]; Y7 A7 F! |double SL = PositionGetDouble(POSITION_SL);/ p7 a4 k+ s; S" ]# T6 {, h
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: k( F `# y' S/ O! Xif(tipo == POSITION_TYPE_BUY)" a! E( ^, p7 H2 J$ U1 y9 O; i
{
- ~. ~" M/ l6 Z) j. D0 [if (cotacoes[1].high > cotacoes[0].high)% _& W. }; \# t# M7 W. L" u6 w! m! g
{8 S2 o2 c' k! g" D0 G5 o2 i |
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
# G" d# O+ }7 c% f9 minfo.NormalizePrice(sl);
" a# n' W6 x p: gif (sl > SL)3 B0 ^2 [. y' w( p" r8 p" f
{
; N e0 V: A: a0 K% {$ [. `negocios.PositionModify(_Symbol, sl, TP);
; n. _9 M3 I% o}: Q7 }) p5 o% n7 Z
}
5 y) h# J0 V# g& }}
% n g3 A! Y7 i! U' _8 a; Relse // tipo == POSITION_TYPE_SELL
: n! x# A' y& K3 B( z{' G3 G* O& m8 r
if (cotacoes[1].low < cotacoes[0].low)
. a* O+ Y% K" Q5 z4 U- p1 N{
/ x3 D9 z) y" ]) z4 Kreturn true;; R2 h" U- T! d; p
}
: \! [3 E) w( H* |! z& n// there was no position
5 E6 b# T( D' U7 Z1 r# Z' vreturn false;
# g+ d" I" o! H5 W}
! o: x3 F6 j N5 E7 d% W我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。7 o$ \* r9 C& g4 N* `. u$ b7 x
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |