私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 [. Q8 u  P' @. h" C0 ?& l+ p
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; y6 R$ j( }. _2 B0 g! q
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' K! m: @3 L5 j7 C; H
以下是制定这些规则的代码。
+ b, t( J) Q) L( {//--- Indicator ATR(1) with EMA(8) used for the stop level...
1 B, s  f: W- W/ F& y# W' Jint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 f- ]. b7 `6 Pint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 H$ Y. T3 ]. w& n9 o/ t//--- Define a variable that indicates that we have a deal...
- H! u) P/ d+ J% A5 Bbool tem_tick = false;
( G1 p9 _0 x0 C- F$ Y  C//--- An auxiliary variable for opening a position3 |0 R1 p4 F& |4 ?) p: g
#include<Trade/Trade.mqh>
3 R& Y# C! S8 b* U" ~. a4 T#include<Trade/SymbolInfo.mqh>
/ F; f8 h- u- c' ^6 RCTrade negocios;: V3 m% c( l* N+ y8 v" Z8 \
CSymbolInfo info;
; _, c$ u- r0 X$ e  G6 z//--- Define in OnInit() the use of the timer every second
/ N  m1 s6 G% G) E" w( Y' A//--- and start CTrade
6 g# S7 R* k# z0 `: ]/ M0 Zint OnInit()
" M# i: L" g; P7 J% d{, @$ [3 l/ r* k- Z: z( ]+ ?' ]
//--- Set the fill type to keep a pending order
5 q: s; s/ V5 U//--- until it is fully filled
) `/ j. d# f" U; x" Hnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
7 _! W9 M! @( _3 y3 T" L% l/ ]//--- Leave the fixed deviation at it is not used on B3 exchange1 m; r7 f5 b! P1 x6 j/ B- f
negocios.SetDeviationInPoints(5);
  L8 m# [) |) f+ D* Q. B5 ?/ k& @( |//--- Define the symbol in CSymbolInfo...
3 g0 A3 h( T6 U  I' B$ N" |: pinfo.Name(_Symbol);  x; n- O. j3 E: h; u! ~
//--- Set the timer...4 B% {5 ^) p0 O' [$ S0 l" ?
EventSetTimer(1);
# }5 ~* n; b; ?' n/ n% e* \//--- Set the base of the random number to have equal tests...: T) O  y% J' B
MathSrand(0xDEAD);, M9 a" T" o8 H+ o. Z8 _  ]) j9 j" f
return(INIT_SUCCEEDED);/ E5 c- p" [* q
}
% t( O6 G6 m) H8 @5 O  W! m. Q//--- Since we set a timer, we need to destroy it in OnDeInit().. K, f1 }  H1 q9 e- c
void OnDeinit(const int reason): N" [+ g7 p2 b) @4 R! P
{
5 X) }' ]6 \* _: b0 B& OEventKillTimer();( k2 ~" D! \, ~, c
}
* p8 X& r3 G- j0 U//--- The OnTick function only informs us that we have a new deal3 [/ k* t2 U" r
void OnTick()
( R6 P' W5 X# L7 `{
. {& u& g; u$ u$ K, stem_tick = true;9 J) E  ^/ M$ I2 H: f
}* s' o4 ?( r* u, Y/ O
//+------------------------------------------------------------------+
; K, A. M4 B8 u  K) s; F//| Expert Advisor main function                                     |
7 }$ U3 B  v' Y$ I7 |  j1 A//+------------------------------------------------------------------+
* ^$ o$ I* j- I& L1 Ovoid OnTimer()+ ^$ n! ?' F: Y. ~2 V9 {0 ?4 l
{7 L; Z- s) K+ M/ w4 H8 z
MqlRates cotacao[];: V- W7 a1 p/ i' @) N' U- G
return ;
  D  A% v% }7 U+ v; {if (negocios_autorizados == false) // are we outside the trading window?7 d9 s9 k- l; w3 t3 X7 c; S7 w6 W
return ;
3 e+ K) s9 `5 ]4 D8 z//--- We are in the trading window, try to open a new position!& A% @) o4 @4 M8 G4 I; Q0 l
int sorteio = MathRand();
) D4 V% \+ \1 ^/ u7 \; H# u//--- Entry rule 1.16 _  ^( q  G; G
if(sorteio == 0 || sorteio == 32767), \0 Q+ B! z! @: q4 c) ~
return ;
# T2 C+ p& I5 c# P; a. `if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' x4 Y% n2 _9 {2 q' O{; \! G5 d3 P/ v8 H6 [/ o
negocios.Buy(info.LotsMin(), _Symbol);* b( }$ x. ?* y  c: j% O
}
( c: Z9 t; w4 R5 @1 y1 Y- melse // Draw rule 1.3 -- odd number - Sell! S6 v2 W9 T) L% W
{
3 r3 s6 _2 I& {% ]8 Fnegocios.Sell(info.LotsMin(), _Symbol);$ F- ]; G9 ]# {9 B  R8 ~$ N. X
}
% G- \! i8 x; l) u}
% U6 Y% L" _  q) h$ M//--- Check if we have a new candlestick...- V9 Z3 e8 [1 `
bool tem_vela_nova(const MqlRates &rate)  o+ V: q( X1 Y$ h1 U/ Q; v
{, f. a0 g% ^7 T. W* d7 i5 q
{
7 A1 s2 o* A" V2 W* Dret = true;" e- B7 A7 g7 a; B3 n, t
close_positions = false;3 z9 T9 j; y  I5 e; g2 E7 B
}& X% y9 n. }+ s: l2 |. x
else2 j" I2 J7 y0 U
{) X2 `. p% a& S) p1 }
if(mdt.hour == 16), {" V! V- \: }6 K- L" B
close_positions = (mdt.min >= 30);
) p. _& r% |& |$ w9 }}
) W  P+ ]! }/ M7 Z}1 |0 x2 q# H0 C, t+ s
return ret;
  ]# ~! a' \3 q0 V}
; v' b# v5 k% f  v+ f' F//---) m7 v. b1 i; Q/ \" n3 Z/ [
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 E6 G. J  W) q# ~4 U6 s{
3 _6 b( J' p  K  S, a9 Yif(PositionsTotal()) // Is there a position?
2 s1 }9 D% `) i& u% _: M: M: P3 K{
9 [% b- k7 X4 @" i. y% A& Wdouble offset[1] = { 0 };: b( v- C' P0 J3 N
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 A' {4 R1 y' [  H1 _2 M0 a1 O&& PositionSelect(_Symbol))  // Select the existing position!1 t: N4 D2 G  C* v" M- K
{
# u3 |2 ~5 k6 e7 e: y- c6 [ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);+ a) k/ ~5 A& i1 ?; e4 m
double SL = PositionGetDouble(POSITION_SL);
& g: Y# j6 s* [- J  U3 m6 Qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
1 j. ~/ b) E2 K) c/ J. sif(tipo == POSITION_TYPE_BUY)
% H3 \* W6 ^8 N{2 C5 l, K/ z! Q, b
if (cotacoes[1].high > cotacoes[0].high). w4 R* M0 h/ m8 [" `& {* l
{5 A4 h3 ]( N7 N* M
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
0 V5 _* U% m1 T, ~! N  r( ainfo.NormalizePrice(sl);
" g: }& B. `5 {5 c' F- w6 K  kif (sl > SL)* y( R* G2 o6 m5 ?9 q2 }
{
7 C4 Y( I" m" Cnegocios.PositionModify(_Symbol, sl, TP);
7 ^7 O/ M5 `8 H4 t" I}
% j# r$ W$ ~, N  u' r}
/ V! f4 J1 F) e$ G5 U3 x/ V' @}
7 @8 n& N+ W$ |: Helse // tipo == POSITION_TYPE_SELL  }5 p2 `4 A5 z/ @4 A# @$ Z
{
( o1 R+ f; s  f/ [4 ^! wif (cotacoes[1].low < cotacoes[0].low)
# ~' w4 y4 ?, G) A1 U/ z% I{
1 L" L1 r' k+ ~) U! ?return true;- g) B& [8 O1 U5 x
}
% n  h, n' p1 i' L( ~// there was no position* a$ r1 {- |) a3 p2 f, W
return false;0 u4 u8 [2 B) s7 q
}
; ?: P4 U" }+ \  I) D8 z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。  V+ A4 ?0 L8 G( m$ e
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-12 17:50 , Processed in 0.407197 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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