私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, k# d. N  z, }% o; A: N. S  d) y, i; O: B
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。. I& ]( v/ ?: m: v
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。7 W0 l2 j1 L% E0 _& a  r) M
以下是制定这些规则的代码。2 Z. e% g$ U2 P. l
//--- Indicator ATR(1) with EMA(8) used for the stop level.../ e- q% B) d" W( K
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 C! A0 G6 Q, i& T& ]int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 }' r* A* }6 r
//--- Define a variable that indicates that we have a deal...
3 a, J, X4 f, L; pbool tem_tick = false;0 m0 E2 ?3 S6 F4 n
//--- An auxiliary variable for opening a position
1 M2 ^- w* k9 x% @#include<Trade/Trade.mqh>
0 ~  J3 f7 G, ?8 l' o8 m#include<Trade/SymbolInfo.mqh>8 ?' I8 g  Z; g+ n6 q8 t: T: B% x+ M
CTrade negocios;. ?/ D  F8 F) O' I% _1 b1 ]- |; X7 S4 D
CSymbolInfo info;
! z5 J0 M4 A8 P) O- |! Q' E//--- Define in OnInit() the use of the timer every second
4 ]1 G0 E5 K/ P//--- and start CTrade
' w  S' K! h6 y# f: e% y6 zint OnInit()
4 D' a! Z% o# S, d- g0 z{- ]- `5 r8 @% Q7 l
//--- Set the fill type to keep a pending order: F  w* O) u& ~( n2 ~0 ~4 }
//--- until it is fully filled$ d* C% Y; [! A- E' I
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
) x  ~! E9 ?. Y//--- Leave the fixed deviation at it is not used on B3 exchange
) u! ~4 t9 J5 J4 f* Enegocios.SetDeviationInPoints(5);, g' a; n! u6 r( k  {4 K
//--- Define the symbol in CSymbolInfo...
8 c9 {, v% @1 t& R; uinfo.Name(_Symbol);! c; u8 m2 y5 M# E# E
//--- Set the timer...
7 o) z, E5 h1 o+ J* i0 |8 V9 j0 sEventSetTimer(1);
0 Y- P0 C( Y. n6 Z: C5 Z//--- Set the base of the random number to have equal tests...% c8 i9 y( {9 D0 A6 l
MathSrand(0xDEAD);0 |8 R2 V+ i& G. x  N
return(INIT_SUCCEEDED);, N1 f3 d! W# Y8 w: A1 f# V# u% B/ T; d
}0 j# t* @( u" I* Z+ U+ }
//--- Since we set a timer, we need to destroy it in OnDeInit().
- C# {9 s. u. Q& m5 \$ qvoid OnDeinit(const int reason)/ d& T/ c2 B# ?& d. k$ H
{
$ A" H( j( M1 R1 z# QEventKillTimer();
! e$ K: s+ q4 C' h+ o}
3 C4 C8 E# s3 o. L//--- The OnTick function only informs us that we have a new deal
; K$ l6 k* G  svoid OnTick()" w5 \! e4 P1 f
{
: `  k- D2 i7 ktem_tick = true;  A- ?2 n' s/ @1 t7 v/ S" w+ W( @( l
}
3 r- B" J! h6 H0 t//+------------------------------------------------------------------+
: F  K  x6 W6 W' z. k" ~6 |0 M+ L//| Expert Advisor main function                                     |2 L- z: M$ S. [# r9 B- |: m
//+------------------------------------------------------------------+
% a1 a2 j2 _) p, |. ovoid OnTimer()7 H. z. X  c0 u+ Z
{% ?# J4 W" |* b* X! e, M
MqlRates cotacao[];
8 k; S  l3 F7 B, C4 n. q1 freturn ;' _& q: }  j) T
if (negocios_autorizados == false) // are we outside the trading window?5 Q' H/ m8 Z/ w3 Y3 U2 T
return ;
* R: X( L! H9 U//--- We are in the trading window, try to open a new position!
& ]2 [8 A' n- D3 yint sorteio = MathRand();  x9 G3 j- n# F/ R* q4 y
//--- Entry rule 1.1. O; g8 Z; }; C& d6 m
if(sorteio == 0 || sorteio == 32767)4 \. B4 h0 z3 ~5 I7 z% i2 {' x
return ;! s/ S6 N9 z. L, i" ?# I$ k, w% e
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
0 F9 o# D: L* c* O' w{: f2 s3 N9 G9 e) V# K
negocios.Buy(info.LotsMin(), _Symbol);
5 L2 P$ ^  e" U}
' [7 u+ ?2 D+ }/ N0 kelse // Draw rule 1.3 -- odd number - Sell
: u+ @2 g, z( s" p' k. }, |{- w4 b4 T" O+ o0 W% K; k
negocios.Sell(info.LotsMin(), _Symbol);
9 B9 H( Q* v8 v: j% y* C* R9 D}
1 e/ o* S, z) ]) f9 z}% U: b; M4 X& e
//--- Check if we have a new candlestick...
* Z' q1 Y" X9 Dbool tem_vela_nova(const MqlRates &rate), ^; ~( D  m  j0 O8 b' l
{6 b( p2 X7 v2 f7 {
{2 W# E$ X3 s5 c) H+ u0 R: `
ret = true;4 ^8 y/ d/ J2 ^8 Z7 H! F' S
close_positions = false;* ~0 }1 b; ?  g# |  ]4 |
}  t' p( f& a% D  c+ A7 z/ s3 g: f
else0 E' F" _; T6 |/ b
{
# h- J( F& ^$ [: u7 m) \if(mdt.hour == 16)3 V4 y: {$ T! x" [
close_positions = (mdt.min >= 30);, P. P/ a- f# X0 K6 h- `
}
3 w* v+ T: W. H}' t) ?5 m- z/ F4 r. n( F
return ret;. z$ H$ k+ G+ p& M6 ?  z
}
# B; ~; j- u, H6 O$ x6 s- ?0 m//---
" S$ y& i7 `4 g1 |' P% J' rbool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); p: D! g' J* W8 N1 q& C
{: a( P0 H- u, k! q" ^8 s9 j7 Y- g
if(PositionsTotal()) // Is there a position?
  C. s: z: c8 L" Y{' G5 x6 K1 ^) u) r4 [1 u
double offset[1] = { 0 };, v$ C6 m& ~7 y. u1 r
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 o; O1 g0 H" g
&& PositionSelect(_Symbol))  // Select the existing position!
; I. v. h! }( f, [; X{9 H+ h# S' p& H/ n. w* ^1 V
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);# |0 o9 i1 ?* x; i! }: n5 I5 N: v$ }
double SL = PositionGetDouble(POSITION_SL);( R5 _. ^- [& H" t  C
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
) I. q2 `  S# f8 Q/ O7 Oif(tipo == POSITION_TYPE_BUY)
0 N# A+ b" B* X9 A$ q{
! ?3 P8 v4 L# t1 H( dif (cotacoes[1].high > cotacoes[0].high)  ^+ e+ `6 S, _# x. ~) x
{! z5 B% a9 X8 r& o+ L: r; i8 q
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];4 P9 n8 u6 x6 z6 ^" B. U% m# o8 a2 W
info.NormalizePrice(sl);
4 j4 {( F1 [1 g$ _$ h1 Mif (sl > SL)' a2 c# [$ s3 E  m! w' u8 h" {& I  F
{
% c; t: m7 y; c8 o$ L; K! anegocios.PositionModify(_Symbol, sl, TP);( r4 @/ {  F' T! b% t1 r/ m
}7 x5 K$ I7 r- D8 Q5 _, M
}, k& R4 L: N! K
}
/ [0 d# K( J9 J1 A% k7 Selse // tipo == POSITION_TYPE_SELL
- U. L* ~+ X) S3 _3 a{
1 i5 G) {' }; u. y  V- @4 Y: Lif (cotacoes[1].low < cotacoes[0].low)
* N- I% T$ t9 r# n{
+ e% Z# s6 b' g5 [$ o  Q7 wreturn true;* S8 h  M+ `  L9 f1 e9 ~" `
}. L4 w0 `& O& U  D( D/ T
// there was no position
4 ^5 K+ [4 @3 X/ D: f0 Zreturn false;1 q& ~0 a: ~: v) }2 N, p, o
}
$ N8 c. D6 k: y! ^# m; ^4 k我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。3 S+ Z5 f2 {& G1 S
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-15 13:27 , Processed in 0.454004 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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