私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
2 ^7 I+ A, h- k! W2 I! g$ ?在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。& V0 ]3 n3 M5 ^! }7 Y
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
- e$ ~4 {& e/ N$ Y3 s0 l' V以下是制定这些规则的代码。
! _: e, D/ I7 V! y( Y//--- Indicator ATR(1) with EMA(8) used for the stop level.../ X* r; l9 R; {# v
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 k$ z* ]# X- I" Pint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);6 ?" v+ J. j: t; ]. v
//--- Define a variable that indicates that we have a deal...
  B8 b; G/ [1 H: O2 k6 \# u, ybool tem_tick = false;
3 D' l/ T- U6 z- R5 t  S//--- An auxiliary variable for opening a position
( H/ ]: l8 e9 l5 e#include<Trade/Trade.mqh>6 q* [8 S) k8 R: b8 b( Z
#include<Trade/SymbolInfo.mqh>/ `/ m) \+ S5 r; G
CTrade negocios;
8 S' p* v: h$ `! ?4 lCSymbolInfo info;6 A- X8 H0 t" A* S8 m0 W
//--- Define in OnInit() the use of the timer every second$ o5 ^" ~9 B5 P- W  B0 `# G3 X
//--- and start CTrade
  r  c4 T' T, z9 |  @2 c9 A8 W! Y+ Dint OnInit()+ r+ [2 n( v. G
{
" |6 W8 p0 k! O2 `* c//--- Set the fill type to keep a pending order
4 g6 j4 _7 k$ e& ]6 L  R1 d3 }( G//--- until it is fully filled; ~8 t* }  L9 J! a3 p% `- U
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
) }/ g  S2 m! p+ W: S$ Q' h//--- Leave the fixed deviation at it is not used on B3 exchange- S3 M# t. \6 S% y
negocios.SetDeviationInPoints(5);
& W8 Q2 Q5 V& I9 a( H3 E//--- Define the symbol in CSymbolInfo...; u5 s' K5 r9 N& e/ _
info.Name(_Symbol);0 B, q, W% k. X6 i4 h) J
//--- Set the timer...
6 X/ _- g8 h+ m' K' \EventSetTimer(1);0 e5 F, e2 k: M8 M' d' I0 ^- R
//--- Set the base of the random number to have equal tests...4 S, }5 z* A6 O! g7 k$ r% Z
MathSrand(0xDEAD);- a7 r3 h5 k( u: o5 f$ e0 i
return(INIT_SUCCEEDED);5 T) f$ P+ Q- k8 O' p( i
}
2 K  g. e2 L, P& k# U" x! |//--- Since we set a timer, we need to destroy it in OnDeInit().
) B2 y& x# U6 }& cvoid OnDeinit(const int reason)
$ S) g) b5 Z  M8 J{! K3 M. I$ e- Z, M% O" V
EventKillTimer();  i- C( }7 ^3 b
}& z# _( |0 @* a2 s2 N
//--- The OnTick function only informs us that we have a new deal/ u& M* v. X9 g7 \! k  a
void OnTick()
+ \5 B$ U9 R3 u: \' C7 @* O, Y9 K5 C( R{# d4 h: c, f& j2 z
tem_tick = true;
9 ?; U6 U7 E" H1 x) a" c}4 G) k  z& P; ^, U! a# h+ d
//+------------------------------------------------------------------+) e$ M/ H. ]# z5 N7 R. S3 J
//| Expert Advisor main function                                     |. d1 }8 }# _7 X& Q- [& c
//+------------------------------------------------------------------+
# q; W" H; X# D/ Z% o  Avoid OnTimer()! K9 L& s" L/ C! X
{& m8 ~) \1 y" `) c# z
MqlRates cotacao[];" H. P/ ~6 s: e. N7 R# J% n7 S
return ;! o- S, s; d- E
if (negocios_autorizados == false) // are we outside the trading window?
% h  I+ u, i: k9 Y! ereturn ;2 x' w" @8 m9 X8 X2 ^5 k' {
//--- We are in the trading window, try to open a new position!3 o; t, R& a: b% {$ W2 @/ ^
int sorteio = MathRand();
- @5 n  d0 S& H1 L) M5 U//--- Entry rule 1.1" B. f# Z! f' I: g
if(sorteio == 0 || sorteio == 32767)& Z. |; E$ |% W' m, i
return ;
% }: V* M- e2 u/ a& U5 j7 J& e$ q3 Oif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
5 o' l& A( o" F  g# r6 f& @7 Y{  w% n% q; `0 }8 I
negocios.Buy(info.LotsMin(), _Symbol);. |5 }+ g' g: G2 S* _: V$ J
}( `0 A! O% J; I2 F5 {3 M
else // Draw rule 1.3 -- odd number - Sell5 y' ?% e4 h7 z3 i: r
{6 W: |2 l  U: E8 n- K; h
negocios.Sell(info.LotsMin(), _Symbol);9 Z& Q; S4 s2 ~( ?, r
}# o: j8 O& v9 N( b* v, v
}
7 O# q$ T9 E- K9 G3 x3 d//--- Check if we have a new candlestick...6 |/ H9 A1 y9 d# O
bool tem_vela_nova(const MqlRates &rate)# O1 P5 F: s/ A1 O- p7 U/ y4 @# G1 I
{
$ W- U4 c: @9 X( Z& j) h1 y0 `3 W{
. M' K" x5 Z+ f% _5 D$ W: b) rret = true;, k# r0 z! F0 a  C5 [
close_positions = false;5 b) G2 C" z( e2 i9 ~/ z6 Y
}
# [9 J' w7 v1 r7 i8 Kelse
6 u0 x7 @7 d3 M  V0 L{8 c3 r) N; k4 n: u  C. _9 {
if(mdt.hour == 16)
2 `1 s& g+ y+ f. T- }. F7 P+ Mclose_positions = (mdt.min >= 30);3 ]& F% ?1 O' n! v  c% I/ c5 |/ \+ M
}6 h; K, q% R& B' k& A1 L2 ?7 [( A
}* R- E) S9 \: [5 P9 x8 f
return ret;
7 V- d" t4 O) d6 I' P" l4 t6 D}
5 |( N* u, h$ G! Z' H2 j. u) n& _3 v//---
) r' z& K" _; K/ b% `0 C) }, H) Pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 S& A* x8 P7 q
{
' G  |/ G4 ^8 `2 u0 `' o% ~$ yif(PositionsTotal()) // Is there a position?4 R  o% u5 v& a: z
{6 T  Y7 B$ {" F( q, ~
double offset[1] = { 0 };
7 I# j) N: u8 Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?0 f, U# ^1 ^% a
&& PositionSelect(_Symbol))  // Select the existing position!
& s) A+ w0 L4 Z, i# l" V{' ^7 h- v  o! ], U$ e" y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);  `7 t, K$ b* P$ h( w: q$ ~
double SL = PositionGetDouble(POSITION_SL);, g$ h9 M9 Y" b4 F1 `3 N1 o& k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
! l* o( |( L, J0 @, d, oif(tipo == POSITION_TYPE_BUY): F2 ~4 ^6 Y4 ], V
{8 T  R* p8 @, ~3 W# g
if (cotacoes[1].high > cotacoes[0].high), |, L; K4 t% r" }% n9 I* V
{) C4 ?# G- ~9 i) ~# e0 Y
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];! b  ~# `3 M3 R  y- C% a8 D6 B
info.NormalizePrice(sl);5 w" e1 t2 T- v( T( O1 b
if (sl > SL)
* ]  L/ h: ~; G9 t( D{) r7 `! Q3 n+ ~8 O/ N/ Z
negocios.PositionModify(_Symbol, sl, TP);
$ e5 c' r2 f) \% {/ Z) w# w- q/ b# c}
) {4 k8 U- T1 D" P, {" A}  a6 E, i- }& A) R5 d
}$ D! H* u2 {# ~/ C9 W, i$ |9 {
else // tipo == POSITION_TYPE_SELL$ k* I% H* l& w- N: T
{
9 w$ u& t' O/ [2 [3 A* L+ @if (cotacoes[1].low < cotacoes[0].low)1 ~3 ?7 Y) w/ u& `. t
{) f  D, o) s# f& N1 ^6 V5 Z9 f
return true;
1 X% ?" i8 A! q% d0 n4 S}
: K) M  [4 Z  k% u) m( Q// there was no position% Q) }* p6 S. l- r6 ?, }' e; Z
return false;
5 Q! l" [5 {. C. s" s7 e1 u}# c" C9 z; H6 V) }4 [+ R
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: {' `& P* a* u7 b+ a到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-12 16:15 , Processed in 0.494806 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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