私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA5 i8 q  R' Y5 t' c& Y" b; B/ U
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' G8 n5 e4 M# A5 a6 }
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
9 w2 A6 ~* d& r; d% q/ e) D; f以下是制定这些规则的代码。) s7 ^5 D4 |( }% q. n% k& X+ |, H! H* J
//--- Indicator ATR(1) with EMA(8) used for the stop level...
; S" `# S7 X3 `" qint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  o* u+ O( b' L1 z) p; V2 Uint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
0 }' B  |! E! M9 B1 F5 c//--- Define a variable that indicates that we have a deal...
& ~" w( c9 F/ w: N# Q$ Fbool tem_tick = false;2 K& t$ A# r% e+ B. K
//--- An auxiliary variable for opening a position. j! F, h  t. [5 J
#include<Trade/Trade.mqh>, ?# `& `2 F. p; E( y
#include<Trade/SymbolInfo.mqh>
- \2 z9 V! n0 H/ F4 Z5 k" ICTrade negocios;, }- C9 X0 U; x& M2 H9 N, P" _
CSymbolInfo info;( C) y' t" N5 t; j$ X2 i
//--- Define in OnInit() the use of the timer every second
6 [! ^  [3 a3 W//--- and start CTrade
. a3 M8 I9 @. y9 k5 q+ \8 iint OnInit()* [- u4 M0 o) z2 f/ }. e
{
3 O- g5 r8 W3 A# Q7 ^//--- Set the fill type to keep a pending order
8 ~  Z4 B- T5 B, J0 A2 G; t//--- until it is fully filled, K1 G; n9 ]) f8 a: ?3 b
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
' v" B) z; w6 n//--- Leave the fixed deviation at it is not used on B3 exchange( x- o) k; ?9 R; d
negocios.SetDeviationInPoints(5);' {6 l+ f' J: R- {; Y1 m! d
//--- Define the symbol in CSymbolInfo...$ X8 E4 ^. o0 J7 R! g
info.Name(_Symbol);
6 f( N1 Y' m. r' i//--- Set the timer...
8 f0 \- s' ]" @& w1 P3 YEventSetTimer(1);- W0 v# N. j% F' w! d7 U/ d
//--- Set the base of the random number to have equal tests...
" d  F- f! e( C/ X% UMathSrand(0xDEAD);0 t' v$ w+ g& R7 V' i% [# u. L
return(INIT_SUCCEEDED);  V6 s: q8 z. b% @  a4 M
}
+ E3 i. R( `( A4 y//--- Since we set a timer, we need to destroy it in OnDeInit().
1 \: X& S/ n4 i0 Xvoid OnDeinit(const int reason)
$ Z9 t- p1 a8 `: Z8 U& v! f5 ?{
- j4 [- U' T% ^0 lEventKillTimer();1 I6 z, l8 w- P
}
% ^$ c& Y9 O: ~0 S1 H9 y! p# T//--- The OnTick function only informs us that we have a new deal+ a9 |# i4 S2 v- S6 s
void OnTick()
  a, ^7 V$ `0 h; D: S! g{
9 R4 l6 t8 ?7 z: T: \- y6 X% ytem_tick = true;; c6 D( y- p. z# d+ W
}( z* w6 m4 b6 X! @1 m; [6 B8 t% h* G$ \
//+------------------------------------------------------------------+0 |5 U' ~8 u- @$ U$ p% l1 B
//| Expert Advisor main function                                     |" T5 G& Y$ N0 h/ E! F; f
//+------------------------------------------------------------------+
( t0 `: p$ U1 c4 [5 ]void OnTimer()3 C3 y4 X) ?* U8 x
{
9 c, |2 O$ s& e+ pMqlRates cotacao[];! G6 k, o% o6 M$ H6 J& @- s9 P
return ;
7 I) q% I  s0 K, q1 Rif (negocios_autorizados == false) // are we outside the trading window?
5 S/ L9 _: H9 X- I! G& |) X( N! lreturn ;
, e+ V4 q2 J$ a: G//--- We are in the trading window, try to open a new position!
7 T- w! U0 ~$ ^3 N1 o% _int sorteio = MathRand();
. N3 P$ D* u3 [' {5 x//--- Entry rule 1.1- J, B2 v+ P* {3 \- r$ {6 s3 J* _( |
if(sorteio == 0 || sorteio == 32767)# z  ^6 Q) {& w$ v1 z1 y" ?  ?2 m
return ;, @: ^% u9 U3 Y0 x
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy* ^& ?* q9 w- G9 h! `
{
9 A+ ~& J+ ~7 P2 e& F# u. b! B0 xnegocios.Buy(info.LotsMin(), _Symbol);" z5 Z1 ~( o" C# o! T( I
}! ^+ ^5 A* @) C* w, i1 l
else // Draw rule 1.3 -- odd number - Sell$ n6 j3 x5 G0 e. V
{2 f( ?$ {% t4 {/ }
negocios.Sell(info.LotsMin(), _Symbol);$ E: ?+ i: Y  z1 L3 ]( i# V
}
! c' Y0 j; g! a  a2 w$ x}
! I# W" \4 l/ v; e. m* u  a- ?//--- Check if we have a new candlestick...
% S) z; n9 r$ R  {$ ^" o" L4 rbool tem_vela_nova(const MqlRates &rate)7 O/ c$ H) ]: {" V
{
; W8 ]! c$ ]+ E. T* i, S% F" x' x7 J{% ]- [2 |' o& x/ \
ret = true;
. v8 X0 W" o6 Y" mclose_positions = false;
. F& T3 ?3 q) P0 v9 g; j}6 d- v' ^0 `6 d! h7 ?
else
! l4 r9 E4 U4 t8 J$ \8 a{
2 R* B3 T& r- W% ]3 R, Vif(mdt.hour == 16)
/ T# U+ ~& H" q4 ^6 }. Tclose_positions = (mdt.min >= 30);, ^' E: y. y$ ^$ z# _
}' b- c& |1 X7 F- O: s
}
! S6 ]7 Y; j% D  B% oreturn ret;/ s1 n0 M2 d# h; k
}, e8 v4 v0 b# _2 c% P+ {
//---
+ E6 z, `% B" ^$ I3 ?1 m# sbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- x4 ]- I' q- o" x{# q6 g, o8 e7 B0 V0 x% k: ~
if(PositionsTotal()) // Is there a position?
/ d* V  L( U! y/ o" r{0 M3 f3 A" N# q" }7 @
double offset[1] = { 0 };
( }' H: \$ K% [- K" Q4 p. {if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?3 |6 ?) W- e  Z* V$ ]
&& PositionSelect(_Symbol))  // Select the existing position!
. I2 z, l' K9 R8 ^9 c7 i. l, B0 b{( M3 x3 [8 O. M! M) y" O/ f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
8 y+ p% m% U7 qdouble SL = PositionGetDouble(POSITION_SL);
* j( M- L7 T, Z: M+ r6 \double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; P4 }3 F- A3 ?: c5 Vif(tipo == POSITION_TYPE_BUY)3 O! ]1 R; m* `$ U8 D' l
{  L; ?  z6 a' h6 X6 L
if (cotacoes[1].high > cotacoes[0].high)) B$ \% j7 [1 H) J- u& R; V
{$ v; E8 ?! B! H3 E9 S1 S2 B
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];3 N) C5 e# `8 [4 E' T/ a* W0 {
info.NormalizePrice(sl);! M, |# x9 h, n% ~1 i1 |  q0 X
if (sl > SL)# W9 A/ i8 x' R! R( A% _  V  V) P2 \
{( ~: k4 i) c) @
negocios.PositionModify(_Symbol, sl, TP);
5 c0 u: o3 j& `+ e4 Y}
- F- E6 M& s+ A+ m2 ]. y. ?) W5 q}7 m1 ]4 O+ s1 P- Z7 w0 z+ q1 ^
}( n* I$ [9 e3 u
else // tipo == POSITION_TYPE_SELL
1 _5 I. ^. A' Q8 X! Z& h{' c! ]3 J( @* f* W
if (cotacoes[1].low < cotacoes[0].low). I4 c2 E( {- r. [+ u# S
{/ N* |& D; @) F: R" \  ]
return true;
/ w! }' w, J7 D3 z}
/ U6 m$ D$ j1 i+ X// there was no position, ^& i. f6 k, |& D
return false;% m& q6 W8 b% b' [& M
}
) v1 m" u" A" _/ V3 P9 t! \我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。5 T. f6 N; [5 m
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-3 03:16 , Processed in 0.474418 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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