启动交易模型,并构建 EA
A* \$ U. q9 `在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 R( a& i0 G: e: d5 h为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 M: T: F% I, B- q
以下是制定这些规则的代码。7 g- b" ]2 ]2 [' J. Q; q2 v/ S
//--- Indicator ATR(1) with EMA(8) used for the stop level.... r2 q( e+ n6 Z) y& h# Y
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 {6 X& c: [* b- k a1 n) v- M
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
* j/ v4 Z% G) e4 f& I7 j//--- Define a variable that indicates that we have a deal...
; R5 F' `' b1 |, Xbool tem_tick = false;
. O: j* j( h, O! A, |. V6 E//--- An auxiliary variable for opening a position' I2 a# h( i; N* ^$ W$ g
#include<Trade/Trade.mqh>/ m1 ?3 q/ {4 W
#include<Trade/SymbolInfo.mqh>
9 K% [7 c/ c: I% G$ E+ gCTrade negocios;
1 F) R" t) G) M! ]/ ACSymbolInfo info;3 L: W+ e3 T) g( L8 w
//--- Define in OnInit() the use of the timer every second
4 \0 I7 N0 V/ M% e4 _) H//--- and start CTrade
0 v0 _) W& F: { e Y( @. `. A; ^int OnInit()" n" r! z2 s7 V3 R
{
0 ~9 h! H( `) D5 ?. z& f- l//--- Set the fill type to keep a pending order
3 u3 r1 t" [7 \6 v( I//--- until it is fully filled
$ }6 f' k7 l# M S% X7 k0 vnegocios.SetTypeFilling(ORDER_FILLING_RETURN);8 w3 ^5 m: T, e. Q
//--- Leave the fixed deviation at it is not used on B3 exchange7 X1 \8 }9 r+ R9 c. r8 J g
negocios.SetDeviationInPoints(5);
; N) t( I4 N2 O" {9 ]- d/ |//--- Define the symbol in CSymbolInfo...
0 ^( e' ]( O2 T4 Z$ Ninfo.Name(_Symbol);, Z/ [ w5 w8 `: L
//--- Set the timer...
$ \ A' T6 p$ G$ dEventSetTimer(1);
# K1 b$ v7 T& U8 v1 T+ r//--- Set the base of the random number to have equal tests...3 o4 o; `5 X+ t/ C* l
MathSrand(0xDEAD);2 `& N, W+ Z9 K q( O
return(INIT_SUCCEEDED);
b' X7 o' {. d! `}. @0 M7 s( ^$ ?' T9 g
//--- Since we set a timer, we need to destroy it in OnDeInit().
3 x: V2 ], D+ Y, A+ wvoid OnDeinit(const int reason)# @" m, }* I8 m( C& ^, A
{
& e `0 m" f9 n5 k' a$ SEventKillTimer();
" h3 C- x3 |; [: ]6 _" Q}
. N. Y3 |' f. x" M# o- C//--- The OnTick function only informs us that we have a new deal% U" ^+ a x7 S& B+ m. r; G5 W# @
void OnTick()
0 V+ B8 H3 R) v# P{# I5 b0 X: ?) z- O
tem_tick = true;; z" d& [5 i" d& k
}$ Z1 _5 b& ]. m* E$ y( R, F
//+------------------------------------------------------------------+. p( B1 m7 w* C5 r
//| Expert Advisor main function |" X) `$ Z2 L# X9 I
//+------------------------------------------------------------------+
" i7 G9 F4 z# K _. q# O7 Xvoid OnTimer()( U$ P) T! I4 H# L" }3 O2 Z6 j
{
; l) k ^* q6 g H, s1 SMqlRates cotacao[];
) f; W" Z- |5 `6 t8 u4 Rreturn ;
8 n0 H" ]. G( S0 q( S* t3 h7 p7 }if (negocios_autorizados == false) // are we outside the trading window?
, F, N% T0 q2 T- b/ S5 Vreturn ;
+ s. r& N4 c9 l0 [! z2 _//--- We are in the trading window, try to open a new position!
7 x: ^/ P5 g* A: m6 sint sorteio = MathRand();8 x2 d9 U: k% h, f
//--- Entry rule 1.1* u+ p5 W. X$ }; J! M7 s
if(sorteio == 0 || sorteio == 32767)
* q3 k6 k5 V/ Q4 x6 Mreturn ;0 e8 M. P. D3 ?4 }$ _. U
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy7 f+ y4 l, S# c8 r
{
# d2 U. X5 j3 ?/ c" e; lnegocios.Buy(info.LotsMin(), _Symbol);0 Q x \2 j; v. r4 x
}/ e/ _- d h R+ ?( @
else // Draw rule 1.3 -- odd number - Sell
6 l6 n6 F! N; L& _' i3 t9 s{
9 W2 m( j9 w+ A5 D4 g8 Anegocios.Sell(info.LotsMin(), _Symbol);
$ V8 X8 y/ Q% T; d3 X# x+ u}6 b% B) w% D# \; l1 G
}
" {* E" q4 A. g; A1 u//--- Check if we have a new candlestick...
3 I8 e) f) T* obool tem_vela_nova(const MqlRates &rate)
) y h0 [2 V/ o% _ G{7 e# S; q) S2 H$ ~3 V
{
0 r( N+ Q3 w/ _2 c" Aret = true;
& z, p% v. ~, y5 Q" W. r- J% [close_positions = false;4 f: L5 k0 I1 }) X+ ^6 q: t" t
}
; `8 J8 q" E1 k3 r; z) Lelse* [" {2 C! C; f' q2 ~
{
+ D, @7 Q1 f# V. W; {% Rif(mdt.hour == 16)
5 j, a6 y$ x9 x- K5 V& k: i8 lclose_positions = (mdt.min >= 30);
, t* b# N1 e& K8 {6 o/ p1 f L: J* Y}, U! h7 B$ j: b& Q ?2 t. h# S
}; j- B5 B9 j6 C: u
return ret;
6 x3 z- i/ H' G6 o}4 T% y) ]4 }5 S! b& v
//---
; D) N! W3 n/ i. t% v6 g4 Ebool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
. i* L8 `, }! Z p1 N{9 s8 E. U5 O- \0 s
if(PositionsTotal()) // Is there a position?
4 Y5 H0 h! a1 W% S5 j{
8 A; h( J5 u3 \' h7 Vdouble offset[1] = { 0 };9 G9 Q$ r7 f; }/ q1 I
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?+ Z* s5 e& l8 S- Y
&& PositionSelect(_Symbol)) // Select the existing position!
9 A* H2 r* u: h* v{
2 T8 G* Z9 R' W! h! J8 S- m" j$ lENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, T( b% j ]$ A; odouble SL = PositionGetDouble(POSITION_SL);7 }$ V* c/ e: X& ^4 d; B
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
6 F8 ^1 [! _" J2 C* c$ W0 V, @if(tipo == POSITION_TYPE_BUY)
! W( X k& O$ y5 G5 a{
. a- L0 U/ @$ H% m# M' _; `- bif (cotacoes[1].high > cotacoes[0].high)( W, I! L% f+ G* _
{4 i- v7 G6 J* a+ B) \. A, W
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];+ h" I" g: }* B- L6 o, w' z
info.NormalizePrice(sl);% u9 v. Z1 q3 g {* \7 d! K8 d
if (sl > SL)
' J0 R8 s7 R9 S{
# R5 G4 h/ Z l! E" anegocios.PositionModify(_Symbol, sl, TP);; }0 y5 g9 r& [! F4 O: K ]7 Y
}; v0 L I* r% T X
}/ J# e/ ?7 Z% Y5 [
}
9 O1 Y Y |/ Z) Jelse // tipo == POSITION_TYPE_SELL8 i; c7 @8 W; f& s# `' g0 M& I3 p- @
{
' B) m' z( Y1 _# T0 D. oif (cotacoes[1].low < cotacoes[0].low)
) X% ]. H* X/ e' b8 H& h8 K{" \& R; o! d$ @% D( v8 ]) P
return true;
/ u, h6 {7 ? h8 V3 B+ k2 d$ Q}( X+ H: w4 W6 P9 T% E* q% H2 W
// there was no position. E: ^2 ~" {3 d" L5 l; i
return false;: E. _+ t/ S4 ]5 Z% _' Z2 N
}( Z. q, Z/ V' @0 o$ r w
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& P- Y6 X4 p+ W2 Y7 g* Y0 _到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |