私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 \& m  J5 c% p6 _% _
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) @0 _- x6 O1 F; V/ o  v
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。" c) C" C# Y- V7 @2 Z: w& s
以下是制定这些规则的代码。8 t9 h5 ]1 p- T% E3 w0 g- Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...
8 x; N& J) Y' r" L5 c) C6 Tint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);3 r' g& u- ]' C2 \* b! j( }
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 [- N4 j& w# W
//--- Define a variable that indicates that we have a deal...# R7 d) M' _) t4 C* l0 ?
bool tem_tick = false;2 G( P3 n/ [" p( U# U$ d
//--- An auxiliary variable for opening a position( t8 Z/ p6 g9 Q! z5 X" j: ]
#include<Trade/Trade.mqh>4 i/ J7 k/ G1 Q4 ^
#include<Trade/SymbolInfo.mqh>7 z5 S; r2 n' a& m1 m
CTrade negocios;
% z2 D4 ^0 T8 T5 ~5 ICSymbolInfo info;
5 t9 V$ E; i1 w//--- Define in OnInit() the use of the timer every second$ q6 u' B  f/ e7 d
//--- and start CTrade7 f6 e9 c. {/ y1 ~! m" @7 m3 F: x
int OnInit()3 \( j3 B' Y# Y2 {& F) S/ t
{
) ^% q( K; o/ I/ p5 f% A1 v; C//--- Set the fill type to keep a pending order" d7 ]3 ~5 J' K1 g, @& a
//--- until it is fully filled6 \8 P: d& d1 [: G
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
1 c8 e0 N& @  N/ F7 P/ R//--- Leave the fixed deviation at it is not used on B3 exchange  d$ q# i  k7 n
negocios.SetDeviationInPoints(5);
& U1 s$ D7 Q8 ^% l. ~//--- Define the symbol in CSymbolInfo...$ I. w: Z1 i2 ]% X
info.Name(_Symbol);
3 B+ r$ Y7 G' G8 r: x$ ~1 U//--- Set the timer...
% L- S6 F* E4 s. Z9 M% mEventSetTimer(1);
3 Q+ \2 |1 `, L1 s2 ]. H& L//--- Set the base of the random number to have equal tests...9 a- _% L3 H5 N" q. H/ e
MathSrand(0xDEAD);# `4 g/ M. D+ ^3 A
return(INIT_SUCCEEDED);3 u" N6 v+ |( }+ A! D: [% D, L( D
}
0 h9 R2 z" A- U//--- Since we set a timer, we need to destroy it in OnDeInit()., P7 n  D* \- \9 k; r4 D5 w; b7 J
void OnDeinit(const int reason)
, G: D9 L' O2 i, D{
3 b5 s( @+ m' KEventKillTimer();
3 n: n4 i3 W0 ^9 \  ]4 q}
* f5 ?; x5 z3 u- ^6 Y4 O) |% Y- }//--- The OnTick function only informs us that we have a new deal" a% |$ X( J, ^% R
void OnTick()5 a/ j6 C5 S* }0 H# H  r
{
8 X2 J* j# j" d% gtem_tick = true;
+ B2 g$ N( l# h* }0 u}8 m( j) w) G* @
//+------------------------------------------------------------------+9 e; ~9 B. J( F9 H
//| Expert Advisor main function                                     |
' t( B% H) r0 S2 A" p; M; I//+------------------------------------------------------------------+
8 U* q9 e# J& p$ o* X4 vvoid OnTimer()
! h; y# h, g7 d0 K6 |! E{
! {7 P, |4 s1 m$ R  O4 QMqlRates cotacao[];% n$ T! [' G, o) \* {/ D
return ;
6 }) S8 J2 [: f& c$ H+ I' oif (negocios_autorizados == false) // are we outside the trading window?
* Q. K% X0 ~0 a5 h9 areturn ;
; y" e! u, Y, n# \//--- We are in the trading window, try to open a new position!
) @  s0 U( @' ?2 `, u. `# y! nint sorteio = MathRand();
5 q* }5 B% V0 K  G* {//--- Entry rule 1.1! ?% R* E* [9 e$ _
if(sorteio == 0 || sorteio == 32767)3 d* X# v: J% T9 v! _
return ;. |9 o1 H0 S* H9 `* v+ L' I
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy7 V0 d1 P! `) R# E0 M
{) H; f1 `7 F0 Z( Z2 V" [& ^
negocios.Buy(info.LotsMin(), _Symbol);
7 t! x% d8 H: Q/ P5 q6 L}
  R( ^5 k0 R+ A' B( Q% }8 n1 relse // Draw rule 1.3 -- odd number - Sell. K1 x) Y1 W2 k* `( X
{
: X% n1 _5 Y  z0 T2 o& enegocios.Sell(info.LotsMin(), _Symbol);
7 S. k! V( ^1 c$ S}! `- I2 t2 l. K/ x3 ~& m- O" B4 x
}
3 b$ y4 i1 S$ k, n+ c//--- Check if we have a new candlestick...& [% k$ `: P; X* W, @1 K
bool tem_vela_nova(const MqlRates &rate)
; ]1 y& H3 q% B  `7 g{7 j# C2 ^/ F, F" E7 e# r- G
{/ l) B! h; M" e3 ?5 ^5 U
ret = true;
% ?" {- ?3 j$ B% d' n$ j8 X. aclose_positions = false;8 _# |( h* H  \$ T; K2 E4 e: J
}
, x5 u% Z( Y2 @; h2 A: x/ ]else
: E1 M3 r' s" P' ]; Q' g9 U{8 m/ ?8 I# K$ o- X/ ?3 r* e
if(mdt.hour == 16): E0 b+ Q' m' r: J0 J* b
close_positions = (mdt.min >= 30);
, A! @, Q7 i+ c' }: c* Z}
+ m4 X, s8 \! {7 o' U/ T9 N}
. B$ V; Q  J. U! jreturn ret;
/ A, I8 U4 }% Y4 p1 K) u}
1 M% `7 ^# ]9 {' A6 d$ k1 h7 j//---7 e$ u9 I+ r7 _8 L& v
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 _' Y8 ~" A( N; J3 o{
* e  d7 L( s6 ~* Bif(PositionsTotal()) // Is there a position?% B  L" i4 v$ X% m
{
$ K0 [" H& v- u* C1 e, ~4 k( adouble offset[1] = { 0 };6 R. {4 ~+ {% v1 x) [" p
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 U3 T( g3 g+ A0 R2 b( d
&& PositionSelect(_Symbol))  // Select the existing position!
5 y! e$ }( A- _5 }7 r3 N$ K{* d8 Z1 N) X) t0 Y6 P, u/ R' `
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);6 g; V) V# \8 a' l; ~7 U
double SL = PositionGetDouble(POSITION_SL);
8 {/ P& g! n- P: hdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ w! C- F# N  Z5 n7 M4 c: x# }* W/ J
if(tipo == POSITION_TYPE_BUY)! Z" o+ q/ }: A% I. C
{
+ R$ `- M0 Y6 G: Y$ _$ N  Lif (cotacoes[1].high > cotacoes[0].high)
3 D! F# r9 Q# v0 s6 x8 K{
& P! d, |/ _0 e6 odouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* w9 B1 [3 E; K4 H4 G( f5 l& h7 Pinfo.NormalizePrice(sl);
) @& `: Q0 {4 \% Sif (sl > SL)
! ], J9 I" \2 w: l3 P6 ]1 }{5 ]" y9 Z7 ^1 S
negocios.PositionModify(_Symbol, sl, TP);5 ?" U! s/ M8 u9 A/ r% t& l) X
}
% R& A% S2 w9 G2 \, `, a4 |}; f5 _7 B: N" r; ^) r" @
}6 L) E/ [( A3 f8 d. @
else // tipo == POSITION_TYPE_SELL6 |1 ]2 c$ T; W4 r8 Y
{0 i4 N' E! M' F; F$ b, l
if (cotacoes[1].low < cotacoes[0].low)! l# g4 q  B8 L. i% {
{
5 M& T6 R$ G8 C: b' J5 ?( ?return true;
; [. A1 s( D; m9 l; ?& a}* @* J  x7 D1 B% T  p$ a( H% f* Z
// there was no position8 a" H+ H, o8 z" e+ i* }" Z
return false;. n4 \- L0 j2 B8 K4 V. z7 X& j3 T
}
9 x1 [$ _1 F7 A# x  v我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。  ~. |# X5 i0 Q% V  q6 t
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-7 08:14 , Processed in 3.009095 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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