私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 c/ }4 ]5 ]0 R5 V: K
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 w5 `0 U! c: D6 F2 e. ]为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。4 Q/ |- T+ @+ H# f& X) }' a
以下是制定这些规则的代码。0 P# e, m# v8 m& a/ q5 n
//--- Indicator ATR(1) with EMA(8) used for the stop level...
- {% a; E& T4 p; h- ?( ?, [int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);: k7 v+ L- e: A. h: R8 s4 _% q: T
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
% _8 ^" g2 w5 q1 D, d2 ?* Y: F//--- Define a variable that indicates that we have a deal...
# \; I) ]  J5 H) f7 ]1 Obool tem_tick = false;
( R0 @3 o% a4 A% o' u3 r8 f! E7 o//--- An auxiliary variable for opening a position/ n4 j& u+ N! H5 m- O
#include<Trade/Trade.mqh>1 j  i3 Z0 m! W# P" V/ |6 y
#include<Trade/SymbolInfo.mqh>
. p6 c/ h& x% v" {; hCTrade negocios;* g, a/ [* R) j% J- o6 i3 F! P( ^+ W
CSymbolInfo info;
3 N- W. J5 M5 D& U: X3 q" Q//--- Define in OnInit() the use of the timer every second& Q8 ]6 b2 }# w/ F, Q# }
//--- and start CTrade
( o4 r! ?: i. o" [) {% x/ w3 [int OnInit()
8 N  O9 Z4 c, _  W{
& |* k6 Z( z, I8 n  |. ]//--- Set the fill type to keep a pending order+ \5 U5 F) x4 H- z* v
//--- until it is fully filled
( B8 R/ l1 X: d- D# L6 Q1 anegocios.SetTypeFilling(ORDER_FILLING_RETURN);5 B+ |( i* G/ H! R
//--- Leave the fixed deviation at it is not used on B3 exchange) X5 w2 Y2 \8 _4 Y
negocios.SetDeviationInPoints(5);
- @8 I+ l2 z) |4 V6 Q" N//--- Define the symbol in CSymbolInfo...
& n" l( A  l7 u: n1 T$ linfo.Name(_Symbol);0 L  F; ]& G7 J
//--- Set the timer...# T9 E) m7 [, s) w- Y! m* p- i
EventSetTimer(1);  k" U0 ?& s0 G8 l+ c* N) Y
//--- Set the base of the random number to have equal tests...# X" A+ S. ]' d; {7 r% o
MathSrand(0xDEAD);
, m2 l( D" ?) x( x2 {return(INIT_SUCCEEDED);) p' F0 Y1 l* x% E" X: W
}
1 ~6 j* E0 K( [4 w//--- Since we set a timer, we need to destroy it in OnDeInit().4 F' P( M7 l+ ^4 X+ k3 y! E
void OnDeinit(const int reason)- g# x* t- M, ]0 s
{
% u3 m/ x3 ]( x2 nEventKillTimer();
. R2 A% Q$ p. o5 G1 l3 O}
+ p0 E, C* ^& u* @8 y//--- The OnTick function only informs us that we have a new deal4 B1 P& x. w8 N
void OnTick()
4 f- y) ?3 g9 ^: a% R, e{' r4 ?4 L( [% l, ~7 K3 J
tem_tick = true;, p' j( g1 y% l0 y8 w% V3 ^/ U8 @2 L9 R
}
+ q4 `: r  L' V$ d% v/ e+ ]" Q8 X, |//+------------------------------------------------------------------+* `8 @5 o0 N* i& X) h5 J
//| Expert Advisor main function                                     |
) s0 B% _! b- n/ L/ c//+------------------------------------------------------------------+
& D+ r) N8 E* ivoid OnTimer()
& T: _# f: s# ~4 {$ \9 _. Y; w! D{
6 E& k/ ]$ I+ @8 q6 d2 n5 g5 t* iMqlRates cotacao[];/ c8 n. I& n% G2 F
return ;
  C) P& u/ {1 Yif (negocios_autorizados == false) // are we outside the trading window?0 k/ k  Z9 d6 K' R3 A6 F3 T" n4 w
return ;
' M% Y7 \" v; B. @$ N6 B1 l) u//--- We are in the trading window, try to open a new position!/ u7 s( V+ j9 e, o) [+ l
int sorteio = MathRand();
& o- H! c% ~& Q3 D- Z  w2 k) i//--- Entry rule 1.1
5 p* @/ r' M  |) \$ N1 Jif(sorteio == 0 || sorteio == 32767)
6 u% K9 E9 H9 A; Xreturn ;
2 c8 o4 f. O! Z! Q2 Gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
4 q+ m- X$ Q  K( F( u{
) S& j: X, s7 [3 m9 R, A+ Tnegocios.Buy(info.LotsMin(), _Symbol);4 z: v: y- f# j1 O- u
}/ ]+ p' w) g* D; u
else // Draw rule 1.3 -- odd number - Sell# a' A7 o" n% r
{' ?* y6 `4 A9 G" _. e6 T% l
negocios.Sell(info.LotsMin(), _Symbol);, p/ Z& C% i( h% r
}9 g6 g' b7 u4 H
}( k2 V/ P5 U4 h, c. s9 b9 A
//--- Check if we have a new candlestick.../ `! v: E9 B1 S9 O! [
bool tem_vela_nova(const MqlRates &rate)
2 b+ b( _8 L* o* D6 D1 o0 G$ ?{
- B$ W( B+ B5 e6 {# I1 f{
( Z3 o5 J4 E% ]0 mret = true;9 a$ f7 O9 v# G% ^
close_positions = false;% O! G8 a; d( U& U8 Z/ _
}, Y1 l( ?/ [: G/ I
else
" ?% q# ~- `" ?{
7 {2 f3 f- G/ w1 sif(mdt.hour == 16)9 B7 B+ Y5 d8 a* X# b( N
close_positions = (mdt.min >= 30);% |# `  f; b& _$ }2 D
}; M4 F: m( M2 z0 c% x
}( C3 Z9 F# j: S! F
return ret;
* K( [: Y( J. X! S1 h}+ y) I/ a* V" M; P, O( b* v
//---1 G% t1 Z; j5 G: m$ k
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])6 ?, }. o- u4 X0 C0 S. Q
{" q& P* h7 z1 {; G: O8 F/ c6 A$ b
if(PositionsTotal()) // Is there a position?  E4 c/ O( [4 S& y3 P' w
{
4 U+ j0 K* I- jdouble offset[1] = { 0 };
: Y; |& ]0 B9 I% B0 qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' E& \, a/ ~0 a$ ^; _& ^* c
&& PositionSelect(_Symbol))  // Select the existing position!# r% ]& h( J. G$ j# Z
{1 u. p9 b! `0 E6 \, a* B
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& j! E+ f0 k) ]7 L! O; {* pdouble SL = PositionGetDouble(POSITION_SL);
  B- K+ ^3 C$ F1 vdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
& d6 P5 M. T$ ^0 L; r$ e8 f+ y7 pif(tipo == POSITION_TYPE_BUY)
4 w& l: N5 B6 D7 l5 V{
) s& M7 O  o2 I7 J8 n: w5 ]) V& M6 uif (cotacoes[1].high > cotacoes[0].high)" X, n1 k  |& g0 \. H  Y8 j) e2 w
{
8 \7 R1 B) P5 S6 C9 o5 V6 ~. qdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];) k, K5 a: A/ }9 a2 `/ q
info.NormalizePrice(sl);
3 ]# h# p" W1 E# j8 _5 q, H7 d+ Hif (sl > SL). Q9 }$ i# L& W0 S" N8 J! e
{
% X; u$ [0 f0 k& s' j( v1 inegocios.PositionModify(_Symbol, sl, TP);
2 @/ c% H# ^2 M9 r+ x}
0 G: K7 i# H& F: N% b# o}, n! s- f% ~& n. L/ C* R
}+ s& }4 Q' O) K( a; Q& @
else // tipo == POSITION_TYPE_SELL
$ |4 g8 i0 x: R; s- ~( ?( I{
, d0 f1 M2 {  @/ ?5 e4 ?if (cotacoes[1].low < cotacoes[0].low)
+ e: Y; }, L/ o) G* _2 L0 A{$ |+ E7 f. F0 B  v* p  q2 v# x
return true;, N, \/ Z$ C0 h% u6 p8 k
}$ C5 A6 g0 H! r
// there was no position. v' L; S3 J1 s) o) r; ]! ^* [, u
return false;
" @" h! f# @9 E5 O" B  A}
- c0 m# E5 T3 m9 {2 }/ Z, c3 ]我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。6 ?) d3 u0 W+ b, j- U, E& ]
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-21 23:57 , Processed in 0.412084 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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