私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA/ \9 D# P3 S' q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
, v5 Z3 S& ?5 R1 N为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
8 @& L; V" F& H/ M以下是制定这些规则的代码。
+ X. C2 \& t3 H//--- Indicator ATR(1) with EMA(8) used for the stop level...
7 ]) O* k" T+ A* o( g0 V+ Eint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  s) W8 B0 b% E2 I: Jint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);; j% V! {6 W2 l3 Y
//--- Define a variable that indicates that we have a deal...1 l- F# ?: c) E1 y7 Q" e
bool tem_tick = false;5 S5 b# i% v: L8 i( E' p6 b
//--- An auxiliary variable for opening a position
  x6 ~3 V7 ]/ h+ Z& V  }2 |#include<Trade/Trade.mqh>! k& b6 V8 D0 {0 i$ P
#include<Trade/SymbolInfo.mqh>
; d8 B8 t' Y: j4 r/ lCTrade negocios;
% B& r4 \4 R7 s: i5 R7 |0 T  JCSymbolInfo info;
& J5 Z$ X  a' V& e0 Q1 u//--- Define in OnInit() the use of the timer every second
' [4 A( |$ h6 X, i//--- and start CTrade9 l0 ]$ r2 c9 z5 [- i
int OnInit()
) q! F- p' R! V' k) x9 q" g' Z{
7 J8 j, A6 {, n: U//--- Set the fill type to keep a pending order; U8 h' H( v! x% f
//--- until it is fully filled) [7 C1 B: L' U& U+ L, a* Q2 r% O
negocios.SetTypeFilling(ORDER_FILLING_RETURN);/ g' @3 N# p, q* R9 s
//--- Leave the fixed deviation at it is not used on B3 exchange
  u6 l3 h# A. _$ v: ?negocios.SetDeviationInPoints(5);
0 W% b6 n# X4 j9 y9 |//--- Define the symbol in CSymbolInfo...1 d5 X. w  d/ K+ g! M3 J
info.Name(_Symbol);
: i' n7 G" B$ T8 F) i+ N$ o//--- Set the timer...- \" d( Q+ V% X- w) J
EventSetTimer(1);) F! k4 ^9 P$ @2 S$ H
//--- Set the base of the random number to have equal tests...
# J8 j9 r* g1 h% D0 ^MathSrand(0xDEAD);0 K' }8 t! D5 \  }9 i4 @1 ~
return(INIT_SUCCEEDED);
' ^; h+ ^  T- H0 X$ G/ |" m( _}$ p' K; @1 x2 d8 ]) A
//--- Since we set a timer, we need to destroy it in OnDeInit().6 ?" Q+ t  U- }- E; q
void OnDeinit(const int reason)
8 I8 {0 W. ^. F+ ]2 U* E{
1 i3 H( x) [* j$ vEventKillTimer();- a& X6 t) r9 k6 p, _
}7 w( e! ^6 y( _( Z2 R/ K* G/ Z
//--- The OnTick function only informs us that we have a new deal
; L% U" M6 Z; R/ Svoid OnTick()0 _: I- Z# l- I: ~7 N; W+ \' u
{
( c/ x6 m1 v! ^) H5 o4 otem_tick = true;8 b. V/ l: o( e& [9 X
}
5 N" b" J$ ~  `# H( D5 \//+------------------------------------------------------------------+
  b$ Q# P/ ~! u* l5 B//| Expert Advisor main function                                     |
) P1 @+ I5 d& p1 c. ]/ n* \//+------------------------------------------------------------------+& c7 \6 l: V* J4 b
void OnTimer()
, \7 H( |2 f* F3 X{) [( |# |* Z) I
MqlRates cotacao[];) g7 j5 g6 R- R/ n
return ;+ s  o' t" @2 v; p! O
if (negocios_autorizados == false) // are we outside the trading window?# z% Y( u( [/ f: q7 W
return ;
: ]. e: L( l$ Z$ F) b1 P3 a//--- We are in the trading window, try to open a new position!
2 W5 r9 b  W  u+ {8 N1 K* Xint sorteio = MathRand();3 f% S/ A9 Q  ~, m
//--- Entry rule 1.1% {9 q: _, C; x$ s' ^
if(sorteio == 0 || sorteio == 32767)
' g0 v. x* A8 W, E% Ureturn ;
  U  L' R) c3 wif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
1 l5 ?3 O; S7 R2 ?  ]' M3 O  S* W{6 Q" t* w" H1 }2 }
negocios.Buy(info.LotsMin(), _Symbol);* Q& O" {5 f( y& S# ?
}9 J5 i2 |1 [/ x2 z  g
else // Draw rule 1.3 -- odd number - Sell6 a3 {9 w4 d+ z  J' r; s
{
- z3 g+ P: h8 w" j! N" {negocios.Sell(info.LotsMin(), _Symbol);
  C* e. ~8 s2 k3 X0 q) k/ E& `}
; ^  s( q! a; Y" t" T, @2 Q}
$ J# _' n1 c8 u# E+ Q( }* A//--- Check if we have a new candlestick...9 |6 b3 E2 D; l$ J) \/ ?3 w) z, G
bool tem_vela_nova(const MqlRates &rate)0 }. q9 N9 U5 S( f% g% h4 H3 a
{/ [( M, [& {3 W0 E5 M. c1 ^- x
{2 H# ]5 g; ^+ h' m! s1 k
ret = true;& }; d: n# z3 t
close_positions = false;+ L& d) W' A5 s0 k! S
}$ P' s4 \( A% x# u% j6 |
else! q2 B  ^# s; H+ m$ L/ J& _1 M
{% G$ z1 A, Z$ b! y
if(mdt.hour == 16), h  a- X. l5 V' U, m  H! P! {5 ^
close_positions = (mdt.min >= 30);# d& v- G9 O+ Y, U
}
1 p' `1 s, N$ P- A8 |}  L0 o+ [# A" g
return ret;1 B  o; x1 m( I: |$ ?% Z
}
) N/ ?2 a1 e9 g7 a8 [//---
: M1 F: X; M. n# gbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# q. `6 @8 e/ q1 [1 H) N
{
! x& W6 n( a9 uif(PositionsTotal()) // Is there a position?) h- r/ `$ e0 a3 {* a
{$ b2 f6 f1 K$ e/ h
double offset[1] = { 0 };% F8 C  O$ l4 e; Z; j, ^  o4 i
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?+ c8 n, p) C- p" W$ V, Z9 N. N9 v$ D
&& PositionSelect(_Symbol))  // Select the existing position!6 {' v; ?* D. _% f/ W
{
# N; L" _1 I& T/ [4 kENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);( d3 l$ a/ T/ J. O
double SL = PositionGetDouble(POSITION_SL);0 e0 }3 i/ l! H9 j% K* l' v; {
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));' E7 V/ P& W( Z/ E* s
if(tipo == POSITION_TYPE_BUY)+ J3 G/ V: r6 H0 [  h; R5 Q
{
8 d3 z- H, M( N' Nif (cotacoes[1].high > cotacoes[0].high)! ~$ ?, ?( z- A6 G0 c. R
{6 _9 l2 Z3 [4 I$ c/ V" W
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ t3 k5 x/ g  D1 }& linfo.NormalizePrice(sl);
0 T* m. ~, |- k, K6 V0 Yif (sl > SL)- }+ H. \$ t4 z
{+ q1 d0 t( `$ ^1 s" W5 R- L+ `
negocios.PositionModify(_Symbol, sl, TP);
; \7 G( ^+ {' L0 s" J2 P4 i8 I}
1 H% c: Z( K2 x$ ?" v3 I3 D* x; b3 J}
+ e0 ?/ x* I/ r+ U% j1 d% D}
* E/ a8 E# d* r% e, S( e* Felse // tipo == POSITION_TYPE_SELL4 K# B' p+ ?& T0 @- ^
{: }  X9 `2 m( V# ~9 l# D+ E6 Q% J) W
if (cotacoes[1].low < cotacoes[0].low)- T' g3 e$ B9 c% J# N% |
{
8 o4 C: y4 m8 i6 @6 g+ ereturn true;
! Q( I1 k) i' T2 c7 Q/ _}
, B6 m" D& A* s& X6 d( h* ]// there was no position
; `* g# q  z6 P( V6 {return false;' G+ p( B6 |1 O
}) A+ ~- R$ z- c$ z/ `
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
; j3 `% v7 W5 d2 M9 D3 w到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-12 07:41 , Processed in 0.943297 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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