启动交易模型,并构建 EA: x4 v! t9 A# a9 ?4 G
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
; {# n( }* l! g为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 v* _# Z- J: l4 f8 `以下是制定这些规则的代码。# b1 L/ m5 S$ \0 z! q2 x+ z
//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ }8 o4 x: z- h2 {# A. k) Rint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
* W( I1 ~8 r% I- Y8 P5 x( uint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
' Q: J" h7 g% q1 v& {//--- Define a variable that indicates that we have a deal...
: k( A' E7 g& P) }2 c9 P0 ?% |bool tem_tick = false;
& q$ w6 P/ C& S% g//--- An auxiliary variable for opening a position
8 H5 j2 Z4 e9 H% c4 w4 a" ?#include<Trade/Trade.mqh>
+ K) Y8 V# ^" }+ k#include<Trade/SymbolInfo.mqh>
. y4 C. |, @# m5 [$ @* {- Y( uCTrade negocios;2 u( @3 w) I$ Z8 `! @+ {2 t3 i
CSymbolInfo info;
9 u) S0 s l' j& y4 C//--- Define in OnInit() the use of the timer every second5 h- Y2 K0 P0 ~( K
//--- and start CTrade
4 i: @$ {+ L2 M/ Eint OnInit()
7 [ X! B5 k1 G. W{
" z/ k' x2 Y+ A$ z+ \0 m/ P" M//--- Set the fill type to keep a pending order
' A, ?. ^- |& v o//--- until it is fully filled
* V1 N/ D5 s, _% X, v, ~( ~" Gnegocios.SetTypeFilling(ORDER_FILLING_RETURN);6 p% J/ I. @6 }
//--- Leave the fixed deviation at it is not used on B3 exchange
. ]7 _# L+ c- ?4 J3 b& Q( mnegocios.SetDeviationInPoints(5);# D: a0 I* M3 L, u$ _4 X4 c
//--- Define the symbol in CSymbolInfo...7 O5 g8 Q( i; W8 r* T f
info.Name(_Symbol);" ?) k4 \8 P# V
//--- Set the timer...
+ n4 o6 y6 _( y5 }9 N( c( CEventSetTimer(1);
: C7 t! G1 Y% `0 P//--- Set the base of the random number to have equal tests...( V) O# f& R2 O' G
MathSrand(0xDEAD);; T9 r+ t1 w& t% X0 j
return(INIT_SUCCEEDED);) ]( k2 [9 k* [7 m
}
) z9 t7 F& W# ~) m//--- Since we set a timer, we need to destroy it in OnDeInit().! B' C) E! S9 }
void OnDeinit(const int reason)
3 x. P0 M Y( R" K8 l/ d7 E{
* a& Q& B. n6 ZEventKillTimer();
9 ?6 \& e7 J8 c! R0 J}
I. ]1 a) u7 l% Q! N3 J+ d! c//--- The OnTick function only informs us that we have a new deal
* G( u* u7 N) L* C {6 bvoid OnTick(). |5 T+ z9 K* Y1 I$ V7 ?) w
{
) r- ?! b" W' D" |tem_tick = true;
8 I3 Z: j- {) R- Z) E" ^2 O) e6 O}
' k, O% Y( |& T) }//+------------------------------------------------------------------+
/ ^9 P* g+ l/ ]: O//| Expert Advisor main function |
: x& E* Y B h8 S4 g- g. ?//+------------------------------------------------------------------+1 T& k9 k" {- e# t. p
void OnTimer()0 z* p- @) X8 l7 t2 t, i# T8 a- G
{0 Z0 S- ~8 m$ a, p/ q1 O
MqlRates cotacao[];
7 u; H7 ^8 q1 \; r6 B5 I+ L2 f& z$ Yreturn ;# P4 q9 i/ J5 p/ {5 B8 K2 s( A9 J
if (negocios_autorizados == false) // are we outside the trading window?: Q! ]% T! N' ~' E5 M. W0 b7 R" a
return ;
" X# [2 Z; E. x//--- We are in the trading window, try to open a new position!" R* n5 |/ v' R3 B" @1 }( K/ Q
int sorteio = MathRand();
- c s$ ^: M+ t0 N0 }2 B//--- Entry rule 1.10 a8 O9 y! h" o$ _0 b* C4 K4 G
if(sorteio == 0 || sorteio == 32767)5 c, z; P7 h- a( b9 z
return ;8 t" m+ H) p1 l
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy
& k$ {; I$ y- p$ F: ]9 `2 K{+ m5 R: O" P) P
negocios.Buy(info.LotsMin(), _Symbol);' i$ K$ h4 P" W3 c+ z) o/ L
}
% i: v0 S+ ?2 \ _) U6 Eelse // Draw rule 1.3 -- odd number - Sell0 t- [# c" A0 P, V7 d/ t, w
{+ F/ E. l$ M+ I
negocios.Sell(info.LotsMin(), _Symbol);
, V! f5 O% Q$ y |. k" o}3 N2 g7 W: J$ P6 t4 |. }1 O
} B3 k+ Q1 d& v
//--- Check if we have a new candlestick... v* B8 j9 L* `& H9 }' D
bool tem_vela_nova(const MqlRates &rate)
3 h2 V; Q' e h; ^{0 K4 ?: B$ o, k
{1 I4 l( r( `# w8 B) @1 U! |4 y2 H$ d% _
ret = true;9 P6 N4 v/ K4 o/ Q! N
close_positions = false;
1 z! G6 x/ j* R( d! F+ b! b" k}6 Z% D& d2 l0 r1 V' `4 T
else
/ q. B; z5 v5 Q5 x" s# L{& t/ e F: |2 \
if(mdt.hour == 16)
0 `+ I/ W m: y0 H) o( X3 D' Z" Pclose_positions = (mdt.min >= 30);
" z" p$ r3 W$ ^4 m2 Z1 [4 ]% b}) I% r5 a( O g; B6 ?( ^0 w
}
z! `/ b1 n! ?; Yreturn ret;4 q8 @; j" N/ x
}
h! Q5 _6 |, K//---
0 i) R. ^$ y* Q, n/ @1 Mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])) d6 r% l5 k% m( {
{+ |- l$ e, X0 Q# c C
if(PositionsTotal()) // Is there a position?+ |5 n6 q c1 K5 S: j2 ?4 h; Q
{0 o3 q9 _0 q/ } X
double offset[1] = { 0 };
/ O5 w7 x4 D: u9 ?. tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. C) k5 O2 f- ~; H, n&& PositionSelect(_Symbol)) // Select the existing position!' p$ C* ]4 _ @
{9 X5 M; z$ W6 W2 G/ Z; Y9 A; N% T- l
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
( ^( w: [8 X b( Adouble SL = PositionGetDouble(POSITION_SL);; x. N. J+ ?/ n8 t+ D2 v% D
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, G9 w+ F9 O+ B8 }* q8 p4 V# p2 z C( cif(tipo == POSITION_TYPE_BUY)8 l y3 \1 Z7 o. y' T, M3 Z4 P3 M
{
7 k( |0 t& Y% X9 i" n1 Cif (cotacoes[1].high > cotacoes[0].high)
2 ~6 v j4 c4 a# c; p9 c( H{# Y; C7 H! G2 H1 k2 W4 t8 n; v6 y2 g
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! c3 q P: w( Y3 {/ N' @4 l$ \7 `2 Ainfo.NormalizePrice(sl);
j! D E8 J# m# u9 k2 F0 Q& aif (sl > SL)
% [$ C7 J" a7 \{
$ V5 S8 i: N: E' @: |# rnegocios.PositionModify(_Symbol, sl, TP);
2 B' s0 L4 J/ t* ]2 X}, ^+ j! A3 H+ B' H
}0 y7 i2 f" @2 g" F. Y3 g
}6 f2 q" T( t3 b R8 s
else // tipo == POSITION_TYPE_SELL1 [6 ~. c8 h* m4 j1 w: e2 B
{
* U2 o5 R. f# P. ~. v1 [' ]. Pif (cotacoes[1].low < cotacoes[0].low)
: u, B* D8 s' a, v; O' k4 h( E/ A{- {; N) q5 a6 B* [9 g; N* M
return true;9 d8 ]" V2 g) ^0 I% P( G$ F
}3 Q1 g% Q; I9 U% n
// there was no position/ S& u9 A# o, ~( g! G
return false;
+ ?( W% E' Q! a' P0 E, _}: J; J$ k: b+ c/ n5 S% p1 }
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。" a4 f& j9 P* Z, e. J* c
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |