私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& C2 K! z+ N( Q; M2 M* k" s6 z在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
' |7 w: _$ o$ F8 |, k! P为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
: Q; d. z  l# L. f以下是制定这些规则的代码。
! X* L* W' ?- B! F//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ b. j* I9 h& [# G8 B4 d  xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);# r! ]5 Z2 y( |* z2 `+ d
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" f3 e3 e, U2 A3 s
//--- Define a variable that indicates that we have a deal...
. d3 d; ~4 c8 q3 qbool tem_tick = false;
6 x" y+ F2 T  Z& j# X6 \//--- An auxiliary variable for opening a position5 A2 d( U5 Y9 E# m* t* F
#include<Trade/Trade.mqh>/ a* g1 y/ C5 Y
#include<Trade/SymbolInfo.mqh>; c! P! }$ K. _9 u7 O. j' J
CTrade negocios;
# [& E- C0 A7 j* p( r% |CSymbolInfo info;
  ]% V! k& i9 B, o1 b$ c//--- Define in OnInit() the use of the timer every second
$ g6 z( `* e+ U. n: r//--- and start CTrade( m  Z1 _/ q, `  i- ?- f+ w
int OnInit()" o! V5 Y! `, N  u; R8 Y
{
! W: c3 {9 Y6 v8 `! W) A) x//--- Set the fill type to keep a pending order
% q+ s5 S7 d2 y//--- until it is fully filled. `1 H1 c( O1 y2 ?
negocios.SetTypeFilling(ORDER_FILLING_RETURN);  |) S7 K, ]9 t8 L
//--- Leave the fixed deviation at it is not used on B3 exchange
! ]* Z& q6 p+ u' q, ~" j5 Anegocios.SetDeviationInPoints(5);  r4 ?% }& Y- r+ }1 ^0 ]( ~- V6 d
//--- Define the symbol in CSymbolInfo...
& C( G& A* v( F; @8 P8 w; j4 `1 A9 pinfo.Name(_Symbol);; d8 a- [) c; O' C
//--- Set the timer...6 ?" Y- w6 b; ?4 y, w' w) w' u* r
EventSetTimer(1);* @1 N0 s6 Z7 |3 d8 H
//--- Set the base of the random number to have equal tests..." {5 U: ]5 |2 Z( m: S+ V1 ^
MathSrand(0xDEAD);
* f) u! _- Z) G6 L2 u& U& `return(INIT_SUCCEEDED);; u6 N) R# l! u' U3 w4 ?& e4 |2 t
}+ F% p) F1 \7 r" I* g, @" a
//--- Since we set a timer, we need to destroy it in OnDeInit().' T; u" s: o" n+ B  V3 _
void OnDeinit(const int reason)8 s: a/ Z& R8 H: Q8 s
{) ?: [5 ?0 \7 ~  F. Q) l8 G
EventKillTimer();3 ~. C; ^: o7 r4 r  N
}# C4 ^& I* Q9 i9 L+ l9 ]3 o
//--- The OnTick function only informs us that we have a new deal6 p0 e6 \! G. P4 Q/ o2 T
void OnTick()
- s: c5 l- ?5 w& P0 w{
' P+ ?0 h6 G" A7 btem_tick = true;
: `' P# E! P) p$ T* S5 R3 }6 x' Q}
$ }& L% s* E& c5 Z3 `//+------------------------------------------------------------------+
/ z$ ?. L9 T- i3 }//| Expert Advisor main function                                     |
* n  m( f1 R, M9 h9 K" @' g3 O+ W/ b//+------------------------------------------------------------------+
8 q, C! X6 p4 Tvoid OnTimer()# Y$ g* ]5 P4 t( Z. H
{9 ^+ Y9 i7 U- m- m8 S+ f# d! v5 m
MqlRates cotacao[];
( J/ v) f6 K+ z# T6 v, }# Ureturn ;
4 c# c  x0 a/ H$ S' dif (negocios_autorizados == false) // are we outside the trading window?/ ]  T5 s% f; ?8 q  A
return ;
+ U8 I) L6 B$ q3 E- Y# ]//--- We are in the trading window, try to open a new position!
* T) w  m# D2 u; _: l& i8 ?' Yint sorteio = MathRand();" O* \2 C0 L  K8 K& N& ~
//--- Entry rule 1.1
- M4 j( X, o! u5 Jif(sorteio == 0 || sorteio == 32767)
2 `  @) D$ R! L9 m" dreturn ;
- l# I9 b4 B- |" H9 H2 Dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 \7 J& L* J- @  k9 u8 t  R
{' w3 H% T8 u+ {5 z; q. e
negocios.Buy(info.LotsMin(), _Symbol);
( G, H9 [, p! _. K; [}
5 J. I; V6 C, K% Relse // Draw rule 1.3 -- odd number - Sell9 S! N+ r' m' F4 _+ M, \& c2 {
{0 W" E5 S; P# }8 B1 ^$ E
negocios.Sell(info.LotsMin(), _Symbol);
! _: _% L4 ~9 ^# B, }) Z}& X& _1 F7 d# i& s
}) c- m( `  X6 s  y+ i; u8 G
//--- Check if we have a new candlestick...
& w8 A9 ~9 O4 Ebool tem_vela_nova(const MqlRates &rate)( Y8 O; ?; X) H  {
{( U/ J! N/ j5 L& A  C+ B2 H! t
{
, ]) ~$ V+ X: iret = true;
" X: R* K6 n, H% ~' hclose_positions = false;  x$ D8 Y4 X; Q! w! b9 Y* h, [5 z
}
3 }  P! l+ J* A/ i4 A7 selse
& G7 x6 b% I3 y% T; S{
! y1 _4 U+ Z" @) P3 L+ |1 C# Iif(mdt.hour == 16)
3 f4 p! H) T6 A* h; @close_positions = (mdt.min >= 30);! H  C' _9 g3 [
}6 C. D4 R; C* P' M/ M; }
}
# E' l$ l. t, q. Hreturn ret;! D7 J8 u2 t" m9 Q8 ]5 V' y
}8 w) f- H8 k. l7 P
//---  G6 }5 m; o$ M
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
% q: x, w0 F+ j3 d1 P; ^/ n" D{
$ x8 R9 D$ P* X: n& d3 bif(PositionsTotal()) // Is there a position?
- J$ `! z5 O; D5 j, Y{
9 D# r5 K& b* Ydouble offset[1] = { 0 };
. s+ [1 M$ P4 o, Q9 C; f. G( pif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?( m0 [: ]! r1 d2 Q
&& PositionSelect(_Symbol))  // Select the existing position!& P* S8 N0 r( {8 U3 `
{
2 F3 S: R6 d& R2 u+ vENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 }1 m+ a2 r2 l; P* k9 u3 }  xdouble SL = PositionGetDouble(POSITION_SL);9 T# w0 q/ B( y
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# ^. g! t# _  M  p% B& {) Q
if(tipo == POSITION_TYPE_BUY)
. g5 L3 c$ I) g5 i% w2 {{  d2 o$ x; ^3 m  c4 o4 I7 |: k, L& F
if (cotacoes[1].high > cotacoes[0].high)( [. M+ }, v% _+ z
{
) O; w& l7 r% G/ u- ]  E9 v4 u& Kdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];6 L& z$ ?' V- ]
info.NormalizePrice(sl);
7 ~/ k- }' S% ~6 i! Bif (sl > SL)" w& t, L4 c+ J# }# |- G
{$ a0 D4 U6 b1 j9 i$ o) z0 [4 z' ~
negocios.PositionModify(_Symbol, sl, TP);5 j' ]* |" n* n8 T4 Y  l
}
6 c; Q; e" K# x9 }6 w0 j}* o: \6 _1 @6 s7 U
}/ F; X+ o1 K1 v' T6 @
else // tipo == POSITION_TYPE_SELL  ]2 t& l" I1 r' X! i
{
; i0 j9 H, R" s" t( z( U' E& D( Hif (cotacoes[1].low < cotacoes[0].low)' _( w; c- C! a" L2 S) ?' D0 B& c6 N
{- j$ t" z- p# s. m# R" s) H
return true;
" C2 ]2 q/ _! D! f* G% J}
0 ^# X; i/ ?8 g+ ?# J9 \// there was no position/ n3 q7 ?8 `  L& z( n( R
return false;
& m& s: ]: n9 N& Z}
/ R/ ^+ g! @) D6 P我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。+ V* A% ~2 x2 D9 }) P. C# C
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-28 00:27 , Processed in 1.398537 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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