私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 b( E* Y5 O5 _4 W在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' B( _# G( R0 p0 i& y6 k% d0 q$ W
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
, f5 X$ j' V# w& Z2 G1 `2 v以下是制定这些规则的代码。( ?1 R8 M# m0 Q! o4 B7 s$ @. V
//--- Indicator ATR(1) with EMA(8) used for the stop level...) C  g) C+ x" J- j1 Y0 I' b
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 L% i- F8 k+ Mint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);1 w$ b, S5 ?6 @7 x
//--- Define a variable that indicates that we have a deal...
7 \( `2 m; D2 {5 U* J1 L# rbool tem_tick = false;. y  X) @5 g' o# ^
//--- An auxiliary variable for opening a position
$ U1 c! s. s5 s2 c; |  ~/ R#include<Trade/Trade.mqh>
4 w; I$ q+ v& ]' ]1 \#include<Trade/SymbolInfo.mqh>8 W; }0 `+ M) s9 p# q
CTrade negocios;
) k$ @' A$ F* }( H6 ZCSymbolInfo info;
; s8 c1 E0 r4 y+ j2 \, Q9 e9 H//--- Define in OnInit() the use of the timer every second
, q6 w$ t- ~( C  n6 M* r  X//--- and start CTrade
8 c2 H( q5 B: n) |3 q" uint OnInit()( `+ v9 d# \* T" ~6 O0 y
{
8 |+ Q) U7 p% e+ A+ o6 d//--- Set the fill type to keep a pending order/ `6 \* J" \& ^# P7 N
//--- until it is fully filled
$ L  Z3 u1 _! s/ Rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);8 l$ Z# Q$ b, X# j+ u
//--- Leave the fixed deviation at it is not used on B3 exchange
$ ^* i1 F$ a8 W/ f  w  p' T; c% Xnegocios.SetDeviationInPoints(5);% s3 D4 g$ m# S( G9 L7 O4 B; @) }" w
//--- Define the symbol in CSymbolInfo...
" H, ~4 ~( u" Oinfo.Name(_Symbol);
& C$ A8 X* W4 Z) k  ~  \//--- Set the timer...7 O0 U" ^6 @* S9 A* r4 G
EventSetTimer(1);
4 ?- I. p( ?2 I8 k% }8 b//--- Set the base of the random number to have equal tests...! }5 V) K7 V% g5 V, w7 c
MathSrand(0xDEAD);/ M8 k: Z8 k. w" u9 m% F: f# d' s
return(INIT_SUCCEEDED);2 b8 c& O8 w' G
}
0 Z& K: K6 z1 Q( i//--- Since we set a timer, we need to destroy it in OnDeInit().! D7 m$ A0 f# z
void OnDeinit(const int reason)
* N2 I1 x* P" t4 p2 y{
  T2 |8 r, Q' p" uEventKillTimer();
$ \3 ?; U7 I1 I0 {}
) r: b. B. B7 y" ~$ w//--- The OnTick function only informs us that we have a new deal* z/ g6 h* u( V/ k8 k0 I, ~
void OnTick()
; {8 O3 O3 _/ u8 u$ D! X{8 z4 V6 }' j6 r0 Q$ ^/ P6 d
tem_tick = true;) [; r9 d+ _: \" @3 N9 j7 t5 j: v
}$ m% {( h, S8 H
//+------------------------------------------------------------------+
* U. D6 v4 `: X, s//| Expert Advisor main function                                     |0 @' J: S+ x3 V) b  G
//+------------------------------------------------------------------+7 p4 X% l1 s8 X! S; w) @7 _
void OnTimer()
6 `( [# V/ ?  U- B# i! Y0 F' B" v{
6 |/ j+ q* |# C( {MqlRates cotacao[];
2 m: H9 ~5 A; L( z3 P1 preturn ;! J  w9 h  }# e1 ^3 y
if (negocios_autorizados == false) // are we outside the trading window?
! u8 q4 r% a& E6 O6 Z  B, @3 Y6 j3 B8 Rreturn ;. b7 k4 w: C3 f+ V: U" o
//--- We are in the trading window, try to open a new position!
6 `2 j% k9 q6 \3 M( ~8 Aint sorteio = MathRand();. T* o. f) u+ e( F# r$ c( ^
//--- Entry rule 1.1
) D# x. R( p6 y* p. S0 _) Hif(sorteio == 0 || sorteio == 32767)
* _0 e0 v  L1 }6 u/ kreturn ;
3 G/ m+ @" M1 G5 u  Eif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
0 x! G, j% {- Z1 l( _5 n{) V+ {$ z+ l) b9 S6 G% q
negocios.Buy(info.LotsMin(), _Symbol);
4 I4 Q% p9 b1 W0 M}9 f8 q; x9 b$ B* j& ^
else // Draw rule 1.3 -- odd number - Sell9 W# t7 [; I) n' u# V
{0 x% X: D' L0 u
negocios.Sell(info.LotsMin(), _Symbol);, b  y6 K# W: \! e0 ?9 N5 e
}
* |& F7 W; d0 D4 _7 }9 d; |}
4 E# d) b( ~" P//--- Check if we have a new candlestick...5 q. u) M4 b( K3 B5 \
bool tem_vela_nova(const MqlRates &rate)
( {; N1 w  r+ c7 S3 \8 w{
3 v# C7 I* i2 l. }- i+ ]' B8 H& J{
( I$ l* G& v! s/ ^- Cret = true;
2 U) t! x" g6 s' aclose_positions = false;
3 R7 M  B# d2 `& l& x( @! q}
. k+ f. t! |1 u$ ^% r" |else9 x. \$ C8 ^# m0 @# H2 W
{
# \( Q2 K" ~3 H5 v7 Vif(mdt.hour == 16)
$ i: F, V- }% u" v+ I5 bclose_positions = (mdt.min >= 30);
9 i6 o0 g8 L2 j}
3 N) V! t+ T" @, {/ U4 K}- S7 K$ x% {2 x- o
return ret;  O. y0 s9 K) J# _2 ^! @/ P& a
}
1 V. U+ G( m# O/ h8 A# G//---
6 p% y+ Q9 H9 f" }& w% v2 n- B6 v% Ybool arruma_stop_em_posicoes(const MqlRates &cotacoes[])5 R& V6 Y, a) h2 @
{
! z6 k, {, _9 q6 r" r# M, a9 uif(PositionsTotal()) // Is there a position?  @- l4 d6 n; l2 v2 U& K, |; r
{8 [8 j  b: F) f
double offset[1] = { 0 };* D* f7 u* m2 S, }
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
! R, M+ ^; q% B' d5 Y  x4 z7 G&& PositionSelect(_Symbol))  // Select the existing position!1 a8 r( x6 o8 V& R( H/ }, P% Q
{, a, }$ a/ b5 e+ d- o, m
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
/ J: a# L) ]1 ^& S2 {& ^) Tdouble SL = PositionGetDouble(POSITION_SL);: F! T) @5 Q  R& v9 _5 s
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
& k3 r* J0 E% l" C' ]if(tipo == POSITION_TYPE_BUY)0 x& z- h) ^# @5 _" g1 R" n
{; X8 n) D& G0 b$ C
if (cotacoes[1].high > cotacoes[0].high). _& ?8 e; m6 d- _
{
0 L/ ^0 @/ ~+ i6 S4 zdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% Z$ P+ G) ^, I1 ^: Iinfo.NormalizePrice(sl);* ^  \) j/ p( t2 t( J( c
if (sl > SL)
4 \( {- q+ p3 A+ {& }: N{
( Z/ I- P4 z. }9 ]* N9 Gnegocios.PositionModify(_Symbol, sl, TP);& c# w6 u$ k' n! z; r9 U
}
( }3 \" [  \$ L8 O& q! u1 b4 j}. Y, \8 F- ^# M5 X# q
}
( R/ ^4 I- U" t& p! `else // tipo == POSITION_TYPE_SELL
- L  O3 d1 y  @{# |  }$ T8 o, O+ B& S. Y, x& t
if (cotacoes[1].low < cotacoes[0].low)( F2 i* Q" ?; x# B  o
{
" x( K4 L2 p( ^* W3 freturn true;3 _; P+ C( o; o; I+ i1 x' i
}6 [5 ~7 f( s, Q4 }# V1 e
// there was no position# |, x/ J9 B1 f! s3 X
return false;
/ U% W- Q9 \  w$ J- K}) I6 U7 g1 ?3 J2 Z3 e4 K
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。% }  w1 d: s$ j) U* ]/ p
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-26 07:24 , Processed in 1.267813 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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