私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA: T. ^% i# N# g+ _% y( p
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& Z3 R/ B$ O( r, [8 K- d# u为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 M* o2 r1 ]! U9 L5 z
以下是制定这些规则的代码。
* c( A8 ]1 q  b  u0 w8 B$ {//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 k" F" s3 b/ ?1 \  F3 fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
* F3 U. u: x+ e1 |8 Gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 f! N# d9 \$ C# t% X: }( n- W
//--- Define a variable that indicates that we have a deal.... w# Q3 k5 }9 C0 h& m, I% Z
bool tem_tick = false;
# s6 F% D1 `" a7 I0 V" e' d9 E/ `//--- An auxiliary variable for opening a position5 W; k5 F0 f) a% i# E$ S0 {$ [, h
#include<Trade/Trade.mqh>
4 u1 K1 F0 I4 P9 ]8 ?#include<Trade/SymbolInfo.mqh>
0 C# v3 a* V8 S3 zCTrade negocios;' m. E4 E0 j$ l+ q( c
CSymbolInfo info;
0 ?& }9 b8 v2 K- m$ P//--- Define in OnInit() the use of the timer every second
+ Q) g/ U" ?! E( G3 c0 G' }) w//--- and start CTrade/ y+ L! o& V  g& q; Z; K( m
int OnInit(). w, S, }# o, A* Q
{* J0 W" S- ]; E; S
//--- Set the fill type to keep a pending order) ]; x6 X3 y7 }" W3 f
//--- until it is fully filled
8 V8 F" \- A$ P( r" L7 Enegocios.SetTypeFilling(ORDER_FILLING_RETURN);
) H# E" r1 v7 o( v2 \7 H//--- Leave the fixed deviation at it is not used on B3 exchange
) ~0 Y) k4 z* I( rnegocios.SetDeviationInPoints(5);
2 y+ Y! C' Y# u( M+ G" \: j8 c//--- Define the symbol in CSymbolInfo...
0 H5 T5 [( G. ~) Q0 Tinfo.Name(_Symbol);2 e  P1 k+ ^2 x8 ^- N1 A' j% {
//--- Set the timer...
% s, a, N- B! s% pEventSetTimer(1);
; T( ]0 `- p4 }# f1 q# `//--- Set the base of the random number to have equal tests...0 J1 [3 f# m  E0 }9 s: |" q
MathSrand(0xDEAD);
+ J& _! r7 }8 z! X3 creturn(INIT_SUCCEEDED);7 [" o! j+ Y$ y8 C/ O3 H7 g
}! l+ K2 H8 g# d
//--- Since we set a timer, we need to destroy it in OnDeInit().
" ?9 _5 n. x9 d" K2 X: j( ^! @void OnDeinit(const int reason)
. h5 @" t9 a' I) P4 @4 d! z{2 {8 ]2 H0 P. D! o9 k3 F
EventKillTimer();. T* h3 c& q. w9 k3 X
}
) S9 e  U0 D) g4 H9 S: t1 c/ x//--- The OnTick function only informs us that we have a new deal
! N8 O2 D+ n, v& I( X# u) O! ivoid OnTick()
( @; o  q' B- z" y5 _{6 k& ~, O1 \7 ^) e) D/ x$ F9 J
tem_tick = true;
" w) J" t- M5 ^! `5 S}
  c) P) l7 T& U//+------------------------------------------------------------------+
! l" B9 f# B1 F" v7 R) d' t% g//| Expert Advisor main function                                     |
+ |& q- C3 H8 A4 V, S! G7 w. t//+------------------------------------------------------------------+; `3 [& i5 k7 K# E7 }; ^% Q( C
void OnTimer()
" i5 ^! K. v% P% K5 I{
2 I8 R$ v. }7 S' i) uMqlRates cotacao[];
: x6 I3 P( q5 Y  A! c* Xreturn ;
1 X. \; W2 X: Z/ a# ]& t7 Dif (negocios_autorizados == false) // are we outside the trading window?$ v" |0 S- A; N% z
return ;
- q) x( z* I8 F, F7 q$ ~8 O" W//--- We are in the trading window, try to open a new position!1 g0 t7 o; ~- w. W/ w; C& \
int sorteio = MathRand();. w) d# q9 S/ n+ J
//--- Entry rule 1.1
* [- F& g+ m- M# }# l: E# hif(sorteio == 0 || sorteio == 32767)  r6 x' Q2 ]" B, C
return ;6 c/ `$ N( `& k5 Q4 u
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
! D& J9 V& R% Q1 S! ~9 D$ E{0 U2 |7 k7 q/ E! v9 ]- ^
negocios.Buy(info.LotsMin(), _Symbol);0 B8 B' j9 Z+ d" H0 p  f
}4 u. y8 h# t: ~" k4 X
else // Draw rule 1.3 -- odd number - Sell
( _5 }, c- H  L{) R1 E3 C) c; H6 {8 P5 b3 w, ]9 V
negocios.Sell(info.LotsMin(), _Symbol);
+ G2 Z) [! p  i$ Q}
( o' a1 z; n' [+ T/ e}4 N# u/ l! j& O  E
//--- Check if we have a new candlestick...9 X& H' x3 a1 ^* Y7 }; _9 }" P1 @
bool tem_vela_nova(const MqlRates &rate), L) J9 s! L. x4 ^3 P
{3 W3 |$ [  s7 ]' t, G* O- d
{3 T% O7 w/ u; \0 i; _$ v9 Y
ret = true;7 I0 c5 V. i$ R: _7 R% D# {& O* W# V
close_positions = false;
5 e" g/ H0 ^1 j# b}2 @4 g! Y9 [& U- k- v; o
else
+ Z! e. _) H  E5 U9 {4 i{
8 a; F3 `; h+ y% x  g/ E5 Hif(mdt.hour == 16)
  w9 v7 v9 K$ W5 o' Gclose_positions = (mdt.min >= 30);
0 D: B8 U0 n5 F2 R# c}- }% \- F5 r- N2 |1 g
}
6 h7 A6 W+ t0 R; ]3 F3 N, s, wreturn ret;- @; w% M8 H% ^, i$ E5 u$ M
}
# X7 `( l6 o2 s3 b1 U) W//---
% B* }5 U6 x* N: b" u, l3 wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
4 ]# N9 {! q- r5 s! o' R) V! N{7 i1 p' d4 T1 E5 u/ [7 N/ t
if(PositionsTotal()) // Is there a position?
8 b$ l. V5 H/ C! m- i" p1 l1 ~{
$ w8 K5 ?$ `# j( N2 E. Fdouble offset[1] = { 0 };, v, q1 Z0 k6 [! N! o9 Z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 k8 G4 i& }: T0 R
&& PositionSelect(_Symbol))  // Select the existing position!
/ M6 J! j5 P) s{
, R' `# J: V% ~3 TENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);# Y. O7 L2 X6 P7 D8 H2 o+ v
double SL = PositionGetDouble(POSITION_SL);
- }" U4 z+ I0 ~double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- v( X5 [9 a$ [, o) i/ J( z
if(tipo == POSITION_TYPE_BUY)
2 Y& ]9 g' v3 N4 Z, y" ]{
. I7 g. Y, C- i; Lif (cotacoes[1].high > cotacoes[0].high)
5 n0 X8 K( G) F- E# T: T{
+ m) t' T3 P& m; S7 j. gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ m3 Q0 S0 M7 y$ Minfo.NormalizePrice(sl);+ G2 F# P* M  Y2 j  @7 \8 b8 w
if (sl > SL)
" O9 y- U, ^: i, x# R" e5 X3 Z{, Z7 Z3 p$ \+ T0 h: K. B
negocios.PositionModify(_Symbol, sl, TP);) H+ I9 ]0 f& r
}9 Y( e& j1 \" M/ o6 }1 S) Z; e+ A
}5 }  \5 W1 |( c. r+ a1 [: k; R/ _
}
" F1 I$ s" D( Q4 h% D+ z% Uelse // tipo == POSITION_TYPE_SELL) j  r3 u5 h7 S5 C' Y& K
{( d0 Q; G+ ^8 q! D# x
if (cotacoes[1].low < cotacoes[0].low)
; X2 Q4 C8 Z0 H. J+ Z$ v{
5 Q; P6 Q" h; n8 U0 f+ H9 q0 Vreturn true;
2 ?' z$ _) [6 B6 Q) ]0 l/ ?! R$ O}
( X/ _! x$ b3 N// there was no position
" \4 C2 A- Z/ k) {return false;  Y  }1 g5 U# C& m7 t
}
* B* v: }1 L/ q  L. {* D' e我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。4 y4 ~4 l1 ]& c" \, t$ m' c
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 19:57 , Processed in 2.855654 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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