私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA1 {; g) }4 d7 w' c  [& x
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 _) x# ^7 I# z
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
. L3 a1 v3 J: ?" F* j' g& V以下是制定这些规则的代码。
, Z1 t# I4 ^) t//--- Indicator ATR(1) with EMA(8) used for the stop level...
# P& o5 F% m8 Yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);  s9 y) {5 Z% U4 @
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
- }8 R6 v! f+ ?//--- Define a variable that indicates that we have a deal...
" M5 t0 V+ u5 L  {bool tem_tick = false;
$ _* H) z& X0 n: _/ \* P//--- An auxiliary variable for opening a position
# Z* F- ^0 Q( w0 `# D% H  L#include<Trade/Trade.mqh>
$ `) ]) l/ `7 |, D# H5 R#include<Trade/SymbolInfo.mqh>
  r8 f+ w% @! vCTrade negocios;3 Y- L# b  M% V
CSymbolInfo info;
2 d7 P5 c3 E, D2 s7 p//--- Define in OnInit() the use of the timer every second
, A) v  o; [; ^/ A/ Q/ {" a  ~//--- and start CTrade
7 C, c, \3 j- g5 N. H( z5 kint OnInit()
# B: z, [: ^5 R{
" N+ F  u9 ~0 m0 r: u: `" b+ t//--- Set the fill type to keep a pending order
" J6 M# H4 j& u1 I( l//--- until it is fully filled
1 s, Y. S& {( _+ n8 R! w% b5 Jnegocios.SetTypeFilling(ORDER_FILLING_RETURN);. F, T7 L/ W/ }' \5 L% ]
//--- Leave the fixed deviation at it is not used on B3 exchange
( K" `. J# S3 snegocios.SetDeviationInPoints(5);  Q& o1 r  f+ X+ m  X- a3 E; l
//--- Define the symbol in CSymbolInfo...
5 L( p, u' e8 ginfo.Name(_Symbol);7 U  R. _& |5 ~7 r% O
//--- Set the timer...
* E, D9 E! @' {, L0 l6 S8 PEventSetTimer(1);
$ D8 r! T( W# V# V& N6 o- N//--- Set the base of the random number to have equal tests...; W+ a  e# _3 Y
MathSrand(0xDEAD);5 E% }. L3 G2 x6 @% P. W# E
return(INIT_SUCCEEDED);
6 e. ~, W: |) t( m}
9 P; O7 Y' y1 Y//--- Since we set a timer, we need to destroy it in OnDeInit().
7 F2 w, L% f# V" v9 |( f* N  nvoid OnDeinit(const int reason): B2 b& B! U) t0 Z0 e
{
1 S5 F2 a  ?1 e4 ?0 {" h3 e* c! OEventKillTimer();( M0 S! k  B7 D% [* l; x
}0 H  Q3 r/ b& v- y
//--- The OnTick function only informs us that we have a new deal
9 F0 Z2 _! `3 l2 t# y5 c% vvoid OnTick()
5 u' e" Z& b9 @{. H7 j8 X+ M0 m# q
tem_tick = true;
. ]5 f3 E' y" x( o}4 q3 ^8 f: J! Q: P# [& Z( d
//+------------------------------------------------------------------+& w) N- ]% s/ a
//| Expert Advisor main function                                     |" Q- F) `6 q' Y  U
//+------------------------------------------------------------------+  L( f0 m& N) u- q- q. @1 m/ W
void OnTimer()
$ N5 C) e2 P2 W; d1 k{% }7 Z# c1 C( s- [" Q( Z
MqlRates cotacao[];
* H3 @9 _" l8 _" J; J/ Vreturn ;
  K  V6 |/ y) Rif (negocios_autorizados == false) // are we outside the trading window?
# @1 E. v9 c& x8 Y/ X5 creturn ;0 B2 b. k8 B' p9 x' t
//--- We are in the trading window, try to open a new position!; X0 X) [7 j& z3 t+ w
int sorteio = MathRand();
6 Y) I" G6 ?) ^! C//--- Entry rule 1.1* Q, W/ ]% `. P7 X+ x. E
if(sorteio == 0 || sorteio == 32767)
+ W# g  _& ^7 kreturn ;& y. q. M; }3 l% X4 R; g0 J. x
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
% ^! l; N, V' o1 @7 O{8 N( p# ?8 W7 O3 T0 [% Y8 k/ w
negocios.Buy(info.LotsMin(), _Symbol);1 ^% Q$ |* Q5 D% h' r
}
# f. V  v7 |3 ~" J  \: v2 J6 _/ Welse // Draw rule 1.3 -- odd number - Sell
$ T) z, r! \6 s: P, g( E9 P& Z{
2 C! g8 B" [) J3 y8 xnegocios.Sell(info.LotsMin(), _Symbol);
, T# {- u4 w) I$ M; ]& Z8 D/ Y}: K: j% Q4 A+ b8 k0 @8 C/ x" B
}9 Z  }* e2 s# k1 |% g5 D/ L
//--- Check if we have a new candlestick...; d" |+ \2 t: r8 ^$ o2 w9 U" Q
bool tem_vela_nova(const MqlRates &rate)
: X" Q  g; P% T* U{
+ f! Q# G9 L1 h0 j{
/ O* ?" V/ s/ u" T5 n. G3 uret = true;
1 T5 u% p& G9 B" j7 Aclose_positions = false;
6 n) B$ T/ e6 x# `7 u# l}
# \) B8 h! t/ U) V" A8 eelse
. t1 E& A; k6 i6 _  N9 Q$ Y8 [{  ~$ H% e: X0 r& X% B, Y
if(mdt.hour == 16)
3 r4 l3 e, A+ X3 Nclose_positions = (mdt.min >= 30);
( Y# L+ u- D9 w. z. A, Q}
  G' A- _3 a$ O4 S$ c}+ o* |/ G( m. ]. W& S
return ret;
# R7 ^& \8 D- J) N( ^' s}
8 q8 Q8 Y4 p( n. m! m: q//---4 k) z$ C0 F4 j
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" i' M2 e+ |2 R' s# I{. l3 x& K' F, L3 E9 R
if(PositionsTotal()) // Is there a position?3 l6 X1 \* H8 W. L3 z  S; g
{
4 N3 z) z: r' P4 Q5 G% ?7 Cdouble offset[1] = { 0 };
% ?0 O( X/ {4 @+ W/ s, Gif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
* i* R0 A1 }+ Y& M, J&& PositionSelect(_Symbol))  // Select the existing position!
: Q1 \2 J7 b3 y# }# j{* W; p# l3 ~2 p  q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& d# Y( e4 v/ ~" Xdouble SL = PositionGetDouble(POSITION_SL);' e8 w& X( B3 Y0 I4 q, O) [+ H
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- i( z& \& P: f* r3 h- @- ]
if(tipo == POSITION_TYPE_BUY)  t" d4 Z+ i( K4 ^; x+ L0 L
{
6 d" w7 _% p0 {. rif (cotacoes[1].high > cotacoes[0].high)" R% ?; ~1 w0 Y0 C# G! V
{) \5 @, O0 C/ ^: m# V
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
; R' B# z2 L0 t3 K2 k% }- Yinfo.NormalizePrice(sl);
( G  C! W" Y' y# _: gif (sl > SL)
* {8 j; T& u$ e, h% A) F( e{" N, ?7 I0 I6 S! `7 `
negocios.PositionModify(_Symbol, sl, TP);5 t1 ?- Q  w9 e
}
. O/ z1 C9 s0 P8 y# ^! A2 i}
- o( B  J! P# E- {4 p/ b1 ~* d! ]}% S" g3 B2 v( d) o! V5 x3 g
else // tipo == POSITION_TYPE_SELL- Q/ i- b/ G# ^" A1 x: i
{
" F; E" e, V* n. I% u/ t# {if (cotacoes[1].low < cotacoes[0].low)" r. i3 I. x. s1 K$ v) |; K& F" T: l
{( d6 R3 k$ ]0 {2 ]
return true;3 V, D% T7 h5 ~# ^
}
" Z$ W4 i& w# g; Q// there was no position
8 Z# c( P* w0 a* D! [return false;
8 R8 I& x; N' @) y; D7 [: ?}
4 _4 x4 S. X8 J6 i我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 M. Z  R) X! Y3 G7 C( Y到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-15 11:43 , Processed in 2.904998 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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