私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA- P4 l$ x$ [! }( K4 b
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
$ [! X  ~0 J. L( F# }3 a! t为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
  q0 p/ N: \7 T1 H0 I2 J' l以下是制定这些规则的代码。0 Y) l% O+ B9 `. o, ]
//--- Indicator ATR(1) with EMA(8) used for the stop level...  ^$ F# M* `" U! R
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ Y$ _; a/ Q" j, D8 \
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 k1 B+ E; f  ]2 D! w/ j. t
//--- Define a variable that indicates that we have a deal...( N. N$ w5 T6 ?8 a) f! F& z) K
bool tem_tick = false;
( L/ U$ F' M( C  Y: p//--- An auxiliary variable for opening a position
4 A' M7 ?  x& H" V& B#include<Trade/Trade.mqh>
' f8 s: X0 Y8 U# \. O7 P- d* l#include<Trade/SymbolInfo.mqh>$ m+ r4 q) `& m* `4 x
CTrade negocios;' y1 |: [" b7 B) o# }% r2 H
CSymbolInfo info;
1 _3 j* t$ F9 t+ _# @8 V7 q//--- Define in OnInit() the use of the timer every second
  M* l8 d, _& G. T, x( L5 ^2 f//--- and start CTrade+ k* [. x$ M. Q; S8 y: [5 q
int OnInit()2 e5 y. [! s* n' V- M
{' {) \5 m3 \7 w( f: \
//--- Set the fill type to keep a pending order
: g) `% n/ {3 y5 A* Q2 }//--- until it is fully filled9 F8 j8 G9 _$ Q/ L( F$ z( ]3 f& S
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
, q; C# p+ \4 n3 P# s2 b: B//--- Leave the fixed deviation at it is not used on B3 exchange! {. m0 l! K( k) [: S$ K
negocios.SetDeviationInPoints(5);
9 E1 Q: Y2 c8 ~6 o+ q( ^//--- Define the symbol in CSymbolInfo.... z: V6 |# ~# j! z; r2 ~9 J4 H
info.Name(_Symbol);7 _  Z# p& p) D5 H* }( _
//--- Set the timer...
: W* b: c2 v8 s; uEventSetTimer(1);
' N0 T" v. D2 [1 F+ ?//--- Set the base of the random number to have equal tests...
. {3 i. p. A' a! K2 u- U8 A# `9 x# eMathSrand(0xDEAD);4 T' z* G3 n( W1 P
return(INIT_SUCCEEDED);4 Q: Q& k2 f5 y7 w& D# W6 d! ?
}
0 q  {  ^, c% c$ @6 I//--- Since we set a timer, we need to destroy it in OnDeInit().7 s+ C- F1 Q6 O) \, P
void OnDeinit(const int reason)
, ]7 p9 B! H) o% P5 S{) V5 [: x; Q0 a) F9 I& d
EventKillTimer();. Q/ ~7 f0 @! C% Z. N( s
}. i  B9 d1 {% y, E0 `0 d
//--- The OnTick function only informs us that we have a new deal! h. q) ^5 g) y; g* w1 y# Y
void OnTick()* o% \0 R% l( j
{
# J0 r  I, G/ f0 H; Gtem_tick = true;9 R  I: e3 r2 O  d9 D
}
- E6 q2 c/ G: ^( |//+------------------------------------------------------------------+
; e. r# j) ?; j# D//| Expert Advisor main function                                     |
% v" J4 l6 m  @6 S//+------------------------------------------------------------------+
; k! D6 T6 y) q7 T" Lvoid OnTimer()0 U7 n) Z  d% ]  Q) e
{0 O0 {3 \" Q0 l, G! o; j; W
MqlRates cotacao[];1 Q9 `: B. O0 }9 d/ n+ L% q
return ;
( m$ J# k- R9 h, C- E9 oif (negocios_autorizados == false) // are we outside the trading window?+ C' @' }6 Q" b7 O0 M
return ;& `# q% N  o9 D( u" U0 D
//--- We are in the trading window, try to open a new position!- a# P/ P0 O4 X, i8 ]8 T
int sorteio = MathRand();% R% Q9 t- C' P
//--- Entry rule 1.1% z9 k9 Y/ e7 x* c
if(sorteio == 0 || sorteio == 32767)
. h& c% p" Q9 A* `) Z# h) Z2 preturn ;
, s$ D* ~2 P. K# q% dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy, A2 Q  m5 ?8 v# V5 Y9 k- a' W
{5 `2 c0 t9 F4 R3 E( k- A
negocios.Buy(info.LotsMin(), _Symbol);
# y+ Q; A3 I  K1 h+ I3 l2 g}" }9 T; J8 P  F. W. ]* _+ q
else // Draw rule 1.3 -- odd number - Sell% |6 u3 \/ v9 D' e; [' X
{
9 x3 v0 C/ k* [! |3 g5 l6 gnegocios.Sell(info.LotsMin(), _Symbol);
: F! B. l0 v& T! G0 z6 N9 W}
- g# b9 M6 I) ~/ C+ P6 |}% V* f5 p( O6 ^* p8 ]9 R( B5 M/ y
//--- Check if we have a new candlestick...! O  l5 r- A/ V: e, n* E2 H
bool tem_vela_nova(const MqlRates &rate)8 v, p4 S" s( h5 e, k9 z! y
{2 Z9 }1 |- M7 O9 _3 Q/ o3 Y+ X+ B
{
! E0 q# e1 u+ d% {4 Iret = true;
4 v0 A7 Q$ X& I& A% J% Q6 uclose_positions = false;' U2 B; e/ U5 J. ^) }
}
& ]0 a8 C5 D% l* e* f! felse
1 S  B& k9 I6 B9 v" p' p{
( u  V2 H% r5 W+ Q) m  A6 ]! i* uif(mdt.hour == 16)+ s0 }5 {+ r$ S) V: K7 a
close_positions = (mdt.min >= 30);% N4 s' F& X. e+ a5 u
}" h0 w, W- v, @7 x' K  S, K$ H
}
6 W% i& x+ z4 g6 H) areturn ret;/ l( x+ P" d5 B6 r1 ~7 m* C0 `
}
6 b3 r/ J$ O# O6 P//---+ y$ u( T6 ?- a* V$ ]
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( w! |- e9 v; M4 I, C) |{2 E9 S5 h, m2 |- f
if(PositionsTotal()) // Is there a position?# ~$ S/ S7 i0 d
{
9 T) `  V( c  H$ J0 S$ S, ldouble offset[1] = { 0 };7 s: G: v: x- u" e
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?& v' ]3 B+ p- a* T0 ~5 b$ h2 T  F7 e
&& PositionSelect(_Symbol))  // Select the existing position!
! t  U0 S* f& Z7 U, a3 }, }{% \. A4 l9 y" {( Y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
( j( ~) ~! \$ _$ w( Xdouble SL = PositionGetDouble(POSITION_SL);
+ U! j# Z! R1 k9 G5 Y3 p( Edouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, a3 Q2 Y; T" I1 D% Oif(tipo == POSITION_TYPE_BUY)- L6 s) z6 ~: S  J" R$ Y9 p! f
{/ v& M5 i1 W0 N5 ~$ q
if (cotacoes[1].high > cotacoes[0].high)
: u% p! }" u' ]" W% W{
2 @; E9 h1 ?$ Ydouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
: E7 G/ T/ \' p" B% U* \+ g" Vinfo.NormalizePrice(sl);
/ n2 {$ R" S5 e# d5 V+ C5 x( Rif (sl > SL)
" I6 f% U' k, o; I, C# n- n{8 ~) T# O9 L, Z. N5 t
negocios.PositionModify(_Symbol, sl, TP);
$ E! ~( L8 D8 l9 [}. q7 G& S/ w* i8 u' R7 W  b
}' `& [# b8 ]9 H7 e6 b' c. t
}
# ?7 _9 j2 O" _. d4 \else // tipo == POSITION_TYPE_SELL
0 o! a# i! E: s4 S8 e{' J. L, {" U9 @
if (cotacoes[1].low < cotacoes[0].low)9 @; O0 m& n$ J0 T0 Y
{
9 A  U. L5 W* g- \/ areturn true;: B# |$ J) k: t7 P5 C+ T
}
2 W; t, M! ]8 `; z9 e# t% V* {! c// there was no position/ a/ P2 l4 ^7 g( F8 l
return false;
" `" p5 r; w% {2 `}$ Q/ [; `% I; j( }
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。% V5 J0 O7 y: [6 ]
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-4 16:28 , Processed in 6.558360 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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