私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, ^! q8 C! {; u0 r在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* R- B( o6 J7 ]6 r  \; I+ }( y为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。* J6 i$ Y0 F% t) q( k0 y/ J
以下是制定这些规则的代码。
# g, P8 i: f1 y( b2 p1 Z//--- Indicator ATR(1) with EMA(8) used for the stop level...
. ~6 j& f. c/ d* f; M7 Fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);$ I1 `: J2 y1 z3 Y
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
$ S) ~+ @* K# ]% E//--- Define a variable that indicates that we have a deal...# q+ y5 |# D. N! Y
bool tem_tick = false;5 }3 A/ X2 V+ i2 d( n
//--- An auxiliary variable for opening a position
- `  B2 Q) `9 G) T2 O0 w) k' C#include<Trade/Trade.mqh>
, [; |& @, e5 W, Z* U( s) q4 V( ^#include<Trade/SymbolInfo.mqh>2 @6 R( ?! u1 Y  l! d; ~% b
CTrade negocios;1 `/ E# T- Y; O0 [/ Q$ B# c/ v
CSymbolInfo info;
. z7 ~! B- y1 n% X//--- Define in OnInit() the use of the timer every second. Q  L+ Z4 `( i# }* c6 [% w7 K
//--- and start CTrade
1 T- f, L# c1 Q# z$ R6 f4 Tint OnInit()2 a. [3 @0 Q, N2 i
{+ k  i8 w. j" O1 C
//--- Set the fill type to keep a pending order, N- s7 H$ G5 E; S7 P
//--- until it is fully filled9 i4 |9 V, n. V1 v" g
negocios.SetTypeFilling(ORDER_FILLING_RETURN);* w% |# d. c1 {0 F
//--- Leave the fixed deviation at it is not used on B3 exchange
5 n" K  `5 b8 S! Q( Bnegocios.SetDeviationInPoints(5);
9 p0 q" U4 K4 U& m2 }, J  `0 @& J//--- Define the symbol in CSymbolInfo...5 X; F) ~8 Q" ]' M. {8 S
info.Name(_Symbol);
! z5 d4 I# Y# O5 P1 r//--- Set the timer...$ s, A& x7 h8 Z5 q, R* Z
EventSetTimer(1);
: x, z% I: f" Q1 ^+ x, a//--- Set the base of the random number to have equal tests...
/ G2 K" A' ?2 q7 J7 u& _- ]) `MathSrand(0xDEAD);
; H# A1 Y2 O" \return(INIT_SUCCEEDED);
0 F2 a) \( R8 C+ S! Z3 G5 @}& l( j. o+ l+ v' G0 i
//--- Since we set a timer, we need to destroy it in OnDeInit().
, f6 f2 @5 P* ^' dvoid OnDeinit(const int reason)1 V# ^9 p* y9 {6 ?
{$ k; I. l6 R  k$ {. P# i7 c
EventKillTimer();& A$ r+ \2 h* X
}8 i* G+ L8 o. S2 g3 I0 B' T
//--- The OnTick function only informs us that we have a new deal
' U% P) d. _5 \. T5 C5 i( N2 bvoid OnTick()1 L  K% S* b) e7 j2 y# H- l
{
' b' i( r7 e, V) ]tem_tick = true;
9 s0 d4 I' f! V}
" H: `/ Y  ?0 t# P3 u//+------------------------------------------------------------------+  _  z5 z' a1 c3 b
//| Expert Advisor main function                                     |4 B! ?3 c+ Y$ G. f  h4 X
//+------------------------------------------------------------------+
8 d8 ?/ r- y/ Q0 Qvoid OnTimer()
) `/ G1 b" t3 x{
6 J# j, B0 r5 B2 GMqlRates cotacao[];
1 K- `3 y7 n! ~/ x! V' Rreturn ;
, `- y  I! w3 kif (negocios_autorizados == false) // are we outside the trading window?
: S# l& X* ^" D: v5 Creturn ;
$ V, P  B& U/ p/ f+ _, i//--- We are in the trading window, try to open a new position!5 `' ]' N0 Q' e- [) |
int sorteio = MathRand();) e% I8 d- ]6 v0 F( R5 M! K
//--- Entry rule 1.1: H# n( F8 V* v& T$ b0 O5 s  o
if(sorteio == 0 || sorteio == 32767)) @+ p* O: J- f; y' ~
return ;
+ @5 F, \, f! B* L; Hif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. m, [$ h) m  ~: ]5 d
{5 D4 d0 g4 Y  N$ H3 R- j" g. v- p
negocios.Buy(info.LotsMin(), _Symbol);4 e# u& [: |# ~0 J% y0 b7 z
}
- J* j# v) V7 D* Selse // Draw rule 1.3 -- odd number - Sell
/ _( U6 ]0 p1 n! t{
0 l: B3 L/ J5 p9 B$ c) P) Rnegocios.Sell(info.LotsMin(), _Symbol);( p$ g4 C+ n3 H8 S
}
) a  @+ r7 L4 Q. O- V6 l}- R. v4 z4 @8 g. ^: D
//--- Check if we have a new candlestick...; {/ }7 o/ ?" _1 @
bool tem_vela_nova(const MqlRates &rate)) `+ q7 L% Q% f' ~; _9 K" I/ c% ^
{- v0 z/ ^, ]' P6 L1 e; k' o2 f# e
{' A0 }2 j: q; N$ D% K8 D
ret = true;
/ v; \: Q1 H( V+ Mclose_positions = false;
# D0 \5 _7 b5 e6 ~4 w( @- P( X( W}. z: t* J. p) w5 Q! M# w/ N
else
, u4 m4 Y5 w' r2 |6 u( r2 n+ D1 T. `{* H. W4 F" N3 {/ }" M3 g+ P4 i6 P
if(mdt.hour == 16)
  D# }, L# }. q, E& U8 k/ nclose_positions = (mdt.min >= 30);
$ ]+ E; ?; {' B1 f: E4 y, U}
4 N/ [- g' o' R- N& }}
; w% d$ G4 d2 M3 ?* u: Hreturn ret;! T8 P% k2 r6 J
}
. x9 l* Q9 }& N' [* u7 X+ {1 c/ Y" c//---5 ?( J' }' b! Q3 ~- o
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 a' Y7 _# S+ @" F" R" |3 C{* p& `; K% j- b) I& J
if(PositionsTotal()) // Is there a position?
- U2 w, h! i2 S+ O7 v7 Z7 c6 U{" I5 g7 Q( j" r2 e# O: q7 \
double offset[1] = { 0 };
% l2 B6 U0 e3 b& j# X  u% Uif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; t& q5 N' c. p6 A5 h&& PositionSelect(_Symbol))  // Select the existing position!
' \. |5 c$ t, w4 _" e5 W: e{
7 J1 R- N7 J' j* ?! xENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);0 k' _) @- C. v$ m# W! O& X. x
double SL = PositionGetDouble(POSITION_SL);5 @6 L" c# ^3 R( q$ U- W" q9 ^
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 [$ H. ^1 L6 u+ P, kif(tipo == POSITION_TYPE_BUY)
+ e# c6 x: n+ E$ h% D{
7 `2 k) q6 R; e- P; [: \5 Pif (cotacoes[1].high > cotacoes[0].high)
2 T+ q0 ]: a, z{
# W  A. m# @( `( o( \( Y' edouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 M% I" l: i% a! P
info.NormalizePrice(sl);! h: y% i' [5 R! V+ u
if (sl > SL); b/ G9 V0 u7 r8 J4 C
{
7 w: Z) c: A  l7 \) xnegocios.PositionModify(_Symbol, sl, TP);( y2 ?) a! z( A
}
' _3 B! }& B5 f5 o6 u  n}5 ~7 b; Z& v' X
}
  m( v' U( y+ O1 jelse // tipo == POSITION_TYPE_SELL7 N3 j' s$ K& y/ J. G* G+ R3 m$ ?$ l
{
0 H  m  u/ ^6 Q" {- f0 kif (cotacoes[1].low < cotacoes[0].low)& M6 \7 U1 [! D4 m1 N  H
{* |& x- }& j2 B  `
return true;& q; n1 \. l0 e' I, M0 w; a
}8 O2 |3 a0 X1 ~- b$ Y( g$ B1 v
// there was no position! y$ [* S& v/ g& V
return false;! s8 _# }. T7 u* s" i
}
# M. q. S2 b- g0 K7 c# t我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 D+ r& C* t, @) p5 z9 {到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-16 18:06 , Processed in 1.287757 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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