私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
2 }* }. O, S' H* L( ~% r/ k在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ P- \4 T! L( g9 G" m  v, L* w为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) Q4 E$ N% p; h( z$ I/ I以下是制定这些规则的代码。
, y! M& ]! B2 o+ @9 i- o//--- Indicator ATR(1) with EMA(8) used for the stop level..., i* [  b- e$ A7 F; |
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; t8 b, H) l1 gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& ]: _+ i3 ~) w$ {7 M3 E//--- Define a variable that indicates that we have a deal...
3 H( H1 [( u6 d  pbool tem_tick = false;& }' v: r6 d2 p7 Z. j* S( `
//--- An auxiliary variable for opening a position, [1 G4 z* F( v2 A0 d6 y( o
#include<Trade/Trade.mqh>' u( ?4 I# Q/ T
#include<Trade/SymbolInfo.mqh>
5 T. A% [) J1 [' V: z; ?CTrade negocios;0 D- u7 L( l! g& W
CSymbolInfo info;: O( D- t3 k9 c3 F
//--- Define in OnInit() the use of the timer every second& L0 k# B/ {9 _+ l
//--- and start CTrade( H' K8 s; H8 ]; ~) k6 c
int OnInit()
) A2 B- y2 M2 [3 S; R{
3 K# s5 ^8 F4 f: f1 v/ J% y- C# Y& A5 Y//--- Set the fill type to keep a pending order. j- I  Z! `) S9 v
//--- until it is fully filled/ i" D' Z; J2 z) j7 |. \; [/ `1 U
negocios.SetTypeFilling(ORDER_FILLING_RETURN);2 a4 `" q7 c5 f& Z
//--- Leave the fixed deviation at it is not used on B3 exchange
# }0 c2 {9 }7 |" i( o  {negocios.SetDeviationInPoints(5);
* K7 a1 {1 Z& M+ K//--- Define the symbol in CSymbolInfo...1 ~8 A$ c& P1 o& D% W' e
info.Name(_Symbol);
" W  u! u. {7 L$ _, T//--- Set the timer...1 x" @# u- s% X% _5 g6 l6 t& \$ P" n
EventSetTimer(1);
$ m9 R2 s( H# V% n" H4 w, I* M1 O//--- Set the base of the random number to have equal tests...
8 S3 @6 t* X( B6 vMathSrand(0xDEAD);+ ]( ~2 a% x- q6 _
return(INIT_SUCCEEDED);5 g; u' g( ^; d9 }, Z! r
}3 c# ]5 E3 c: R9 C/ n3 @# I
//--- Since we set a timer, we need to destroy it in OnDeInit().4 L: Z5 T4 M2 t3 |3 a0 z
void OnDeinit(const int reason), O  E; D$ s: ]& o% f9 @+ S' ^
{
4 y6 e; |' @0 O% U0 u( R, fEventKillTimer();' p$ S& u: q6 O. c! M* T
}
* D: j4 V$ n4 [) h' E) O8 l//--- The OnTick function only informs us that we have a new deal+ _6 Z( V: e) }/ y
void OnTick()6 S9 s* a; O- j& b, I1 [
{
4 }& _% r; M2 L- T, t) jtem_tick = true;* `- H* Z- }& a. f: }. N
}+ A  j& q; h' ^0 P( t% [
//+------------------------------------------------------------------+
# A+ W5 V% M: f4 o+ j  W//| Expert Advisor main function                                     |6 I: Y$ p4 r) _! y5 f
//+------------------------------------------------------------------+/ y0 B( S0 i  R$ z( I- y
void OnTimer()
+ f+ a  F2 x6 c6 v{
( u$ r# D2 S1 B9 t0 RMqlRates cotacao[];/ v) v6 T. B) C& R
return ;6 F7 C( B/ L9 O
if (negocios_autorizados == false) // are we outside the trading window?
' V9 Q$ g+ @+ C6 y5 h1 Z4 wreturn ;
1 Z1 e& q6 J8 V//--- We are in the trading window, try to open a new position!+ S- d* l0 o$ O) Q7 b( m: u
int sorteio = MathRand();
0 O* o7 R, I8 {//--- Entry rule 1.17 D9 R9 }' v+ k1 h
if(sorteio == 0 || sorteio == 32767)
# _% h8 N& z% g  yreturn ;
0 {6 i& m% N; Dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
1 q" M2 O" q4 L2 X{' d  R' M. M8 t( X
negocios.Buy(info.LotsMin(), _Symbol);6 a% c. L  [$ b5 \: r: j
}  ]: j2 s7 z0 d9 q
else // Draw rule 1.3 -- odd number - Sell0 ^5 ?/ I* c; h. a, o
{
. g- ?2 E- N* c# J" S+ t- K! onegocios.Sell(info.LotsMin(), _Symbol);
7 D' R+ ]3 l/ ?+ ]9 A* W) }. Y6 y}
7 x9 y( `2 ]4 D; B5 }}
/ C7 o! S  M% i) ?/ ?9 l//--- Check if we have a new candlestick...
7 g5 `" v/ U* \6 L5 q3 N9 Ibool tem_vela_nova(const MqlRates &rate)7 W! M. e( W! M( l
{1 i2 H4 F5 e7 c: ~3 ]. f
{
# O3 L$ |/ V% P+ x% e! G8 D' ~ret = true;
3 J! ^- G; x1 H) M( E% o9 Hclose_positions = false;
5 z5 q# F% j; l" ]; G}# Q( V4 L& Z$ z$ N
else8 W4 H3 M" B4 r) R6 E" P
{) T0 P5 Y) L, T0 N7 i/ }
if(mdt.hour == 16)* r% u* B8 N8 U" Q* ~3 ~) s
close_positions = (mdt.min >= 30);* U# M3 A) v; T' u; `9 r
}
8 A. I' Z  s& E9 _5 D3 o" G; a}. N5 Z* f' w: c) J* ~9 e, p
return ret;* l+ `3 W* k$ j7 u
}
" p+ {* A9 G: P" B4 J& n7 e! s//---
: Q/ l' h; k# ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! {' X) d3 S8 C4 y
{
9 o( s1 Q: V* x: M9 v& qif(PositionsTotal()) // Is there a position?- f+ a  G& T: F
{
! y2 J' h% C1 `& d% ?; Y$ cdouble offset[1] = { 0 };
- L8 l7 ~" V' E4 Vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?" `& C, f1 Y' Y8 [# @0 o/ d$ A
&& PositionSelect(_Symbol))  // Select the existing position!
( c2 l4 r( M+ d3 ~5 I9 Q" t{' s% F+ F. k) f* Q5 S% i
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
% W" |- w" M# c! `double SL = PositionGetDouble(POSITION_SL);
0 l% h! U7 `1 n  xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));* ]9 W' ^8 ?7 _
if(tipo == POSITION_TYPE_BUY)
- J3 C9 g+ o% ]$ {  J{: E5 ?! \! ~9 n7 C4 `/ h3 s' }- L4 b
if (cotacoes[1].high > cotacoes[0].high)( ^( s7 {; i1 X. @" l+ A
{
  h/ l. J6 H3 \8 V5 Y& m; ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* c6 e" N0 E' W$ U
info.NormalizePrice(sl);6 {+ {7 E$ Z) ^( P6 L
if (sl > SL)$ |5 b" @7 }9 M# e2 ~
{
5 X/ C( d8 V! F/ A; \$ ?, X/ `negocios.PositionModify(_Symbol, sl, TP);
" z" F+ L$ I) r' M' a}$ a7 b6 E. X. J) o
}
- A2 K! |4 J' u! N6 L}
$ R, f5 @7 s8 M+ Y. Qelse // tipo == POSITION_TYPE_SELL; C5 c' f! T  m0 A4 Y
{
7 t3 r; c0 r% O6 zif (cotacoes[1].low < cotacoes[0].low)0 x2 O! O/ F  T
{
: e! T! m/ a: P$ Zreturn true;
, M0 k: C/ G0 y: H4 P3 K) W9 \) t}
' F. E, G) \2 y2 P: Y7 ^) B// there was no position# c8 R6 M9 D5 U! F+ p
return false;8 z5 }  Z  t- V
}
  B! Q! Z* j3 E( z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。! v( K) }/ a8 \/ i; B
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-1 11:05 , Processed in 0.831359 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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