私募

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz

期货量化软件:赫兹量化中为智能系统制定品质因数

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' Q4 n6 h$ F1 \! f在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
8 y) o7 D! T" D" A- ~为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。2 {+ U0 V# D! y5 E8 Z: w& x( x# L
以下是制定这些规则的代码。- k  |2 Z0 H5 z
//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 ^9 |0 m+ m4 Xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, L6 W- n. v" A) N+ Wint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 Z8 r% @8 s) O+ T' F//--- Define a variable that indicates that we have a deal...8 Y$ ^( ]3 J: a* u( f) t% T
bool tem_tick = false;
, S! j5 y! n3 d* G( d//--- An auxiliary variable for opening a position; g/ f4 Q7 G; _' T* Y5 s, Z3 d
#include<Trade/Trade.mqh>- j$ D  Y) b) A+ P
#include<Trade/SymbolInfo.mqh>
% t, t8 B& P- S  ~! fCTrade negocios;
+ E& g! W- B0 E* g* `CSymbolInfo info;  i4 a: J7 E2 J, e
//--- Define in OnInit() the use of the timer every second
$ {  P/ X+ w2 N1 `: n' y, X$ {$ y//--- and start CTrade
* P1 y9 i- }7 _7 s) f" l6 S, Z: `int OnInit()
( R9 D7 Z, @: A2 m{
$ _. v$ h* U. s* @3 w+ r//--- Set the fill type to keep a pending order9 p( v) {- l3 {# f- P: {
//--- until it is fully filled1 ^7 o1 d& L( w. K& x5 F
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ z% a$ i/ C" o6 c0 e//--- Leave the fixed deviation at it is not used on B3 exchange
- I% v, I2 l3 w# l) vnegocios.SetDeviationInPoints(5);0 j; R% Z: R* d) r3 p
//--- Define the symbol in CSymbolInfo...
) K& H# N: `- K& o) Iinfo.Name(_Symbol);
7 j8 \& u! z7 ^# X//--- Set the timer...- \3 F% f7 ~- v
EventSetTimer(1);
1 ]( y& E% p7 [: ?& g//--- Set the base of the random number to have equal tests...
4 L( s" i2 S- g/ ^MathSrand(0xDEAD);
0 o' f- }" F7 p4 }2 [4 w7 Hreturn(INIT_SUCCEEDED);
# E. ^' [6 U2 R. d7 l}
: z6 r% i" e5 @4 p* m) t//--- Since we set a timer, we need to destroy it in OnDeInit().
3 ~% Q2 i/ ~5 S" F" j, Jvoid OnDeinit(const int reason)3 s+ V& t* |  J+ H0 L& g" x
{: q  D  `8 Y% l: o( Y
EventKillTimer();
( A) u3 Q; H; u1 w1 @}
$ M- V3 I; ]0 r//--- The OnTick function only informs us that we have a new deal
4 K0 b- W/ |" S* y& wvoid OnTick()
4 l* {/ t- X& c& w{
4 J! \% h8 y/ P) Gtem_tick = true;; E- L0 V* y) g9 q" F
}
" {9 @9 w. l" D+ z4 a; A//+------------------------------------------------------------------+
$ ?  I4 K/ I0 z, v. F" U3 b( n- C//| Expert Advisor main function                                     |2 f5 \! x: ]8 ?3 c) K& q  x
//+------------------------------------------------------------------+
7 K5 X( Y- m! ^4 F, Kvoid OnTimer()
( ~% D3 |/ R  D1 Q{
* g1 b! f3 }5 Y  }* PMqlRates cotacao[];
# F7 N. N/ e, u, [( S  V  K! Wreturn ;2 H) M4 _' }9 ^9 W
if (negocios_autorizados == false) // are we outside the trading window?9 Z8 u. \% ^0 s. c
return ;
6 M: I8 S3 @' o* m1 D# `- M//--- We are in the trading window, try to open a new position!; A: w6 u  \9 w2 H! m3 G' q6 m9 W
int sorteio = MathRand();& P/ @. Z$ @2 ]
//--- Entry rule 1.1
- R$ p! g# Q  L7 ^if(sorteio == 0 || sorteio == 32767)% g, g2 c  A8 d( \# T) h# W7 @: F
return ;+ Z4 N8 X8 U& O1 R. ~& c
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
( Y! ^1 N! r7 h- @0 \: h{
9 j9 W" I& l1 f5 v  P2 cnegocios.Buy(info.LotsMin(), _Symbol);* n" [# u0 R: u
}
& w0 q6 R6 P& e, xelse // Draw rule 1.3 -- odd number - Sell% K' m9 h  p2 K8 h* Z5 a
{: N, u  @$ x) y/ K9 h
negocios.Sell(info.LotsMin(), _Symbol);1 [) M, H( Y% _) {& d
}! y( b& h* U; C* d  c7 c" j
}
; A' h4 T5 K+ E  u6 q; R//--- Check if we have a new candlestick...5 M6 w5 h$ D2 f4 \7 C# y
bool tem_vela_nova(const MqlRates &rate)5 }" q& _% T8 e& i
{
. d& J1 G1 n" j5 {. Q6 s% s{
: Z9 D1 U$ ?! o# \6 \ret = true;" @2 o  B2 O; X0 n7 b8 Z
close_positions = false;
6 P6 F  J4 h' W2 p# k}  c. H) x& ^* y/ C
else& v4 X& |2 K1 }/ F" S
{; o3 D7 i/ y1 K3 V  U4 \1 G
if(mdt.hour == 16)
( v0 K3 ^0 K6 g) e+ d9 l0 h9 b$ F/ c9 aclose_positions = (mdt.min >= 30);# J8 {9 o! ~; P2 ?3 P1 ?
}! H$ Y' i5 ^4 K% O7 f
}) l, |; O" a- U% Y. s4 j( _8 H
return ret;
& a+ p( D5 {& n$ N( M0 w0 N9 y: p. m; T}
  _9 Q; a9 @! m) i//---& G/ o. B. N8 m( ~" X5 s, Q$ a( h: t% P, j
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])) D$ \/ ]% `* e& b. g# S
{2 s" N* p0 U& I" ]3 }
if(PositionsTotal()) // Is there a position?7 c8 m% R+ x; h$ {& d$ l1 _
{$ ~& `, a5 A0 b  `5 {! i2 I
double offset[1] = { 0 };
' K3 K) _* R( E  e3 wif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& \' F' `+ M5 W: f8 p&& PositionSelect(_Symbol))  // Select the existing position!
" V! }8 ]) n) R. y{
% d# u7 H1 \- q, bENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);7 _9 r/ `/ q- W' Q0 }
double SL = PositionGetDouble(POSITION_SL);% B( Z0 N+ k) m! _5 L
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
- \- q) L  |/ k: H' Qif(tipo == POSITION_TYPE_BUY)
8 x" `. T+ W  |4 O4 z{3 B# k2 {+ M5 L" n
if (cotacoes[1].high > cotacoes[0].high)* ]) X* F0 r0 w4 [9 W, v
{
* d. m  N0 i" E, g, P/ sdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& A7 O# l' S5 i& K: i/ J0 ^* R1 r$ Yinfo.NormalizePrice(sl);- {  \% l; |- \: m/ |2 K
if (sl > SL)4 q9 k+ ^7 i. y1 s& M- U
{: M0 c9 i' [5 P1 p* A! G7 Q" n& ?
negocios.PositionModify(_Symbol, sl, TP);
$ E& L) B; o2 ]4 P, b% N+ f4 y}  F! Z2 I+ ~1 C% n
}
* g! g/ t/ @) |, P( z+ |4 V+ g}0 R1 [9 I1 Y: \" O* ^- x
else // tipo == POSITION_TYPE_SELL7 r8 y+ J5 V2 Q- g; N% Z
{3 @6 ~" F% y6 g) o0 |6 R
if (cotacoes[1].low < cotacoes[0].low)
, W3 J  Q0 g: B% Y{- ~) k2 |, @5 O
return true;
" z- g) G0 }- M. s6 k2 R, H}
4 r) l5 ^1 p7 @8 v2 f6 ^5 c6 P// there was no position
# v4 [- R3 v( S) z1 l3 `return false;
8 X* F" A3 l( U}( f5 N+ G! B" \
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
" i+ x- k" ]" x! W到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2025-12-31 05:46 , Processed in 1.023017 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表