私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  G. U- s$ }, w. n) J- `/ ^. I
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
2 @) ~# A9 C! \; Q* @为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。9 V2 j6 x" ^& u; i" h) j. ?
以下是制定这些规则的代码。$ b, o; D, z; E* ~: M/ z
//--- Indicator ATR(1) with EMA(8) used for the stop level...) M+ f3 u4 O  ]  _" E
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);+ f+ L! F2 }* C& w9 G0 D
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);" [) z8 |8 o" j9 K/ r
//--- Define a variable that indicates that we have a deal...' o. Z% L% w: g1 L! ^8 g
bool tem_tick = false;
! Q, ?6 g, G1 }. H; o; y& ^3 M//--- An auxiliary variable for opening a position
3 {, u6 J# B/ n; H* L1 |; K#include<Trade/Trade.mqh>
& a0 }  l! y$ o* K; {#include<Trade/SymbolInfo.mqh>- |+ X7 Y0 E5 G% f
CTrade negocios;. O) J) {/ j3 b* i7 \9 }* j! ]
CSymbolInfo info;
1 v! \/ f) F2 \7 p2 @. e//--- Define in OnInit() the use of the timer every second
" U# p# d* F/ Z) v/ S//--- and start CTrade
8 h! i0 k$ ?, j) c* _3 H- wint OnInit()
0 P/ o) r- C- m/ I9 ]{
* K! j  \& w" S9 b! U. `//--- Set the fill type to keep a pending order
; S* q' V6 x) L" A8 g6 R2 C" i//--- until it is fully filled5 z, T" Q- {; }+ d6 m) U
negocios.SetTypeFilling(ORDER_FILLING_RETURN);5 x3 t/ U4 \  r
//--- Leave the fixed deviation at it is not used on B3 exchange% y! ^3 c6 b( u) ^. M6 ^
negocios.SetDeviationInPoints(5);
- H0 Y. d9 T' O% x9 c//--- Define the symbol in CSymbolInfo...
, S7 X8 q! u2 rinfo.Name(_Symbol);
) w4 u5 X- `4 X8 ^//--- Set the timer...
- D# Y/ @+ r1 _5 J" eEventSetTimer(1);( h$ w+ E  {! w! ^! i" B8 l8 W
//--- Set the base of the random number to have equal tests...* Q8 ^% Y3 K7 h4 ?  w! D; l
MathSrand(0xDEAD);
  L) ]* J: ]% Breturn(INIT_SUCCEEDED);$ U3 {. q4 I) @, g
}; g9 D3 x0 Y, k6 e$ R: F8 s: o
//--- Since we set a timer, we need to destroy it in OnDeInit().
% x  F. Q6 z0 V. @" Uvoid OnDeinit(const int reason)
) g% Y; V8 l! |1 ^' Y% m4 n{
  b2 j0 C4 {% A. ~: _: ~EventKillTimer();
: O& x& G# m- C. B; Z}
# H1 \" T. p, \9 j  q( B//--- The OnTick function only informs us that we have a new deal
# [1 Q& E* \$ s; t4 x+ n9 n( wvoid OnTick()5 [4 O: H0 f' ?  N! s
{: S2 `0 z- ?( j: N0 L# m3 a
tem_tick = true;8 j' ~% g  w+ X8 E* Y) R
}
: D( p% _, y* e- E! q2 e% w; ]9 {//+------------------------------------------------------------------+0 T* N: E+ ?* R, ^% d6 w" u0 e! |8 B
//| Expert Advisor main function                                     |
% \& U( p" M; z+ M" a//+------------------------------------------------------------------+( ]% D+ e. c. w+ j0 K
void OnTimer()
, H, W+ F- E0 s2 w( e{% Q5 J$ ]: n# T4 A7 D% L
MqlRates cotacao[];
  v; L; x6 r" u+ H, b' ?return ;# t% j& `0 ?9 U* E
if (negocios_autorizados == false) // are we outside the trading window?5 y" h! d$ x: k! }& s. R
return ;
, n! T- R( b) l& ]//--- We are in the trading window, try to open a new position!  q5 s, ~$ Z0 T- b+ |
int sorteio = MathRand();
' W  `# Q5 G6 I/ v6 b2 _. H9 Y//--- Entry rule 1.1/ i* X6 f+ x$ a0 {& H& s( ^! B
if(sorteio == 0 || sorteio == 32767)
4 q/ ^) f; k( f5 Q# v, E) ^, Nreturn ;
  Z+ ]3 o8 I0 d& ?) Yif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
