私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA$ I7 l( B( q( s5 s. P9 R% A% n, u
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。7 }! @$ Y, R3 P4 u- E/ u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。8 W% @+ K' J1 I) {& ?
以下是制定这些规则的代码。  j, c  T/ q+ P; T
//--- Indicator ATR(1) with EMA(8) used for the stop level...
# \: n& s- Z& V4 ?0 fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
' X2 J! S/ c/ H3 [2 b% G7 K$ cint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
8 d5 i. z6 d! A' D8 O; e//--- Define a variable that indicates that we have a deal...
3 i4 P7 W. i1 obool tem_tick = false;
+ q3 c( q8 h. l//--- An auxiliary variable for opening a position
* G% n) f3 a, G- K! L#include<Trade/Trade.mqh>
0 X, |; M6 |; L5 n: M#include<Trade/SymbolInfo.mqh>
  g1 }/ t- r" C( T: CCTrade negocios;
' e( ]/ W' H0 \' _: A' u. |CSymbolInfo info;7 N* }$ b* r0 ]
//--- Define in OnInit() the use of the timer every second& u1 J  M$ S9 D2 y- e8 y9 N$ N
//--- and start CTrade
8 R9 B, v" n- f( Q/ vint OnInit()
$ l# E& `. D) ^% w' j7 }1 o! u{8 X! F* @' M0 ]+ `
//--- Set the fill type to keep a pending order( \# s& h9 q. L# R0 O& ~7 U4 Y. X4 j
//--- until it is fully filled
7 R% t6 K6 J( I8 o, ~: z: qnegocios.SetTypeFilling(ORDER_FILLING_RETURN);/ v$ |1 n+ `5 A) X# ^) Q2 X- _+ Y' Z$ x
//--- Leave the fixed deviation at it is not used on B3 exchange  S) ?) d4 M, k# H- N
negocios.SetDeviationInPoints(5);: _6 |( o0 h8 \) G
//--- Define the symbol in CSymbolInfo...# H/ s0 U1 m3 M/ R
info.Name(_Symbol);
9 [1 t8 Z2 k& R* I//--- Set the timer...; M& T  U2 t1 a+ {" K
EventSetTimer(1);$ r; V* V9 y9 Y1 F/ R+ `
//--- Set the base of the random number to have equal tests...2 J1 m9 x: _- s/ F7 |& L
MathSrand(0xDEAD);
9 @6 {* q" W  j) t5 }. greturn(INIT_SUCCEEDED);
+ M6 M+ y9 z" A5 I7 a' V* c- \}
6 d) ]3 t& |% S* o9 P# y8 g//--- Since we set a timer, we need to destroy it in OnDeInit().6 V+ W" `' m+ E  {+ C) Y
void OnDeinit(const int reason)4 s9 G7 A5 I! _! V  t* _
{5 U; |$ M# ~& K( J. R
EventKillTimer();
8 V: _( D1 e9 d  {}, p( j6 a. T9 p+ c3 v5 q8 P( A
//--- The OnTick function only informs us that we have a new deal
+ [$ K- ?& }: C/ j* B* ?void OnTick()& K  J4 a" F% U1 v# p) s: r
{
1 b1 g( D  L- E2 Dtem_tick = true;
" A, R& `' Z7 b/ ~8 Z7 e/ |+ U}  ~% I% ?2 j+ @
//+------------------------------------------------------------------+: z0 V7 C5 s- ^4 m
//| Expert Advisor main function                                     |% ]% p1 B) D- q
//+------------------------------------------------------------------+
" l2 H  D  i  T# @! yvoid OnTimer()
& }7 O7 q7 e  L7 a1 n{
2 y" U2 ?/ r4 ?  H' I; \" LMqlRates cotacao[];6 H% |$ _$ t+ ]% b; f, g4 Z6 ]
return ;
8 L! Q* D5 }: M' Bif (negocios_autorizados == false) // are we outside the trading window?
$ e- y7 Q' o2 f9 Zreturn ;
! ^- M  s9 d$ ~6 l6 {//--- We are in the trading window, try to open a new position!
* g* [' R! U2 G& Kint sorteio = MathRand();, S0 x% j- ?; {2 g  M1 b
//--- Entry rule 1.15 h: d8 b0 n5 ~1 Z7 e5 S
if(sorteio == 0 || sorteio == 32767)
; t- X( N) k4 p/ ^$ treturn ;
4 C- c' ^3 O  r0 y4 Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
2 _3 c' \+ b: Q{
+ o- e$ Z8 X( q. b7 H$ _& F% Mnegocios.Buy(info.LotsMin(), _Symbol);! @, S7 [- Q" h8 ^
}8 f  F: ~/ R! D- i$ I
else // Draw rule 1.3 -- odd number - Sell3 c5 `: X5 ~' g/ Q1 f0 J
{
; x5 N, ^0 a5 rnegocios.Sell(info.LotsMin(), _Symbol);  c6 u& [5 {: _) \7 K9 t0 ~4 p
}! p3 _+ a) S: J  q' O
}
3 a. c, @! }$ ^- \4 q$ i% v( b//--- Check if we have a new candlestick...9 F( B" B( {( ]! B
bool tem_vela_nova(const MqlRates &rate)
/ K1 \8 V  Z. K  N) p. Y2 D& e  }{
5 {1 ?9 A4 L1 b" w2 X& P& r{' R3 ~! g" D& J* }5 q
ret = true;
  n: }2 ~* q7 z4 B* R7 C  p7 gclose_positions = false;
( U& @) d* v. d; z4 I: C1 u}- b6 d/ H' P; z7 Z* A
else
3 D9 w/ x+ N' x, t" x+ ?, ]: {{
" S. B, w/ k; w: w2 e; E5 Dif(mdt.hour == 16)
& `; W, ]: k0 H1 F0 P# F5 i# r2 Oclose_positions = (mdt.min >= 30);/ H( u9 o6 ?4 ^; t+ v* o. f
}
2 w7 `! z  W5 R0 c5 V}7 E2 {, S- H" |" j+ a* p/ I
return ret;: f% N+ u+ |& p
}: t6 }+ u& \! d: C5 e& u" s
//---* S1 G4 J. Z7 B$ {8 H1 I. q8 u
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])  s0 }7 L0 z( _( v) V+ U% z( G5 `5 U
{
+ C* F$ ~4 @. {3 l0 Q. h4 nif(PositionsTotal()) // Is there a position?9 q2 d5 a- @' U# }4 H# K+ T
{
% `2 r- ~; m& A+ rdouble offset[1] = { 0 };  ?) `3 N! I  p+ K1 Y8 V, G) ~" ^
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 c: m2 l: X3 i& j: t2 v" i
&& PositionSelect(_Symbol))  // Select the existing position!
5 D0 u1 a5 I. h2 D0 |+ p3 ], H{
0 t1 D, w0 U* p6 E3 B$ fENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);* ~. T9 S0 B& ]: _. z) q0 t& l1 ~+ \* S
double SL = PositionGetDouble(POSITION_SL);
( E: l( s/ C, ?2 o2 U+ Odouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, a, E& n* T/ z/ ~! }if(tipo == POSITION_TYPE_BUY). q8 J& z; d2 w6 z- X
{
3 V+ ]& b+ `& q" n( n2 L7 Z" |if (cotacoes[1].high > cotacoes[0].high)! H- i/ Y3 F2 z% u
{5 o0 F8 i# ^5 W+ l) ^, T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];% G6 z# D2 U: o. w5 K& @
info.NormalizePrice(sl);+ W' T9 Z$ l8 b: Y
if (sl > SL)% W) k, m* K3 b- ~9 s; H2 p4 A
{
( s3 ~6 |* Y; N- l' f  v* |negocios.PositionModify(_Symbol, sl, TP);
8 Q& S2 E3 \# [$ l5 z$ d7 X8 z}
1 }7 ?+ D& M  ^& d8 R- f' x}1 L' e' O" \. h  r: B6 t
}& W! S+ b& R4 n: x
else // tipo == POSITION_TYPE_SELL
7 |' S( Z$ k. F: q; S{
# a) l/ N2 O( w; G4 U9 E  \if (cotacoes[1].low < cotacoes[0].low)
; O! L+ c% K; ~/ H# r* W+ ~/ Z{. [' p- S- e  f7 A
return true;  s# [) n6 |# c' g: P8 H& S3 _
}& `0 e, M1 L( A; H5 ^$ S/ a4 p
// there was no position
- u. ~% f8 @7 l; f' B7 O" jreturn false;
8 G8 U! G" P7 o* J}
* O& L3 {9 ~6 N( P7 B# n# \我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 K$ J- g3 W  q  ?7 M: \0 J到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-15 09:47 , Processed in 0.590643 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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