私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
# p1 Y7 R+ K* G6 D1 `: u在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 v1 H; I8 w. m" U5 \为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 O! s' j+ `/ `  T, p
以下是制定这些规则的代码。2 c9 }; ^6 m* |& t& l5 _" c& z
//--- Indicator ATR(1) with EMA(8) used for the stop level...
1 g3 Y- s9 e7 M3 U( iint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);' o- O7 U8 N/ t
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
7 r) q! x9 w! c( v" d3 B//--- Define a variable that indicates that we have a deal...
$ R! @" e, _5 i) jbool tem_tick = false;! V7 A& n6 Q5 T/ d0 L" K/ h2 m
//--- An auxiliary variable for opening a position6 @* ]! y8 M. B% U8 S7 g2 u) k
#include<Trade/Trade.mqh>
  {1 H5 `0 o/ v: H0 `#include<Trade/SymbolInfo.mqh>) C- ~' x6 U5 I7 O/ E
CTrade negocios;: p; I: A+ [2 g/ O. b
CSymbolInfo info;
9 V  s+ P4 y$ e5 \$ Y/ @: U//--- Define in OnInit() the use of the timer every second
0 `) w3 G- h! L. H$ ]//--- and start CTrade+ F3 X4 G" M$ i) P
int OnInit()* s( ]9 R5 \2 Q% r6 s, C
{
; B' {$ A+ S, `( ^9 G7 ?//--- Set the fill type to keep a pending order6 w3 m- z  [$ s4 S
//--- until it is fully filled
9 `# Y& N- _/ V0 w' Znegocios.SetTypeFilling(ORDER_FILLING_RETURN);: M* D# U, Q4 @" u7 q, R. g
//--- Leave the fixed deviation at it is not used on B3 exchange
0 i  e* y% _* ]2 e9 F; \0 l1 _negocios.SetDeviationInPoints(5);
( E2 x7 z$ }: \, B5 \" Z/ ^//--- Define the symbol in CSymbolInfo.... x1 h/ P8 y* o7 t
info.Name(_Symbol);: m0 B, {  Q9 b# D( ^
//--- Set the timer...- t4 s4 c" R& e  m
EventSetTimer(1);
& U6 v- W1 x4 C6 o! Z! _/ ~, X//--- Set the base of the random number to have equal tests...
6 E$ L4 C. m: vMathSrand(0xDEAD);. ^7 r" ]' d  ]5 [! r9 V/ d2 r
return(INIT_SUCCEEDED);  d) V6 J& \. t4 i0 }/ E- e
}
& _4 {+ Q  S5 T3 X1 H7 G1 L( ^" M& e//--- Since we set a timer, we need to destroy it in OnDeInit().
, E( W5 A1 K2 w9 T1 avoid OnDeinit(const int reason)
( ^5 ]2 p: H' P( A% y{  b! \/ ?4 D% @
EventKillTimer();) M" t( F2 j1 M/ a! F* b. J" |
}9 @- s- w& M5 v" O6 f
//--- The OnTick function only informs us that we have a new deal2 ?6 Y1 B# J; K" W2 k. u
void OnTick()# U3 g# V- N7 Z2 g/ S4 [
{
" g; s- w7 y# ^2 i* i$ \3 N" r7 Qtem_tick = true;
0 ]' \0 \) F( w+ _  x0 J}
  C  m7 F  R. Q' Z- ~//+------------------------------------------------------------------+3 _8 C# K8 g3 A, }
//| Expert Advisor main function                                     |. I* {7 F2 @+ _2 H- c8 [$ ^* V
//+------------------------------------------------------------------+
3 |- s4 Y  }% f1 O( gvoid OnTimer()
! b: J8 X/ L3 s# V  C, U{# {( `- ]. g7 C
MqlRates cotacao[];& z8 E$ R& `8 x
return ;3 @9 [+ A/ z4 @- v" V3 T
if (negocios_autorizados == false) // are we outside the trading window?( X1 f0 |: V5 ]
return ;0 P) B+ ?" a( d8 R0 z# |7 k" g
//--- We are in the trading window, try to open a new position!! O$ j7 t% r, q: u, Q6 W
int sorteio = MathRand();/ c& ?7 S) x3 B; `+ `; u3 q; b4 h
//--- Entry rule 1.1( x8 r* A9 t; |& x; y$ K
if(sorteio == 0 || sorteio == 32767)/ `$ W6 V6 M$ B2 L! S: ]6 E2 ?$ R
return ;
; y) [4 j8 p4 M1 ~. _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 F3 s" [7 c1 n/ z! e- Y
{
4 n/ G6 R$ G% O6 m- R3 E3 S" D* B: Qnegocios.Buy(info.LotsMin(), _Symbol);
. {1 X+ q8 `! y1 L  I. P}: A8 }% e, Z4 d- X+ }; t
else // Draw rule 1.3 -- odd number - Sell2 s; ^7 U5 z$ ]: U+ y
{3 G8 a6 m# z+ g+ X
negocios.Sell(info.LotsMin(), _Symbol);7 }: t. d4 w1 V- N  @, T/ p
}3 i! W* F  D4 i6 p
}! H& B  |! W- a: L
//--- Check if we have a new candlestick...) i. l& E$ y7 E8 _, P. f- d) O2 ?
bool tem_vela_nova(const MqlRates &rate)! t4 x- `8 w4 d" d% ^2 z
{0 o: }$ M1 C* y" u! ~: U
{
- `6 j. B( \1 [3 j+ n2 Hret = true;! Z' E  B3 Z# }" a: ?6 a$ S
close_positions = false;
. Q8 b) z4 x. P  a* d, g6 }! W}
! q3 \: x% f. Y, `, T& S! T9 _else& ^% v1 c! e8 s; z* i
{
& K) [# h2 x/ y3 J0 tif(mdt.hour == 16)
' C6 D9 N+ F' \4 O) v- q/ _close_positions = (mdt.min >= 30);, I" w1 T! e! i& z4 ~7 Q5 Y' K
}! ~4 R# w+ I2 U0 o$ X' o6 n0 Y
}
  Z$ h. o* b$ ~6 R& w( x0 R1 _return ret;' ?5 m) {  a$ l4 N
}% {! _1 v4 ?2 H. w' H
//---
1 Q: |3 C) x3 ~2 mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])) o5 b4 s- n4 ]/ d1 w
{9 J" {& Q$ O' k- U5 @
if(PositionsTotal()) // Is there a position?
8 B! J5 p. J5 @* W{4 _- o8 M) J& y1 z; H' C
double offset[1] = { 0 };0 t- q: k* {) J# D& i
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
' H( z& k% d% t&& PositionSelect(_Symbol))  // Select the existing position!( {. j4 a1 O: [8 E
{" ]- J* c5 q; {
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);8 s! Y2 e. C4 h" E
double SL = PositionGetDouble(POSITION_SL);
8 B3 k* @1 ?. ^* b( R, P8 Kdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# t. r  Z$ |4 y/ e2 o/ k4 f
if(tipo == POSITION_TYPE_BUY)
; x; n/ p; t3 [- n5 Z/ B{
  T/ p1 y; n$ V$ q8 {: L/ jif (cotacoes[1].high > cotacoes[0].high)
9 m/ c' G1 O1 m& u9 [. h{
. I1 _( n8 c5 pdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];) L* W/ e5 E8 A- x7 z9 ~
info.NormalizePrice(sl);3 u6 e" w# U  H" l
if (sl > SL)
' b+ u1 _+ u( Y- g1 \" _' p{
  @& _; z2 P: [9 R+ ^negocios.PositionModify(_Symbol, sl, TP);
3 s, V3 W1 ?4 [# ]}
% Y4 B  D5 y. H5 `}
7 y+ R7 V* e! L" B}
7 d' g% Y% W" y* v, T1 ^" m$ N) }else // tipo == POSITION_TYPE_SELL
0 S  v$ [6 X! \. i{1 @8 u4 W! i  b1 ?0 w5 b
if (cotacoes[1].low < cotacoes[0].low)
/ u) x6 Q. i% H2 x! t6 c{3 B. q5 y) C  @0 R! Y$ O
return true;
8 h. I# |  q3 }$ W}( v- u' K: l7 R2 e0 s) I4 l
// there was no position
% G9 i2 I2 g( R$ Oreturn false;
( g6 |$ x; d( X1 T3 `8 E; F}5 `0 E  C$ }3 M$ v: h2 a/ V
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
2 `+ r3 c. A, A8 [7 r/ s到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-24 00:09 , Processed in 0.980724 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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