私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
$ h) p& S& x1 D! m8 ]  e; F在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。9 y4 `2 M9 |0 n  i: y& y; _# X/ R
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。4 i0 c3 ~% m' g) ~% B( |: Q
以下是制定这些规则的代码。" C, a4 a: U# k" T5 ]
//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 Y1 y$ V- X6 s2 Q: L1 w1 vint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);4 i: B6 @7 g7 J7 y* E) A
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
( h& `7 x# ]7 g. W5 ?: |//--- Define a variable that indicates that we have a deal...
0 G; x8 t; n& [3 v: ybool tem_tick = false;8 h3 J) s3 ?& e' n8 i2 Y. o
//--- An auxiliary variable for opening a position/ A$ F, s: d% D9 D9 W4 p( V! V
#include<Trade/Trade.mqh>  [7 T: Y+ ?- E+ J. k% `
#include<Trade/SymbolInfo.mqh>, ^; ^8 M8 T2 d$ l" T
CTrade negocios;
1 f4 _& w/ _9 K0 W; q* U0 x7 qCSymbolInfo info;% R0 }; E. g6 H
//--- Define in OnInit() the use of the timer every second2 e. m5 Q3 B) a
//--- and start CTrade4 `8 F& E- W: H9 \
int OnInit()
, V: D9 g2 E4 q{
3 _0 {: |7 l+ q1 s# M! S//--- Set the fill type to keep a pending order" \+ V. D1 p6 e
//--- until it is fully filled
$ u! f7 x+ s, W5 T! b8 pnegocios.SetTypeFilling(ORDER_FILLING_RETURN);& L. \4 c  w( q4 d  B5 _
//--- Leave the fixed deviation at it is not used on B3 exchange
( g8 m7 C2 X& L% J7 ]% \: Q+ |6 P. P" ~negocios.SetDeviationInPoints(5);
- X" p" k0 m+ N2 `//--- Define the symbol in CSymbolInfo...
7 A- u5 D: s( @( Y+ K6 {info.Name(_Symbol);6 f0 W& c9 H# r0 P2 k" j1 \* x4 O
//--- Set the timer...1 {, x$ Q. ]+ a8 ?! ^$ u( F
EventSetTimer(1);6 p6 ~' I; a7 R/ k+ p2 K
//--- Set the base of the random number to have equal tests...6 ]# K$ w* h% E. A# k5 l$ g4 J" A
MathSrand(0xDEAD);
! N4 Z/ H9 k: M, u+ Preturn(INIT_SUCCEEDED);
2 t& t0 q9 [& D$ |7 C}
( T! ]# o) d" H, a2 U$ O, p//--- Since we set a timer, we need to destroy it in OnDeInit().; x3 X# K4 O( ^& p  q7 j8 _" W! v
void OnDeinit(const int reason)1 V8 Q5 z! e- H& a* H' b& f
{
: j" @9 [9 l$ J6 B9 a7 A; |EventKillTimer();
, l; U2 \1 h' y& |- K1 H, \}* F- [; h* |- L8 t* y* c
//--- The OnTick function only informs us that we have a new deal4 }' n! O/ f  ^
void OnTick()" ^5 I5 _5 ~  U  M3 U1 C- j
{
5 V7 D$ [7 X5 I+ l# n; d( qtem_tick = true;
( }& B0 c8 m+ {3 j$ {}
# V& z" h% w4 Y5 f! H' J$ I& [  F: O//+------------------------------------------------------------------++ v# `+ C( J% O# j9 [! h
//| Expert Advisor main function                                     |; k4 H% n0 D" C$ l) Z+ o) F  B' R, h
//+------------------------------------------------------------------+" X# t) K& H* D
void OnTimer()
# j  a0 H2 [& k6 Z5 f* Z3 A* x{
5 E6 W8 H0 a) ^0 Z' ZMqlRates cotacao[];) O5 F3 H( e  h, m; m
return ;
1 Q& `* ^& ~. x5 y( eif (negocios_autorizados == false) // are we outside the trading window?
$ `4 U* z. ]/ O  e( x, j: freturn ;. I( i  V4 O, O5 L, d9 c7 v( E! }
//--- We are in the trading window, try to open a new position!
1 p1 G4 d) S: K  W+ G! Kint sorteio = MathRand();3 n# `( C4 \7 z, H. J, A2 w0 Y
//--- Entry rule 1.1
" q# q- b6 B6 o* l# Yif(sorteio == 0 || sorteio == 32767)
' k, N; i, b% b4 Q6 F( g8 S6 `( G5 Sreturn ;
. O* E! ]: f! \, B9 @% {  Hif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ x0 V3 l; {& I) e
{: C9 [# B# l& `4 G8 v
negocios.Buy(info.LotsMin(), _Symbol);
6 A5 K' w6 c% Y) s7 Y}( {6 z+ X4 ~( d( p! Q( V* o! n
else // Draw rule 1.3 -- odd number - Sell' w2 D" P' u$ y+ t3 W
{4 c, ?+ `6 k1 V  m/ c) @, ^5 T
negocios.Sell(info.LotsMin(), _Symbol);
2 o0 G) f, n& j+ W}
+ v% Z* y- D# T. ^}
5 b4 G) o, |% _( F5 R//--- Check if we have a new candlestick...
* v2 A5 x8 q4 N; I  `- Z- ]9 Q; nbool tem_vela_nova(const MqlRates &rate)0 Z+ e3 A( d0 A! m& ^! M
{+ I3 S( F, ^$ o) x2 K+ Q8 V$ r, h
{2 x( S8 [( v1 }2 `9 P; g8 u
ret = true;
: e$ T7 s4 W) I4 }4 V  i- \close_positions = false;
2 X- }* d+ w8 k' C7 o- ?}% {6 \7 s& a2 m9 L# Y( [
else( ^: p0 t- `, H
{" L6 o* c& a  D1 b, }3 O/ F& P7 d
if(mdt.hour == 16)" r0 m3 n" @: {' s' Y7 H+ f
close_positions = (mdt.min >= 30);" W% p8 U' t4 r; K- Y
}8 f6 n( p" D, K/ c9 ?9 w
}9 O& m, a% i) [/ s/ h! K; r' m9 Z
return ret;
6 ]' r, F" y: O4 ]2 O3 U}
- A* H0 ~1 G' L' ]' K, {//---3 h4 y7 v# ]% U# g" U
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
5 W7 v1 f/ U6 p- I# `{& x$ v5 B- P; h8 c6 D
if(PositionsTotal()) // Is there a position?
) H1 E& _) l6 s' k6 M$ a+ R$ g5 A{( v# P: P1 x* z; L  E+ G( g, _
double offset[1] = { 0 };
. p  G. g/ L  A7 Nif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, U% k+ k$ K; J9 C7 M&& PositionSelect(_Symbol))  // Select the existing position!
8 j; y8 J/ B# q+ u3 Q3 T, j{& y- b/ M4 z" `: ?9 c% Y/ X7 O( J
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. N9 _# b/ k3 l, l1 j
double SL = PositionGetDouble(POSITION_SL);
3 U* P" ~6 L; B) f- Y4 X4 E7 ydouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
# j; |4 G) D5 sif(tipo == POSITION_TYPE_BUY)# t( B$ u% N# Z; {$ A  u
{
( j3 A% p) D5 R1 jif (cotacoes[1].high > cotacoes[0].high)
8 s" G  U9 b" L- u0 N5 ^{1 Q# Z: O, u* e2 [
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! Q3 m" j" v6 m0 x7 l9 V8 Einfo.NormalizePrice(sl);
) [- e7 k6 j/ R) oif (sl > SL)! `) t+ e* E0 x1 Z/ x3 n, `
{
9 D4 d8 J9 Z7 ]/ X- S3 Q" ^negocios.PositionModify(_Symbol, sl, TP);6 @" d1 I$ g# k1 d' N  Y9 R
}' y5 c. R) F2 M! C6 C& U
}- q3 w* g' Z  q( L8 `+ ^
}
) V3 O2 i% f7 v% xelse // tipo == POSITION_TYPE_SELL: d# j) s1 ~# M  h8 A; s: W* M
{
" e/ a1 [" Q, }1 E: Dif (cotacoes[1].low < cotacoes[0].low)
; _- E5 C! h, t0 k) P/ u{
: @8 L# G8 Y; W; |return true;' A7 D: S% D+ ^7 ?: p
}# w. L8 I) }4 Q( C+ @
// there was no position
& g1 D" h) h: h& lreturn false;
; f, \! @, L' I! R: L: s7 x}- n& I' c5 {3 [$ W# ?. T
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 v) R: I. B$ [/ L& P; S1 |
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-9 12:54 , Processed in 0.440211 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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