私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA4 X* C- h% d1 e! h  j0 S4 d2 |* s
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, H( i, M: [( L( Q8 A! m
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* @5 K( L& O' i, H以下是制定这些规则的代码。
& j- W5 n. x9 q0 f% J; `* e, _//--- Indicator ATR(1) with EMA(8) used for the stop level...
  k+ z  @. B  uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ `/ `0 y' o. V4 |0 u6 A9 m
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 l2 Z5 Y5 c, Y7 n
//--- Define a variable that indicates that we have a deal...
1 z# ^* A% _% T7 u: Wbool tem_tick = false;2 _6 d& u$ Z8 g
//--- An auxiliary variable for opening a position
# h  h- b% \: |' J) s#include<Trade/Trade.mqh>9 V( Q; d: k6 ]
#include<Trade/SymbolInfo.mqh>
, C: B/ x3 L; C0 ]: e+ KCTrade negocios;
2 i) X# x6 G3 L6 h/ Z/ W- ]CSymbolInfo info;
& `) h% d3 i" a" F+ S+ |; n//--- Define in OnInit() the use of the timer every second
' X% ^& n* d! i* i  i  ?* y//--- and start CTrade
4 r) ~' R7 ?) V, Y* y, Qint OnInit()- F: g/ W+ S5 A6 d: s, e' F
{
% f/ P" @% Q7 q. _- ~/ M7 W//--- Set the fill type to keep a pending order( u: g' d/ J5 W/ Z
//--- until it is fully filled
# k, Y5 |$ i3 |+ ~/ d0 U" enegocios.SetTypeFilling(ORDER_FILLING_RETURN);5 n1 E2 a& W; S  y# R7 a# ~9 E" h
//--- Leave the fixed deviation at it is not used on B3 exchange
& }( }# E: E3 W: |7 h: Bnegocios.SetDeviationInPoints(5);' }! t. g5 a2 l2 i& D* F1 z
//--- Define the symbol in CSymbolInfo...
+ [& @9 Z& |9 K. Einfo.Name(_Symbol);9 ^, t- R$ a% [
//--- Set the timer...' V8 A+ r+ ^$ T& Q7 W8 p$ A- @3 ^
EventSetTimer(1);
. K' B$ ~% a  J- P//--- Set the base of the random number to have equal tests...
' x, x. Q$ t' ^( _MathSrand(0xDEAD);: Y7 P1 G3 v( h- O1 V
return(INIT_SUCCEEDED);
8 p1 ]' Y4 J9 B1 D* K9 i}! N: @# e2 m( K' N0 {4 b
//--- Since we set a timer, we need to destroy it in OnDeInit().
) k! [, C3 J* t/ i# [& avoid OnDeinit(const int reason)" w5 f+ ~/ \% ~0 `& C" {/ E
{7 @) {& d( q' D  o
EventKillTimer();+ I! l6 Q- b8 e$ N$ ?! s$ k9 M+ O
}6 O' j$ }' E* E! `4 G0 S1 r* s
//--- The OnTick function only informs us that we have a new deal3 e3 t7 a9 D9 ^8 r$ w) U
void OnTick()
, d4 }2 D( ]4 n, e, U0 G9 Y{$ r! E: @! W/ b' J* h
tem_tick = true;8 m3 F1 ]7 {9 K! c2 h
}6 S, b( y" N+ S
//+------------------------------------------------------------------+
6 v0 ?. z$ D7 S( L$ v( ?. B8 |- C+ T8 _//| Expert Advisor main function                                     |1 _( ]2 _2 D7 p1 g2 Q) I" l
//+------------------------------------------------------------------+5 V$ T) J9 F" A* d) G# N; |% x
void OnTimer()
  P6 E) t) E: G9 a2 A$ Q{
& m  x- F. z9 K) ~MqlRates cotacao[];6 p* ~% E$ p8 s, w1 Q
return ;0 B! U" r& `! J; b( @3 Q4 o
if (negocios_autorizados == false) // are we outside the trading window?, S7 s3 q3 G0 e2 G0 i, L
return ;
9 z, x7 m3 [; {% d% X+ u8 Q; L//--- We are in the trading window, try to open a new position!
* h+ t/ L/ {8 L# p: b4 Rint sorteio = MathRand();% z+ H8 ?; a7 j" K# W) x6 ?$ u1 Y8 ~# G1 L
//--- Entry rule 1.1
# ?- g" ?7 H7 b* f; b# w! s( z/ S6 Oif(sorteio == 0 || sorteio == 32767)
; J7 O, V3 Q6 @9 F( O: a+ s6 w% G; greturn ;$ f' _+ |7 q: r+ v) I9 Q& a
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 w6 r: T; ?! c" J: V9 j8 l! n
{# v! k: d7 n! W$ S+ g. M8 p- ^% X; i+ J
negocios.Buy(info.LotsMin(), _Symbol);
1 H$ F5 P  S* B, ?}
& I5 }7 l7 `$ R* o& t7 Lelse // Draw rule 1.3 -- odd number - Sell( c( t3 C4 R. X: W7 G8 W
{: P; q0 I+ _" d
negocios.Sell(info.LotsMin(), _Symbol);; S2 q- [+ @* K8 e) R" D9 f
}
  A/ b0 G( `, P}: W' q" S+ U4 K- ~  d
//--- Check if we have a new candlestick...6 m9 x" o( t9 e  y0 V. _8 I/ \
bool tem_vela_nova(const MqlRates &rate)
6 B& O( j, L2 y( ]) [& p{; O. o. {& ], d! j$ ?- w+ k6 h
{9 @6 d0 y; b) T' P2 _
ret = true;5 b3 `9 ^( L+ C, N0 [$ u/ B' c: I( x
close_positions = false;2 ~# ~4 j' ~8 }( @; C
}8 C4 t* W* c0 z% G8 l+ \' n
else7 O. V" c9 r$ F/ T- ^* S: L4 W
{
/ m% p8 W. N4 z/ [5 [+ O: A) oif(mdt.hour == 16)* f+ p' I, M6 x
close_positions = (mdt.min >= 30);4 p/ v' f1 b9 o* e" ^  r
}6 U0 }. C" ~) m" G% m' j
}5 M$ C- Z0 S% P8 u, p
return ret;6 t+ S/ `5 U# A/ V9 [$ w: U5 G
}; f0 z$ h5 H% `! X; @
//---6 l9 o2 Q+ E- p- r# i4 A7 H! p! c7 H
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 W% @$ t4 s) U6 G) {! k( ?{
/ m, y* Q2 V- _) lif(PositionsTotal()) // Is there a position?
1 x7 T! {0 P( u' G$ |{4 {* q5 C$ f) x
double offset[1] = { 0 };
1 B7 ?3 k( i2 Iif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
7 `7 g/ M# y& @6 h. _&& PositionSelect(_Symbol))  // Select the existing position!. N2 {. D8 }* V4 }+ C, z5 X& s
{" z: t4 o" x0 U2 _' B& A0 b
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. G* f2 S  ]  R; Q$ m, J8 z
double SL = PositionGetDouble(POSITION_SL);: m& ?! V5 R+ `* Y7 y
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));: ^0 q! ~; o+ N: F2 i; Y6 H
if(tipo == POSITION_TYPE_BUY)
: O6 E# p* Z+ ]: l# @{+ H" o2 o' O- A+ [, p
if (cotacoes[1].high > cotacoes[0].high)
( E% U$ X+ f) D0 [  \$ i% m( ~{
8 N8 S. v7 s& k: r" f# C% i2 a( c0 sdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];& Y8 q: s+ v7 ?0 Z3 w9 @4 o! n8 b
info.NormalizePrice(sl);2 ~/ E  ^0 ?/ n9 J7 [1 i% W
if (sl > SL)
/ q5 f+ n- d% K; v' H1 r0 i{' x/ p( x7 Z2 O6 q0 O( M7 l7 l
negocios.PositionModify(_Symbol, sl, TP);, @  K8 v$ L& F% n$ @, k
}4 R' N: E. x$ j9 a' [3 n+ Z
}, I0 x8 r! ~; d& a9 c5 }% z
}. Y8 \* h. m3 j3 d- h3 x
else // tipo == POSITION_TYPE_SELL
+ g& e* b  {6 {( ?) t1 [- u{
# }* `; J! `4 W0 ^- `$ ^) Z2 I4 m! Iif (cotacoes[1].low < cotacoes[0].low)
( D. h2 q" j! l3 [{
5 d2 Z( c7 k+ c8 l- sreturn true;0 u' \0 }$ Y* B8 Z3 f: z" z6 @
}
4 Y9 s6 _6 g; V. U: |+ K- [// there was no position4 \7 h( Y# \* a0 a8 q
return false;5 A+ r9 d8 I2 n3 A8 w
}
; y; k" L5 e* W我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& [# W, [$ N% k: |3 Z$ w- f# B  F到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-30 22:56 , Processed in 0.446190 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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