私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 ]5 x; J5 C. t0 p) U0 n3 a, ^! g在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。" T5 ~1 l1 P) _% U; m8 y1 Y) J
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
3 M' l+ u& S" J. _5 D; }以下是制定这些规则的代码。
% {- d: Q1 c, c//--- Indicator ATR(1) with EMA(8) used for the stop level...
. c% T; N6 W( ^* gint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" l+ A6 n" V" Z+ M2 J2 fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
9 g5 S% C8 l! B6 \//--- Define a variable that indicates that we have a deal...0 e5 V$ `7 ^/ n7 ^# x
bool tem_tick = false;
9 k& Z/ ^1 L. A. P$ v6 _//--- An auxiliary variable for opening a position
4 x  Y( o( M# L3 w' i#include<Trade/Trade.mqh>
! I  g9 n; H. Q/ T0 p) s4 J#include<Trade/SymbolInfo.mqh>2 ~! {! ]2 W/ H' H. n6 p
CTrade negocios;
% g8 p* s# G. B* Q8 NCSymbolInfo info;0 g. h* I  L: c% k( @6 p
//--- Define in OnInit() the use of the timer every second
; x7 Y4 T/ D, \+ @7 l! s2 Y//--- and start CTrade- ~. P6 p! F, W$ [# y
int OnInit()1 c( J- f( [/ C
{
2 k+ J. f$ z" p//--- Set the fill type to keep a pending order9 ^$ t/ h" ~( o0 |" F4 T' ^2 v
//--- until it is fully filled7 I+ L! d  n1 }8 ^
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
: ~9 f$ B3 s( {+ b) R3 N; t$ d//--- Leave the fixed deviation at it is not used on B3 exchange$ K& H( p  z# h; }* A9 r' U
negocios.SetDeviationInPoints(5);& K* ~7 j3 o- B# V# O! \
//--- Define the symbol in CSymbolInfo...
3 j/ o1 E' M3 K; T3 g/ Cinfo.Name(_Symbol);
4 e5 W8 h* t! l" g//--- Set the timer...# r, K  \& |; x$ Z
EventSetTimer(1);
3 U. b  M( ~2 [, s1 d2 ]* h//--- Set the base of the random number to have equal tests...
: T# N* F+ U" C" I  _, A: RMathSrand(0xDEAD);- e6 w+ g- y2 K$ a
return(INIT_SUCCEEDED);
+ W3 U( @/ P1 Q- h" b}) p2 j7 Z( b8 `+ M- ~8 s9 C* K
//--- Since we set a timer, we need to destroy it in OnDeInit().( c. b3 I2 Y2 X# g) }  T; z
void OnDeinit(const int reason)
) r1 @7 F1 V; Q{7 N0 e2 Z$ g. g( v$ K; y
EventKillTimer();
6 @- x8 n+ B7 I* {5 I7 r% Y7 |}
+ p, R, l: x, @% I! a& _//--- The OnTick function only informs us that we have a new deal
7 z- R1 k. ~3 Wvoid OnTick()
) k* E; j3 h2 h' {1 K{
7 ?: B! u8 {4 @6 Ptem_tick = true;: c! p3 H: u5 h) {( r9 Q/ T$ m
}+ z% C7 W" R& }& z4 {- l7 Z+ B
//+------------------------------------------------------------------+
* p3 p; A- Z4 c9 g) t* r% C//| Expert Advisor main function                                     |
0 s; ~+ E) `# B& ~4 n8 {//+------------------------------------------------------------------+
8 [7 O5 ?2 m* `! o) ]% \& Pvoid OnTimer()/ Q/ \+ E2 ]: S: w
{
% Y1 y. V: _/ J) j" ^9 E" _5 eMqlRates cotacao[];- u6 F6 C+ G, T) R) M
return ;' s' C$ ^! g3 D7 t) _' k
if (negocios_autorizados == false) // are we outside the trading window?
% G& E& o6 O+ jreturn ;
/ k, B/ B! p4 m9 `+ x' k//--- We are in the trading window, try to open a new position!! Y, E- N  I$ I
int sorteio = MathRand();
) g" r, n0 x' o//--- Entry rule 1.1
4 d/ T: O4 i( U# k' tif(sorteio == 0 || sorteio == 32767)
; \. s+ \  H$ @! U9 {5 W3 vreturn ;1 s, _/ q) `) O
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
( f; d+ w8 i  Q% v) z  ]{
& m3 o2 c0 T3 j2 K. M1 O- Bnegocios.Buy(info.LotsMin(), _Symbol);
4 n( [' I) T0 w7 v9 h" A, R0 N# }+ D}
6 j9 p) [! ^" Delse // Draw rule 1.3 -- odd number - Sell
7 _/ ^7 n1 b$ d2 s' O' e; N1 ~{
* M, U. K' u/ unegocios.Sell(info.LotsMin(), _Symbol);
& d0 P) {' H' v) {9 `" O}2 o, z( h, }. B' ?. F
}% D0 x  M9 D! s
//--- Check if we have a new candlestick...% ]# o8 V/ b, @# Q+ n3 V8 l
bool tem_vela_nova(const MqlRates &rate)3 b3 W5 P! {- ?4 _
{2 M! B5 Q% B  b* P/ t
{+ {6 D; k8 h3 _& ]* {
ret = true;% {1 w* z, @/ q3 J% s: O1 R9 W7 R) X
close_positions = false;
1 t' j' }# ]0 y( U3 v: F}  Z( f% r, B- e* {+ _* B8 P
else
, n  M: o" L. C  V6 b{+ Q0 g7 {7 }* ^& b* X  Q3 |& l
if(mdt.hour == 16)6 o4 W& ]" r" f5 g2 w1 f* ?6 A7 ~
close_positions = (mdt.min >= 30);3 A1 h: c. P/ Q5 G
}# q8 M+ Q9 b9 z: Y
}
, n4 l( D# i7 R) E. treturn ret;- d! S. a/ @3 P' }' \
}3 A$ t; }& Q9 m+ W! j9 u- P+ L
//---1 ]3 T% Z2 c3 w' p' a' k6 Z0 s, o$ B
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); ^9 r. I5 J# B5 [/ R5 y
{
0 H- d+ v3 |% j5 Eif(PositionsTotal()) // Is there a position?$ V1 j( C/ g( m, |6 J& r5 R% i
{
! j1 r. s5 I& u1 \: adouble offset[1] = { 0 };# W7 w) }5 \9 o! U: f" U
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. q0 ^2 ?3 S5 f, C, w( z&& PositionSelect(_Symbol))  // Select the existing position!
4 x% w# e  w' w  d{1 A0 v3 f2 g: k( V* r) n
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 S# y, x. z/ N+ U# H) Y; K( ydouble SL = PositionGetDouble(POSITION_SL);/ h' ?9 B( v$ A, f
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
8 ^+ f( C6 H% z* Z7 ]if(tipo == POSITION_TYPE_BUY)
7 l. ~+ Q5 ^6 C{( X' ^/ @7 K, V" `9 n7 m& R8 D8 w" z
if (cotacoes[1].high > cotacoes[0].high)
! D% Z3 ], c! F# B9 J) g: s{
9 h9 q" v  G7 e6 rdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 q8 z. O+ D* ^  O( _info.NormalizePrice(sl);" C9 N6 V' f, Y7 H9 j
if (sl > SL)
# O4 h5 ^! j# _{
1 [0 R$ k9 U0 a# O3 F; J* Fnegocios.PositionModify(_Symbol, sl, TP);! j; \! T$ Y# H' B! h- D' ^( h& ~
}" K3 \" N7 @! ?8 ~3 a2 p
}
$ V0 R  u; S1 L}
; T6 j& W3 ?" n% t' felse // tipo == POSITION_TYPE_SELL1 q+ u& [: |+ U$ n! ?0 x6 d
{/ m: G6 D* u, h2 e0 z$ f
if (cotacoes[1].low < cotacoes[0].low)  a  Z1 x$ z& N& Z$ n0 n
{
9 _& ~9 a( O/ `. J2 hreturn true;
0 |' D& L7 H6 f2 K}
7 N; r8 Y; A% P7 l" `// there was no position
# e, W5 l  w0 v- @' |  ereturn false;2 n9 @6 ]' i' ~( Y1 v. k5 Z
}
# e8 I& g, R# l  b- x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 e8 w' h" E) B  r2 k5 Q4 l- ^
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 12:18 , Processed in 0.816779 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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