启动交易模型,并构建 EA
. i" ^" L, P: \9 c2 k; z- r' y在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 h- q8 r$ Q# V, W$ H6 t' ?为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: d7 X5 J- h+ z( Y9 t, g以下是制定这些规则的代码。
' u$ ~! b: q- H5 z* D//--- Indicator ATR(1) with EMA(8) used for the stop level...) ~3 ]* Z( | y; r' ^$ ~$ _
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- V7 W( s+ E* }# G& Hint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ L% z2 r9 k) o- u5 y+ r
//--- Define a variable that indicates that we have a deal...
6 m6 v! W V7 K2 R1 `4 \bool tem_tick = false;
, M# Q: f, ^$ X5 i! O1 x. z7 F3 `2 G//--- An auxiliary variable for opening a position, l1 d1 ?* o" }$ H7 ], ]0 `! `
#include<Trade/Trade.mqh>
( U1 D3 H2 L4 H1 ]& D) T2 |1 M* J#include<Trade/SymbolInfo.mqh>
& j9 }4 b) |: H7 M6 {7 G, v8 zCTrade negocios;
6 G( n; U# P! }% WCSymbolInfo info;$ d; D9 e' E* v! s" m
//--- Define in OnInit() the use of the timer every second( \) u2 |/ ?: @5 M- l8 x1 ]% ?$ x
//--- and start CTrade
# p* h+ o' D" Uint OnInit()
% H. V1 y1 m+ ~/ x1 ` e{& C5 o) Q6 w4 ]: D, C, ]
//--- Set the fill type to keep a pending order2 ^2 Y' n# D/ W% ~) u; p
//--- until it is fully filled/ [3 J2 D+ ]) u/ k
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 I# M9 {# ?, S7 r. o) ~
//--- Leave the fixed deviation at it is not used on B3 exchange
( E- t3 O& h! D; N7 H! \* d8 {negocios.SetDeviationInPoints(5);3 p( U5 `* |8 i% J. S! Z- |; [
//--- Define the symbol in CSymbolInfo...
7 U* u" x! l+ ^! W, [+ E; Q7 q, `info.Name(_Symbol);& F7 k6 F" I: f, M) k
//--- Set the timer...
2 P/ m4 R$ e$ D2 MEventSetTimer(1);
7 d7 o* T" j0 q% v9 ?/ O8 ]6 E//--- Set the base of the random number to have equal tests...' [- \' i1 ?9 U7 o$ m6 q+ {! @
MathSrand(0xDEAD);) \* L0 q( h9 S# \7 \. C- S
return(INIT_SUCCEEDED);' a3 |/ Q: m4 T% n. M# J
}# V6 i$ r4 `2 W2 j" F/ T" w4 `
//--- Since we set a timer, we need to destroy it in OnDeInit().- W: P% I2 n' K3 D* R) t7 A! p
void OnDeinit(const int reason)3 N/ q& Y3 n/ ]8 p2 W
{
b# X+ a k! Q6 V# z5 I5 u3 LEventKillTimer();9 N$ \+ {' l& G3 r0 b* ?
}
& W6 J! g, F! v8 r; w' O9 M, b//--- The OnTick function only informs us that we have a new deal" [# _; A' y5 k1 O9 z0 M8 ^
void OnTick()% }: N9 e1 f9 W# l* Q) D
{
1 I5 { w+ p3 V( e2 u6 {+ D0 M0 Wtem_tick = true;/ |) ?4 F+ l# j$ W$ @
}8 y3 ~3 _; \0 Q p9 U
//+------------------------------------------------------------------+# l* B1 }# V H# T
//| Expert Advisor main function |
( E. z* G" k+ T7 |8 N- r; C//+------------------------------------------------------------------+
' |7 `1 V+ K3 }* Ivoid OnTimer()
) ^2 O5 M9 o! Q( x& [- q2 }, a{/ {' ^+ u4 h7 q/ B& P, f _
MqlRates cotacao[];
2 B$ G4 f5 J2 F3 v2 e* Q7 Areturn ;
3 G5 W/ V0 p4 d+ Rif (negocios_autorizados == false) // are we outside the trading window?' E) y7 _1 C% T! A% s
return ;
% ]& w! g0 ]0 n9 s! ~+ z$ L- x1 _//--- We are in the trading window, try to open a new position!
8 A- @: U2 c9 oint sorteio = MathRand();. F7 G5 f, s! Y6 B$ T9 h
//--- Entry rule 1.1% {% }2 ^+ S& f( l3 V# |$ R
if(sorteio == 0 || sorteio == 32767)
" E0 ^% q, R% u3 ]& x) Ireturn ;5 F9 v$ ~( f! D" D/ D5 {/ ]
if(MathMod(sorteio, 2) == 0) // Draw rule 1.2 -- even number - Buy
0 Z9 j4 x C9 B$ D( N{: K0 t% Q) r, i- v9 |
negocios.Buy(info.LotsMin(), _Symbol);$ J2 b; _2 k: I- @
}! s+ }; T" a7 {/ m8 J
else // Draw rule 1.3 -- odd number - Sell
9 d1 o& k% h, D \{( P& _( P3 z- i4 [2 G
negocios.Sell(info.LotsMin(), _Symbol);7 H0 n9 G1 l5 |/ i6 l3 U# o. A
}+ v* E# M5 i( F6 p, s
}2 f U3 H x/ H* R9 g- O- H- ?6 c) @
//--- Check if we have a new candlestick...
, l" ~; n9 m/ m& J# V1 Hbool tem_vela_nova(const MqlRates &rate)
9 @, I4 a: [, t{5 | U# y/ x/ J1 \) L2 ~
{
4 k0 w- N( E9 M! G3 S: c- Gret = true; B. j$ A4 C$ q$ q/ q4 d+ w
close_positions = false;! f$ y4 z: }: s- U8 `6 O- U* F) S
}; p% U2 S$ ^5 f
else5 g6 @( [* u: |' L. K$ N
{
" Y2 o# J1 y2 O0 r! Dif(mdt.hour == 16)3 k' P# a* ~5 T9 j9 N* |+ h
close_positions = (mdt.min >= 30);
, t2 A1 T: O, r' S; \2 y} M6 P* M' {# F) |$ m
}5 i. m; L. x" p& A
return ret;
" g$ y) |% a, _# d}
" X$ q# n/ V( M( A. h7 X3 T//---
/ \" f4 P7 P2 J) _bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 T8 l1 B- d. O8 g1 j1 f{
6 J0 Y9 G' B! ` sif(PositionsTotal()) // Is there a position?$ h1 x( o4 t8 v9 L8 D6 c
{
7 e2 L$ p, u3 r) x& r% G+ A4 H' s5 `$ edouble offset[1] = { 0 };
5 }% y7 x, w- Y* W: X% Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
% q6 x0 n. @4 \0 Q2 O6 `1 @&& PositionSelect(_Symbol)) // Select the existing position!9 @$ D% ~2 ~9 E, B+ |0 ], W
{
2 M2 ~8 `- A. n2 h4 j3 B. bENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, J8 x& t' g1 _double SL = PositionGetDouble(POSITION_SL);
$ o \6 t; y# B7 f6 U- ]# g) x6 Ldouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));' T2 B. y2 ~& D2 E. {
if(tipo == POSITION_TYPE_BUY)
3 h, ?5 [+ Y/ }5 v$ x2 d, \8 _! z% p{
' V( R! s' L7 Wif (cotacoes[1].high > cotacoes[0].high)
2 Z) b! o* U) Z! _{, L1 i$ g6 `( }+ ?6 F* m: z/ l; {
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
" k4 R3 Z0 ~. y+ c9 ?info.NormalizePrice(sl);
+ ?# p" C- Y) a; }' Q) c w# Oif (sl > SL)' j3 D0 x- V+ q3 {; m
{8 x2 x0 E% `6 v" O2 u
negocios.PositionModify(_Symbol, sl, TP);9 y* J2 D4 k% e/ T/ D7 [
}
. K/ Q8 t. I2 b* u5 e" o) \) L}
. j$ L( v& P& b {: o5 S1 A& @}6 T$ w6 S R$ ?9 ~5 W
else // tipo == POSITION_TYPE_SELL' L0 O1 e% n. d7 P9 o( y* q
{. W4 V$ r8 o9 C4 f9 k
if (cotacoes[1].low < cotacoes[0].low)) _( C* W1 o% d/ `
{9 i& H! \2 N% O# S# d
return true;
) D" I& ], |: b6 X: s}
1 J' x! n8 c6 L2 A- ^5 i8 O. G$ }// there was no position0 l, Q0 N) L1 o/ _/ l. [
return false;
6 e; Q' P. P/ a: U+ R( t}
! p% ^+ r- Q$ F. K' a) \/ R我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
2 C# x+ L% i1 O, f$ ^4 c" f% O到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。 |