私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  S: L& r! q6 K$ i7 I
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。4 z2 C; j: v1 P, z) u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 w  m+ ~" T1 M0 ^( Q
以下是制定这些规则的代码。, y4 U- W* r# }* S5 o! H! M1 H
//--- Indicator ATR(1) with EMA(8) used for the stop level...& @% w' U4 m6 \
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);; ?' }9 k- p' V. o* E+ M
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: M; ^5 F6 o' y# |, H/ T" A0 c$ E) E
//--- Define a variable that indicates that we have a deal...
+ m! X& {5 P3 T, U/ Gbool tem_tick = false;. ?. v; w5 o: Q6 v" J
//--- An auxiliary variable for opening a position
$ e4 ~1 T0 L+ m4 g; s& [: d) Z#include<Trade/Trade.mqh>
+ d: S9 x  q0 Q/ X8 `7 H8 s% I" U/ G; y#include<Trade/SymbolInfo.mqh>
" [- c% B6 u( n9 LCTrade negocios;* `# x4 U1 I- K( }. Q
CSymbolInfo info;5 C$ K* O: l+ {$ A' e* G  _- [
//--- Define in OnInit() the use of the timer every second
1 k& o6 n- B" J9 r4 Z, ~9 ~  k+ @! D//--- and start CTrade8 _% l; W5 B  d: `
int OnInit()9 B2 k* I/ t' o, x8 B: c- |; N
{# `; ~  {. Q  X4 r$ e8 N3 l
//--- Set the fill type to keep a pending order3 w5 _' F3 U" |2 j! w% N( J
//--- until it is fully filled* c1 C2 }' k$ B4 D: x
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ A7 j. B! y' E2 x' i% n' u# \//--- Leave the fixed deviation at it is not used on B3 exchange
- w/ i. a6 k2 o2 H% p4 |negocios.SetDeviationInPoints(5);
, T8 I: B. }+ R9 X% T//--- Define the symbol in CSymbolInfo...7 C5 Y' i& O% K2 b4 Y# i  Y) E8 k  k
info.Name(_Symbol);
7 b9 `$ j8 O4 J" @//--- Set the timer...2 s( `, j+ t& j7 g
EventSetTimer(1);
) L. H# r+ K/ N+ z; ~9 g//--- Set the base of the random number to have equal tests...
3 ~; R) d) i4 j+ U, k. }0 V9 m- Q: _MathSrand(0xDEAD);! t+ f  l+ }9 H$ O3 s
return(INIT_SUCCEEDED);2 c, _+ o5 m# O, V! f
}$ a1 {# h9 x3 \9 {( b% [
//--- Since we set a timer, we need to destroy it in OnDeInit().1 `  o( m1 F6 D4 \4 |: p
void OnDeinit(const int reason)
" R) u  I9 R' M/ I' H7 s{+ R- _8 _# @  g& a( x# y
EventKillTimer();
1 h& @3 G: L' V  p# |( K) `  c}
. {, f* l) W& }! H! p# ?3 S  ~//--- The OnTick function only informs us that we have a new deal
2 y. s" g7 b# W6 ~void OnTick()
: F$ f# w& l8 x{
& @5 _" z' y1 {- R2 L$ Qtem_tick = true;) t2 q( A4 {( Y# b4 V+ |
}) M# f2 @: C# X; C
//+------------------------------------------------------------------+
  h/ P6 ~& O# E2 y. ?; S//| Expert Advisor main function                                     |
