私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 W1 q- E4 V# m5 H2 z
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 V( [( i  h. u) j' _2 X' O: ^$ E
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. j% O- t7 L1 w, e
以下是制定这些规则的代码。2 p. g# R4 O5 o+ }" i: C
//--- Indicator ATR(1) with EMA(8) used for the stop level...6 H% a' j& O, Z  W' c
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
( H9 p0 {: i# }$ \- S! S- Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
# a5 S! ?/ }6 ?' Y//--- Define a variable that indicates that we have a deal...' q, T8 [# z- C# B. ^& o$ e, _2 n
bool tem_tick = false;- C1 |- L. q) s$ F. c
//--- An auxiliary variable for opening a position
' N, v  }: Y; C) u7 m3 D#include<Trade/Trade.mqh>
8 b' @0 _. w, x1 G6 m7 o; U#include<Trade/SymbolInfo.mqh>/ P- V$ P- o1 D; c7 R/ m9 q
CTrade negocios;( y: B% b: K8 a+ W1 }
CSymbolInfo info;
& y* P' |1 I* F) D* d//--- Define in OnInit() the use of the timer every second
5 b& \6 v9 u( v( k- J8 A, K, @//--- and start CTrade. p$ R, `% E) k; y" N8 S
int OnInit()
$ l( i/ k7 T. L1 W+ s. A4 o{
3 d7 d2 r" K4 H! w//--- Set the fill type to keep a pending order' V5 {1 G0 O; R
//--- until it is fully filled
+ B0 k1 _, ?" S1 g+ [negocios.SetTypeFilling(ORDER_FILLING_RETURN);
7 F# W. [- H7 C- H! {. [//--- Leave the fixed deviation at it is not used on B3 exchange! w5 t2 y- Q& C" o% G
negocios.SetDeviationInPoints(5);
# O7 F& W  F* z% j//--- Define the symbol in CSymbolInfo...
& {- s7 W9 O8 A; ainfo.Name(_Symbol);6 x; J: ~- L( C8 P: Y# [
//--- Set the timer...
9 c0 K# ^5 [% ~! S% ?EventSetTimer(1);  S  ~) ^, p' o4 v1 u$ T
//--- Set the base of the random number to have equal tests...
5 x! n! I) G* d' b8 lMathSrand(0xDEAD);
8 p. \2 \$ `% d/ Hreturn(INIT_SUCCEEDED);1 `3 ?" b) B+ X, m  u
}
8 ]1 R5 z  K3 b# U" u//--- Since we set a timer, we need to destroy it in OnDeInit().0 x8 Q. o! T; D! g, U- D( s
void OnDeinit(const int reason)- o1 k2 _& [) f
{8 ]' _5 ^# c& n) ?0 @3 T! i5 e8 P
EventKillTimer();, k! T) T/ {0 {* }3 k7 x2 G
}4 \! R; \( w6 P; h( G; T* p
//--- The OnTick function only informs us that we have a new deal3 {/ G$ e( ]$ I7 P5 y/ o" l
void OnTick()% N+ ^. K1 g3 \9 a
{
- f( S' l2 a3 ~, p: }( ?5 c8 Ktem_tick = true;; r6 G' I, j( L" e
}/ z. s# b$ c1 i7 h
//+------------------------------------------------------------------+( X2 i6 _' p; ]- N
//| Expert Advisor main function                                     |  L# v" ?# g$ _; C! m+ z
//+------------------------------------------------------------------+
1 |6 Q) D- w: R$ D* l" ?void OnTimer(), `) ?3 T7 W6 ]2 \  r
{
# s# Z' O' K2 |0 h6 fMqlRates cotacao[];
5 P/ z0 w8 N% [7 p- ]. N' Ireturn ;
  X) R1 M! A6 T$ p# l& a4 Tif (negocios_autorizados == false) // are we outside the trading window?
) n# s* |0 p& O+ G9 E6 }return ;
/ {' `# U; p  t/ A" u6 \, B//--- We are in the trading window, try to open a new position!
( T! g$ a3 ?( A7 \int sorteio = MathRand();
  J- L: g/ }: s, f  O7 x0 x//--- Entry rule 1.1
% Y- p$ C7 `3 Eif(sorteio == 0 || sorteio == 32767)- W* Y$ O& _" [9 }0 X
return ;$ X8 K5 X4 Z2 }# G8 i
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 T" L8 }( b$ m5 x
{
. k* l, }9 x- x& _" A+ u% dnegocios.Buy(info.LotsMin(), _Symbol);- o7 v1 y1 j) U5 ~( t2 o/ d& v
}
) {! ^; Y0 m1 S3 J8 s% h, {else // Draw rule 1.3 -- odd number - Sell
7 q4 {% {9 {5 L# `- N{9 W- R/ e. f3 G) l
negocios.Sell(info.LotsMin(), _Symbol);
" P# p7 N7 m% E& d}+ q; m! {* A: t  V, L7 u/ S# \
}
- C  k2 _, |- ?//--- Check if we have a new candlestick.../ ?4 F3 O2 O3 X) J, [
bool tem_vela_nova(const MqlRates &rate)( a+ h* \( E3 Q
{4 {6 J; Y8 Z9 L* l. _/ k& e# h* K
{% @: T: `: [& ^; Q4 |5 D$ _" H4 J
ret = true;
& b$ w* a7 ]8 p' S. V- Yclose_positions = false;
0 s3 T  T4 ?' \. q' a}. A, W) G% h& @) k& |, |3 Y
else- S  l3 w5 `' \* @( y2 c( U/ j, f# T
{
( w' e; ?1 k4 u1 Dif(mdt.hour == 16)
- i  d8 }( i& `; Hclose_positions = (mdt.min >= 30);
1 Q' F/ F; S9 Y3 N1 H}; _1 {1 q/ f! g% p9 y9 u
}; U+ z2 w3 G4 B6 v( `
return ret;! d& [) J7 e" Z8 }  r* c' D: @" `
}
8 Y/ m8 b# c% P3 T1 l//---
; H3 {7 p- i( X: @- ?  k9 g5 \5 wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])  u& P# ?9 A& B% f' F! i. s
{4 Q/ Y9 q" N+ [# w  E0 h' V. h. n
if(PositionsTotal()) // Is there a position?
2 E% Z8 e& a1 f; n{3 d+ s: n) ]( Q
double offset[1] = { 0 };) N# u8 j) N6 D- J+ i* [$ l* a+ P
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
/ O! Q6 o  {0 u/ e8 E& k&& PositionSelect(_Symbol))  // Select the existing position!
( _$ p: Y2 @3 R+ P  ~% C# L1 h{5 f* B$ I7 g9 p! L1 W% ?% {8 W- v4 W
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' p' J5 T9 _' H& |( Z( F
double SL = PositionGetDouble(POSITION_SL);2 Z+ ~+ ?; h  X8 f
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 r0 K# X! A7 H: \
if(tipo == POSITION_TYPE_BUY)
# ?; I) o' `+ _  ^" Y# \{
! _$ a/ V6 z0 tif (cotacoes[1].high > cotacoes[0].high)* C$ [2 g- H1 k1 A- c5 x
{
! Q2 k0 Z/ a- B: r1 F  fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
+ }. H8 F6 ~% h6 X! a8 ~2 L4 dinfo.NormalizePrice(sl);
$ x5 k: ]6 j0 K( E1 q+ w" S) q$ jif (sl > SL)4 p( o  E2 b- A8 k" }0 f  Q
{
" x9 s7 Z$ t3 Lnegocios.PositionModify(_Symbol, sl, TP);
# {; ]# k/ I6 N( c- }}1 {" m4 N# ^8 \+ Q
}
1 R$ \( m' u5 Y# Y5 P6 }/ V}
4 C% L5 ~! p6 ]' U9 T9 Nelse // tipo == POSITION_TYPE_SELL, `; y  p* d0 [% M( R3 k
{
4 i3 X, a; i- F, e( @( [& p. gif (cotacoes[1].low < cotacoes[0].low)
. V# y" f/ p8 V{
" V( c. M& ^1 K6 J' Kreturn true;) t: n6 s( B2 L) F3 x
}7 s6 ~9 w; U! t0 T
// there was no position# q0 F  z( ~1 e
return false;
1 o# L. n4 h% A  l- R2 Z6 ?}
2 o8 h! B0 k' Y; b; t* x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& U- _. n9 O( O, U' _8 g到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-27 09:05 , Processed in 3.236865 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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