私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# `6 c& J1 C: \/ Q
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。/ o, X$ q9 y" e, d( }2 d: O/ L
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
& x: l& r& E8 t4 a以下是制定这些规则的代码。' E  }) Y1 p8 b0 E  M9 D
//--- Indicator ATR(1) with EMA(8) used for the stop level.... l2 W) G% D5 A
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);; E3 p) l2 J, J+ |. b
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);) S' B, \0 H. w7 @+ @
//--- Define a variable that indicates that we have a deal...' h2 g9 K: I  [. w" ~( K& j% G
bool tem_tick = false;: l' b- ]/ C" S0 U8 D; l
//--- An auxiliary variable for opening a position4 s, j: X9 j  y
#include<Trade/Trade.mqh># C, D1 R. v1 A# ^: Y% W" e
#include<Trade/SymbolInfo.mqh>
; D' V! w/ H  `CTrade negocios;; \6 b' k! t0 l+ v) _
CSymbolInfo info;9 C; s3 k2 s+ x% `8 Y# R1 v5 D
//--- Define in OnInit() the use of the timer every second7 c5 b% U( j% H! F& w# p# k
//--- and start CTrade- r$ E8 {$ G- w% f' K
int OnInit()$ g1 }2 a/ a; K4 j
{+ G3 ^- W  v. t( r) v8 g% Z6 D
//--- Set the fill type to keep a pending order. q, g5 f6 D+ C' H' @2 @
//--- until it is fully filled
% O* u5 ]6 K6 H7 _7 rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
5 T  L8 _9 h/ P5 h8 |9 N//--- Leave the fixed deviation at it is not used on B3 exchange3 y* ~/ u" \4 O. a- ^
negocios.SetDeviationInPoints(5);, {4 w) p9 _5 S$ C! z: }5 J% n
//--- Define the symbol in CSymbolInfo...  {& ?- c! `& P4 ?
info.Name(_Symbol);
4 R5 ~2 u* J3 H9 [7 y' J3 L, _9 \  p//--- Set the timer...
  \* Y7 Q# j& u1 G+ m! hEventSetTimer(1);
1 I% N9 d, n$ V4 H! B" Z& l& G0 J//--- Set the base of the random number to have equal tests...9 T* e1 q) o  e9 T' a) f* T; [$ r
MathSrand(0xDEAD);% D2 u- d+ C5 x
return(INIT_SUCCEEDED);
& ]" W4 F! b: R5 x% C; ]6 ~, w}$ c, y  j( i) O0 K- B5 _2 s: s
//--- Since we set a timer, we need to destroy it in OnDeInit().( W0 h& k3 J0 l1 j; Y3 Q7 u
void OnDeinit(const int reason)3 E# z7 G7 f4 z# f2 s
{$ I" p6 `2 D' |' l7 @, R8 G
EventKillTimer();, v1 Q& Y) T8 e8 T& z+ j% J* k& [# y
}' U) a% I" ~* P/ z" o
//--- The OnTick function only informs us that we have a new deal% F& M" \" q5 k1 G0 C+ I5 i4 \9 T
void OnTick()' C2 x4 _. q) B
{
8 |7 O6 o- D$ c8 |! f* h, H( W7 Ntem_tick = true;# t' V7 [2 _7 |3 x& M& W
}
! B# ^0 }; f' T5 F* T  E) j//+------------------------------------------------------------------+! q* D2 {0 @4 c
//| Expert Advisor main function                                     |
2 u1 B; M* O' C3 N$ p% I//+------------------------------------------------------------------+2 ]& i8 K( a  P7 e4 k7 x
void OnTimer()5 b& f! B3 |' K/ [" k. `  {3 K) w
{6 F) b4 M: A  W
MqlRates cotacao[];
% ]' |( e! V; greturn ;
3 b4 p+ G( ]$ w% A/ h" w- Jif (negocios_autorizados == false) // are we outside the trading window?6 l6 b' J* `1 n5 N, ^, q. d
return ;  K# V0 o# E' R, H' n' Y) D
//--- We are in the trading window, try to open a new position!
% i6 p% |' q5 H) rint sorteio = MathRand();
( \& I) F% j6 B" O& U. p//--- Entry rule 1.1
9 G& H/ A2 d9 K/ H/ X3 |if(sorteio == 0 || sorteio == 32767)9 ^9 B; e- L( ]
return ;! D% u+ U9 P) X1 T) }
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
5 M* x( g' z" \( i% a, D% y{/ l  [' d8 b9 m  v* T5 _
negocios.Buy(info.LotsMin(), _Symbol);+ D3 h8 m. U3 D( m
}& \9 s8 T2 D) G* q9 E+ p
else // Draw rule 1.3 -- odd number - Sell
. z* |$ V- Q: S5 T' V{
* u- R5 d2 m0 z7 g! }negocios.Sell(info.LotsMin(), _Symbol);
( |' N: b' q5 |}* b# u* O+ \/ r& ~# e1 R+ f" ?
}2 ?0 s5 x* j8 r, F# @1 v
//--- Check if we have a new candlestick.... Q  p: \. Z* T" z' F2 p
bool tem_vela_nova(const MqlRates &rate)
$ G1 [: \: |4 H+ z, r{
9 `$ `6 W1 S+ E; F* @0 ~( n# ?, j! _{
  |0 j  y# g% a& T( H6 kret = true;! o6 h& B" g, S. Y) f. o! W4 r
close_positions = false;
/ m; ~" N2 S! m}( d# Z8 h; @7 }) `
else$ z' G6 g& O- M8 T4 e$ s
{. R( C  z1 ~+ D# s* M1 \$ B; _
if(mdt.hour == 16)# M2 v- Q; P" ?: @9 K! l* |. K
close_positions = (mdt.min >= 30);$ q# q6 t" M; E( E8 z
}
9 ]! v9 @) H5 v+ T! Z, ?( q}
4 e/ m$ |4 z0 B. @- `* H! oreturn ret;
, k# I! x6 B% x8 M/ q& O, S8 [}/ b6 ^& c1 M- s$ t3 O. M
//---
! d0 \  V# ~. S5 D" E! _bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
( Z( Y/ I/ n7 {* N3 `, z{
, S  a; q) a0 z# l4 B- Oif(PositionsTotal()) // Is there a position?( K& f& }7 P$ `6 _5 o7 X
{$ A- L( f- ~1 h
double offset[1] = { 0 };
; S' o& h, G( N4 p( I5 vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
: f# {: J9 T* A' u" ]3 z* `&& PositionSelect(_Symbol))  // Select the existing position!2 B7 z% ~" e, `! M6 U0 E: o
{0 U/ _1 ^2 S8 N& o; a1 `& K
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" D5 f% U. ~# _2 M0 ]9 ~
double SL = PositionGetDouble(POSITION_SL);8 E* X4 D( w+ U  U
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));3 h5 R2 O7 l3 L, s0 d: J) c
if(tipo == POSITION_TYPE_BUY)
" ?3 x" V! q1 S0 P7 y{
& E8 O7 n! a2 [3 d3 |- o" Zif (cotacoes[1].high > cotacoes[0].high)1 m: T& F! h& K3 F: \- f  G& M
{
. V8 X" {: \! B' {double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. Y' J& [- Y: [
info.NormalizePrice(sl);
: x4 F) D3 s4 [- Zif (sl > SL)1 k* |4 p$ I& P1 W5 @' ~
{
4 r+ `0 S! v2 u+ s8 j3 enegocios.PositionModify(_Symbol, sl, TP);
" f/ H2 A9 z( ^5 X) A}5 f% o% [: \& Z2 h! ~# V
}
; |1 J0 u: i$ P6 w}4 C9 q+ A! ~$ f
else // tipo == POSITION_TYPE_SELL
9 Y* c5 r1 F8 j0 R{4 m5 A3 A& u, c' u
if (cotacoes[1].low < cotacoes[0].low)) U# W7 q7 `5 b! j& t
{( N3 |8 y- N: C1 ]
return true;
$ e7 ~7 k& l3 F1 {* Z: j* n5 d}5 X5 f, q: z" R* \
// there was no position  U) z+ m4 @+ K* J
return false;) _/ \# e; \" y  c, e0 Q. W
}
& k" T4 T& ^# H我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。, h  i( _; K7 z/ F% c
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-17 18:28 , Processed in 0.577169 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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