4 z2 z9 }1 R' w6 p0 n5 b. ]//+------------------------------------------------------------------+1 q) Y2 k8 K2 A. h
void OnTimer()2 s2 u$ e# e, F, [) D7 z, N
{
% M6 z- W: P9 b7 a6 ~MqlRates cotacao[];
' N. u$ d/ Q& j1 g7 v& Breturn ;$ N; B! M/ C, E1 L. }0 X/ j& C+ P. m
if (negocios_autorizados == false) // are we outside the trading window?
2 [% t; k6 T8 M# |return ;# o0 p1 u, N9 E* k) i
//--- We are in the trading window, try to open a new position!
, ]! D! N5 n4 b* J$ v6 _) eint sorteio = MathRand();
6 J2 U5 T1 s  C//--- Entry rule 1.1. r; c5 v6 s% Y4 h* G5 E5 L" q  a
if(sorteio == 0 || sorteio == 32767)
6 q: L' k0 Z% S( creturn ;6 x% p# W, Z! Q' ~* A+ [; |
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
$ T5 I4 U, p# e: n* G' u{
! \1 z: u4 x: o3 |: Z( \negocios.Buy(info.LotsMin(), _Symbol);0 b( U" ]- @# B8 Z; \
}
% R" L/ }9 a2 Lelse // Draw rule 1.3 -- odd number - Sell& C; z& t1 u! \  ?) J" R" ]
{
  u, t  d8 }0 W/ D$ k. }  rnegocios.Sell(info.LotsMin(), _Symbol);
# h3 a& R9 G2 P  b}
. b. p$ x7 Q( Y" [}6 u  Q9 D8 {  K# q
//--- Check if we have a new candlestick...6 f$ \* K! Y$ k0 G3 z( Q
bool tem_vela_nova(const MqlRates &rate)
4 L# D3 J# h7 a: z; T{* T% f5 T& J  H' Z: r- f
{. e- [+ X3 U$ J7 }- J$ S5 k3 }
ret = true;; Q) v- n) a7 A8 h( t3 b6 @
close_positions = false;8 E! L+ o8 ?! |2 v3 T8 v
}* B7 ?2 r: z3 Q: f) T9 [. u
else9 Q4 N4 u' S4 X1 H
{
: r2 S5 \2 L& M: t. e, _if(mdt.hour == 16)
% l; m9 M( r2 L! Z7 R; R% Hclose_positions = (mdt.min >= 30);: s9 g, t. m) L# q5 O1 @* t* x
}
: P1 e! b4 e6 A- k  ^. e8 w}
7 ^7 f' {2 @& s7 s8 C0 ?return ret;; W, F+ {! z* s1 d  t
}4 F: J; k+ q; s) Z
//---& x- a/ P- m& A4 f9 t7 I3 W; i
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
6 ?- d) N5 E. l" [& K1 }: I% G) o{
% N$ f/ l6 z" J/ x" v2 @. Kif(PositionsTotal()) // Is there a position?/ A  F* \& W$ F/ x6 s; o6 W0 ]
{' E1 \4 S# V$ i( Y4 \
double offset[1] = { 0 };
: Z. p* R: A4 U9 T5 O. |  yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?% g5 L% m! o, E7 U- A% s2 A( [
&& PositionSelect(_Symbol))  // Select the existing position!7 G1 w' t( G! r
{
" {8 ^* x, x) B- zENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);; t; v' p% f% ?# o& }
double SL = PositionGetDouble(POSITION_SL);
' [9 Y  ]% q8 T9 F5 @2 E& A" Z8 ddouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 f, r* e2 R) P+ P  H' F* `
if(tipo == POSITION_TYPE_BUY)
  L1 s8 P/ G7 }' v5 e8 E, Q& B) F{
- G& `8 Z+ C  Oif (cotacoes[1].high > cotacoes[0].high), P3 ^( A& ?- Q' @% B3 d
{
  v4 i' x$ s: y; k7 gdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: K! \; M3 R) y/ l9 |9 @
info.NormalizePrice(sl);4 f4 ~+ X( X/ J. G# Y. c- N) E
if (sl > SL)( `5 r- v3 k5 @4 Z
{
. H7 q. E7 o3 M0 F% C2 V# e( X5 Fnegocios.PositionModify(_Symbol, sl, TP);7 h; L2 U( [5 O- D: J+ E# W7 h0 a
}5 L9 Z# n1 M+ q; p$ x
}
$ R2 w5 W! Z  I8 N}+ X3 [; S* j( {- o" o
else // tipo == POSITION_TYPE_SELL0 r0 t9 @3 D. s% g
{9 c8 m4 k, ~% u6 p( F' @/ F0 C
if (cotacoes[1].low < cotacoes[0].low)" |; f/ X) s8 i7 [
{" [# a6 u8 ], F+ H' Z, ?; ]
return true;- r* p8 A# y) d/ o- w; [
}
. f5 z! F$ n5 F: ]// there was no position
8 A8 z- n2 P! {4 X" kreturn false;) z; B2 g3 V: M9 C- t
}3 H: Q( f6 [  {; m# a/ x
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。1 J6 q/ M# d7 l1 `1 B
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-2-13 20:41 , Processed in 1.551846 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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