私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 L: E+ z: U) C. T3 G
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 E2 _: Z6 _2 C3 O9 X! K
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 n# k+ n0 P# l- c9 F* \0 K1 f
以下是制定这些规则的代码。
1 @% o. R2 J  @$ n( t. ^//--- Indicator ATR(1) with EMA(8) used for the stop level...; U% s0 I) `+ H% X. I% x8 M( F9 t
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ q: ]$ i: u* O+ B
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& j( P* n! T; V" m1 N* B//--- Define a variable that indicates that we have a deal...
+ m8 S& E$ g2 _5 Y. S7 ^bool tem_tick = false;# \7 y" c9 Y) j, w3 [+ l: X
//--- An auxiliary variable for opening a position8 N1 X2 s3 }( v4 a* P1 I& E
#include<Trade/Trade.mqh>
* L0 H- g5 ]# n" y#include<Trade/SymbolInfo.mqh>
4 }, T$ f' ~3 f9 h) s5 d( zCTrade negocios;
/ P' e2 G* g- U/ D2 zCSymbolInfo info;- c+ J/ s3 R) c0 D: A+ _, _
//--- Define in OnInit() the use of the timer every second
. H0 f# Z# Q2 l: X3 F6 w" C//--- and start CTrade
; d! T/ L" ^7 y  Pint OnInit()5 S% K- n' T! x0 A! V
{
' n, o3 |6 V4 B//--- Set the fill type to keep a pending order# R. P4 n; e- g+ g2 F& S+ x  H6 H
//--- until it is fully filled8 U. |  a* x8 S4 X/ p( K
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
. `4 g# V4 R! }- J* T//--- Leave the fixed deviation at it is not used on B3 exchange
& F! Q$ |' L* r( }" vnegocios.SetDeviationInPoints(5);
9 |* K  Q+ d/ h//--- Define the symbol in CSymbolInfo...  c6 l' w5 T! a! `  P" E
info.Name(_Symbol);
' M6 ^- V! d# Q0 ]2 K7 y- w) d//--- Set the timer...
5 H2 l; g% n5 c0 `( l5 `% l4 M- fEventSetTimer(1);2 F  }6 h3 }9 Z: ~
//--- Set the base of the random number to have equal tests...
7 V8 y; d: t2 f; }MathSrand(0xDEAD);4 b. P4 F# h, C; y1 E* J
return(INIT_SUCCEEDED);
+ ]' G# m; x6 q! B, H" O}- H0 f; f, r$ B9 i
//--- Since we set a timer, we need to destroy it in OnDeInit().) ~' j& y4 ~; U+ b6 ^
void OnDeinit(const int reason)
2 R+ B" N0 ~/ e- g{
. ]/ s, _9 ?, o. QEventKillTimer();2 s$ a* e* s7 m( _1 E8 M7 O
}  I# H7 z  e( d+ C/ c
//--- The OnTick function only informs us that we have a new deal
1 `1 ]* z+ j  [* Lvoid OnTick()' ?0 {4 |0 F7 A$ L. B1 `5 O
{# C# A& U9 K, `# q
tem_tick = true;
, _6 }0 c1 E6 B7 h}& b1 N1 D5 ]  Y2 Z* h
//+------------------------------------------------------------------+) \: p& _5 J5 Y) B  Y4 N# |
//| Expert Advisor main function                                     |
' |- a5 m) Y. v( ]//+------------------------------------------------------------------+' f/ [( Y1 b% i
void OnTimer()
1 X5 e/ m- E5 P' [6 g$ l{* \) p3 a: w* \
MqlRates cotacao[];
/ P2 v; O2 r# a3 O8 A' Rreturn ;
9 V! q# C2 {+ D) w. Sif (negocios_autorizados == false) // are we outside the trading window?
  N2 \- q8 d6 T( _- vreturn ;2 g2 D$ y) ^+ ]/ E
//--- We are in the trading window, try to open a new position!" B3 X1 P! q7 e0 Y
int sorteio = MathRand();
) P/ u" a/ H" V( P. A4 v, O//--- Entry rule 1.14 k! |7 x- ?; m. Y$ X- X; F
if(sorteio == 0 || sorteio == 32767)
* A: b+ g( k$ F$ areturn ;6 D- v3 P0 f5 y$ a' P6 C" E
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy; q8 K8 _9 q$ r# p+ L
{
% i2 E2 a# ?+ d& l0 ^negocios.Buy(info.LotsMin(), _Symbol);# O5 j: Q# f; s: ~) O/ z1 H
}- `1 d! s$ s5 w, A3 u* A6 C
else // Draw rule 1.3 -- odd number - Sell& Z( R' N7 H+ Y, d
{
% y2 f* e( v0 F* Knegocios.Sell(info.LotsMin(), _Symbol);5 u9 o5 A& A0 K# n, G' [
}
: I$ c7 h2 ~% A& G: R}4 u6 g4 O% n7 f
//--- Check if we have a new candlestick...2 e! P" S. b6 y; e
bool tem_vela_nova(const MqlRates &rate)2 J7 l$ C! r' P; h! A. A7 [
{
7 M6 y4 k9 ^" c! ~$ M{2 b9 P% N0 h1 i1 F( ^; m
ret = true;* \0 ~; E. z. F
close_positions = false;! i8 p; x7 p) x+ O
}
' S+ S1 ~: d6 i: W; C# kelse* j  u- i" [1 N& n0 x6 j
{# y, [( L3 L$ U$ B$ z
if(mdt.hour == 16)
) B6 D  c* H, ]: _0 U" Q4 Sclose_positions = (mdt.min >= 30);
6 Y" t& X+ z* e. `% N}8 d+ i/ @- f: J7 `- G. B1 z0 v4 ^9 O
}8 {+ [/ a; \1 ~( c
return ret;/ c5 ^$ X" i, d- v) K# F3 R% ~' s
}6 ~+ E9 z9 L8 y) {
//---
2 r1 A( ^: `; X; u' X$ }bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! M/ x$ ]  G3 l& |9 V2 C5 q+ i! m{$ e3 o/ X  Q; \; x
if(PositionsTotal()) // Is there a position?+ N" ], V( c/ G% A- [- V) q
{
# Z, P; s2 y! a' d" b& \+ P- |6 u  adouble offset[1] = { 0 };
" b: L  D# e' s! `. l8 oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 N9 @/ e7 f) `3 [$ P  ?/ Z( A
&& PositionSelect(_Symbol))  // Select the existing position!
% ~  R4 O6 V# c4 m" O1 S6 Q8 b{
, z( m% z% {1 A  b' N/ ?# C7 c. CENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
- V( `% C* q9 [; y/ p, xdouble SL = PositionGetDouble(POSITION_SL);: r8 J2 C  L7 W4 j
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 A4 d" ~6 J4 y& g' v# P
if(tipo == POSITION_TYPE_BUY)# d. k$ d2 w$ `1 H7 @, Q4 r
{0 n6 ~! m  L. e. W8 P4 z
if (cotacoes[1].high > cotacoes[0].high)
  \2 @, n; g6 ~' K8 ~{
5 i' ^6 x! j/ mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ j; ]  X: s2 X# e  p% Vinfo.NormalizePrice(sl);
% D. v: [3 |4 p4 v, w* L( mif (sl > SL)& F, N) v! \& O$ w& j% ^
{
2 e5 Z2 z, `" @1 Cnegocios.PositionModify(_Symbol, sl, TP);1 s' a; B) g& B, ^! L% M# Q
}
, V% E. ], b0 w: o}
) K2 A! e& Q" y, _# g9 F4 e}
% H: J8 P" ?2 i( v+ R) a# e- H) n1 H0 ?& velse // tipo == POSITION_TYPE_SELL
) T, ^- J1 C* I4 F0 Q& V1 N4 N{, A1 [9 `' B% |) J' u
if (cotacoes[1].low < cotacoes[0].low)1 S; |- p6 G! Z' B, S; a" F3 K
{. c( M5 F8 s, g7 F/ ]# T
return true;% I2 q9 d% f9 [, H7 H9 F0 s& u
}- I$ [: U" y# ~/ D
// there was no position
) ]# I) \4 m. L8 j' o: Hreturn false;
; L* l& ]1 n/ a}
5 j6 _" ~+ C. H我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 |$ b; j( K  o
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-29 12:22 , Processed in 0.418374 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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