私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* Q# y  V# x2 R% ^在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
  s3 ~: v& c2 k1 F2 M; U+ h" E, [为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
$ t6 v% z% g% k" _" J以下是制定这些规则的代码。: x# _! E4 w: ~4 V3 ^. m
//--- Indicator ATR(1) with EMA(8) used for the stop level...& c* r$ F" l6 d6 O# T; {
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);% v3 a" F$ o$ k7 d# A% }
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# Z# H' u( ?" r//--- Define a variable that indicates that we have a deal...4 w0 k% r: Y9 |9 h# b: K# A
bool tem_tick = false;2 U  |" B6 F+ _# D# O/ I8 e! `
//--- An auxiliary variable for opening a position  c# y" X5 X9 J$ ^- a0 \+ `
#include<Trade/Trade.mqh>4 d/ i) p) C5 ~5 c9 v2 N
#include<Trade/SymbolInfo.mqh>
, _$ |; w6 `8 T& \4 b$ J  NCTrade negocios;
* x' G6 U; ^# r0 o9 Q" l8 K( BCSymbolInfo info;
/ P8 H0 ?2 d+ a/ p/ b//--- Define in OnInit() the use of the timer every second
5 @2 W0 }3 m( E//--- and start CTrade" f+ Y9 C, J4 |9 y* Q7 M' e2 I/ `
int OnInit()
$ Q( [4 a( O! R( f8 n{  ^$ p9 R7 y0 t: V# f/ G
//--- Set the fill type to keep a pending order
; c0 @' R3 e/ `0 ]//--- until it is fully filled
; K4 P. _7 |/ s) d. K) n% rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);/ C- J) i/ z& F6 G: ]
//--- Leave the fixed deviation at it is not used on B3 exchange
1 B9 I& L; N$ k2 Ynegocios.SetDeviationInPoints(5);
0 Q6 L  K7 O3 |+ l! T& L- t//--- Define the symbol in CSymbolInfo...8 C) Z  U3 W, f8 z3 ]0 ~+ a& z! |
info.Name(_Symbol);6 s9 a# ~8 X2 Y: {
//--- Set the timer...
$ n; y! o0 Y1 a- d- EEventSetTimer(1);' n8 ~9 [# s9 n
//--- Set the base of the random number to have equal tests...  L) M0 ]* |# C5 Z' j
MathSrand(0xDEAD);! r8 j- t2 C! y
return(INIT_SUCCEEDED);2 _9 K% a, k1 A( a
}0 D. N! p! w0 o+ \$ W
//--- Since we set a timer, we need to destroy it in OnDeInit().0 @: h1 F3 j6 f8 L
void OnDeinit(const int reason)2 `: U; s8 w2 @) ?
{% v6 N% j9 x8 Q+ G5 ?( y. E$ ?
EventKillTimer();- o0 O8 E2 l, h) Z* k
}
- ~# h7 ?: l# j; |% g, u* L//--- The OnTick function only informs us that we have a new deal
; M% m& f, c+ t4 dvoid OnTick(), N6 K4 U$ n5 G
{
/ \2 _% x) E4 s6 k. }- |( p  jtem_tick = true;
# _8 Z( F# r4 `: ^! t- j/ b0 u+ g/ g}
0 k& p4 f: r1 F3 y( A//+------------------------------------------------------------------+
! z! `3 d8 H* f3 s9 U, @, y+ x//| Expert Advisor main function                                     |% z$ S- X8 L7 _0 @, ?
//+------------------------------------------------------------------+
8 b  b: d" V9 C9 x) Bvoid OnTimer()  v. E8 a! `3 J6 h( P- _: P4 W4 `0 l
{# X, ?2 d& `. z& [
MqlRates cotacao[];
6 _; @. k( l9 V; |+ nreturn ;
; V7 F2 k. x( U/ _- u( Wif (negocios_autorizados == false) // are we outside the trading window?5 O* A/ m& A" b' H7 O0 T
return ;% G2 ?/ ~0 @9 z' Z
//--- We are in the trading window, try to open a new position!
6 c; l2 ]2 T4 j$ K! E0 f% Jint sorteio = MathRand();3 Z2 }( p; p8 v7 E
//--- Entry rule 1.1
& J( F) {  Q9 ]7 i; Jif(sorteio == 0 || sorteio == 32767)& O  ?2 _2 u7 c. ^9 X7 k
return ;- G# X3 i: ?/ k$ Y" Q$ P/ g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
' P0 n$ s0 s" m2 t, {* O{7 _9 h, V6 t1 h$ C, l$ |" W
negocios.Buy(info.LotsMin(), _Symbol);4 d1 J3 g9 |2 O' \4 H3 P6 n
}
6 n8 F! h$ D1 ]# [5 j# ielse // Draw rule 1.3 -- odd number - Sell
1 \" g3 Z4 P9 ^{: E( Z/ ?( d8 v9 b* N
negocios.Sell(info.LotsMin(), _Symbol);0 X% E1 V% u% Z) M* @, ]  \6 O$ q4 N% d
}
$ ]% B1 `  @1 n" ^" p: ]}
  `4 Z3 G/ Y9 i; ~, I7 \4 z//--- Check if we have a new candlestick...
