私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, s; D- L# e$ U+ i' T4 ?
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 u; [! h! P% q; {6 K3 Q4 X
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 @( x4 G0 F) \5 G- E3 C, P0 V
以下是制定这些规则的代码。4 X$ p& U* a) a  ~& R/ X
//--- Indicator ATR(1) with EMA(8) used for the stop level...- g  }7 {) R- q: Z  H( d+ w
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
6 ?5 @; @0 w$ n7 d: Fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
! L- X' t) R$ M$ {3 N: }//--- Define a variable that indicates that we have a deal...7 V2 g( M6 L* O) {2 H. T4 v8 H) |
bool tem_tick = false;. Q) G" t4 W/ y9 L9 w7 L( l
//--- An auxiliary variable for opening a position. P1 q7 H6 ~8 z$ l
#include<Trade/Trade.mqh>
, }1 f( X) j" `9 ]! f2 [. u#include<Trade/SymbolInfo.mqh>1 p7 y+ D! [) G( a# x  T  s
CTrade negocios;
# T2 X& I/ g  h1 _; W, f* u/ eCSymbolInfo info;
6 s( O! N1 w. m  K# m2 l) I3 U//--- Define in OnInit() the use of the timer every second
7 l0 ~, G  n7 X% s, Z//--- and start CTrade
+ n$ F) L9 U- G2 E$ a- wint OnInit()
/ Y0 M5 ^' s7 S- u{
7 M0 h  Y1 \6 j  J; `//--- Set the fill type to keep a pending order8 A: i; V" ^) M6 q$ {( h
//--- until it is fully filled/ P/ w5 i: a: `; S! e% X
negocios.SetTypeFilling(ORDER_FILLING_RETURN);4 [% E! w8 m- B) K8 |& i
//--- Leave the fixed deviation at it is not used on B3 exchange
9 c) |3 F! i3 J1 k' W+ K* \! q2 S4 \- K) enegocios.SetDeviationInPoints(5);  m/ w- r) z. f% j$ C, C
//--- Define the symbol in CSymbolInfo...4 j  T& V% H9 C! j# z
info.Name(_Symbol);% x& |# }  ?: d; n2 i, I+ i4 k
//--- Set the timer...  {: m* D* x7 K0 D( o% Q( r6 f
EventSetTimer(1);
- ~. ]9 w+ _5 g1 E3 k8 z//--- Set the base of the random number to have equal tests...
* t* |! C. f& n' L. u, QMathSrand(0xDEAD);- y4 ?! t( y% B$ b
return(INIT_SUCCEEDED);
/ ]' X1 z8 J- `}
+ S  X: x; n3 S8 F//--- Since we set a timer, we need to destroy it in OnDeInit().
  f0 [( x  j% @: D3 I: Q& \! @void OnDeinit(const int reason)! T. z( c( w6 K" @( L, N: k
{
, x# ?! F8 `) `! I: r2 w$ TEventKillTimer();
$ m& h/ O. A# r}) c' g9 T% u) Y* B
//--- The OnTick function only informs us that we have a new deal
0 N/ B3 R) A7 m7 T6 ~' o* Evoid OnTick()7 M, `: t% Z7 h5 P  b4 b
{
" p( J' h2 I- Ztem_tick = true;
5 A, s2 Q1 S/ y; A! B; v! j2 H}, k- c1 ]) A: `) U6 B! d% r
//+------------------------------------------------------------------+* j5 ?9 f& _: S
//| Expert Advisor main function                                     |
2 \2 R! v" s. m  L+ @: l//+------------------------------------------------------------------+
" ~* a! N4 Q% ~" t, s" cvoid OnTimer()# K: J9 I: c, `7 t# E, s1 N
{
2 s, _* a7 }  ]* P7 c) u9 xMqlRates cotacao[];4 V+ _& g  l+ P$ S( r0 R+ q$ ~" b- I
return ;3 H' R4 I; N- m7 x1 c' B( Z
if (negocios_autorizados == false) // are we outside the trading window?: \" W( O1 }8 ~2 Y
return ;" d; V- @/ i& z! b+ l
//--- We are in the trading window, try to open a new position!
0 U; Z" U5 J/ e+ q7 Q, _  Zint sorteio = MathRand();% I9 D0 n5 ^4 x
//--- Entry rule 1.1# d! q. q. {) E* d) q# }
if(sorteio == 0 || sorteio == 32767)
; d; |) J8 ^# g, H1 ^return ;/ }1 y7 i) E  k# K9 Z
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
7 \+ t- n, R1 X) J+ |: |8 d* E{
0 L' D4 H. Q" G) X" d8 p2 Z: Unegocios.Buy(info.LotsMin(), _Symbol);
0 r8 p& F* H6 @2 d4 \9 E! k}, l  A$ G2 V. B2 ?4 m
else // Draw rule 1.3 -- odd number - Sell/ S! W1 J7 W' S! l" V
{
! I) j3 L% K. U% N7 D- @* a* Hnegocios.Sell(info.LotsMin(), _Symbol);0 M  s/ H5 G- Z
}( w5 r' m4 w% Y: d9 Q
}2 a% R: l; }2 y; Z8 }( y) g$ c
//--- Check if we have a new candlestick...  x9 _5 [& n9 O8 F) f
bool tem_vela_nova(const MqlRates &rate)
' q- p) m6 K* V/ u- h) p{
$ p7 ]" E2 |- A7 Q8 e2 b{1 y8 ?4 m8 A( [+ M! v
ret = true;2 J  P1 o0 d( S
close_positions = false;) D7 ]5 o" I- a0 N
}. a4 t  p0 v8 E8 V/ B5 g4 I7 t
else4 a% _( s8 V9 k  w2 m3 j
{6 M( S% c+ e, n* N  y; ^/ `' A
if(mdt.hour == 16)
4 e- b0 I6 {$ }* ?close_positions = (mdt.min >= 30);7 o- s" p5 ?# \+ ]" m. @# J
}) Z8 ]: A) k* U( n' A  a
}# P: F$ x) u! V0 k8 i
return ret;
' W8 C6 i  P% g7 Y: U( _}/ _7 B' Z! \# M" b
//---9 q) K$ M. p, m4 O
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% N% E* z: l$ r/ K- j
{9 N- R. l& ~5 z0 C% o
if(PositionsTotal()) // Is there a position?
9 ?  W* Q% j+ C* B/ S0 s2 A{
  Y( X, ]- ]+ T1 y: c% Xdouble offset[1] = { 0 };
0 J9 [7 R# N/ F$ Z4 T5 M0 o9 ~; hif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?9 P3 K4 R2 X  ~9 O
&& PositionSelect(_Symbol))  // Select the existing position!
7 R* ?% K# g7 ^+ f% D3 r{1 D. q# k# I& @% O& H, J3 H
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 p4 R  i0 W! d. N  ^7 ~7 p
double SL = PositionGetDouble(POSITION_SL);
9 @. y% K: O5 P2 x  O( X8 w& qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));) N$ d/ i2 Q1 n$ ^3 S( R6 N' E( X
if(tipo == POSITION_TYPE_BUY)
, l+ r  v( {/ Z( \3 z: d{" }  m5 s& M( T( q5 l* _, h
if (cotacoes[1].high > cotacoes[0].high)( D# ]; w$ x1 O1 |; K
{  w: c/ C& L2 i; l
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
1 h- ~; L' X/ t1 c) G" i, uinfo.NormalizePrice(sl);+ y8 E  U3 A, O
if (sl > SL)+ R/ m. ]; K: C" y) d
{& \. |; z7 G) z
negocios.PositionModify(_Symbol, sl, TP);* y/ f% L& D% w* V* _- x/ B0 B
}
& Y6 ]' @& T7 {}* D0 F) y% f4 F; b5 k$ D
}$ ], }- x2 x% N6 d& p  I+ ]
else // tipo == POSITION_TYPE_SELL$ [( Y; ^1 Y& \# K  T/ |
{
! p% x4 ?* [& a0 Sif (cotacoes[1].low < cotacoes[0].low)8 O4 T( a# W: Z8 C1 h* Q! f& V' _2 e
{. x/ F5 U; J3 {" E; |5 \
return true;
  D2 N! l* Q+ [  U}
: Q2 r7 P+ M$ ~. H! I: J// there was no position
+ ^# ~# k5 _0 z# Preturn false;
/ R1 S/ R/ `- T5 x! q}" t7 g! s5 i+ F9 c* m% S: G
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# {" y& u- R9 ]" _- n4 D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 16:48 , Processed in 0.966999 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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