私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
6 o, Q. n8 r) S% @8 T- s在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
' l" |( V" i; Z9 z; c& @6 i+ f为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; _8 {( E5 w" M7 w) O; C8 U* U
以下是制定这些规则的代码。( U' S5 l, ]" h& [+ h" b
//--- Indicator ATR(1) with EMA(8) used for the stop level.../ j) j: z! }, O# @  w
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
3 I( D: ?! l" ?4 _! Aint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
' r0 h1 y. w6 I//--- Define a variable that indicates that we have a deal...
. y0 j2 I- K: c% V5 O& qbool tem_tick = false;
/ o8 x. V- ]& c) G$ Y* n! n//--- An auxiliary variable for opening a position
- d7 p1 c% {9 o6 q% Q; n#include<Trade/Trade.mqh>- W; u, f- h; `
#include<Trade/SymbolInfo.mqh>- [- Q3 \5 p+ N! c; T# d9 l! E
CTrade negocios;
/ j- c( W% n  ^+ i9 k% ACSymbolInfo info;
5 R( E+ z$ w# [) n, H6 B  A- |; X2 Z//--- Define in OnInit() the use of the timer every second' k) h0 q; I6 j$ G- g2 w
//--- and start CTrade4 w/ |# N+ C& e
int OnInit()6 K9 {* Y5 c$ v4 b" I& v
{
+ l# {% `  {+ i" F& u0 O//--- Set the fill type to keep a pending order
& b2 B6 `9 B6 ?" L. a: W8 p, G5 p//--- until it is fully filled
0 ^8 s( y$ ?! Y0 w0 O% K& Knegocios.SetTypeFilling(ORDER_FILLING_RETURN);2 ^: s( W0 D6 {+ d
//--- Leave the fixed deviation at it is not used on B3 exchange9 p& w1 Q( j" I0 M7 Z  r: B
negocios.SetDeviationInPoints(5);8 T& R5 d/ S3 c2 s* P
//--- Define the symbol in CSymbolInfo...
( n" e! z7 k6 c( Sinfo.Name(_Symbol);9 f& W" U, E# w! A3 V2 p3 g6 }  L
//--- Set the timer...  Y8 p- U% B5 P. G; R
EventSetTimer(1);/ j* p4 j, v. O
//--- Set the base of the random number to have equal tests...) b6 R  t, A* M
MathSrand(0xDEAD);; w; t" v1 T; X) f2 m
return(INIT_SUCCEEDED);. e& R) C+ m+ b6 _* o0 G1 C) T2 H6 I
}
; j& U, K1 g+ y6 G//--- Since we set a timer, we need to destroy it in OnDeInit().
7 N. [  @: _" _/ v, E' e8 Evoid OnDeinit(const int reason)4 H4 }3 e* N& }
{) h* F- |" l( H/ n
EventKillTimer();
0 y' Q9 |! M* a4 h4 G6 B}7 `5 {( L& R; O5 ?( Q
//--- The OnTick function only informs us that we have a new deal
6 y/ ]( v: X* V% d# j+ ?! wvoid OnTick()$ h. S: k5 ~$ E; k0 X" i2 f6 [
{5 N, p, V4 ~" I/ p
tem_tick = true;1 D0 g, n3 {2 e$ b- b$ d" ^- }4 y
}# Y2 \# r$ F+ m) @2 m
//+------------------------------------------------------------------+4 X6 {! y# I2 s; K# Z! }
//| Expert Advisor main function                                     |; F: O) d" y; \( u4 [9 u# f
//+------------------------------------------------------------------+
. B3 ^+ O6 D! v/ Z) |void OnTimer()
! a1 V+ W, X9 `* c4 q{
  d" c+ C9 l; x& Q4 qMqlRates cotacao[];' u- k- n6 h* `& h! q( U6 @
return ;
, [% x! `  t8 cif (negocios_autorizados == false) // are we outside the trading window?# [1 h% e. P( e. `3 y: s
return ;% }/ N3 p* s% g0 V9 H5 I" B
//--- We are in the trading window, try to open a new position!3 H2 B9 Y- s+ G& i0 a7 |
int sorteio = MathRand();3 E( ]$ b3 j' q) X+ r# q' s
//--- Entry rule 1.1
  l( J! S$ }1 Y' M5 a( hif(sorteio == 0 || sorteio == 32767)7 N& Q+ f5 D3 \# k5 L
return ;6 T! ^( w) k( c, B) B/ b  z; V3 q
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 s- f' G. ?  p& [+ M9 L7 a8 ~1 z
{' d1 d4 B7 w7 c1 _" v2 m: m: m
negocios.Buy(info.LotsMin(), _Symbol);5 a( I. J' {% H  O
}; ]: ~+ `9 p, f% j6 c/ u
else // Draw rule 1.3 -- odd number - Sell
1 l. H* F) y5 f# n- ~% a7 y+ `5 j% ?7 s{
8 Q9 K8 q+ H4 ~8 S# l( anegocios.Sell(info.LotsMin(), _Symbol);0 Q& ~) h$ u7 \, E# b: F% p3 @* C/ V
}
$ d& l. o+ m0 @1 g$ ?}
- B8 H0 Z' @+ G- A//--- Check if we have a new candlestick...; g9 L" b0 n4 }2 q4 }2 _* |
bool tem_vela_nova(const MqlRates &rate)
7 ?7 c$ D4 s, R6 `! z' }! e{
5 G! a6 @. b' C5 _+ o* i/ F7 a  l  K{
! E3 E/ R# U$ Yret = true;8 k5 h- O2 l* b: a
close_positions = false;" P. p. z4 ?4 F( A
}
9 \' B, t' ^7 b, `else
( u) D' i- E9 A! Z: f1 R{" S2 W) l- N% J" G7 M* g/ l
if(mdt.hour == 16)" G; N0 L; A. \- `2 D' X" {, @+ o
close_positions = (mdt.min >= 30);
6 m6 P: @. @' F* ]' {" w. l% j' i}
9 t0 [$ p6 r' Z' q0 A( C9 s$ B}9 P$ K* l; h' \+ M& R
return ret;
9 t. K& S2 J9 d9 ^, d}
1 d7 g2 z' d5 J5 M  d. i, c9 C//---
0 j! a7 i' T. l2 S. o$ D& m. h6 rbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
  D' j$ }& M7 Z) \: W+ l# @{
3 _$ o) o5 L# M+ G* H# @% Qif(PositionsTotal()) // Is there a position?
. t0 l  N- M" b! G. n; Q0 i{$ X- f; x6 B8 z; O- r0 o
double offset[1] = { 0 };1 [' P: z; c2 Z) `% D% y
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?5 A4 l  X' p7 a/ p6 c% |
&& PositionSelect(_Symbol))  // Select the existing position!
0 y2 N) ]/ w: y# M! ]2 T/ g+ k{
0 k# B8 n+ Y  L" c; PENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
! i6 F# d9 x- ]3 I) l$ k: Hdouble SL = PositionGetDouble(POSITION_SL);& L; Q8 y$ E3 L0 j. N0 g
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# p  J. o5 B$ K% b
if(tipo == POSITION_TYPE_BUY)7 l+ W- v2 N! z7 d
{
% |8 [# G8 Y1 Y/ d5 J1 D8 v4 cif (cotacoes[1].high > cotacoes[0].high)( V- l" U3 \& S0 S( [! f" Q& k
{" ~0 l$ w2 l* Q$ _2 l+ v
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
3 a2 r% @+ }: q4 H$ N  q2 hinfo.NormalizePrice(sl);3 N! T# V. `7 b* a; b; G
if (sl > SL)
8 R! w# Z- T* \9 f{6 P+ x, a/ t5 Z( F# X
negocios.PositionModify(_Symbol, sl, TP);
, p+ ]+ v9 \* V}
5 \5 {" E, A% \! z) }6 i- Q- q! e. U}. S! j' y. j& a( J$ s  }
}
/ ?& t9 v7 N* T  oelse // tipo == POSITION_TYPE_SELL
, t. F9 ~+ w: f' O. ^% a1 L8 y{3 G2 }4 {- V2 C/ v* k" N: O0 g& I
if (cotacoes[1].low < cotacoes[0].low)& N7 u, y/ c* }3 D
{5 ^/ g1 |" Z& \$ m7 B6 V3 b1 u8 X
return true;% t$ H# J% X* a" h  T2 s
}
' j4 n& B% C7 R4 v$ m" R+ U// there was no position3 l; V0 i: E9 s' k: ]
return false;0 s! `2 Z5 s1 a
}
2 e* ~' L9 ]& A6 G( Y1 j我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& H; z4 v: f7 a; l" s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-16 12:03 , Processed in 3.022239 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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