启动交易模型,并构建 EA5 y5 h! m; t, w7 y+ C. [$ G
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* V6 ]% C7 b! V+ N% p$ R
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 d$ Z& z& x, P4 }( S6 t4 [6 \
以下是制定这些规则的代码。
( W$ M" z& U: P" a# t//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 `/ P; ^) `/ y3 Z) F M* K4 `int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ o! \9 e% s$ n
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 a* z3 t5 Z H5 T5 @% J0 J2 q V5 ]//--- Define a variable that indicates that we have a deal.../ t# B- [+ r1 k$ D+ L0 u
bool tem_tick = false;
/ a" ^! ?6 W _# g8 _. i# v; z5 X//--- An auxiliary variable for opening a position; v! A! `0 u' C1 Y( | c" c
#include<Trade/Trade.mqh>( D7 H8 }; r) V9 h- _- A3 ~7 m
#include<Trade/SymbolInfo.mqh>2 \" a; |: f9 F7 X$ j) e# D
CTrade negocios;
! b; v* i2 |$ i, e1 KCSymbolInfo info;
. O2 N; ]) d q//--- Define in OnInit() the use of the timer every second
) f) f/ C9 S Y* \7 a5 K7 x3 K//--- and start CTrade
1 D% r) H4 b1 B9 ^0 w% m/ mint OnInit()2 Q# [ `, n- _3 D/ D8 Y+ u
{& P E8 ~5 M1 A
//--- Set the fill type to keep a pending order
( D) |3 ]* A4 h( L! L, S# H/ P//--- until it is fully filled
5 K G3 x" U0 F2 \negocios.SetTypeFilling(ORDER_FILLING_RETURN);
/ Z5 y# J6 o/ i8 t//--- Leave the fixed deviation at it is not used on B3 exchange
9 r$ @& F8 s. a3 r q8 [3 Anegocios.SetDeviationInPoints(5);4 w2 z/ s7 k8 [: s: R
//--- Define the symbol in CSymbolInfo...( S& S4 ?6 c9 V# I/ Q
info.Name(_Symbol);
) _# s+ D( \4 T4 P5 P//--- Set the timer...6 t% n, s& q& X' q9 _8 \8 a
EventSetTimer(1);- @3 o3 f$ C4 p
//--- Set the base of the random number to have equal tests...
& K2 c' z% K, Q6 lMathSrand(0xDEAD);
" n+ Y7 `/ t" t1 X- m' ~4 G( l6 Lreturn(INIT_SUCCEEDED); e, x. l& V+ u- V! q6 a( U; z# F6 l( N
}2 O) V0 g4 k6 f( ~+ C! s5 @# n
//--- Since we set a timer, we need to destroy it in OnDeInit().' l( ~* H A! m- A7 f
void OnDeinit(const int reason)2 I6 l3 y" o+ x" j- L
{- G- |2 x! l. O4 v' |, l
EventKillTimer();
9 U6 v! |5 W1 k+ x$ z}
; g+ k: ?; X5 }' O, K//--- The OnTick function only informs us that we have a new deal
/ P" K8 c$ _- d; a0 Y wvoid OnTick()
; d8 r6 y" {% x% x \ \* c{
2 C5 K; R5 d3 Y& `2 dtem_tick = true;+ n/ T5 ] f4 }5 e- M; A! D
}
" K o# \. q5 A) I//+------------------------------------------------------------------+* x! E3 @1 J4 h# g7 w" k
//| Expert Advisor main function |
- }/ g; R) O* O; W( t* N/ ~- Q* b//+------------------------------------------------------------------+* ^6 n0 q! v/ C6 T" o# F) d
void OnTimer()
( w. d. ~+ p- V9 T p{
8 \' B4 v, r$ Z0 sMqlRates cotacao[];
6 K1 m' `, k5 k v! b& e, ^return ;5 K7 @! U. | t! j- F$ R
if (negocios_autorizados == false) // are we outside the trading window?- m3 W; r5 {7 ~# C4 y3 Z7 j
return ;) Z) T/ T9 j7 f& R
//--- We are in the trading window, try to open a new position!0 G3 {; \$ S8 A: S- w' G
int sorteio = MathRand();
1 V: j+ P- b, b& v! f) Z//--- Entry rule 1.1; V& z& {6 P7 Z
if(sorteio == 0 || sorteio == 32767)
: c) f2 Q- D R9 T# [8 Sreturn ;
4 r5 R) d, C1 u. O% a. Tif(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy3 ]/ }3 T) e2 H, @: o d9 d
{$ ]" l+ [4 Z$ L
negocios.Buy(info.LotsMin(), _Symbol);
9 i. \: _9 {% s2 b8 E3 x& G}/ o' o7 c' [' y6 h9 _ ~
else // Draw rule 1.3 -- odd number - Sell
$ o& o- X( c- T, L1 u8 y{# ~1 Q5 y. k* P2 J G5 @9 ]2 e. {
negocios.Sell(info.LotsMin(), _Symbol);( H0 s) i( n: X0 M+ h/ {
}. y1 z. y" m5 E C% F* w
}3 H9 P {! z# A* k
//--- Check if we have a new candlestick.... w' G; K" R! b; f1 {7 _6 j
bool tem_vela_nova(const MqlRates &rate)
# y# S# Y# P; }% {' T& D/ y& u{' x" e0 o" [1 G- q
{
8 u3 D w! K' v5 T+ fret = true;
% y3 @; P+ C5 o2 J/ b5 sclose_positions = false; C1 @/ u1 f! X! h1 o4 g- }# l
}
9 f7 {5 {4 p$ ?5 Y1 w1 melse9 N; b% }) R$ E) b* {8 ^# ]/ |9 i' O
{
* }" E- ?1 m; Z7 T3 h1 ?: o+ rif(mdt.hour == 16)6 ] u8 ?' O. `' T0 x
close_positions = (mdt.min >= 30);2 Y" a. m- |/ f7 F+ {7 y5 D
}
( l. K" e+ J9 B) [ `. p$ T, A}
' k9 W0 B2 D1 |7 ^: Xreturn ret;
6 _& [) P2 A& @- k}
0 X4 I$ F! I6 o: d; `% R//---$ F, O; y" x6 w6 i5 i8 t- z
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 ` I9 K6 J5 D# B4 A* f
{0 y. [* P: k1 P4 H: K
if(PositionsTotal()) // Is there a position?
( V5 r# `9 P% I{/ {! {2 v" g; q; K+ ^ G
double offset[1] = { 0 };
8 x$ L0 N0 X6 `/ L' ^5 o/ q* r4 Mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& L: w- I8 o% |5 J5 o% ^3 B9 s&& PositionSelect(_Symbol)) // Select the existing position!' c. E" e* w [5 V" ^& U* k, x
{
$ m. X/ A; G$ A% l8 M; H# l4 QENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
x Y X: _3 Fdouble SL = PositionGetDouble(POSITION_SL);! _% P$ {) E; }- H* J
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! N( Z( R s! d0 U4 P3 i
if(tipo == POSITION_TYPE_BUY)
+ Q; @( W9 ^) m! L{: Q. }" V2 ^1 y y3 O; u
if (cotacoes[1].high > cotacoes[0].high)
% G( o7 D" V" K5 V{ l# i0 [+ ?. u# ~- @- J
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 w# \5 W/ b1 ]& W1 \. U; pinfo.NormalizePrice(sl);( G6 \, a. V! _2 Y6 }
if (sl > SL). X6 {* Z5 U! X
{0 K7 b5 ]# K* Z# f4 J* n, n( k. p. j
negocios.PositionModify(_Symbol, sl, TP);; ^( w% I: O# P& F: m# n$ K
}7 V _. ^4 n" f7 _: g) R
}" y& j* v9 s# a, }4 z" i5 h
}
$ k1 w# Z& r3 M9 K, A! i8 r1 aelse // tipo == POSITION_TYPE_SELL2 \9 R) ]# \$ S' b5 i5 t- ]- Q3 T1 z2 L4 q
{8 q0 r. @( l, N* A
if (cotacoes[1].low < cotacoes[0].low)
1 N( K0 c/ \/ ?1 m9 ~+ T# w+ `4 g{
( L: ?) Z6 x7 I3 i+ Ereturn true;2 u: n5 E+ G9 h6 D2 L0 |
}) @$ w) ^ w. S y
// there was no position
- H# R" \3 f8 v0 nreturn false;5 G5 _& z9 R2 M4 e' s, M
}' L5 E' {" _# n5 v, F4 }; f& N8 l3 ?1 h" e
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
( q+ K2 z1 F5 Q到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |