私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
2 h+ x1 c: e& D$ @在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( u0 O  ^- m( N( x; C# o为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 M4 _( z; J7 U7 S9 I  i3 {4 V, P以下是制定这些规则的代码。
$ q4 \4 f4 j4 |; m//--- Indicator ATR(1) with EMA(8) used for the stop level...
. r( a2 k* u$ xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 A" P1 ~5 `8 S  j6 P( p1 N
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);) }, n! K. E4 m
//--- Define a variable that indicates that we have a deal...
' n7 k& Q# p: a3 J/ mbool tem_tick = false;
+ V& y  c  q% ?) f0 I, ?//--- An auxiliary variable for opening a position
- x& ]* n7 r5 Z1 Z1 d! K#include<Trade/Trade.mqh>
! B+ N* c' P* n  I$ z* v: M: p#include<Trade/SymbolInfo.mqh>* [% O7 O* v$ z0 G3 `  m' `$ Y
CTrade negocios;6 z$ \5 N0 m2 u8 {# G" Q
CSymbolInfo info;) `$ M, Z2 k2 x
//--- Define in OnInit() the use of the timer every second% C0 ~( ]9 @& F0 L3 }
//--- and start CTrade; Z! I- [  m' H2 y$ \5 E8 \5 c$ X) C3 s
int OnInit()
# E' G* ~+ J9 [{
; y% L# V& ~" D+ K3 c//--- Set the fill type to keep a pending order) \4 ]9 O" `9 A
//--- until it is fully filled0 H# Z/ ?0 [4 U0 c
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
- @0 y+ _" f8 i/ O; K) X$ \) Q3 J//--- Leave the fixed deviation at it is not used on B3 exchange
9 B1 M% H+ m( x; b7 I6 s4 ]7 Inegocios.SetDeviationInPoints(5);
. p4 Z' `4 `) c4 D, n* U//--- Define the symbol in CSymbolInfo...) N; ~. T- n: A, G( a9 p3 K% l
info.Name(_Symbol);1 D9 l9 M) X* p) S- n' k+ N
//--- Set the timer...
4 d$ U) @" A* [- tEventSetTimer(1);
9 w& s$ Y$ d3 h( T2 N//--- Set the base of the random number to have equal tests.... s: W+ l( M0 z
MathSrand(0xDEAD);8 `) ]% J2 S) K! C3 ~5 O/ h2 H
return(INIT_SUCCEEDED);
% v. c* q- p/ Q7 A* X}
2 q. j' G0 B  G4 V; @  h//--- Since we set a timer, we need to destroy it in OnDeInit().2 W: x( e' {& s' E) q6 `5 x
void OnDeinit(const int reason)1 Y) r; g3 j9 N" b- T3 J9 p
{
7 l1 N( @( R, Z7 tEventKillTimer();
# x& x. K8 U& K) E& N7 I1 D) R0 T}
( x) y2 O/ n+ P; k//--- The OnTick function only informs us that we have a new deal
: A, ?8 s) C8 \2 F+ j- E- kvoid OnTick()
; _: J. o. M. Q1 L# J7 K; d{
- i1 [, D# H& ^+ ~7 k2 E, Dtem_tick = true;5 N0 q3 H, C0 x
}3 A( D. v5 k3 [# I1 o$ Q1 I
//+------------------------------------------------------------------+
: e. V: D$ ~  b$ ~5 X1 s//| Expert Advisor main function                                     |
; r; Z2 [2 a( k/ u) O//+------------------------------------------------------------------+
3 H" k! ~5 t* F! X- k; |2 qvoid OnTimer()/ u' y* Y' U7 H3 {/ B; `( n
{( Z4 {  F/ F6 L* B
MqlRates cotacao[];
- r; P1 p4 ?) x% z. c0 \return ;" I4 `3 |5 U: V* z% ~% t
if (negocios_autorizados == false) // are we outside the trading window?
3 ^% i& @+ s2 A, P% _1 ~' Yreturn ;# l) U2 G$ e4 V. c
//--- We are in the trading window, try to open a new position!
8 Q8 Q. i) K5 {int sorteio = MathRand();. d( G: z/ m( Q4 q
//--- Entry rule 1.1
' W8 L2 C8 O. ]: z7 |3 Y- W: u3 vif(sorteio == 0 || sorteio == 32767)# }9 c/ R7 C5 _3 `5 [5 m
return ;
$ z0 P8 X, h: Wif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
% `& p" a, H- y' ~. g! ]+ W{
: h& u: g+ y- o: j5 W8 C- Lnegocios.Buy(info.LotsMin(), _Symbol);4 ]8 n) O" _3 }0 X" E7 t
}5 J( e; R: B! S9 b6 p, A  O6 G) |
else // Draw rule 1.3 -- odd number - Sell
* m2 u7 Y8 x' E. u0 J{
/ X( r* o3 @( enegocios.Sell(info.LotsMin(), _Symbol);
: E9 P2 _4 T' o3 }( x}2 {/ b6 ]4 @! ]! f
}0 ~& R! ?4 k& D- v. _3 |8 c* I
//--- Check if we have a new candlestick...
- a' ?! H; w! Z6 u5 ebool tem_vela_nova(const MqlRates &rate)# X" Y3 ]6 D- C. d, n
{* h  d+ Y, v3 z; \9 M
{
  ~6 E% U, M  e+ M5 U1 r8 Rret = true;6 j0 d0 g( Q3 p. N' g. z' ^
close_positions = false;1 g/ J- I0 z8 m
}
3 ]3 A6 S- A) C5 F; Ielse0 e3 x8 c4 o6 r3 t! q
{+ I3 Y  w% L* q' p' C/ V
if(mdt.hour == 16)8 Z8 S! W. _/ ^. e
close_positions = (mdt.min >= 30);2 g9 m+ e) _2 c/ S, w0 k: C
}
# B9 S/ a! I3 Z. H6 S: z2 f" \2 J}8 i& y0 X5 S( u8 k
return ret;2 j, A0 t$ s8 J5 j; I+ p
}  d5 X$ N0 q9 O* E* N& e
//---1 \& L& Y; Z3 ]* C* ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
, d" j) q4 F! o3 P9 T+ g8 i& ?{& I4 g* Z7 A! A7 p6 O
if(PositionsTotal()) // Is there a position?
0 r; [2 Q0 q; B1 `{; D/ ]7 H, P6 l" N9 d) |! O
double offset[1] = { 0 };
) U( o' m5 T1 N' E+ C# yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
- \! |$ X; p7 f&& PositionSelect(_Symbol))  // Select the existing position!- w8 j7 R3 l0 p4 K
{
4 t1 K4 x5 }, I7 v+ aENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
( i' t, s+ u( Z" Q4 h) edouble SL = PositionGetDouble(POSITION_SL);
0 X0 A) h% f* {9 A/ m" j4 mdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  k9 I  u8 C7 b+ r3 tif(tipo == POSITION_TYPE_BUY)! F" L2 f7 h' ]9 P8 G' K
{
  C; X( a: o' P/ P; r9 a9 j2 cif (cotacoes[1].high > cotacoes[0].high)
$ j/ m. o, ~( h* b3 v{
) l  {4 S# ~1 s' [1 i+ S* bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& w& d: G+ d  o* j1 a9 cinfo.NormalizePrice(sl);" u4 Z6 S# i* I; Z
if (sl > SL)9 c7 u4 D. H$ T: M+ O
{. H4 p- M! j5 [: K
negocios.PositionModify(_Symbol, sl, TP);
( m. o1 f3 L  L( G/ I5 p, N: A/ x}
9 [3 W0 [$ {6 L}6 t& g" I) D/ C" d" n
}
  ]2 }3 c( ]9 N; I4 c. P9 qelse // tipo == POSITION_TYPE_SELL
. q6 A4 p9 O$ N- i: q9 @0 B{$ m$ J* R) ?/ b: i2 F2 l6 N( `3 u
if (cotacoes[1].low < cotacoes[0].low)
$ a2 `) M: ]/ L5 g, {- k1 X{$ N  m! Q/ m8 L, _/ B" H
return true;
( p2 {! ~6 s- |3 Q/ |; Q8 F0 ~}
" C6 ^8 Z5 P- c/ Z. i// there was no position
! R( u! w+ W! d5 H# X% O9 oreturn false;
& t7 w% F: \+ M, M2 b}
3 W& U! {- I" T) ^我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。& c/ ~+ b: N: L& I  V; h. d3 `* K
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 03:06 , Processed in 1.612169 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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