私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA5 z; H9 Z4 ^2 }% o- S( r9 o
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。( s( a/ F( T- O% e1 X
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。1 W6 E1 f7 ^1 s
以下是制定这些规则的代码。! |9 U: |2 f. _7 }' r* V( E' ~
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 l9 a- c1 g4 }' a5 M  m+ nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% |5 j/ g$ L  i! {3 zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& V; K# v) z, {//--- Define a variable that indicates that we have a deal...5 ?4 g; i( O- X  [' _! m
bool tem_tick = false;; @/ y# M3 F* V! A
//--- An auxiliary variable for opening a position" i' }9 s, ?; C3 O8 H
#include<Trade/Trade.mqh>8 I; C. v8 |9 h8 Q' Z: c, A$ z
#include<Trade/SymbolInfo.mqh>2 g( i7 Z  e9 O' O
CTrade negocios;
" h. G: v4 B8 N5 H. A4 B6 ZCSymbolInfo info;
) g# N# F$ [7 g& m) q//--- Define in OnInit() the use of the timer every second
) R+ {& U8 X. Y5 v- ]2 \" G0 O//--- and start CTrade
6 {5 Y5 S: r; ?, u: Z1 Pint OnInit()+ c% K: M4 x4 O$ ^+ d
{
; C3 n( W6 f+ t2 T4 c2 L//--- Set the fill type to keep a pending order% A9 `: @" y+ F1 h$ a
//--- until it is fully filled
* a0 q+ ?* w/ r3 B" E3 Qnegocios.SetTypeFilling(ORDER_FILLING_RETURN);* S' @: J7 {+ B( B" r8 X; m7 k
//--- Leave the fixed deviation at it is not used on B3 exchange
; o; L; @3 ?6 k. Jnegocios.SetDeviationInPoints(5);- \$ y/ [" O% V% _
//--- Define the symbol in CSymbolInfo...: @5 G+ _: n# b2 [+ n1 l
info.Name(_Symbol);* a5 ^* X1 S. v2 h
//--- Set the timer...
2 H3 Q9 j8 |2 b( S/ U- o) JEventSetTimer(1);1 {9 I# s. C6 ^3 @7 E% N
//--- Set the base of the random number to have equal tests...
- C1 m/ g7 }& o/ n- AMathSrand(0xDEAD);
# c6 A1 a! ~; Z3 L2 F6 m. f; Jreturn(INIT_SUCCEEDED);
6 L  c+ f! r& Y+ B' B}0 V( d: ?* x* X! h: w. o/ d. w, C
//--- Since we set a timer, we need to destroy it in OnDeInit().9 T5 p5 u2 |$ Z6 q1 T
void OnDeinit(const int reason)
2 t9 s1 U# [- s/ l. }{- i; P( H! j7 l% X
EventKillTimer();; L+ u4 `) S, c/ H( K& e% a$ r+ g6 R
}0 [  R9 j# J4 q! `/ E2 k
//--- The OnTick function only informs us that we have a new deal+ a2 }3 |- b8 [9 F$ J7 Z; Y' V5 x
void OnTick()
" }7 G/ ]! L$ B, B6 R{7 d8 f% J) b, z' b
tem_tick = true;! }, k: W& N- ~8 G2 ]( k, g; l
}
4 u& K; T6 L0 y5 C+ ]- c6 k//+------------------------------------------------------------------+# I  @3 X" b, x  v- \8 v
//| Expert Advisor main function                                     |/ t' d) F# |4 n' I* j& X3 W  e
//+------------------------------------------------------------------+8 Y# A5 L' ^+ F( H9 ^
void OnTimer()9 |; g) v9 ?8 ?" }) v9 p' R! y
{8 D( r) @& x9 C2 P
MqlRates cotacao[];! B$ i6 E% Y2 j9 y$ p% `
return ;
0 z$ _& [  y6 w+ T% fif (negocios_autorizados == false) // are we outside the trading window?2 n3 ~6 `9 v2 S7 J! d
return ;
# Q! m/ l. C: q/ ^  b//--- We are in the trading window, try to open a new position!
4 r: q: g- |& r% \! Y! zint sorteio = MathRand();/ i2 [' \! ]" K7 U5 y4 f
//--- Entry rule 1.1
! }) ?7 |3 R) _- _! }. T) Y) \1 t% Kif(sorteio == 0 || sorteio == 32767)7 c( G4 I! \! w1 r! k. R
return ;9 J# G7 ]# i3 F: g2 u9 N
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy; j: e/ r5 c9 I( X8 h
{6 c7 B. u; ]3 A' D2 I. Y
negocios.Buy(info.LotsMin(), _Symbol);
+ L' z5 X6 c  ?% A9 B}
: J. S6 N4 p! q. W* u, nelse // Draw rule 1.3 -- odd number - Sell
4 _  M# ]7 X7 o# P5 n{
5 s$ n2 A6 Q( K3 H. r5 \negocios.Sell(info.LotsMin(), _Symbol);
2 _7 z( C4 l0 n. W2 V& R- D0 h}0 d9 t  g8 p+ u9 b  @7 q
}9 k! t5 K2 [, k+ o5 ]. E" t
//--- Check if we have a new candlestick...
. q8 [( E- i; e% ]! n. L5 Fbool tem_vela_nova(const MqlRates &rate)( j8 H. z9 e9 D/ l
{6 H0 [' {6 I" ]% U7 g* X
{% Z" i5 x. E( \0 S; a
ret = true;
) l! K, u5 f0 [2 Z# Z% t: M- Y* |0 d, Uclose_positions = false;& x* y& k$ ?+ W! ?: a) \" R6 _5 M
}& M2 |, q6 E  i( H5 t
else; U3 C7 B% q% |/ Y" v' f- j! E
{8 A2 G* f! M. l: C2 t# M
if(mdt.hour == 16)
7 R5 \/ t) @* N( V1 Wclose_positions = (mdt.min >= 30);
3 m7 _' c  _' s2 E}( T' }4 [; P7 j
}3 h! P! L! X, r$ |" e
return ret;
  t& i9 q  G: N# v}
' D3 g; b. g: H7 @( Y6 D. {//---
, s' b: d1 y% b+ B6 k$ fbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% q5 Q( N- q4 W/ A6 J( x! q
{
' N5 Y" x7 @. B; F6 o# v4 K8 rif(PositionsTotal()) // Is there a position?& }  {' D5 W# c& a0 e/ w* F/ a5 |2 e
{+ W, M8 i7 `9 y$ O$ M
double offset[1] = { 0 };
: ~9 v& A; _* y0 ]2 Oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# u) ^5 x* u9 w3 U7 k& B" F/ S
&& PositionSelect(_Symbol))  // Select the existing position!  B, f+ G: z+ E* m
{5 }5 ~7 B" G1 w6 P1 z
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
) {3 A# w4 R. L, g  E2 [) G; p# Udouble SL = PositionGetDouble(POSITION_SL);! N$ z% |$ ~' [4 A8 R
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
6 `9 l* f: X4 Y7 rif(tipo == POSITION_TYPE_BUY)1 G  D$ H" a6 w( S% m+ h! a4 o
{9 @+ l4 U" G. x$ V  I
if (cotacoes[1].high > cotacoes[0].high)" c4 g' Y; D" u8 }
{
0 S: s9 d$ W8 D0 f5 c, W. idouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% m' k( P8 {/ z$ D/ }* Q7 pinfo.NormalizePrice(sl);
9 G* f' u1 R5 q8 ?8 T/ m+ iif (sl > SL)9 Z" E( i4 w( m5 w9 u- n
{
# C* u0 r+ ?, g/ h& S% T6 Snegocios.PositionModify(_Symbol, sl, TP);
) Q2 W# ?2 _5 r) d  e}) _" T7 f/ R3 [4 J
}% p* `- M# ~& P2 m$ h. {4 c0 W! E5 w
}+ |7 p% }1 B$ n2 w+ m/ P6 S
else // tipo == POSITION_TYPE_SELL
  {7 m2 @/ ~! g9 B$ E. U9 o{
5 M4 l6 O, C  j1 G8 yif (cotacoes[1].low < cotacoes[0].low)7 p6 o1 f  U& C$ a6 ~, Z" l
{
9 |# A- K: Q( S. ~; Wreturn true;5 }; I/ z+ i' p  V
}+ u* L/ C. g+ K: \+ D
// there was no position6 k* l8 s6 D  K* w
return false;
/ L' A: J4 g2 h' ~" }}4 x/ H# K. |! m- I, d; I2 z+ j. p( O
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。4 p6 k/ F9 k6 v
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-22 16:13 , Processed in 0.431649 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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