私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* ^$ m+ g/ Z% p& ]) U
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, K  P5 ^% l7 {# t9 j% P" R5 e
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
  v& c* R6 g. S3 ]以下是制定这些规则的代码。' B7 c2 [% w8 M6 ?, X
//--- Indicator ATR(1) with EMA(8) used for the stop level...
  \% s8 b6 K- p" f9 u# h4 ~int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
( w4 \, m$ k( h0 z* b. kint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);0 I, [- K: @, T3 J% b
//--- Define a variable that indicates that we have a deal...! M( L1 [' E: I" w  D& Y4 [
bool tem_tick = false;
+ _$ h5 J. X. T; \/ a3 y! t/ w4 }//--- An auxiliary variable for opening a position5 y. l# E6 C; w( t, v/ ~$ X
#include<Trade/Trade.mqh>
$ |; n& [. K: k4 }3 U( \#include<Trade/SymbolInfo.mqh>( z* b2 f4 t7 T* S
CTrade negocios;+ W/ I3 z. A6 Z( b2 p% B3 p; K
CSymbolInfo info;4 z. G/ \3 L. f: Q6 ?1 z3 [9 p
//--- Define in OnInit() the use of the timer every second& X+ f/ f5 X" K8 }  d
//--- and start CTrade
: o$ h1 _# z  s5 X# h9 Bint OnInit()
  u7 Q  q( [0 x2 d) m' J; X{. F/ [6 Z6 ]& W8 e) n  v. i, @8 x
//--- Set the fill type to keep a pending order
. |, H8 M, h8 G' z8 m0 G//--- until it is fully filled9 O* h# j, Z7 T- F( c
negocios.SetTypeFilling(ORDER_FILLING_RETURN);( M2 H) b5 O! C2 K" L) t, Q
//--- Leave the fixed deviation at it is not used on B3 exchange
% W/ C6 N. J: V' g% N3 G" V+ ]- xnegocios.SetDeviationInPoints(5);
+ ~" T) u" O/ d2 n- M9 S//--- Define the symbol in CSymbolInfo...5 Q- _) b& \. l2 _" W
info.Name(_Symbol);
7 Z. `$ o9 w2 v) Q) g//--- Set the timer...
2 g$ A+ r& m- g3 q  \% M7 wEventSetTimer(1);2 W& R1 f' O' n+ s7 K+ v% V
//--- Set the base of the random number to have equal tests...
9 Y1 K' ^& m% t( L3 \0 |$ K  WMathSrand(0xDEAD);
0 i. s) q5 i+ W; G9 C9 [  }, dreturn(INIT_SUCCEEDED);
( W. {  ]( g4 @4 @}
# W* L9 g0 h, F( w& ]: a//--- Since we set a timer, we need to destroy it in OnDeInit().6 {) M- q* x. x0 S9 v$ ?8 E2 R  c
void OnDeinit(const int reason)
' H, a" w/ z% v: u{
3 V. s5 _. e: S1 [7 ~! W9 K: kEventKillTimer();/ }# |; m; J* W  e" k- o
}
  P- O# f! M7 p& f$ t! O//--- The OnTick function only informs us that we have a new deal
; K# ~* D2 B4 ^& vvoid OnTick()
% q9 A1 \+ n  D- O8 B# P$ b; K{
* J) b( e, P) Z$ o& dtem_tick = true;
: d6 O; D1 l) h! d2 X1 D) w}
0 C9 ]$ F8 V) x  |" R7 m//+------------------------------------------------------------------+2 `$ t7 o5 w4 W7 n
//| Expert Advisor main function                                     |
% `' b/ V/ P% v1 O//+------------------------------------------------------------------+
9 L( l: B: h  x) W" C5 ]void OnTimer()
% f! M6 [3 S& t; f/ ?$ ]{
, c$ ^  ?) K* E7 qMqlRates cotacao[];) A7 M' y' F0 V) W$ U: r2 l, [5 m( e
return ;
' y  N" X" {9 P* K% c% I* Wif (negocios_autorizados == false) // are we outside the trading window?- F0 r# K9 B' Z/ ?/ X
return ;
$ r7 d3 O+ |/ t9 B5 v7 c; k0 V! Y//--- We are in the trading window, try to open a new position!) r% p, @4 s4 z' B6 I2 `
int sorteio = MathRand();
( D+ C- Z( q5 W. V9 r; w5 w//--- Entry rule 1.11 P/ B) \& }; ]6 z' V( T4 r3 c
if(sorteio == 0 || sorteio == 32767)5 c+ @8 N$ T5 |$ f* m
return ;
! A0 I& V" t, R. Z; L5 D( K# xif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy( ]- y( o$ A6 \" u+ n9 }
{9 h2 d2 A- n9 s+ R$ T
negocios.Buy(info.LotsMin(), _Symbol);
4 B5 c& E$ L: X. \% W# l}
2 g2 z6 n; s) N4 Telse // Draw rule 1.3 -- odd number - Sell: l' H' T& @: d7 X, X& ^7 z
{
! r3 J+ c7 r% B3 }$ mnegocios.Sell(info.LotsMin(), _Symbol);  t; H' F- @& P( m7 g
}
: K$ E/ }  h( c: s5 `}
* M7 o0 E: i* k/ g0 d//--- Check if we have a new candlestick...# c( t; N0 s8 U7 S8 [4 O" x" F
bool tem_vela_nova(const MqlRates &rate)
1 m$ x( v; F* Q  O, N# {* j{* y% b7 [# I+ L6 K( f8 P5 F
{7 ^4 ^; g4 O5 r! M& Q+ ?1 Q
ret = true;0 Z" s6 a( o/ P! @/ o5 V/ x
close_positions = false;' v' ]) s4 E* ]* f) L1 y+ l7 L# b6 _
}
/ ?5 T# y9 _! W# [* V! `! celse1 {: J+ }7 R- v; J4 Z
{
3 |8 H  J& \. l% y6 F6 f# ^if(mdt.hour == 16)2 k2 C7 n  g; G/ v
close_positions = (mdt.min >= 30);/ k% o! a) s# H) ?8 G* h  o' z
}# C5 @8 Z0 H  g& t% \  y0 o/ R
}7 p) X; h) s# ~" X
return ret;
/ U/ ~4 P9 G! z& j}1 U# g% |1 ~% [; ^9 R! m* p
//---
0 D- o5 ~( s' B/ E" t; b1 tbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
+ ~# w6 Y* X+ ?1 p2 r/ y+ k) B$ x% b7 `{2 n9 O0 o/ Z) J
if(PositionsTotal()) // Is there a position?+ I# Z, J7 x9 c% Q
{
1 }: C% b4 Z7 tdouble offset[1] = { 0 };
$ ~, B5 r. F( V( ]0 Uif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
) a6 i% q$ _* p' U) Y' R/ @4 E& N7 \&& PositionSelect(_Symbol))  // Select the existing position!! |; x! M" X8 \& u' W# U  i
{
8 m/ M4 l) D& F% I5 \) }1 }+ H0 v. GENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);/ X1 `# F- C& T: E/ N+ M0 q& W4 K" k
double SL = PositionGetDouble(POSITION_SL);! W; @% Q+ r& D4 W0 [4 s/ r
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));1 }/ K) |% S0 l+ S( X- E" u
if(tipo == POSITION_TYPE_BUY)  u. ^/ B$ l, ]6 S5 E. {
{/ T! S8 |; F# x" M5 B$ O: Q
if (cotacoes[1].high > cotacoes[0].high)) x1 q, `3 q4 f3 ?; \8 P
{" i/ g/ t5 ]2 V! q
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
) W4 a8 S9 b; F+ G1 O: `) winfo.NormalizePrice(sl);
9 E; ]  R0 p" u! Dif (sl > SL)
( ^6 f  A$ B% d{: d7 \4 F! p6 q8 O- ^" Q5 S
negocios.PositionModify(_Symbol, sl, TP);: `0 X7 v: K4 X0 a9 s
}6 K5 P2 Y. |$ C+ |& Y
}
4 T( [% d$ z" }, F}$ y+ V7 C) a# \1 u+ A9 g
else // tipo == POSITION_TYPE_SELL! g/ s! l: N: z9 j: m9 F4 N
{
( [1 C* z  [' Lif (cotacoes[1].low < cotacoes[0].low)
# s. C) V  b" E  c{
# {; j8 n! F5 Z" T/ }return true;
: B# {" h, V/ [6 v+ `& z}* ~$ Y, B) F; R) |5 S: |
// there was no position4 g7 m# T  l  f: |- p9 W4 o6 G. }, b- |
return false;
8 J% C5 D  S* |4 D) P% `}
7 {) J7 Y: i% d/ Z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
- q" O4 C3 f2 C& n1 o( u/ K0 x$ N到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2025-11-24 20:30 , Processed in 0.646978 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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