. a" e0 h3 y& U5 l5 ^& Q{
0 K0 O# t# [$ ]6 ]2 t( t3 s' Q4 c/ Enegocios.Buy(info.LotsMin(), _Symbol);
+ f0 E  y3 m1 \0 F1 L  [+ U, W  U}
/ j8 ~, p  Y; \  W: \6 P  R' Belse // Draw rule 1.3 -- odd number - Sell
# v) l0 ?' Y1 w0 \* F  \! d, |0 x! G{' l3 O0 ^1 u9 [. Y; h, W3 p! W# i. B
negocios.Sell(info.LotsMin(), _Symbol);
: A2 ~1 e" q' v& k  [8 l}
: K- ^+ y! n( h9 t+ w# e+ l  S6 b; W8 k}* ~7 k" G0 M; @3 G
//--- Check if we have a new candlestick...
) U- L4 ~+ u8 V& o4 ?bool tem_vela_nova(const MqlRates &rate)
. V! w; }) i: Z$ h# q- W' X{
* z* h5 z1 @  Q( j0 s9 Z3 m) j5 ~+ @, \{
9 N9 C& z- P: h% @3 k) A: iret = true;- {0 `; n8 S% l9 S" @6 B* u
close_positions = false;$ V8 {2 _+ V5 x% x1 T
}
6 G8 J1 |, T- u+ M4 F" @# }else
% k4 `4 ]3 j1 h+ f) b: z{
% T- R! h# b6 H( M. a5 cif(mdt.hour == 16)
2 z6 X& g8 n8 U/ P7 k! X1 z5 Wclose_positions = (mdt.min >= 30);
' c$ q, M4 R( A/ J0 Q/ B}- i- A# Z4 A1 I* z. R0 C
}
4 c4 X0 [  \# b, K  Areturn ret;9 K# w/ [* ~( j( ]7 y
}/ ~( Y" h; @& g0 V2 C/ ?) r
//---2 W- W' b' Y0 a2 E
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
  Z+ Y) @- b' B) G8 O  q8 q6 i{* }; X7 q) y% L, c# s
if(PositionsTotal()) // Is there a position?
0 r& j6 {9 Q! h. i{
5 T1 X# A. j3 E9 n9 ?% }2 X# sdouble offset[1] = { 0 };
1 n% N, z# {6 vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ O( C( d( D: q* n! g
&& PositionSelect(_Symbol))  // Select the existing position!
# t6 b2 v! d0 O4 @* U* t# J{5 h0 u( U: ?  {+ X% V
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);% G/ Y3 X% K2 d5 T
double SL = PositionGetDouble(POSITION_SL);9 g( `6 x6 L! e1 b5 O% b: t
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, f& k" A5 g/ a: \if(tipo == POSITION_TYPE_BUY)6 w# g' w9 Y1 _! ^0 S  F
{
- u# y4 p# b5 H% Fif (cotacoes[1].high > cotacoes[0].high)/ ]2 |) }1 R2 m8 g# Q* ^
{
4 n; R$ Z$ j7 E/ Cdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& P: r/ j! i. C5 p5 rinfo.NormalizePrice(sl);2 {- y* s. Z" V' G% Y6 m
if (sl > SL)
7 F& P' f0 c' n5 f; i/ c{
; b% H: R. C: O/ j3 C" Y; o- Y, Pnegocios.PositionModify(_Symbol, sl, TP);' ?* J' T$ z" z2 \% m1 e
}: i" N: `8 u7 f4 ]
}0 m2 p- B) P4 D6 z4 H; D3 \- E8 E
}) x3 z6 e+ }8 }
else // tipo == POSITION_TYPE_SELL
, J* j: k6 _3 R  A, p7 x{7 W( J5 h9 F4 M, O
if (cotacoes[1].low < cotacoes[0].low)! N% E! x# a5 N
{
- K: k# s& z% z0 ]& @return true;
5 L, @7 s0 Q1 U4 [, K}! i# `; A9 j7 P/ I  y
// there was no position
) h  s6 w  [9 _$ `9 `/ [3 R; Ureturn false;( D. ]9 I( E9 Y" H: e+ T% o
}
+ w3 F0 {' O9 P5 l" P. Q0 V6 e我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 w) s& R9 ^" ^3 E8 f, N到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 13:09 , Processed in 0.422996 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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