私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
1 K5 g/ B& A1 N1 h. ~- I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。7 x' L3 t$ n5 i& J& ^
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。" [: q8 b3 M# w$ j
以下是制定这些规则的代码。3 R* q$ q% }9 c( ?" l
//--- Indicator ATR(1) with EMA(8) used for the stop level...0 V: G8 o5 C3 X- Y5 }  W" v: X" J# m
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 m2 g) o+ J2 O8 ?int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);! I% D2 ^# @7 l. D- ]/ T- ?) N
//--- Define a variable that indicates that we have a deal...# w1 ^) t  C2 j6 T( T/ i4 E8 L7 u
bool tem_tick = false;5 a# V" \3 H& f0 Y6 k
//--- An auxiliary variable for opening a position3 D; F7 x2 N8 b
#include<Trade/Trade.mqh>
7 _5 T' R; O1 C  ]#include<Trade/SymbolInfo.mqh>
; a. i* F  F( r: k7 ?( L4 J0 PCTrade negocios;
) z7 f3 B' D! N# D6 b6 b( `CSymbolInfo info;
" g' _1 g5 J, [//--- Define in OnInit() the use of the timer every second2 _9 X. v4 ^- i( w
//--- and start CTrade0 a+ @; C* u) T" J& d: o0 m/ P
int OnInit(); O! a* V4 u) W4 Q9 u7 X
{% F: t" R) D5 j' T" {" }7 U* r
//--- Set the fill type to keep a pending order
  f8 G+ C% G# J& x//--- until it is fully filled
' f2 ^1 P5 \4 a' [3 h; X/ [negocios.SetTypeFilling(ORDER_FILLING_RETURN);; u- ?: i6 y( M- L+ h) _7 I1 k9 `* m
//--- Leave the fixed deviation at it is not used on B3 exchange% W, @5 M8 p. ?- X/ F, J/ u9 G+ B) v
negocios.SetDeviationInPoints(5);
$ w2 l8 L2 d) [  ~1 ]//--- Define the symbol in CSymbolInfo...* z6 n0 k% q1 M% K/ W
info.Name(_Symbol);, ]/ j# X$ Q# [# I
//--- Set the timer..., B3 G: {) C8 ^; U
EventSetTimer(1);- D3 W$ y  Y6 ~1 G  Z& y, k2 L. P
//--- Set the base of the random number to have equal tests...% U. ?$ M/ P1 M! T& w6 \0 m
MathSrand(0xDEAD);
! d* M8 b! E7 ~! Nreturn(INIT_SUCCEEDED);0 D# L5 m& S9 g! v' D
}
+ V7 V9 A( V' L; N$ ~2 n: f) t//--- Since we set a timer, we need to destroy it in OnDeInit().
+ P' Y$ I( f  O, vvoid OnDeinit(const int reason)" L  m! f# v4 j8 W3 M7 @# p
{( S. {. d, P' c$ j' T
EventKillTimer();3 v% X6 h3 W% c2 M6 c
}
! u7 S4 d$ @  |+ R//--- The OnTick function only informs us that we have a new deal
6 H: X: `$ O# [, C( n0 mvoid OnTick()0 Q; L7 w# ]) j% u
{
, H3 I. w; c  W  N6 R$ e$ `tem_tick = true;$ o- h: c4 n% r9 A* p
}+ i) T! v3 {- I2 z/ d  a  J
//+------------------------------------------------------------------+: w1 x, H" N+ i: C  w9 K, r
//| Expert Advisor main function                                     |3 _) R6 `4 ]9 A* v
//+------------------------------------------------------------------+
3 C6 e- W8 G8 V( C) O9 Zvoid OnTimer()
* B% ^: N, e- [4 A2 [{
9 A) S* H: F7 U* W9 W; UMqlRates cotacao[];' x% D" Y# v( G! p0 r( g
return ;# K, n  p6 o' _* }: t  y# Y4 Q
if (negocios_autorizados == false) // are we outside the trading window?% O7 s' O. b7 [% O4 X
return ;; C" `' q) y7 h6 M2 M  J
//--- We are in the trading window, try to open a new position!
% ]; b3 J5 g% Uint sorteio = MathRand();
" W" T$ O" n9 x, k//--- Entry rule 1.1
  [0 f8 o" K6 ?4 eif(sorteio == 0 || sorteio == 32767)- p' O7 ^% K3 L- ]; _8 p7 n9 x
return ;
; B8 S( r6 o3 _6 y* Z+ F; kif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" c9 i. o/ ]' ]  H' T
{
% J  G  m# f; Znegocios.Buy(info.LotsMin(), _Symbol);4 p0 y- H$ e; A6 r
}
0 m5 T  ?8 B& {1 k0 q8 Telse // Draw rule 1.3 -- odd number - Sell" T( h6 ?+ a' A* q' \) y
{* A1 F! H! r! l- Q8 T$ Z
negocios.Sell(info.LotsMin(), _Symbol);
' q& L+ r; [/ `- |}. H" x8 c8 |  h% N  p! F7 |( o
}
4 \: n6 }( e4 W+ @5 @0 t5 m1 y//--- Check if we have a new candlestick...
0 ]' g0 ^6 R* l) M/ J6 \! V1 rbool tem_vela_nova(const MqlRates &rate)+ N+ f' {: _4 M) k, x, [
{
7 ^. y4 h; Q- R5 z* P0 F, Z{* Y3 \  {0 A" m
ret = true;# M& O1 W- @. f7 b# y$ L
close_positions = false;
+ x/ |6 N5 c, R$ ^3 e}" _" \; G2 k- @5 G  C+ K5 n5 b
else( F9 ?6 l. ]5 {% F: b5 q1 o5 U
{
. k' I5 f5 t0 H9 `" ]+ |9 D6 Xif(mdt.hour == 16)8 t4 e: F1 j7 B0 A' C/ `
close_positions = (mdt.min >= 30);. [9 T  N) t/ E) f
}
$ u# }: z0 m* _- ~6 I" W}
- I" w+ y" {9 P) q# @return ret;9 f8 |4 Q! B: e. `$ t6 Z( Q8 C
}
( R" H, |5 d& t0 Z4 g//---
& l9 w5 N# e2 F$ Nbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& |9 [& v* N- p; D( n' I{, E7 ^/ C; p$ E' A6 h7 [8 E3 Z- F
if(PositionsTotal()) // Is there a position?' F: X7 ~( a# k: i4 S
{
7 o8 c9 r7 ]) Q- w$ z) H  Ndouble offset[1] = { 0 };+ t% l' ]+ Q% Y
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?" s6 I5 j, l7 _. K% [
&& PositionSelect(_Symbol))  // Select the existing position!
  r  \  R" W" b5 j/ R! ~1 {{, @2 b1 Z) }" @
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 M" ~: w9 h/ D- T
double SL = PositionGetDouble(POSITION_SL);, Z: ?- E* Z+ d1 S
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ f% F/ p! N9 c4 Vif(tipo == POSITION_TYPE_BUY)" H# f/ \2 e& ?
{
7 {; Q% V) C1 g6 E& d3 l8 p9 K. f9 Uif (cotacoes[1].high > cotacoes[0].high)$ Q# Z# V; J8 g+ `  N+ o
{3 X; T$ E  Y2 M/ U( H
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
/ i( r0 N- G& K1 r1 p9 Tinfo.NormalizePrice(sl);8 h  M8 K# E6 N- Y) {: L" k5 ]
if (sl > SL). r4 z- f0 k2 m3 h: n
{0 U; m8 O2 J0 P8 ?
negocios.PositionModify(_Symbol, sl, TP);" X9 c# z+ q+ V9 ]
}. Y8 B$ ]8 o- ^( G" H
}: B: z; f2 H- g& C  t
}
, r8 t& W! v/ R6 }4 lelse // tipo == POSITION_TYPE_SELL- n# @+ K+ r4 G( |* n
{2 r2 ^. j, g. N/ A4 N4 x* ?1 b
if (cotacoes[1].low < cotacoes[0].low)  Y( |( d7 x4 E: b5 K
{
6 @! j$ ~/ l- c' Ireturn true;: P; p. _8 ~, Q6 b, K7 J% z
}
& l  J2 b; s: A5 @) }* \// there was no position2 F* u& a" v' b7 s! p
return false;
' A( U3 R( r/ E3 E2 H}. F2 p" v- W4 X. n: f) @4 g! @: J
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。% J# l# U$ ^$ I
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 05:39 , Processed in 0.643050 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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