私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
8 s* N- q9 d* X1 x2 f, b在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* p5 m$ Z" \1 j' E- Q  r8 q3 p为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。& T1 x: l4 j2 l+ V. M7 a: m
以下是制定这些规则的代码。
- x$ X. u0 G' G" j# s//--- Indicator ATR(1) with EMA(8) used for the stop level...
( p5 p; |' @' H; l6 G" Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);4 _# t; U) B( w3 a( t# {" t$ A
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
$ L( y( l+ w0 W//--- Define a variable that indicates that we have a deal...  u9 f0 Z$ T- x5 w% K
bool tem_tick = false;
5 {& h# x0 v" C  H//--- An auxiliary variable for opening a position
; X4 w8 [* E, K: \! q! o#include<Trade/Trade.mqh>8 I$ D* M% f) m# b: F+ B) ~' j
#include<Trade/SymbolInfo.mqh>
$ T9 t& A8 R3 x+ d& ~/ |CTrade negocios;# m! D/ b3 {" A& m5 l+ f6 }+ ~
CSymbolInfo info;9 `. t5 s: t: l, Z- V
//--- Define in OnInit() the use of the timer every second
) v, {$ u" N, d  {5 c//--- and start CTrade
- _) ?9 e8 v, {: g) B# d9 Iint OnInit(); v& c: v/ e+ L+ Y
{) P) T: n/ {5 x( [* V% Z' L3 h
//--- Set the fill type to keep a pending order
  h( o3 L0 h1 \. ]5 z1 B//--- until it is fully filled
+ u# l! K  x8 S" r/ }0 gnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 c0 M7 Y" s- I3 `6 r4 J% p//--- Leave the fixed deviation at it is not used on B3 exchange6 b9 R" `9 h1 l/ l4 I
negocios.SetDeviationInPoints(5);
  J  P4 [0 P2 V. R# ^//--- Define the symbol in CSymbolInfo...
* g. F; t8 B" T( {$ j$ Winfo.Name(_Symbol);0 \: P  d( g" c' w; w  ~- N9 B
//--- Set the timer...
" X$ _- M6 I- BEventSetTimer(1);1 w1 K$ {: ?+ c+ t/ g* ?6 ?$ ^
//--- Set the base of the random number to have equal tests...
. G* G( U, V, hMathSrand(0xDEAD);
. v  {% k- e4 c7 r; A; nreturn(INIT_SUCCEEDED);
1 i  G# u: Y4 X3 [( W}
% V; C4 a1 ~0 n+ m: R4 n7 `% e//--- Since we set a timer, we need to destroy it in OnDeInit().
: u0 w4 h4 B' w+ X: f  e, I. d, \void OnDeinit(const int reason)  ?- j5 ~9 A2 j6 O+ X- H5 A, n
{
" j6 j+ m% b3 Y9 LEventKillTimer();
4 j! \% |" y- s) v) Y" W}
' c+ q/ N7 @! Y$ {//--- The OnTick function only informs us that we have a new deal4 \1 t2 a' W) [  S' x- b8 W" Z+ b
void OnTick()
  @. O7 W7 Z3 y{
0 k( C. t& {; Rtem_tick = true;
/ q2 r- Z. {4 }: `: X; j% P}
' D  v& h, J0 F//+------------------------------------------------------------------+
; s. @+ [6 h6 l5 T7 }: p//| Expert Advisor main function                                     |
, e0 E# o  e2 x3 L+ W+ n//+------------------------------------------------------------------+
8 A6 y9 ^& A% n9 qvoid OnTimer(); O8 W$ X: x+ R1 ~
{& F2 f9 Q* |, S. S0 r# t& g& v
MqlRates cotacao[];. A: ?( L3 X# \/ ~0 J) y8 g$ A
return ;; f7 R0 r: |0 p% t1 O& x9 K! e
if (negocios_autorizados == false) // are we outside the trading window?' [) X, b; K- u# E1 k
return ;7 V" j0 Q/ S8 r8 J% |* S1 H, I
//--- We are in the trading window, try to open a new position!) ?2 I8 s6 e9 Y9 }2 ^
int sorteio = MathRand();
5 ?. |. F* ]+ Y//--- Entry rule 1.1" @) w2 y: q* r4 [9 u/ s
if(sorteio == 0 || sorteio == 32767)7 l  B) j( V" I0 @& }
return ;6 v8 k! ~4 s  M! c) A2 M
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& ]5 s- x5 }9 ~9 n6 x3 u
{
+ a7 z! ~1 x; Y: ^negocios.Buy(info.LotsMin(), _Symbol);2 X. k3 p7 {; x; j# L& }9 R! j
}
7 b9 }5 F$ |4 b/ g3 d$ ]else // Draw rule 1.3 -- odd number - Sell5 [& ~5 P& m- ^1 ]% U% m) i2 D
{8 c* e$ Q/ ^2 v  L7 C2 f, a
negocios.Sell(info.LotsMin(), _Symbol);
9 t8 i+ |+ A, d+ `}
! u7 r9 U# s/ h3 x- ^6 @}3 M* y0 Z# F; ?) M$ X5 x6 {
//--- Check if we have a new candlestick...0 w, Y4 \7 L) o: R6 B5 S
bool tem_vela_nova(const MqlRates &rate)8 h! _/ a, [- k* Q; \1 L7 L
{
+ p0 ~" m# F9 p7 g; B{, C; f8 V6 h+ m( S
ret = true;& y, L' J. C) u3 }
close_positions = false;1 G7 j2 z: a8 W
}' n5 [: Y  C% v7 r4 N, g6 Y
else3 j$ Q  j7 s9 d# t, U
{* `7 ?" ?7 n- ^: p' _* g+ t- |2 X
if(mdt.hour == 16)
; v7 L# r# `, L" s1 c- cclose_positions = (mdt.min >= 30);
; x3 i2 x  ?8 w2 L5 M7 h}
6 S5 p  u6 C7 t' U" `" Y' |}- k5 B7 K+ t! E' Y
return ret;( f, C" }4 i! U1 s
}' f$ C4 X% R! z* Z
//---6 k3 {0 H. j" G6 `
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])8 z+ H# p" J& F1 k
{' s- i9 }' b% |2 f& ]: z/ G
if(PositionsTotal()) // Is there a position?
+ L& v. o$ v1 |{& |* m4 B0 J: _3 r% u* y" T% b8 T
double offset[1] = { 0 };8 w# ]. ]+ q* P6 {! _) |  Z. L
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. s9 c. g9 w- T9 y
&& PositionSelect(_Symbol))  // Select the existing position!* w, z6 d; O5 g/ Y( l
{  N! v: q! ~# E6 D
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);, L  D3 q( i3 I" o) D# q8 {
double SL = PositionGetDouble(POSITION_SL);6 N4 E7 F# k4 r: i% L
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  j+ ~4 ]/ t5 m8 m; ^if(tipo == POSITION_TYPE_BUY)# O& n  J# F% y( n7 X
{; |* Y9 Z* V5 k: a( u! ^! o
if (cotacoes[1].high > cotacoes[0].high)
' W& g9 J0 L. K/ r5 ?6 V1 w{; u. z# ]  E) Z) b2 r
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. q. S, m) c4 C. T7 x  H
info.NormalizePrice(sl);# J. g+ u0 i% G- {' c
if (sl > SL)- ^% a) b  h5 ^
{
& o- \8 r; P5 ?1 Hnegocios.PositionModify(_Symbol, sl, TP);
# q1 l" q8 y1 T/ O4 q}' v  w. D9 H. @8 w% J8 D( u' K
}0 b8 ]% K% a" M% |
}
: v) [' N% ]7 W0 A  pelse // tipo == POSITION_TYPE_SELL
$ B* B/ B2 q3 [{# I8 K# X5 u  S4 f1 x* e9 K9 _
if (cotacoes[1].low < cotacoes[0].low)
7 g2 F' ~$ Y4 k" \+ J/ l& D{
2 `! a3 g- A% A, B8 G) |/ kreturn true;2 c) X$ m1 u1 ~+ H( b- R
}
* ?: A% G+ k% h: L1 f: v// there was no position
: P% ]4 p* ^3 G$ {0 Areturn false;
7 F: z6 c1 E& |4 ?}
' i" B- N8 M% d: d3 _$ |0 }1 z我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ H+ f& A- v2 i" @% d! A
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 20:58 , Processed in 2.422581 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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