私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- f. C  y' g: L2 l1 o0 \
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
2 q- d' |( E! f& N- H为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
4 \$ u/ A- n; o3 k) q以下是制定这些规则的代码。1 |! \( e% J& _( s: Y
//--- Indicator ATR(1) with EMA(8) used for the stop level...
2 q6 n0 q+ ?" S* r( i( p5 rint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);5 f7 J) B5 A$ u9 `1 e% U' z; n" _
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' z2 ^0 G' ^" \- t- m1 Y' T
//--- Define a variable that indicates that we have a deal...& J; J8 I# r* ^0 h  L& T
bool tem_tick = false;
$ Q: s; ^8 ?( L" ~//--- An auxiliary variable for opening a position5 n  t* j$ }, J4 r$ w8 C6 O+ ~
#include<Trade/Trade.mqh>" ~3 \/ e. j* ~( Y% j3 O
#include<Trade/SymbolInfo.mqh>% z1 @+ O. J, k7 \
CTrade negocios;, W/ x+ f1 }7 s: O6 j  h
CSymbolInfo info;8 B, w5 ~; m4 C5 U
//--- Define in OnInit() the use of the timer every second. S8 l2 z! f8 R% O
//--- and start CTrade( I# m8 x% R0 u; \
int OnInit()
3 z# A8 k0 O0 L# h2 S1 R  y{- D/ L( _, m* X0 d
//--- Set the fill type to keep a pending order& j& s. g/ H* O
//--- until it is fully filled  B  d: T0 g  C- `" C2 ^% J: l
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
( T$ V/ Y0 W/ s% W9 L//--- Leave the fixed deviation at it is not used on B3 exchange
3 p" A. r' W8 f+ ^9 P3 C5 rnegocios.SetDeviationInPoints(5);
' z) P- M% _7 U* y//--- Define the symbol in CSymbolInfo...
( D$ k8 O, F- x% ~info.Name(_Symbol);, n) b7 Z. J5 j- I( i
//--- Set the timer...' J6 K5 \/ l: a
EventSetTimer(1);
3 @% }$ X$ O" W! f8 G//--- Set the base of the random number to have equal tests...( H5 \1 U; k- R0 D
MathSrand(0xDEAD);/ ]5 e4 T0 D3 i7 O7 Z) m
return(INIT_SUCCEEDED);0 Z, w. I: w; e  H% ]. p% Y
}
6 x# X( m* W2 }2 ^0 C8 G) y//--- Since we set a timer, we need to destroy it in OnDeInit().
# z9 s# _$ ~  T0 L* J  cvoid OnDeinit(const int reason)7 r9 r' Z; T, x0 t# t# p* ?! }# f
{
. n% |0 w, u) Z+ n$ o% V! _' XEventKillTimer();' ~, h* N! x' R1 I( `; R
}
$ D; z1 f8 \" M& X( [//--- The OnTick function only informs us that we have a new deal/ e; K" I8 \8 a- s
void OnTick()
' P" ?/ Q1 q+ L6 q) b{/ Y  s; B& ]7 M, s+ p
tem_tick = true;. v+ k1 u) z6 m  J% }
}5 u- _0 [% x& b: J/ D1 ~% ?
//+------------------------------------------------------------------+
7 G$ |) U" E2 j) @  T3 y; W//| Expert Advisor main function                                     |2 A- g, A+ B9 B$ I5 i
//+------------------------------------------------------------------+9 u, K# ?6 O; ~$ o
void OnTimer()+ Z( I9 z$ H% T) D( Q' k
{
2 i, v' }3 s& Q( |0 X4 gMqlRates cotacao[];. K8 n' J6 N4 Y/ s
return ;
% O8 W5 z& D8 Y- S' r- s  `if (negocios_autorizados == false) // are we outside the trading window?
! B/ ]' G- i$ r0 z% sreturn ;
1 H+ E3 J5 _% Z6 W. _% `//--- We are in the trading window, try to open a new position!2 u- }7 W. q( }
int sorteio = MathRand();
& c/ S1 I3 `1 o; D( I. m//--- Entry rule 1.1
/ G" T' u# {4 s1 ?! Gif(sorteio == 0 || sorteio == 32767)0 Z* h" I1 e- E3 c0 r# P! C( a
return ;
1 d% R/ W6 ~/ X& dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
+ X9 K0 J0 Y+ t( i* M{$ n. [8 ^( X7 }- {; w8 d
negocios.Buy(info.LotsMin(), _Symbol);5 C) g) Z/ n6 Q+ Z. A- a
}9 I0 e- s2 {, [' f9 l' O
else // Draw rule 1.3 -- odd number - Sell
: q8 R0 S: b9 ?  l{" a* K9 y' q0 H8 l8 x4 D3 X# L
negocios.Sell(info.LotsMin(), _Symbol);8 y+ A3 a% f4 X
}
& ?; g9 _+ j( ]2 ?2 \5 e  \}: |6 n9 Z; E0 v
//--- Check if we have a new candlestick.../ ^9 Y* n( j, s+ N, I$ X( l
bool tem_vela_nova(const MqlRates &rate)
/ z  J: G  P! g/ p{1 z% k& T# x8 \) K- x: ]
{! f5 W) o. D0 K( L) _' ]
ret = true;. `) Q/ w4 d; q5 {% h
close_positions = false;
5 x+ ]2 F: g9 @# H5 t+ Z8 w) U}1 E) r; Q2 G: ^9 D$ u- B4 |
else
$ [. b& q" B- i' v: t5 Z% X- p* x{
5 {5 Z& L+ M/ vif(mdt.hour == 16), D0 T- t0 _6 o: o9 _3 A
close_positions = (mdt.min >= 30);' _, ?6 @. h; w  T
}
9 f' i3 S- t: [2 x# K3 z}
' [0 E" E& q! L% y- u- n" [return ret;/ s( L  Z5 S& t! V, W/ F
}
0 R3 j: t% {4 u* e5 M# Y+ _& P9 J//---
( X6 H* ^' o. `/ \. O) Lbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# J/ J! H0 {4 q" k1 ?
{. Q# r- B* ?# d- B/ |
if(PositionsTotal()) // Is there a position?+ s  b  K+ ^& Z0 t
{1 m9 @$ E1 X; m+ u4 q
double offset[1] = { 0 };
' e4 k  f" _) D! bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( B; ]" q, g. K* a- m&& PositionSelect(_Symbol))  // Select the existing position!# j4 E# C. H- S) h( Z
{
3 R7 i7 v! x. F$ W  D! n- IENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);; d7 c+ N, ~1 d8 r# {5 @5 F2 r
double SL = PositionGetDouble(POSITION_SL);
' r: M* q0 V7 i3 H! D3 T9 [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));& i+ h/ N0 O% y, s2 y
if(tipo == POSITION_TYPE_BUY)8 K! l% O* f. W+ K4 @' _
{
' W6 r- F6 n1 e0 I6 \if (cotacoes[1].high > cotacoes[0].high)2 v. b- K: s  K, H/ F
{
" B& [* }! O: V5 g3 jdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];$ _: g8 ^' z/ a4 J+ F8 L2 p
info.NormalizePrice(sl);
) Q1 U& K  q: H8 o- Mif (sl > SL)2 M9 f: |1 b6 r
{
) c$ @5 M4 @1 n7 U! Q7 Bnegocios.PositionModify(_Symbol, sl, TP);
5 N) S: O) ]( F5 V) k3 m}# u7 I* s, x. e3 d9 I3 Q4 y
}" c$ z: u1 S1 d- y7 q& K
}- U' s' o8 E! D7 H
else // tipo == POSITION_TYPE_SELL+ {" C# R0 I$ n7 H
{
2 G2 @: L8 T3 E2 z1 j) B0 Wif (cotacoes[1].low < cotacoes[0].low)) y% J8 M& Y  M7 U# G! F+ i
{" a% d+ ^. i$ ]' Y
return true;. \% ?7 }. ?2 `/ k1 v6 ^0 l! h6 N
}
% S5 P# e$ v, ^! O$ w6 x* {+ G// there was no position
8 ?4 C; n6 }/ G% l. H( P# M0 ]return false;
3 u& B7 d' M- o}# N0 L3 P- ~% G$ v! e$ R0 _
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 W5 z5 y2 f+ m, h, }
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-15 02:01 , Processed in 0.404597 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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