私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA" {) L/ @5 B: ~: X$ o. h
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
7 U& R" f0 N0 G/ D" {2 d为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。  x- L3 n( n! h' k) w3 Y6 V3 m
以下是制定这些规则的代码。) @. q( q% F4 P$ E5 R, H4 d
//--- Indicator ATR(1) with EMA(8) used for the stop level...- ~: X3 Z. ~" }$ F
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 p7 D: E1 F& e& J% _0 q0 X1 Gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
6 A  b; h7 y. d9 W' H//--- Define a variable that indicates that we have a deal...
" [( i! w: b! W, s: Jbool tem_tick = false;; g; l$ I' `2 z& E
//--- An auxiliary variable for opening a position
4 D! Q3 \/ T3 P1 X  g#include<Trade/Trade.mqh>, |3 K4 @, W, u
#include<Trade/SymbolInfo.mqh>
# {  {7 z1 M' Z3 C" lCTrade negocios;
: Z: [$ w) N6 z' t; E: o" W0 K9 CCSymbolInfo info;3 p# ]2 t! H$ @$ r5 A- ~" m2 \0 P
//--- Define in OnInit() the use of the timer every second
6 O' ?# \: T% h+ ~$ ~//--- and start CTrade
( ~* a( Z  ^1 p) q1 jint OnInit(), Q: A1 |; @% z0 ^6 S( c* U
{2 c. u. E2 d0 b% i
//--- Set the fill type to keep a pending order% h8 C$ d  u' K. d
//--- until it is fully filled' H% V6 D$ X$ a9 m
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
9 G% M9 Q5 i% z//--- Leave the fixed deviation at it is not used on B3 exchange4 M. f7 @  h. e" S
negocios.SetDeviationInPoints(5);# y* [- f9 e5 m/ j2 P
//--- Define the symbol in CSymbolInfo...
( g& P$ N+ e% ?: S# Zinfo.Name(_Symbol);
2 F* \; c  B. [2 v3 P& I" C//--- Set the timer...
' ]  U  k5 ]5 D# G$ h& d4 e8 G' C  ^EventSetTimer(1);" k0 j6 a4 W* z7 T$ E. O0 w3 V
//--- Set the base of the random number to have equal tests...
! |! }/ I3 m& T1 o1 ]MathSrand(0xDEAD);
2 q7 m3 ?6 p( ], X* areturn(INIT_SUCCEEDED);
0 Q! O0 Z# E2 q}- A+ O/ v1 E) t
//--- Since we set a timer, we need to destroy it in OnDeInit().
+ m. N2 I7 [! x& \' u; f& r" Svoid OnDeinit(const int reason)  s5 o; u% _  Z
{
; E# Y2 u' X, I0 A+ j7 R9 SEventKillTimer();
$ ~. l+ M+ f5 B# Z}
$ Q8 S# K7 C6 A3 `//--- The OnTick function only informs us that we have a new deal
, A* Y$ @2 _% c& ?8 w; Kvoid OnTick()& {* T1 F$ p: C/ B: O
{3 u2 l" v/ `; j7 I, \' \8 N- I
tem_tick = true;0 @. V7 h% G: H% `0 W7 T) F' w" }4 {
}
! u. Y8 E9 ~, m( C) S//+------------------------------------------------------------------+9 l3 J0 X( g' E1 a. X' q
//| Expert Advisor main function                                     |
) ~! K) u. `( g2 N" q" c//+------------------------------------------------------------------+
' d8 h+ i3 p$ D: v9 ?( kvoid OnTimer()# G/ p7 M  q4 _2 Y4 l. ^
{
3 l9 q/ j+ T: IMqlRates cotacao[];8 X4 q' a& y" H* l5 i
return ;2 x# I" }# k: _7 f8 `
if (negocios_autorizados == false) // are we outside the trading window?
0 F/ t+ K7 q% g  L$ j5 p8 {return ;& T# y( M6 c( B& ]. g$ A
//--- We are in the trading window, try to open a new position!: i+ a& l  C2 e/ W/ @
int sorteio = MathRand();
  V& D  r6 F( D//--- Entry rule 1.1  c$ G# Q9 [% q" p# Q7 ?- U
if(sorteio == 0 || sorteio == 32767)0 d' Y2 P$ A2 z- R7 n! V- P
return ;
- ?% G7 M6 r5 X% _if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. ?/ q1 J+ Q! Y. q# S, T- v1 i
{
5 w1 l$ Z. o: F; K/ L8 snegocios.Buy(info.LotsMin(), _Symbol);
5 t: F3 F( L8 q# s' _, w6 P}2 c) o  W* R' R" ?" L( ~$ W
else // Draw rule 1.3 -- odd number - Sell
! R+ [# i( q' V& q& ]: v3 W3 |! e{) ], t( X4 Z! Z1 G
negocios.Sell(info.LotsMin(), _Symbol);
; ?2 u  ]8 I% r$ [7 ?% H}; W: O& h! e0 n* ^% I, Y( X: f# e
}7 S- q; e) x) q; G) ]
//--- Check if we have a new candlestick...
% Q) b/ O( e6 b  J; n% k; cbool tem_vela_nova(const MqlRates &rate)
' E' C/ i! `: K8 @{" T+ r% Z- ^+ `9 Y
{
5 ?; g9 G+ Q1 [& Qret = true;
  i7 `) C1 ?. I) R( }+ _close_positions = false;. {' i5 d3 R& @( w% u! i# F7 s
}
2 X+ R6 C" d( m: ?1 ~  z( belse
5 i4 w6 e1 v. q$ O, r8 u{7 C/ i' }- E# H9 v6 W. U* M: ~' X1 l
if(mdt.hour == 16)7 r: ]6 ?& z* o0 l8 B& o9 |
close_positions = (mdt.min >= 30);1 o1 ~6 ^2 V9 l) n5 h0 q7 t
}# F6 G7 O' H* x" p( f7 {1 `
}
0 |! ^$ V- o* ?1 Oreturn ret;
& d6 {* ^7 b. e, ?" n6 R) j}& x  F: e* E/ U/ Y
//---0 r+ P$ o9 C  ]/ ~
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% n$ {, m% n) g0 V% Y
{& x# G) H2 ^, _  u) c2 X6 o
if(PositionsTotal()) // Is there a position?5 P  ~1 K# C. O  t' e" t' E
{$ s7 S2 A  S" }2 a1 A
double offset[1] = { 0 };
$ T: U7 y; T  h2 u8 f/ S" oif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?/ U% Y5 n7 L7 _4 R* K) l
&& PositionSelect(_Symbol))  // Select the existing position!5 x! m' u9 ^# t3 s5 p
{6 Y% L5 R; F- A0 P; x, Y
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
: j) ?7 m5 }' d. j' B, ddouble SL = PositionGetDouble(POSITION_SL);7 Z7 E9 `1 b' _  d- F
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
/ ?1 J, {. J7 F4 yif(tipo == POSITION_TYPE_BUY)
; r; d: l5 {5 R0 C7 s2 O* l{
7 ]5 }% J. O" T# ^8 F, Xif (cotacoes[1].high > cotacoes[0].high)
' y4 a0 }- Z& K4 @% R& A{
0 s8 c% P9 j( rdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];8 b& e$ C) @- g+ |+ P* _8 c+ H' n
info.NormalizePrice(sl);0 P- y5 \# C$ u7 I' {
if (sl > SL)
: [% h" ?) p9 Z0 Z5 d7 I1 N{
3 g# N: X8 B/ A" onegocios.PositionModify(_Symbol, sl, TP);' R! w: D; {! _1 k& r8 I7 G
}4 P: D7 L6 @% [  _
}
0 Y( Z  W/ ?. [. ~3 t5 v/ r}
2 C+ L; u2 U* J  r! Velse // tipo == POSITION_TYPE_SELL
+ \6 V+ p( T9 `# P# [2 o9 _{" i4 j, l% @, e, ]! I
if (cotacoes[1].low < cotacoes[0].low)
5 ?( w' b/ S( A/ t( S  P{( C" @1 D. g! s( l
return true;$ Q) g* L& K( y0 N0 \( Z( ^
}
5 K4 U) b( x7 a: r: C// there was no position, l) r0 b  i  }/ x. }0 d: j7 j$ P
return false;
% ~0 I( I3 n. Z; q% Z* N}
5 h# c2 B  r$ }1 l, L+ k我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
, R. |2 k+ y& M6 c4 o到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 17:06 , Processed in 0.744990 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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