私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* W, U1 V$ m# V. M
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
+ ?* ~$ U! y! m& y( y为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 x& A0 ^! E! B/ k( N
以下是制定这些规则的代码。, B- V" y/ d' N+ d5 K/ x0 I
//--- Indicator ATR(1) with EMA(8) used for the stop level.... v' I: Y5 t% M3 c8 N
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);3 }( C! X$ D  Q) ^" y7 z: @
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);0 S) k7 p1 z9 n" u
//--- Define a variable that indicates that we have a deal...
. o# s7 r& O  H* xbool tem_tick = false;& \1 O  i3 d3 `
//--- An auxiliary variable for opening a position
- [" F* G  s1 u; a  g#include<Trade/Trade.mqh>
; c. X8 U0 Y# I) G#include<Trade/SymbolInfo.mqh>! w6 M+ {+ a- w+ Q+ \' I5 M
CTrade negocios;- [+ F9 [; D; D0 B5 G8 ]
CSymbolInfo info;
! V+ n0 j2 O, k1 E* p//--- Define in OnInit() the use of the timer every second: z" E8 u" i) m* B
//--- and start CTrade
3 F: g! i! z8 ?) C( U. qint OnInit()
  r. z. k6 O: U( C3 u. C* u{
* b* e. ^/ k! t3 i7 x& g  z//--- Set the fill type to keep a pending order: @: z( y, J! i3 {
//--- until it is fully filled
5 O- n7 g7 f1 G6 x) D  ^! Knegocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ }* x0 {8 z! C7 G8 [; D3 W//--- Leave the fixed deviation at it is not used on B3 exchange2 {3 z; A, n4 I& U0 C( S
negocios.SetDeviationInPoints(5);
& x3 _- R  C2 Q//--- Define the symbol in CSymbolInfo...0 Y1 V& m! o$ @% |: j
info.Name(_Symbol);
0 e' g) Y1 X2 K6 ]1 R6 E//--- Set the timer...
  n- u1 N, f5 J) p$ U' uEventSetTimer(1);- G* i- x! Z$ k
//--- Set the base of the random number to have equal tests...
4 C6 p. w' L9 q4 e8 ~) k5 J+ zMathSrand(0xDEAD);
! h4 K* f" A2 j$ B0 |return(INIT_SUCCEEDED);
. a/ J9 Y1 H. G. K}' _2 S6 B8 h& R! Y2 `
//--- Since we set a timer, we need to destroy it in OnDeInit().
& b; R9 L  Q! g5 y) C7 m* vvoid OnDeinit(const int reason)
' W! B2 k9 E% F1 f  X. d) W{
5 b+ ]* c5 z) x  U8 z! o6 yEventKillTimer();6 O0 t- u; d& E: V4 d( L
}; [3 [* Z0 F; ~& I) {8 Z9 v" |  O
//--- The OnTick function only informs us that we have a new deal) h" g3 k  y" Y7 n: I
void OnTick()
" r) _3 {0 z+ B6 b3 I' h{
( z! o, u( X/ }3 j" {) {tem_tick = true;
% A5 M, p4 E% a9 W3 Q}$ K1 q2 A5 F) a/ \5 P5 g# C
//+------------------------------------------------------------------+0 m9 k5 y* G8 }. U" C3 g" B/ J
//| Expert Advisor main function                                     |
/ C0 c- |3 v2 N" B//+------------------------------------------------------------------+
& z6 G! S* u) m8 W, e/ {void OnTimer()
4 x/ h3 x  C' J9 I0 E# S{
9 P7 Y* k% {& g- G6 E7 TMqlRates cotacao[];! a1 ~; p- h, I7 q- ~6 e8 I: D
return ;
# i% N4 o/ I  p1 kif (negocios_autorizados == false) // are we outside the trading window?0 B& a) H8 Q& J( P
return ;$ Y9 U. p  W; L  A" o  O
//--- We are in the trading window, try to open a new position!
  _( T1 {: p# G9 [! ?1 P& F1 o: Bint sorteio = MathRand();
' F0 u( c; V. l% z//--- Entry rule 1.1/ J& p+ R: [9 e' p9 o) n: Q3 R
if(sorteio == 0 || sorteio == 32767)4 C- s7 J* y+ q% J
return ;% s( j; ~. c6 \; {: q" E
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
, Q! |- n- ~7 v6 L3 B. {. [{
( s; V0 c) B4 i! A. x! C0 ]negocios.Buy(info.LotsMin(), _Symbol);9 Y& Z/ p: q. k
}
6 w; w$ p) E  Q( a1 ]& Y4 V- \0 A7 selse // Draw rule 1.3 -- odd number - Sell
" U2 D3 C) k- I, g0 i, h2 O4 w) ]% }/ W{
3 n. M/ U+ F. B& f' ]! ?9 qnegocios.Sell(info.LotsMin(), _Symbol);1 H% p  D7 z1 K; T; U, N2 {
}, ?4 {, F5 U/ Q# I. B. i9 k
}
1 `% l- P; R7 Z) s* ^//--- Check if we have a new candlestick...
+ V' e' j, B' O3 g3 @( Lbool tem_vela_nova(const MqlRates &rate)
3 X& ~1 _* ]/ d! ^% q1 j- Q' M{; M2 q: V0 ~. w) i+ W. C7 v) G
{
0 r, Q8 o- @/ \( m7 z  Dret = true;' m# q5 }/ u4 t( h& H7 N8 U
close_positions = false;. q4 ^8 E- D' f% e, r; ]
}
" \* O- E( J  O0 m, ielse* U4 S% L! V; u8 N9 A7 V+ V
{  n, H5 k9 W* O( ]( h2 A. {
if(mdt.hour == 16)
) v( `# \' s, w  kclose_positions = (mdt.min >= 30);) k( `) X% \  |. ]( s
}& n% @* Q8 V5 U( O$ r
}
/ ^9 U% b7 M  ^" Q/ }return ret;, F- d& `5 I- O4 O
}
& \0 D* h8 ^: ^% C- B; {- K" m//---; L% l% y0 _7 L7 G. }& _# V2 |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
3 y* x" |8 D# @{
6 d4 |5 n6 }# D# A6 O+ {if(PositionsTotal()) // Is there a position?
* H$ k* U. C  ?& q. x' M" v{- F2 |; B2 U$ T
double offset[1] = { 0 };
! D0 i( }  j0 M6 qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' j) o/ c/ c: F( R0 G" `# X* i
&& PositionSelect(_Symbol))  // Select the existing position!( o$ ~. X# N3 M
{1 G3 O) X/ `+ C) k4 m& R, X* [' b
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
' u1 D8 \7 P$ u( }+ [double SL = PositionGetDouble(POSITION_SL);0 M9 ^! w/ S4 V! h! O& U' x7 q+ G# ]
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
# z2 J, n% \* s' S5 wif(tipo == POSITION_TYPE_BUY)5 M. M6 Q: F8 f* w
{
& ]: p1 K6 e- n+ yif (cotacoes[1].high > cotacoes[0].high)
& H$ ^: k& T' P' N{
4 M: i9 Y" B8 b6 s( a% ~; |double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
' g9 _" x7 p' j  j; Ninfo.NormalizePrice(sl);* v9 `( w+ {+ g& ?! l; c
if (sl > SL)1 m2 a' H& N. ]& ]: T' U
{; ^0 Q( w. i& g, p; J7 ^
negocios.PositionModify(_Symbol, sl, TP);
8 K" n, p# a2 J; Y}
% F; j/ ^9 S$ J- R5 h}& X6 h8 S, Y. x( n  l" a4 J
}( n; D- b( A- F1 H
else // tipo == POSITION_TYPE_SELL* `& X2 A" e  A4 J
{
0 }9 f2 O6 y$ X; mif (cotacoes[1].low < cotacoes[0].low)5 |  z1 c4 @1 x5 a# g
{
) Z$ m" v5 b3 G. N1 `1 ~2 areturn true;
) B/ _( J' G4 C0 E6 x2 V, {0 f}0 m+ g, u2 n* }+ M+ A
// there was no position& F2 l/ j1 J: J  I; D& b  P: ]
return false;
# }( s# Y9 o4 i& `}
, a. M# R2 p) ~* F) p! L我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
( \# \4 _2 b5 [# _到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-17 15:47 , Processed in 0.662578 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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