私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* W$ `& J' h( w5 ^3 ]2 o" J* B在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。" h* q: |) ?6 O
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ n1 R/ y3 ^' V5 H: ~1 v/ z: ~以下是制定这些规则的代码。7 z- M8 V! o6 s/ K* Q2 o2 ]7 A
//--- Indicator ATR(1) with EMA(8) used for the stop level...
" R' }$ \4 \6 Q, ^int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);7 _; ]/ ]$ w# E1 G  v4 @
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 l* L4 z1 Y% v% @/ _& b( m" y
//--- Define a variable that indicates that we have a deal...
& S& V; g" X5 G9 P9 t: K  Dbool tem_tick = false;
2 q( ?0 n; y) A, K//--- An auxiliary variable for opening a position6 z! _6 B$ ?! d
#include<Trade/Trade.mqh>
$ v: i) R+ J/ ^1 T#include<Trade/SymbolInfo.mqh>
& b9 l8 w4 B5 X2 a; I% @3 `CTrade negocios;. _: r  u/ G8 b) j2 S2 ~
CSymbolInfo info;/ W& M  R9 u+ x7 D
//--- Define in OnInit() the use of the timer every second
/ t/ M7 f/ ]( s* o, b//--- and start CTrade
$ e& z+ T' p- Gint OnInit(); l- G+ f4 [. }+ v! l5 w1 a: z
{
( u) K" H. `- F3 [: E* ?4 h//--- Set the fill type to keep a pending order8 d5 g8 x: G. w2 h  N0 t$ v0 K
//--- until it is fully filled! @( w/ z( r' c* }6 y! x% y4 z
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 D, [, T! [' L& [% M, Y
//--- Leave the fixed deviation at it is not used on B3 exchange( d; }8 A9 W* a& f9 {, E: S2 K
negocios.SetDeviationInPoints(5);
: K8 [4 |9 ~1 B//--- Define the symbol in CSymbolInfo...
# [& g* x% d9 m4 `/ X+ ?# Winfo.Name(_Symbol);
( c. ]8 b' l( ]0 d, r: ~* W//--- Set the timer...
/ q4 Z. b/ h- U5 dEventSetTimer(1);
) a7 H4 S4 v4 }$ s- O5 ~5 E! Y//--- Set the base of the random number to have equal tests...
9 G8 o8 z8 A( @5 e; K* m; N/ o& [MathSrand(0xDEAD);4 a% N- ~% B8 F& a4 Z5 P
return(INIT_SUCCEEDED);
2 H$ z' O/ x" M; G}# i1 z9 L' G8 I! L* i) v& G
//--- Since we set a timer, we need to destroy it in OnDeInit().
- w+ @: R  h9 evoid OnDeinit(const int reason)
9 V1 x1 L! q0 n{4 m3 ^& U" n6 A% l9 t
EventKillTimer();
$ C7 i' w: d9 ^4 M( c' ~2 m9 U}! b. K5 C% W; ~* i9 O
//--- The OnTick function only informs us that we have a new deal1 x  x* {5 |+ _$ d' ^) f$ r5 a
void OnTick(). ~6 w) w- H* n, f, ]- {
{
( ~; s+ Q9 Z" E. L6 {# N# Ctem_tick = true;- Z8 t- h6 U1 G) W
}
1 k2 ~: f+ t* d5 g- P/ K//+------------------------------------------------------------------+( ?) n" ^5 g! O
//| Expert Advisor main function                                     |
% P4 ^. z9 m8 t0 \: m//+------------------------------------------------------------------+$ g# s7 C- x1 [- `) U
void OnTimer()
2 o  E2 d& n3 L3 N* W1 A7 _9 w{! E$ V* U' n7 y; ^3 m
MqlRates cotacao[];. Q; X2 t. l8 ~# `5 ^) H
return ;$ e  p. @+ u9 |6 \/ T: O$ U0 F
if (negocios_autorizados == false) // are we outside the trading window?5 w. h, O: k5 Q) z
return ;
* U! f: H0 ]' ~8 ?0 }1 I% z9 V; K0 V//--- We are in the trading window, try to open a new position!- K: v$ z" S3 ?& s* I" w* l0 x
int sorteio = MathRand();
0 }8 f4 R7 v( ]! v//--- Entry rule 1.1; S, `8 A, u( ^# t* X' ~1 P
if(sorteio == 0 || sorteio == 32767)
& P: _7 U" I( Preturn ;8 G# I" h2 U$ I7 u5 y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& z6 }! R* a4 _3 S6 B
{
! X# F" b( r& e4 E- |negocios.Buy(info.LotsMin(), _Symbol);2 `+ G  S9 m* Z$ G
}- U: i, a! Z9 I& Y' v
else // Draw rule 1.3 -- odd number - Sell
4 |6 h3 F3 K7 m9 ~9 ^$ L{
# a' F- O8 N! B8 e- j8 M2 Y' R7 |negocios.Sell(info.LotsMin(), _Symbol);: r: s# b& N8 t# h& w
}
$ ?  w$ I7 z  r/ n! C% c}: f/ x5 \+ n* a8 L7 L$ T! L* w
//--- Check if we have a new candlestick...
- Y6 |; [4 K$ W+ k: \# e9 e9 Y5 r0 mbool tem_vela_nova(const MqlRates &rate)) {8 t7 j, n; ?( _9 {
{; J. a& K2 m9 H; q
{7 o% ~7 x2 L; g. S. _; ?# ?
ret = true;
! f' N% F% ?  I7 @8 ~0 @( ?4 e* Fclose_positions = false;4 @* \: d; Q8 O2 x0 C
}
9 K$ q; D( ]0 R* belse$ z7 |' ~( P) D& L* q1 U
{: y+ S4 k6 j+ s' C7 |9 k
if(mdt.hour == 16)
$ P& N+ ~0 V" T7 f, |4 Nclose_positions = (mdt.min >= 30);
( K, v- t( e" d9 L- J3 ]( ]}
) a9 D6 \' Y* K: M9 i3 f+ R) N}- ]9 n# a; r3 Y$ Q5 l
return ret;
9 C" w+ ^$ c. @* P0 F0 y1 ~4 M2 G, ]}' s5 Z7 J! ?' L. k- `
//---
& m$ \9 c0 n8 l# B3 j/ _bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])* q- ~# c6 f$ K0 G! J
{
0 |' m" T2 ~; q1 G% Iif(PositionsTotal()) // Is there a position?7 a8 y! |3 ?+ ^0 d6 ?  p( b6 P1 f
{
5 D3 e9 C7 Q4 y  y+ z7 Y. bdouble offset[1] = { 0 };* g: L9 o. ^( B6 D7 o2 P4 `
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. p) ^: k! L+ E. R&& PositionSelect(_Symbol))  // Select the existing position!
$ E* M; S0 U+ {  V1 s9 g{
1 o5 [3 _6 z* T+ {' U4 ]8 rENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);- y6 R) \/ F! R, G8 {
double SL = PositionGetDouble(POSITION_SL);
3 j# g7 x# O9 ?) Ydouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 u& M8 j( N4 i; A4 Bif(tipo == POSITION_TYPE_BUY)
- d; y7 Z3 P# R4 ^{
3 C5 H# c: _% J. Q+ d8 Yif (cotacoes[1].high > cotacoes[0].high)
. p% o0 R# w4 s' ]" D5 l; p{+ [: B: Y3 o6 G  Z
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 A+ h; G$ c6 F+ l5 R
info.NormalizePrice(sl);; H  D2 h9 {7 ]4 r; E
if (sl > SL)
3 {  i( n% d+ P4 k{# e, b9 l  c$ x% o: a+ h& q
negocios.PositionModify(_Symbol, sl, TP);
0 w0 w5 g2 F/ L9 [}
/ j# R- Y3 i8 N( I8 q}: C3 f7 p8 H, C7 N
}
2 a3 G# n4 ^& t- N: i; [else // tipo == POSITION_TYPE_SELL% m' x6 _$ Z* K( a4 H- v: J
{" \4 o4 b: p. I) ?' F- m- l
if (cotacoes[1].low < cotacoes[0].low)9 ^3 x: q* h9 [* A  ]9 x) j
{
) S5 z5 t$ _) o6 u* sreturn true;; f* p, x* y3 O8 O3 x
}
7 {$ w+ x) u$ }// there was no position
1 [$ n' s/ I# t! m( ]5 t! Z$ Preturn false;. j1 d8 G& q2 B$ ]* g% Z2 K
}
4 Y5 m- w2 h7 @) F我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 J6 S; W: @4 _2 Q* G  q
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-18 04:27 , Processed in 1.456361 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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