私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA5 G8 ^9 t4 d& Y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- g, {& _* B- r# f0 h, F0 p为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. e, e/ s# |- ]5 y1 p' b: `( o
以下是制定这些规则的代码。
. I" T) A1 r8 m% e) g//--- Indicator ATR(1) with EMA(8) used for the stop level..., |# J) d0 A2 `+ r
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 Y. y9 \( L8 r3 s' Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);, }# O3 Q, _, h
//--- Define a variable that indicates that we have a deal...& G& F# D  ?% w, R. E
bool tem_tick = false;, g: T( O) Z- F, `. M( g  I& l
//--- An auxiliary variable for opening a position
* {: ^/ f$ h) h& \; H8 j#include<Trade/Trade.mqh>
7 `: r4 G, v0 W. V#include<Trade/SymbolInfo.mqh>: b8 n6 |( _/ g9 C: j/ v
CTrade negocios;
3 K$ y& @; ~& F* u* s& N, R$ GCSymbolInfo info;
) p2 A5 T& ]. k# \( N//--- Define in OnInit() the use of the timer every second. K7 K7 c' @7 S% b  {, e
//--- and start CTrade& y+ j5 U* x) `$ w3 R
int OnInit()$ J* `2 V3 I6 g
{
  e# A* |# f, G( V. J, N//--- Set the fill type to keep a pending order
+ S# }0 d2 Q# G//--- until it is fully filled. N4 _' [2 v* X; Y$ e) V: \5 A
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
9 Z6 F1 Y( F" d- n% G* V1 t//--- Leave the fixed deviation at it is not used on B3 exchange
2 C" b: I1 T/ V2 l1 ^. gnegocios.SetDeviationInPoints(5);
& a6 A& P1 X* d6 Y/ @5 d' `//--- Define the symbol in CSymbolInfo...
2 n# x2 V' M" V" B' |% Cinfo.Name(_Symbol);
- Y$ j6 |5 T% m8 W8 r//--- Set the timer...% }  M$ l# X$ u0 K7 F! V$ a4 F' B
EventSetTimer(1);
! ]0 K' I) b0 Y( d6 G0 |; k8 O//--- Set the base of the random number to have equal tests...
, E( s' J' f0 PMathSrand(0xDEAD);
0 ?; }5 ?8 K8 D) L" V) creturn(INIT_SUCCEEDED);. Z; w5 z6 U7 \
}5 f7 w( ^+ Z: T1 `: s$ n( C6 B
//--- Since we set a timer, we need to destroy it in OnDeInit().
9 o0 R" x6 R5 K( [" W! vvoid OnDeinit(const int reason)
+ Q+ I) o6 [* I9 L- Y$ r( O{
+ x& j3 p0 r; ]9 b# l& N, REventKillTimer();: A! [) R5 z% Q& Q0 g: o
}, W. V2 f2 b7 A+ Q9 K  E  r0 O
//--- The OnTick function only informs us that we have a new deal
3 P2 f% |8 m! `, w! M# i  gvoid OnTick()* D6 M- R6 K4 X3 R4 z3 K
{) [" e$ u2 v  B* e
tem_tick = true;
, e  y: I* @/ m: {}8 C, Q! s- w! E& [& e0 B, H
//+------------------------------------------------------------------+: |6 q$ N( d! ^" o& p& N
//| Expert Advisor main function                                     |+ H/ t4 B" N) B! t$ a/ z' k9 p
//+------------------------------------------------------------------+# ^! ^$ z2 b( D# q
void OnTimer()" L5 M8 d2 [$ [% W, S; ?
{, P# E5 T: o9 |: K3 n# H7 L
MqlRates cotacao[];) l. W+ W  x  s8 s6 H7 n) j* ^
return ;& k3 U$ r, }5 x& b
if (negocios_autorizados == false) // are we outside the trading window?
5 {, i8 @8 Q9 {return ;
6 _' p0 ]' B4 y//--- We are in the trading window, try to open a new position!
6 T$ W6 p9 H5 U0 s* y# t) Uint sorteio = MathRand();
5 ^2 H  {3 G: ?& C8 I//--- Entry rule 1.1
8 X$ Z* f8 Q4 C$ M0 Fif(sorteio == 0 || sorteio == 32767)0 B: W2 p4 [* g2 H# R( Y4 Q5 y: ^
return ;
8 J5 f  c/ m, P+ ~% J7 Iif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. {4 E2 S2 E) W
{
5 i  W6 U8 v1 q2 T% r& Q4 A* `negocios.Buy(info.LotsMin(), _Symbol);% A8 a! V5 }. N
}
0 q# u0 [0 y+ k- ^9 b2 ]& `else // Draw rule 1.3 -- odd number - Sell
* y* m- m- d, q6 O{; s! |8 s8 g% e9 ]& `4 \2 S* Z
negocios.Sell(info.LotsMin(), _Symbol);
' a! o# i, s: \  a}4 \6 |: w0 n) p! e3 x- J' X& I. y
}
+ t" l  h7 s, D8 B# L//--- Check if we have a new candlestick...: J. D) _2 Y7 y8 e
bool tem_vela_nova(const MqlRates &rate), W) `) o5 R9 E% \3 u* \, H6 J
{
( o3 S+ a) H; [{$ k& A/ ]. ~' E, s0 }7 [2 @
ret = true;+ Y9 `9 N- J8 [$ B. j: `; S
close_positions = false;/ W7 n- u- r6 k: M3 k$ s' A
}, j# R2 w5 X6 `/ D1 K, [5 l3 e
else
0 [/ v  V" p- w! ]& I* f/ O& R{8 p) _+ e4 @9 H' o/ U- g2 C
if(mdt.hour == 16)0 k8 Y0 N$ I2 y3 X3 a6 g6 ~
close_positions = (mdt.min >= 30);. E$ _! z0 T9 n+ k5 O8 n, `
}  z% h& g, f9 R: t8 M9 T9 P% S; q
}. l- V7 R* ~- _* ^# g
return ret;
# Y7 i2 @' e2 D) w7 B" E4 W  \}& ]0 ~+ L9 ~4 H9 b8 J" y1 G
//---
. x- |( y  A7 n/ |5 }bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])3 _! e7 U( N- C+ p2 j" I3 T
{0 A, Y8 B- E; B* D7 d3 m) a
if(PositionsTotal()) // Is there a position?
9 a) W; Z8 }5 D( X6 ?{
8 S- V( T$ ?2 ^, R! E4 G% W6 kdouble offset[1] = { 0 };- V1 V+ c$ V0 H$ F3 ^! E) u
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ Y1 A7 J, O0 P5 O- _&& PositionSelect(_Symbol))  // Select the existing position!$ J( ?- C1 X- e
{: k: @6 k! K/ Y' B' ~
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 G5 }7 P" X0 J8 t& Q6 c* ?$ N
double SL = PositionGetDouble(POSITION_SL);
: p! l0 B3 s! D: [, a  \, [1 Jdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ @2 P% y! O/ C- m! Uif(tipo == POSITION_TYPE_BUY)8 d4 F% h4 x+ L4 O) ^* Q
{
8 [: v9 U8 N# K2 l. X) I% M0 iif (cotacoes[1].high > cotacoes[0].high)
5 x& p5 x' {3 R- \{
+ h3 Q) B; N: o+ a  qdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];5 C% v" J$ r& V& ?* `# @
info.NormalizePrice(sl);
+ ^4 B; g9 ~* R* v$ b9 K: `if (sl > SL)+ X: w1 U$ c( N  J
{
0 `! G6 H4 {5 p# n! y4 Q8 f" cnegocios.PositionModify(_Symbol, sl, TP);0 K8 S; V' ]  M" ]: i4 w: n
}
. @5 W% h9 D. E, L0 L% S}
' A0 L: r3 s' E) f! ]: G& `}
% o: `' }8 Q5 xelse // tipo == POSITION_TYPE_SELL) T5 ?8 j$ V7 h, d0 }/ B  _; c
{% g) G. D  A1 A2 ?' z+ L2 r1 Y
if (cotacoes[1].low < cotacoes[0].low), Y/ j9 Q- R" D% |) |6 \# L0 A
{5 I1 g" o6 S2 H9 ?
return true;
- U" y- @; v6 c% s# D4 v9 g}
/ m4 b  X# a* `) ?// there was no position
. d2 c6 h4 P% O$ F+ Treturn false;; N; Z* ^1 |" Y- b. S. W7 _
}; I# j" z6 g3 N6 P0 X0 C6 K* a3 O
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。  U2 Z% p$ s8 U
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-14 21:16 , Processed in 0.951406 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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