私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' h) n7 e7 I, l9 r在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; @3 O  N. F: ^6 e1 _: P
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
8 z2 q& Z( N0 @# c以下是制定这些规则的代码。
3 x/ v4 \7 }; @6 M; H7 R0 `//--- Indicator ATR(1) with EMA(8) used for the stop level...
( Y1 ?+ m/ a+ c6 h  t& nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);# @! G1 k# h* D$ j
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. H; e, @% L: J& ?5 t- t//--- Define a variable that indicates that we have a deal...+ r" j2 Y/ s# ?5 [( s) F7 o
bool tem_tick = false;
( U8 K6 q4 u% }; W1 f//--- An auxiliary variable for opening a position) f% _$ O# U3 v2 Y; \8 |
#include<Trade/Trade.mqh>
; T' D  }8 R5 w#include<Trade/SymbolInfo.mqh>! j" K2 A* s0 g
CTrade negocios;( |- Y* |: D" X" m' i
CSymbolInfo info;9 Z. R  |* Y: e# I6 L" `8 W6 U
//--- Define in OnInit() the use of the timer every second5 b! [3 l$ g3 q- ?1 Y2 h; G' |) H
//--- and start CTrade
% P+ {$ U/ f9 W# r! D; Jint OnInit()
. k! \: t/ y$ b  Y( i{! t; k# X- g& c4 [! Y
//--- Set the fill type to keep a pending order. m1 _; n2 ]% I# N6 ~
//--- until it is fully filled
* m& h( P4 C" q7 E0 Y1 Tnegocios.SetTypeFilling(ORDER_FILLING_RETURN);) M' q' c! I: O, ], \% [+ }: b0 t
//--- Leave the fixed deviation at it is not used on B3 exchange
8 g& ^* n' U; G  ynegocios.SetDeviationInPoints(5);( x* N( W; j8 O) X  s
//--- Define the symbol in CSymbolInfo..., y7 O! I& k' o, V" G: @
info.Name(_Symbol);! C# H/ N7 v0 V
//--- Set the timer...
( M6 W+ B) A8 n3 zEventSetTimer(1);: F. R- _' G9 c  e
//--- Set the base of the random number to have equal tests...
! I4 }% k( r$ T% A: ~" _MathSrand(0xDEAD);# ~. h. ?  T0 M- y3 l1 q
return(INIT_SUCCEEDED);! j8 _1 E' K- t6 a( k6 P2 _# O
}
7 ?9 S: b. N" Y8 z$ w% A//--- Since we set a timer, we need to destroy it in OnDeInit().
6 K# A3 W# E+ |3 \1 Q2 ^% P! pvoid OnDeinit(const int reason)
. g! Q# ^4 U! F7 j& q{
% `# r! J8 O2 T9 Z" E. j  `EventKillTimer();
$ }% D/ H! p% b3 m8 o' l}  ?9 ]# K/ I2 A% K* f& q
//--- The OnTick function only informs us that we have a new deal
; k9 z$ h7 ~- tvoid OnTick()) V9 U1 W/ `# N3 U: h3 k
{
( s5 z* ^; R5 w! X* s2 K1 H7 otem_tick = true;
, ?! |5 ~( O6 J& P. a8 L}* A, j5 w0 ?) q! W; k0 ]
//+------------------------------------------------------------------+
+ p; I; ]/ Z- _//| Expert Advisor main function                                     |
1 e+ [: j3 c+ K, s" y9 M4 `9 m  x//+------------------------------------------------------------------+
! C9 w) ~4 O- rvoid OnTimer(). A8 k6 O- C+ R& @
{
; [2 J8 _& m5 N; ^MqlRates cotacao[];( G: v2 L3 g7 N4 l
return ;* u1 W. O4 h, `7 T# z6 f
if (negocios_autorizados == false) // are we outside the trading window?. Y6 F" h2 H" T
return ;
5 n& d# p  G# e9 C6 |9 v//--- We are in the trading window, try to open a new position!
6 ~/ Z9 q( I3 D7 o+ B3 Nint sorteio = MathRand();" H  I$ X( L+ z
//--- Entry rule 1.1+ z4 u( _7 U1 S: ?
if(sorteio == 0 || sorteio == 32767)2 U9 ~' |: |' o
return ;
+ u3 L1 x6 n) rif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy) d9 g& n0 T: z2 f
{
3 i7 y6 W3 l# ^+ D. K4 fnegocios.Buy(info.LotsMin(), _Symbol);) }0 {! v5 z* m7 C& M* n0 _9 y0 m
}7 m2 u# [# u! B* |; n
else // Draw rule 1.3 -- odd number - Sell' V) h4 p: z; Q( E
{
$ F( L2 _. b+ p* Vnegocios.Sell(info.LotsMin(), _Symbol);4 {7 {. h2 o; E7 g$ [# J7 r
}) t, l! U- }/ F% u7 l& U
}5 ]. Y* D2 c1 g1 `
//--- Check if we have a new candlestick...; E" J0 s) ^- i' c$ k
bool tem_vela_nova(const MqlRates &rate)5 E- z. Z, }+ G+ m7 d1 e% @: F' y
{
, j  z; T5 I2 A* N) {7 x$ E) a{0 ^0 r* s$ ]" F/ b$ k, A( T
ret = true;% ?9 b7 p/ B7 \! m' R7 ~
close_positions = false;1 l2 \8 ]4 M1 u
}
, U: L2 v9 v( b, m* {7 }/ Welse
0 @9 ?3 ]3 s2 A& l$ N% Q{* r# h7 C  ]6 i9 L: Z
if(mdt.hour == 16)
+ F$ b* Z" l- H5 E( L" {, sclose_positions = (mdt.min >= 30);
6 h* b" o  V( r( I9 _# ^9 I4 n6 k}
3 Z' K2 S3 [7 C. a1 y& A}7 V6 X5 z. N2 U8 m" g
return ret;
5 Z- e$ S8 G( h& b5 }}+ r' {3 |7 F% T' B
//---
! g+ q, @1 i- v9 \! bbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
' W- E% T! O3 F; M1 Q( B1 o" E{6 V1 S3 o. U3 B3 t- x9 m/ Y. N
if(PositionsTotal()) // Is there a position?
/ z: H1 S' G, a/ ?0 v. ~8 ]7 o{1 U) d' \; A2 i- L: n+ E* s
double offset[1] = { 0 };; l0 @  A9 l5 g) G* B& n  i# D
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?% Y1 c+ @3 `) t- r: N( b. v
&& PositionSelect(_Symbol))  // Select the existing position!
  W2 X( c( L" e, N) S- [{5 S. h+ O+ s3 f8 M9 H) z! \
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
4 Z/ R" D% |+ a8 O% Ldouble SL = PositionGetDouble(POSITION_SL);7 Q- ?# L5 E+ l
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 u" V7 G9 g. k' k7 b9 h* qif(tipo == POSITION_TYPE_BUY)
# k0 L* z3 h6 I$ P- @% }$ f{7 J8 y% F: M) r  }$ s) p
if (cotacoes[1].high > cotacoes[0].high)  }) L/ F* z' }( t; v+ `& e) z
{
( F0 e! v! |4 q1 Q7 e& u* Rdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
8 c: Q& t( t' uinfo.NormalizePrice(sl);
) A# ]$ C  {2 [if (sl > SL)! L* F6 j. y% ?2 n/ T5 _
{% o+ K% l, H2 y5 ^
negocios.PositionModify(_Symbol, sl, TP);; @3 P; l! R( L, |  L% a
}
$ z8 U3 x7 K( r1 G5 _6 S; V2 o  A}& ^# Z; c2 J* b% P+ n: g9 m
}4 ^) G$ A  @8 k. N. V- F
else // tipo == POSITION_TYPE_SELL: K  `% d! i! J6 M( o+ a
{/ J; ]! m6 o6 Q' @! h
if (cotacoes[1].low < cotacoes[0].low)! G; n) o* A. c
{
" e! y, K' X( B* P+ z8 creturn true;) I5 o; z8 a' t
}
) t; Q  t7 Z) H. g1 H3 B! M# a+ g// there was no position
# i2 s' [* b4 T; K" T8 Y0 Treturn false;7 R% K* {- d: H' ?* m
}
4 x: `# T' J' B5 e我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。9 Y; ^% v+ S: Q
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-28 20:05 , Processed in 0.544342 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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