私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 Q3 d# F+ I  D1 w0 V  I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
0 Y  e1 o2 {1 U  M4 i为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。$ ]3 Q# ]! X, Y/ o! t" n
以下是制定这些规则的代码。$ _( Z! q8 f6 B4 f0 {: V
//--- Indicator ATR(1) with EMA(8) used for the stop level...
/ \7 y8 ]4 o/ C4 s! Jint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
5 R4 [0 v+ R- g  T6 m1 fint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);) D. O2 d* a. C6 v& ?
//--- Define a variable that indicates that we have a deal...
0 |- m6 ]6 f, b0 S: l  f2 @bool tem_tick = false;) h# b2 U/ g, B1 D/ w
//--- An auxiliary variable for opening a position
1 W% o/ r( \1 q3 [: ?#include<Trade/Trade.mqh>; d9 a5 ]5 y& ~. Y9 v# r( x& E
#include<Trade/SymbolInfo.mqh>
, W7 A! g- L7 I+ u  ^' E* ]CTrade negocios;4 T+ N+ ?& r6 ~6 v2 n# g
CSymbolInfo info;& P% r: w: S, s* E
//--- Define in OnInit() the use of the timer every second8 _, U) E1 |9 m% F% @( J( L6 e4 w+ ~5 Q
//--- and start CTrade
& J( R8 z* }4 `8 Z9 Gint OnInit()$ M7 N3 K2 e8 ?0 B
{
  w2 o3 r* e& _. Y9 f& B% h//--- Set the fill type to keep a pending order
* o+ H' p2 d: B9 c2 y' g9 Z. B//--- until it is fully filled
$ E# b/ q& A1 v3 D% L9 P2 Xnegocios.SetTypeFilling(ORDER_FILLING_RETURN);  [" w  U6 J( W
//--- Leave the fixed deviation at it is not used on B3 exchange
* @6 h# V% z0 [* ?+ Onegocios.SetDeviationInPoints(5);0 A" y9 Y, j8 J5 G0 z) C
//--- Define the symbol in CSymbolInfo..., b! s6 {6 L5 e+ t: a# _1 S% j* W
info.Name(_Symbol);
1 T, e) l, V( d6 m9 ^//--- Set the timer...4 p5 Z( x, O$ @9 y
EventSetTimer(1);
) G5 ~2 A9 F3 [. X8 h0 W//--- Set the base of the random number to have equal tests...: c8 j! }! }% k1 k+ Y8 ]2 H+ C: p2 X
MathSrand(0xDEAD);
% j) U8 ?3 }+ r# o  a+ \" dreturn(INIT_SUCCEEDED);" ?. L/ s8 M3 P* M$ x/ O! Z
}
" j- e& ?: K6 Y8 Z//--- Since we set a timer, we need to destroy it in OnDeInit().3 L8 w6 i7 f* \% z/ n3 U) x6 x
void OnDeinit(const int reason)
) g, u; Q/ e! G4 G8 V{& X$ `7 I) R) b. l0 W' _% d
EventKillTimer();
2 s0 b* `* ^9 S}8 T! @+ x9 p) J
//--- The OnTick function only informs us that we have a new deal
- K1 ?/ M5 j) y2 y5 K+ R3 uvoid OnTick()
) V  W. c( U( j" M- Z5 i( ]{' Y/ |' w, H. v: R$ a9 Q  j  w
tem_tick = true;
, C4 A" a9 E( L" x" A; y1 f# [4 q}, J0 |0 {! w3 q7 h8 }- ~
//+------------------------------------------------------------------+
) d% ]6 e; ]' U' u//| Expert Advisor main function                                     |
# v/ D3 a0 W5 k* N1 G//+------------------------------------------------------------------+
# Z% H5 h( p% G1 }8 Yvoid OnTimer()
4 m9 C% a1 C$ \7 d{9 U- u9 [$ ?  a" x
MqlRates cotacao[];& w7 V1 g2 K) U/ d1 f, j* W' [
return ;' T; s# \# M. M' f; {
if (negocios_autorizados == false) // are we outside the trading window?: r' Y+ ?9 ^% R; Q! Z0 y& n/ f
return ;: E, N2 A" J/ p% [( c1 A
//--- We are in the trading window, try to open a new position!
8 v; K) q$ V+ i) Uint sorteio = MathRand();- M8 [( B5 D, p; D# T4 p! X
//--- Entry rule 1.1# E1 R6 c) ]$ C! Y* H$ J
if(sorteio == 0 || sorteio == 32767)
5 _0 S, ~7 J- o5 f+ `4 i$ E3 sreturn ;
- C: ~. Z% f6 W# w% tif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 b& }; `# ]0 o4 p3 K
{
, L5 Y2 l  ~0 K, G* v* R5 C- p" d. ?negocios.Buy(info.LotsMin(), _Symbol);
) ~. A1 o9 Y/ ?: e" p$ a}
& L' o* j- d% q/ x/ v( felse // Draw rule 1.3 -- odd number - Sell
  h& [& S2 P6 Z{6 K& R; @6 i# W$ Z
negocios.Sell(info.LotsMin(), _Symbol);
$ n" t0 F& G5 y) `& |0 C}$ w4 S8 ?: _, }
}) K9 q) Z! l% `+ T7 `' ~
//--- Check if we have a new candlestick...5 |: r8 l* A, p. ~7 n9 }  Z+ N% ^
bool tem_vela_nova(const MqlRates &rate)
7 x& N+ q6 y- v# t& Y3 Q- D{- F! Q# o- A% B# b  M
{
* G. m8 S& Z2 \% A( l: n0 cret = true;
, S- I1 G! d8 i. wclose_positions = false;
' w) H! f) x  I: k- m6 F3 U" H}; |- H% M! s" u7 z1 b
else4 Z2 k$ w1 K( y9 V6 f" |3 O
{/ h+ [9 P6 m3 d; q7 C: N" k
if(mdt.hour == 16)
7 b6 Q# q# K2 R. `close_positions = (mdt.min >= 30);8 k, ~2 e' Y2 @. i: J' a
}
, ^  ~) \0 H. [- \8 g8 L}
3 }1 t, f  L1 @+ h/ d' S8 V) Dreturn ret;7 _9 B  Y; u! \! O- z  \0 Z5 p: Z
}
" s: g4 H5 e  t" R  k9 B5 j' t! w8 c//---% R; C7 U3 ^( z7 f+ ?0 `0 h
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 T3 `0 u5 S* i. V
{
9 C3 i8 R9 c6 @7 ~! zif(PositionsTotal()) // Is there a position?8 A: M2 X' h. {  F+ a  g: I
{
; S( I& m" Q) i& H5 w' W& R# q  ddouble offset[1] = { 0 };
8 f1 b/ P# l0 E6 U8 P" _0 Wif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( {; J& [2 @. y/ p&& PositionSelect(_Symbol))  // Select the existing position!
6 z  W7 D+ @4 y4 M* W' W{7 d# B3 w: r3 D) `9 F
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 \0 O2 g' ^' ]double SL = PositionGetDouble(POSITION_SL);
# X7 X! V* p, Z6 `double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
! Y/ |  Q5 p9 f" @if(tipo == POSITION_TYPE_BUY)
9 e; g; M7 y9 Q! f: m{
3 b( A3 P% y& A( r( W1 N1 v  V/ zif (cotacoes[1].high > cotacoes[0].high)
8 y1 e6 Y% V( `: g: [{+ b- T0 U9 |9 z9 n6 x: u, f
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
  d# M3 s5 g/ }: V  u1 Winfo.NormalizePrice(sl);2 P7 |7 P: w) s: V) B0 C1 S! \5 U
if (sl > SL)
1 {( n& c. h# a{- x5 d- r2 c' j+ ^
negocios.PositionModify(_Symbol, sl, TP);
: E  s  r/ w8 N9 N- I1 X$ j}7 d' x: L- B" E" ?- P
}5 g( M! c7 k& D, _# b  d
}
3 y& ]9 T8 @  Q! w/ ?else // tipo == POSITION_TYPE_SELL% V- y; q3 b# A* |
{
' ^! `" G2 {( \1 Q9 \) [if (cotacoes[1].low < cotacoes[0].low)3 E- S7 J8 {+ q/ y0 E5 J3 o  B; V( T
{9 P' k9 w0 N/ p: F1 d) p$ J2 J
return true;
8 X/ J8 c# Q) H. V& E}
; e. D0 }6 C' ~7 t5 o( I// there was no position- ]& ~/ h6 r  d5 h3 o- @
return false;
- L( i/ D" n, d1 v( T}
$ X+ f6 F$ p' R$ T& I  f- c' U我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。+ ]+ |( i! ^1 l* 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-4-15 03:15 , Processed in 0.736823 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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