' j5 c8 ]0 E! T* Fbool tem_vela_nova(const MqlRates &rate)
6 b+ f2 J, C/ {{$ |6 [# H0 C/ ^7 ^6 _8 B
{
) w; _- C8 \$ k0 }" Aret = true;
7 [( R  w3 |  b+ C+ T3 Gclose_positions = false;: ]- x2 w( J# B) x3 y& f
}
6 s7 X. x5 Z4 }3 C4 M* n, g, ?4 `# telse. f( j4 u* E3 ?1 a
{
& q2 t, z$ D9 ^) _9 Mif(mdt.hour == 16): f. T6 w. g3 D& w3 K+ I1 a2 U
close_positions = (mdt.min >= 30);3 E6 W+ f( s+ S9 F" k
}
* M* A# O8 D! O' V}' Y' h, V, ?- T  _6 G
return ret;: S6 {: H) A( Z# x
}& m5 e6 M& a( Z/ s: H1 j% S
//---% T- T+ b9 {+ U/ b
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! ~7 ]7 R& \( x9 ]3 K0 N
{* r( F, y, b" ~: w& U+ Y) v7 J
if(PositionsTotal()) // Is there a position?( M9 B% Z) c8 l3 s$ _+ `
{
* _* Y" O' ]: X% S( |double offset[1] = { 0 };
& |; g+ P9 _# N% x1 Oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ ~# e2 \4 i0 L( B2 \- @& |$ X
&& PositionSelect(_Symbol))  // Select the existing position!& Z2 G  R4 ^& P& ?1 P5 V
{
+ J0 y, l1 V6 Y; ~& x7 T8 o8 W3 I( OENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);, j/ F% E+ [9 @3 v- ^! f7 T9 K1 x& \, V
double SL = PositionGetDouble(POSITION_SL);/ M# G3 h& D; q2 @# W: H$ {
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
. A  s! F8 u5 ]1 Q4 Oif(tipo == POSITION_TYPE_BUY)8 V2 W/ {, L2 W+ H  L
{; S7 m5 Z$ a1 U. K. B( \$ l
if (cotacoes[1].high > cotacoes[0].high)2 F' d- P1 o  p* d& ~
{) C4 M* q8 ^% s+ X( V
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! @/ Q& _" R' K7 k% B2 x# f& O4 q' ?info.NormalizePrice(sl);2 a+ t1 ^6 Q3 J' ^
if (sl > SL)" V4 U3 m+ v5 B2 B6 v4 Z, V$ r" C
{
0 u) j. e% L  h; c$ D! cnegocios.PositionModify(_Symbol, sl, TP);
" n  v2 g9 x3 |% M8 m+ T  \}
* j& T$ u% E; O" E+ P}6 g+ u# B5 z! [
}
$ r) u: G) E/ k! ], V4 w( b" C) qelse // tipo == POSITION_TYPE_SELL5 s" v+ \. H2 i7 g3 v2 n
{
; T  ^1 _7 m' D+ lif (cotacoes[1].low < cotacoes[0].low)9 I9 T$ C" U( Y5 |8 ?) f1 Q  ~" @
{
7 _5 {+ g+ ]5 |return true;3 Z, z' {% @  `2 f7 z
}6 N( K+ m3 a& ~% o! M# X: v
// there was no position
9 B1 C( U4 z! l% J- ^return false;
$ g# l" v7 u+ Q7 u) T/ C) D2 T  V}1 w/ x& F9 W. O: ^- K! W
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 n; o7 e0 S1 R( @7 R% g到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 01:45 , Processed in 3.269928 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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