私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* i3 u3 o0 X) L在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 L7 H( w( T# Q+ r6 k6 x' j为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 d) B7 e1 W+ i6 v
以下是制定这些规则的代码。
8 x) [! W- u: c$ e//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 N' i3 K0 o2 Nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- y4 |& p* R' n$ L  D' rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: N! b: U$ ~9 @) b. {
//--- Define a variable that indicates that we have a deal..., I3 U9 K9 [2 W
bool tem_tick = false;# F; n' M7 `$ d  ]
//--- An auxiliary variable for opening a position
2 h. L* S! k5 h% y#include<Trade/Trade.mqh>- ~) f% a- p1 r
#include<Trade/SymbolInfo.mqh>! a2 x4 q$ f. P9 Y
CTrade negocios;
2 ]0 I. C9 \2 t) y7 F6 U8 M8 H! @CSymbolInfo info;+ @& i. R7 w7 q% [
//--- Define in OnInit() the use of the timer every second
( i2 v: F+ K* J//--- and start CTrade
& e3 W9 F6 {) A+ sint OnInit()0 _+ j: i7 B+ j. z
{  ~3 ^6 X/ j6 G3 e
//--- Set the fill type to keep a pending order" \! k6 j# v0 R0 l, A; r
//--- until it is fully filled! K' r! G; `$ d( a# T
negocios.SetTypeFilling(ORDER_FILLING_RETURN);& \* q, t# B* z. k& l( k# R
//--- Leave the fixed deviation at it is not used on B3 exchange- v8 v/ _0 d+ B' O3 L
negocios.SetDeviationInPoints(5);: x* S  y/ b, i& M% y) M) ^
//--- Define the symbol in CSymbolInfo...) f' z' o: y5 z& D: \) T
info.Name(_Symbol);) u- Q3 E) S* y7 H% z8 V
//--- Set the timer.... p2 s: k- J& d9 S
EventSetTimer(1);, l) Y: c; r) q
//--- Set the base of the random number to have equal tests...# D9 h+ F! Y& J$ i) h* r& u2 @
MathSrand(0xDEAD);$ c- Y& {4 }1 x8 A
return(INIT_SUCCEEDED);7 S2 T5 u7 ~- {0 E# K0 n, a
}8 d- @0 l, q4 W0 O) G  t- P
//--- Since we set a timer, we need to destroy it in OnDeInit().
# K  x' }1 s# p% Kvoid OnDeinit(const int reason)
/ p5 a6 p* o5 [! X2 B4 m  y7 [{$ X/ O2 {- _) R# h0 X; |
EventKillTimer();6 _, W0 @) `5 s' h5 I
}
8 D2 [" V# E; I! k  s7 R; o, Q) t//--- The OnTick function only informs us that we have a new deal
+ T, q1 v# ~; |- g: C3 ]void OnTick()) f8 Y* I7 T6 b! x& P2 T# {
{
1 t# J9 V2 n  h0 x+ Ctem_tick = true;
- @1 _* O1 R. b$ X: d}8 A; R# A& h6 Z
//+------------------------------------------------------------------+
) {7 b0 L$ L/ E* b+ s& N//| Expert Advisor main function                                     |. I. D& X0 N- p0 p. g# U9 t/ T3 T& W* C
//+------------------------------------------------------------------+
& U) C/ z, w8 L0 k1 ^! d  Y! m9 B0 \void OnTimer()# F3 k; J; D6 }5 k# I
{; z! x7 V3 ]( @
MqlRates cotacao[];
# g3 m/ v8 w, ~  W: z( T1 }return ;7 T* W6 N" {' \' M& e# H
if (negocios_autorizados == false) // are we outside the trading window?
( |% Z+ K. o# N) y' u$ s# \3 oreturn ;/ Q7 I: W" m# G/ L3 ]4 {* r/ y
//--- We are in the trading window, try to open a new position!% o2 k/ H1 a. h# A/ p- ?: Y
int sorteio = MathRand();* H& S4 {" M% I, e7 ~7 }2 V
//--- Entry rule 1.1* T/ l* ^! J# q3 P( X4 z1 ]0 W1 g2 {
if(sorteio == 0 || sorteio == 32767)
! @# u; j; H- @/ d: d5 \return ;5 {( U2 H5 `9 s6 T9 g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy! P7 b0 O' m) q1 N# f
{
" k% u5 c6 ?" s6 l/ o: Tnegocios.Buy(info.LotsMin(), _Symbol);5 W6 \' F/ M1 L9 i8 U
}
( g+ p+ e5 g2 \9 b4 I7 B$ K. q6 i# Uelse // Draw rule 1.3 -- odd number - Sell
6 D4 {9 ]4 Z* R( D7 ^{
. n3 a3 d* C8 c& j: ^' Tnegocios.Sell(info.LotsMin(), _Symbol);: P) Q+ ]2 j7 T5 C6 F  H5 u
}# D/ Q& r" [$ o  Y+ A; w
}: x1 v) ?0 k5 o" W# I1 N
//--- Check if we have a new candlestick.../ N# w! l9 X9 B
bool tem_vela_nova(const MqlRates &rate)
' L7 L8 c$ r: N5 i. S. l. V2 x{1 G/ c$ h% ]: k7 E
{
# _( T! V. b$ e/ o7 p' ^# Oret = true;" t) X/ z4 g5 R. w; c$ J
close_positions = false;
: Y3 J6 o; r6 c5 {! {}* _  a- T1 G; Q' m: r# Z
else
* B- w4 V$ x+ e{
- b$ D" |- @% b2 V5 rif(mdt.hour == 16)& R5 a* [9 C2 k" H( J: C4 D
close_positions = (mdt.min >= 30);  F! m0 {8 d) p1 o& m* a5 _: d4 p
}% w9 t7 }0 U* V4 D
}& {. O7 p9 x& D& j
return ret;
  _2 q* G1 v- y: m, e- h}# G0 G3 u& A9 A- R/ o
//---, ~1 _2 j0 r5 E- z
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]): R4 L1 @4 ?. U% y5 f
{
. s( p% [: ]9 R: G9 L: V* j7 Kif(PositionsTotal()) // Is there a position?
' q/ {& [9 l1 u$ j0 q{
$ m7 M  q" r) J7 {' X( Idouble offset[1] = { 0 };
9 f' O* F- \- C3 x5 gif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
: H& j/ E" _1 D' `3 \% F&& PositionSelect(_Symbol))  // Select the existing position!
( y' b! l4 b( T) |{
$ E; ]+ [1 ]0 I% O+ ^& f) qENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
* r: S3 q3 `! {' fdouble SL = PositionGetDouble(POSITION_SL);6 o5 [3 d* G# ~' E
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 [) ~8 q: s8 B/ I
if(tipo == POSITION_TYPE_BUY)
8 \* b8 T, W- D% m5 d+ U{
4 S6 [1 y" n: @if (cotacoes[1].high > cotacoes[0].high)
  R8 D) v" R1 G( Z, v- s9 Z{. C! E6 K! A2 p2 h+ U
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
- @" D5 w+ {- X' L# Ainfo.NormalizePrice(sl);
0 Y8 V/ r7 s- Y  @if (sl > SL)
* E9 n" h  D# s0 l: h7 S{
! l) S( ^: ~, b( v; `) A9 g$ onegocios.PositionModify(_Symbol, sl, TP);
7 ^$ b- r% j9 e$ n2 [4 n7 [9 E. w}0 V: l9 m: V; @- P
}( `1 B( r8 z- b/ _" X
}
! m) l) q5 a/ t% S; I/ r7 `else // tipo == POSITION_TYPE_SELL7 L/ J. d. L1 q8 r, H7 R1 ~  f
{# ^, M# V) ^. M: ]% W% [3 y7 \
if (cotacoes[1].low < cotacoes[0].low)
, r# i  E% d7 W0 }* P4 H{) [: A# |# V) E- Z5 y7 G% z
return true;
$ o/ ~; w' C8 d2 H# H( {}4 M( z! g' Y! k# \* i
// there was no position
) Q' e0 n9 y3 o( zreturn false;
' z$ [# b! C6 J}* x* {* E/ l8 n3 C" u. k, O
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# s1 \! F* w+ U% e) B1 N
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-14 18:39 , Processed in 0.655507 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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