私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
" [  D8 M8 _# |; V& E在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; H+ Z* T4 e( N/ Q* P! C- n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。1 s& G* J: D1 j  c- j2 @$ B4 s/ ]
以下是制定这些规则的代码。
" Y5 c) O; u5 [! e- p% d- k) l//--- Indicator ATR(1) with EMA(8) used for the stop level...9 e+ X, c6 `5 R) ~
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" l+ W* O# m6 m: X0 h3 f* l& q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);9 ?0 v9 q% E- X) T# {/ t: Q! P
//--- Define a variable that indicates that we have a deal...5 ^6 _- \- i) u3 [, K% k2 R; V
bool tem_tick = false;
( Y& _+ l3 v7 P' [$ E' L0 P//--- An auxiliary variable for opening a position# h( J9 p( Z& N
#include<Trade/Trade.mqh>
0 B$ Z* k4 G9 ?" @" p#include<Trade/SymbolInfo.mqh>% M0 M8 O2 N% U% t/ r- n' k1 s' J1 F
CTrade negocios;
* \. p+ i" v1 n% A2 j$ aCSymbolInfo info;
6 {' j+ I+ r- {1 t" w2 p6 i//--- Define in OnInit() the use of the timer every second
6 l+ p$ K9 N  ?3 e' x9 O; S//--- and start CTrade, t$ V0 \4 r) t7 ~9 n
int OnInit()
+ u5 B+ n* h" P" H{
4 ^# R/ z: z0 x: O% y/ f4 R//--- Set the fill type to keep a pending order& [: H( f) n9 |# a8 C9 J
//--- until it is fully filled
& l, Q2 k3 }: h' C& }$ @negocios.SetTypeFilling(ORDER_FILLING_RETURN);: t% a) s) k. ?$ ]) p
//--- Leave the fixed deviation at it is not used on B3 exchange* w- q1 r( Q$ C" R- R
negocios.SetDeviationInPoints(5);
  z$ M; h$ f5 t  R6 y" p//--- Define the symbol in CSymbolInfo...
# ]( b/ J$ y2 E" Vinfo.Name(_Symbol);" ^" |7 r* l( l0 ?/ k) I% ^0 D
//--- Set the timer...
+ K" G6 v0 m% h( q& wEventSetTimer(1);6 @6 n. m* N& K0 |- p
//--- Set the base of the random number to have equal tests.../ ^1 L/ H1 ~  c; x2 b& E) x% F
MathSrand(0xDEAD);
: Y' F" V+ X' t" v, g/ ]+ T- X9 wreturn(INIT_SUCCEEDED);4 J& d: `& ]! Y
}$ W- k+ L- [! J. F) D
//--- Since we set a timer, we need to destroy it in OnDeInit().; M: }, ~+ W, K9 n3 |( {
void OnDeinit(const int reason)
5 ?. z9 p; K0 }" E{
0 _0 ~1 q& R$ s, uEventKillTimer();' n3 a& O; E% e& F
}0 S6 R# D# Q8 [, ]4 |9 A
//--- The OnTick function only informs us that we have a new deal
; x+ r/ m" _" L) W' v/ fvoid OnTick()1 d  P7 V8 X9 ?! G% b
{% T: \. ^- P. ?! w4 ?" ?' g" N
tem_tick = true;# V+ `4 Y  s2 A  G7 E9 J
}! c, f4 I9 U- S1 ^6 l
//+------------------------------------------------------------------+# g' {) p' P5 H8 |- R" c0 c
//| Expert Advisor main function                                     |% i$ W- N6 L; ?# L0 c% Z
//+------------------------------------------------------------------+
5 V7 P7 y+ A0 G3 |void OnTimer()
7 P9 m" V' [1 E  g{2 r  W8 H1 b. H/ T$ M2 \
MqlRates cotacao[];3 l! t5 r: t$ S0 {: n$ S
return ;" S! O( i6 M& X' a. Q/ I# f
if (negocios_autorizados == false) // are we outside the trading window?
4 P: c7 u0 I  w, }2 E# _return ;
- L: e8 ?4 n! R3 i0 [//--- We are in the trading window, try to open a new position!
' G" D3 V+ L" [5 }7 hint sorteio = MathRand();
: N% Y" O& r6 Y9 Y$ z& P//--- Entry rule 1.1
: t' l# i0 o: |- Jif(sorteio == 0 || sorteio == 32767)9 T( u# h! Y* |6 b
return ;; ]! t2 L; O- z
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
- b& L5 V2 a% Y2 |{
/ U4 c0 D: K0 {9 Enegocios.Buy(info.LotsMin(), _Symbol);% N3 E7 o$ {' y
}
: f" p' I: p1 h' o1 [9 Kelse // Draw rule 1.3 -- odd number - Sell
, x6 F* |) K! e1 \{* k! t9 Q$ v8 U: h' x9 Y( b$ ?- o
negocios.Sell(info.LotsMin(), _Symbol);4 k" J5 ~# p, G' r& K
}- s1 m6 b" n' q: ]6 {4 r& A1 _
}
) g$ ?8 w5 f  H//--- Check if we have a new candlestick...; ~/ M1 M" W  C: d" R- R
bool tem_vela_nova(const MqlRates &rate)* t6 U4 c. X- }' c: _0 ?: M9 U
{
4 ^/ t( y1 k+ d{. l$ t' ~2 M6 O4 {( r! ]
ret = true;
* }& e$ W; ]' v% C( ]" }3 aclose_positions = false;4 d" _' E2 `/ A8 Y: Z
}
! u! |$ u- K, O  jelse& w. g* O) v! R8 W2 U% t$ X' q
{
5 E1 e3 F4 |0 s: d- z5 Uif(mdt.hour == 16)
& q% V( q& \# H! F% g& ?; `close_positions = (mdt.min >= 30);
7 [$ d/ Y3 y4 p" q( `! r1 i/ a2 Z}- X8 d8 ~4 x0 @2 D7 v+ u
}
# `! m; O& J  q% @* J$ h7 breturn ret;3 _' }- e7 ^2 f2 c2 [
}
1 N' f8 ~8 e" ~' F4 w& d4 @3 b& }//---
) R- J+ m, f& C4 nbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])5 Q, e* f+ w, Z
{
; ^8 C! `( t% ?7 u) hif(PositionsTotal()) // Is there a position?
! l' V" W4 ?9 J{
1 W# e1 T& H2 b) k' Hdouble offset[1] = { 0 };
6 L/ f( i0 O4 N2 j8 b- Xif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 Y1 f6 b( N1 U- E4 `( b& }&& PositionSelect(_Symbol))  // Select the existing position!0 w' K# b4 ]) E
{( [- z9 ^$ ]  h( b
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
4 C1 l# c; @$ i# ^) T. Idouble SL = PositionGetDouble(POSITION_SL);
& @& l( t, h0 A! e- S; bdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));2 V$ c; c* P# {2 r2 _6 a
if(tipo == POSITION_TYPE_BUY)
5 S. C: e$ \$ N& V) ]{
8 I, x% v; j: e5 R' K/ T- wif (cotacoes[1].high > cotacoes[0].high)' D6 c/ j3 Y6 G
{
# {* e' O7 e7 q% b& {8 l  N1 h/ o; @double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 |+ n/ E- }8 k, h$ v
info.NormalizePrice(sl);5 W( ]7 w( ]) o$ S
if (sl > SL)
2 u4 M8 h+ J: M& B& l9 _( a: S{$ G8 K9 L9 U- P4 S7 ~
negocios.PositionModify(_Symbol, sl, TP);! k  [- l. h  u
}2 Z) |( Y: Q5 j/ e! N( e) W! w
}
! z0 h' @3 n% ~" P0 L0 R}9 e2 V7 x( ?2 ^5 O- D* Y
else // tipo == POSITION_TYPE_SELL4 k$ Z- p; _1 z. X0 f
{
# I) V. X/ t- \8 p* \* fif (cotacoes[1].low < cotacoes[0].low)$ x; d& C$ [" j7 E) R& ]7 l
{' E7 L# |* e* R5 t- V
return true;6 o$ O; c& g3 y/ ]2 B6 W! ]
}7 l6 q& W0 o$ r* v3 o( g8 v, _
// there was no position
, Z) [$ ]8 z' b8 J+ O( u# H  areturn false;
$ w0 b' b- ?9 K; w}
  C% b+ G+ L, Q- p( Y3 D; L我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 {/ a2 i% |# @
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-15 19:00 , Processed in 0.600009 second(s), 38 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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