私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- R; D! I4 ?/ |% Y+ z2 Z
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
- k9 X1 [. c. B/ q  x" r: f为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。/ a: a6 X* h( v6 f6 [  r# \
以下是制定这些规则的代码。
! c7 Y, D- @, Y; Y1 {/ `//--- Indicator ATR(1) with EMA(8) used for the stop level...' r! {: ?$ @" R! w" |  e: z  w
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);: l1 M0 v7 Q7 W
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 C9 \! U. M/ _: a  ~2 L0 K% u; d//--- Define a variable that indicates that we have a deal...
, B4 E( H( a- \bool tem_tick = false;3 z/ x# x' [: S! M/ d% g% L% P. l
//--- An auxiliary variable for opening a position
, V9 u4 p$ W  f#include<Trade/Trade.mqh>
1 I- \8 o8 ?  V% r#include<Trade/SymbolInfo.mqh>; C1 a( q+ d6 e) S8 K
CTrade negocios;0 J& i) A, A2 r0 t5 V9 t
CSymbolInfo info;
$ ?! W% W$ m3 v4 ?- l3 t8 u  l//--- Define in OnInit() the use of the timer every second; T2 I; r. u* Q
//--- and start CTrade
0 W7 Z$ Y( @) d8 Xint OnInit()) W  l( h+ K* S* X
{/ U! ]9 z+ V" S7 w$ h" O% i
//--- Set the fill type to keep a pending order/ ]  l" r9 h# Q' _) }7 K4 X$ M
//--- until it is fully filled) k2 F/ c4 |& z6 B3 k
negocios.SetTypeFilling(ORDER_FILLING_RETURN);, U, k$ o4 l) c- Z3 x
//--- Leave the fixed deviation at it is not used on B3 exchange
, T0 O' a& r! C: ?, h+ |3 ~negocios.SetDeviationInPoints(5);$ H* W) z- v. S
//--- Define the symbol in CSymbolInfo...
8 Y- I3 O9 Y) a7 A0 Vinfo.Name(_Symbol);
, ]: K, V6 [$ O. C+ w% G/ Y2 L//--- Set the timer...
% l* Y: e& [( e6 \3 B1 NEventSetTimer(1);$ p2 E3 P- N7 F0 s7 v. p# Q  G& {
//--- Set the base of the random number to have equal tests...
6 o. L; s5 X) g! e; L8 dMathSrand(0xDEAD);
( Z8 c  q: n1 preturn(INIT_SUCCEEDED);
) g" N! w+ u* x5 ]3 X9 y" b  W}+ Y" B* I' ?7 m4 b! L
//--- Since we set a timer, we need to destroy it in OnDeInit().
8 r, ?. G/ X( |3 l$ _7 Y2 k. A; Rvoid OnDeinit(const int reason)( u, g; ?6 Q3 I0 G7 \; _
{0 {, j5 `0 v$ ^5 b% J5 z+ Y# f
EventKillTimer();
8 s: }, w! t" C' E3 G: W}
7 J& Y- b4 J0 u! x//--- The OnTick function only informs us that we have a new deal" D  p6 k1 z+ t1 b, o+ j- g$ m
void OnTick()
& F8 |( D- n) s' b, q{
9 r; \8 \8 Z' `: L0 Ttem_tick = true;
/ l3 B) C! S# I0 I! U}
) P0 M/ ~  y4 D; n  K# ?//+------------------------------------------------------------------+
. b- \/ N- f( ]- j//| Expert Advisor main function                                     |: X* ?0 e" s+ Q* s, Y
//+------------------------------------------------------------------+
% X. O1 \! Y( r  bvoid OnTimer()
3 ]2 y$ E& {2 Y' |3 m{0 T, A% H" n/ n& y/ L0 m
MqlRates cotacao[];8 k7 C# U& F* T8 v
return ;
' d8 O8 N: \3 R! p) F' bif (negocios_autorizados == false) // are we outside the trading window?
% ^! ^7 m: h$ B: |9 j" r. Dreturn ;- A; |( _, ?: B. u
//--- We are in the trading window, try to open a new position!6 g5 P1 {: Z% F) o5 I/ R0 O
int sorteio = MathRand();& v% c/ Z% h; z( k4 R! w3 d
//--- Entry rule 1.10 ?9 m' _' s, w" ?3 h( ^: w! ~' U' P
if(sorteio == 0 || sorteio == 32767)/ a( J0 b$ V. I* X0 q' J
return ;4 k. A! O4 Z9 k2 A
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& f$ ^9 q( r1 [$ p# ~, n( c4 F{
9 @( P3 S7 j9 Y# v/ E/ V. H2 |# @negocios.Buy(info.LotsMin(), _Symbol);7 D2 y6 N1 J! C+ s( {- I
}
) O) L8 h" p% Kelse // Draw rule 1.3 -- odd number - Sell( G$ q1 v# I; q4 ], S; w0 S
{& }2 \; w( C$ x, n& J
negocios.Sell(info.LotsMin(), _Symbol);( q/ h0 D* `- b+ ]
}
  s3 _# f6 S, J7 R4 |7 `* s}
7 e! r! P& \) z& J- E//--- Check if we have a new candlestick...
, ?. C/ K& R; k4 j4 W% e4 Gbool tem_vela_nova(const MqlRates &rate)$ `% V% _* ]4 V% _! R
{
/ g; o5 ]5 c3 I% G{: o: N7 {+ m+ D% X9 e. I5 [) C
ret = true;
% b, J. |: V9 o$ s% tclose_positions = false;
, f8 H7 X( W* u  j% C}+ `4 F( C( Q; @* t
else' x' {( Q4 t; f& a/ T
{
8 y' v6 H. q  {) ~if(mdt.hour == 16)
5 K# `: K; Q$ B0 }close_positions = (mdt.min >= 30);
3 X$ _, y4 B; d" B  k2 |1 M  ^}' Z0 I- _. L* |# ^: G' P$ C
}! w% B8 }/ D2 C! W& m: K" b9 N
return ret;! |% u8 x) f/ r
}
  L: r) E3 Q3 O; ?//---
1 ~' A8 F8 n0 t$ ~/ R# x: rbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! w7 J- N8 v: k, r6 y( a# X" ^/ d4 K{
) U; I1 i4 m: L& r$ f# aif(PositionsTotal()) // Is there a position?! z  e/ w3 [7 ~7 J; l
{
. t+ z/ Q; |* R4 U! Xdouble offset[1] = { 0 };+ u7 `& r/ B/ o( _8 y
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' S! B: g1 ^+ k6 I2 [+ [
&& PositionSelect(_Symbol))  // Select the existing position!" u+ S3 i* V' a) ]7 q
{% t0 Z% w5 d% Y/ U7 U5 [% [
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);6 U5 s8 R" @/ o( Y0 ]
double SL = PositionGetDouble(POSITION_SL);
" P) _- L* }* g. H# Y- X6 Tdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));( ]7 E2 K0 i4 l; V# q# W
if(tipo == POSITION_TYPE_BUY)
9 H. [1 ]; a1 I5 f{
4 e5 V# b& w( cif (cotacoes[1].high > cotacoes[0].high)0 e. Y- Z6 H2 ?. ~; M
{
- j& I' A, @9 e5 S4 s! o$ _; ndouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];9 H# N* C' ~" N
info.NormalizePrice(sl);3 W' I0 u& O1 V# U1 c1 K2 W2 n8 I3 N, u
if (sl > SL)
  b- T% S6 G9 d# U0 z+ C3 R. u& ~{
9 L6 a7 X0 R' z6 J! ]negocios.PositionModify(_Symbol, sl, TP);, f  i8 \1 Z/ U/ X
}
0 @- t1 Z* W3 D, w- \5 s}
6 G' R: s5 l7 H( j& w( F0 |- G, Y}1 k$ \# g$ d& h* f" _! `4 ~7 T
else // tipo == POSITION_TYPE_SELL# ]) j$ B5 b+ }, e$ w- O" \7 f
{
- Q, Z! c3 X1 C/ f2 Y# A( Zif (cotacoes[1].low < cotacoes[0].low)7 R) @( X. R; |. L9 l
{" l1 b' [1 \5 i
return true;
9 Z* v! Y- H# y  k7 l}9 S8 X8 W$ z- z: ~
// there was no position
% W# j/ w5 l, @: y: j- Vreturn false;
6 k; w0 L6 l  Q% T8 l7 P2 M; [0 _}0 w1 R. N  `% ?! z' P
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& w2 y! ]. E: c/ T% W& v; m/ {8 G到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-27 14:04 , Processed in 0.751123 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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