私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& l1 n4 f9 g) C- L; s( c% p" w! j在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 K9 ~0 F6 b* T/ c6 e5 z, |" `" ?为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。* V3 q" W7 J  n
以下是制定这些规则的代码。* [, d3 f* Z* v2 E1 u
//--- Indicator ATR(1) with EMA(8) used for the stop level...
4 r6 e& s  g8 l( A7 bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
+ o* r4 H  V& Yint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. M: G0 J* I! a, I9 X//--- Define a variable that indicates that we have a deal...
7 c( U8 V5 t" |- Q' Bbool tem_tick = false;$ L0 F, }& O6 g4 R/ p
//--- An auxiliary variable for opening a position
5 U1 k/ \% L8 _! S% `  B#include<Trade/Trade.mqh>
8 i6 W7 }+ V' _2 R0 E6 t#include<Trade/SymbolInfo.mqh>- A  u- j. m+ p5 A$ g
CTrade negocios;! |7 b* m2 j* m3 C! j" n$ S
CSymbolInfo info;
$ e/ w- A, \% _/ a; c//--- Define in OnInit() the use of the timer every second
  n- |' A, S# w/ n% c5 n//--- and start CTrade* O; L$ ]1 z2 u3 m& P* v
int OnInit(), L: I% a0 q0 t7 P8 \: S3 R
{
8 K' w3 B+ {% n8 s) ?" Z//--- Set the fill type to keep a pending order
4 g  `& G, z3 |+ j0 R; ^//--- until it is fully filled5 j# j5 I( J) K; |) W" z  n( O
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
5 i) e9 y( L$ I  Q9 |6 U//--- Leave the fixed deviation at it is not used on B3 exchange
* ?! y. D% C* f' y1 vnegocios.SetDeviationInPoints(5);
. ^/ r* G' [, K5 S# ^//--- Define the symbol in CSymbolInfo...
* q& k! _' D/ U' Linfo.Name(_Symbol);
& y) j" M4 B6 h/ [//--- Set the timer...
1 H; n2 H: e/ K. E5 PEventSetTimer(1);  s2 q5 T- g* m6 F6 W6 h3 I: a. r
//--- Set the base of the random number to have equal tests...
' B7 S  E, `! j' s2 xMathSrand(0xDEAD);! H& B9 K7 p% b" Z- P9 |
return(INIT_SUCCEEDED);; T0 g# c- B1 p: R! i
}
! R& C: s$ b% Z# ~* u! Y% K# U//--- Since we set a timer, we need to destroy it in OnDeInit().
1 C+ H9 v8 ?2 H) Wvoid OnDeinit(const int reason); X3 i+ V+ ]4 U# J& ?$ h
{  p5 t- @; I- I% U
EventKillTimer();8 y  {4 y' Y$ P; ~
}, [/ G( `9 x% B4 c0 ]
//--- The OnTick function only informs us that we have a new deal; D0 p; r  I) N* l' G
void OnTick()
0 `7 o* w& ?; x' r4 ]) w{
, v  X( |5 B4 l9 ~tem_tick = true;
8 N" F* F) t+ @  e- y+ z3 Z}1 G. B9 ^) p8 L. k/ [! [
//+------------------------------------------------------------------+) I  {' A- r, `* y  n0 B
//| Expert Advisor main function                                     |8 m) ^) b0 F% u! u' V3 b% C
//+------------------------------------------------------------------+, J) A; y7 U' p$ J& b+ Q2 M
void OnTimer()
  `% f$ Q! _; _; z* b{0 ^9 j$ H- P( j8 V$ y
MqlRates cotacao[];
! C. W. `: l* x( J5 lreturn ;! r" b7 z8 G0 g$ X+ _
if (negocios_autorizados == false) // are we outside the trading window?' g0 R% E) @$ z" t
return ;& b7 p+ W$ d9 a; O8 t- e7 b) X
//--- We are in the trading window, try to open a new position!
5 o& D9 {5 s2 f- V* T! Bint sorteio = MathRand();
8 z* A- c$ }) r6 ~: ]//--- Entry rule 1.1
4 ^3 c/ M: @/ \' n8 fif(sorteio == 0 || sorteio == 32767)# B/ q0 `4 I# @1 A2 S/ O$ k5 \
return ;7 ?7 z4 n3 Q+ W7 _% [7 B# Y' \
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: P' i* t" Y; g4 m{* q- |' H! l/ T5 A* S8 n
negocios.Buy(info.LotsMin(), _Symbol);
, a$ i! b0 k4 |}2 t1 e+ Y4 K0 e3 x1 q3 G
else // Draw rule 1.3 -- odd number - Sell
  R5 I0 y# ^# W8 W{) p; `3 j2 Q6 N$ H' y
negocios.Sell(info.LotsMin(), _Symbol);
+ O; Y9 t& ^- a6 p& h}
3 [) E9 r* i) `& i% \7 J( `' \2 V}# h, t$ E* p/ {" H/ C+ p2 c  D
//--- Check if we have a new candlestick...
- Y9 Q! |! l/ M7 e; {1 v% `bool tem_vela_nova(const MqlRates &rate)
4 l& \* o7 j' d5 k. s# Z{
3 q3 Y; n6 N" p4 x{1 ]/ o# N5 a- K& `" ]$ ^# j3 F
ret = true;- o. X6 L# }7 T1 J0 E8 P9 b
close_positions = false;. Y; b. ]2 q) Z) O! c% I% l
}; I; R5 F. o7 H$ C" _
else- F( X. h8 `9 P3 G' O% ]6 i9 p( Q
{6 j. h: z, M! u
if(mdt.hour == 16)
* b3 H( P  S- n4 M0 ?- r2 d$ E, kclose_positions = (mdt.min >= 30);9 r" f6 D$ i2 ?: N0 X2 L8 h) ^
}
' _9 O! e# C) F9 A7 H7 s# d* M}$ g' h5 ]/ ^1 k6 |% @
return ret;( @, Z2 G( u( R) o
}
( a0 r3 _# o' w0 @# Y( H# d//---
3 ~* q) C- x  p1 Y& L0 H5 n& m. Qbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
$ I( X% i6 x) B0 ^0 k$ \{
( r) ]6 k8 f' R3 G  b) ?$ @2 Rif(PositionsTotal()) // Is there a position?
# J$ Z% A, M  _- O' m: P8 J- h{
/ N1 O6 c( [, v1 y# d+ Kdouble offset[1] = { 0 };
* ^* Z" ]9 E" rif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 m5 V" j0 W) d: M2 M& O
&& PositionSelect(_Symbol))  // Select the existing position!
9 J8 A& w& L- G# T! I  i{1 B$ T  |; a: _. G* P- [
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 I  m5 _& x+ l. A! Kdouble SL = PositionGetDouble(POSITION_SL);
6 \, v) f+ p( A# `& S( Qdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ j" O1 Z' L$ {: l5 s) p
if(tipo == POSITION_TYPE_BUY)0 w2 J! L' ~( c5 I+ Z; V# p
{
" t/ G8 L" B5 T: nif (cotacoes[1].high > cotacoes[0].high)
, F. t* t  U, `- p, Y# n1 f{
- y+ o9 P$ k: L; q4 Bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];5 X9 V8 J9 l1 T9 d! i
info.NormalizePrice(sl);
0 l2 K$ X1 j7 V! V4 xif (sl > SL)# w. [" M9 l9 H1 O2 g' o
{" P& i8 _2 ~. A' ?5 g( G7 c
negocios.PositionModify(_Symbol, sl, TP);1 g2 U; _( Q+ q
}: w1 N3 K3 O3 ?
}2 E' @7 M2 h# q- d
}
& G' ?/ e' q) W9 C, u2 T* oelse // tipo == POSITION_TYPE_SELL6 S. R$ n; }& y8 E( Z- M
{% z- \% P- x  c
if (cotacoes[1].low < cotacoes[0].low)
- x1 t5 C  }2 [; G; W1 J{9 u: ]+ h/ n" y. {# G7 l5 V
return true;; ?( Z3 l8 k( J# b6 W2 R
}
  T5 O: U$ i! S4 E0 A& ]// there was no position3 j" R! G+ p3 a$ @
return false;
% `7 j6 {, A0 B* Z9 x: N}
( E' O* g8 y" z0 i我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 e, I4 S9 v+ ^( Y, [' h
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-25 15:51 , Processed in 1.307071 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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