私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
9 T) w( ~+ e, ?( i& U/ r) U6 H在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, m, z, Z( ]' {' Y3 i
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
& }* M' m5 x$ W  m% Z以下是制定这些规则的代码。
. T% S& {! k( X4 H7 u//--- Indicator ATR(1) with EMA(8) used for the stop level...
6 ?+ q$ n# P2 o& I" T3 g! X0 a' e  hint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, w1 ~" n1 ]2 \' Xint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' X8 m6 l8 H1 V3 U8 b$ Z7 ^( o. a
//--- Define a variable that indicates that we have a deal...
2 S+ m6 V8 P8 |8 K' l- Pbool tem_tick = false;
6 X& T+ p; J6 K! R9 t. v1 f; ~; Y//--- An auxiliary variable for opening a position8 |  x3 \, g3 |  J
#include<Trade/Trade.mqh>
, z# i; d7 R2 n! a7 }/ E; [#include<Trade/SymbolInfo.mqh>7 j+ t& |6 G" X8 g! ]
CTrade negocios;: A4 o* T' W' `1 N3 V: J' U
CSymbolInfo info;
4 A' M( g- p; p# b  p9 w$ {//--- Define in OnInit() the use of the timer every second* g3 a5 @) O3 r% F4 H1 s
//--- and start CTrade
+ C+ m* H  J+ d6 Tint OnInit()
9 s( v* L7 N  J0 B{
; [$ ^+ c. b1 T% e( ^//--- Set the fill type to keep a pending order
  _8 z6 r" y. t, ]6 d//--- until it is fully filled% v, A1 k  K; N4 h0 p# d) ~6 O7 `
negocios.SetTypeFilling(ORDER_FILLING_RETURN);# M* R$ }; c+ \) A9 v( j
//--- Leave the fixed deviation at it is not used on B3 exchange
7 }2 N( k& ]- G3 s: m- ~2 G( |negocios.SetDeviationInPoints(5);4 R  r9 P) u1 c- F. g
//--- Define the symbol in CSymbolInfo...
! ?5 h) c) u- Xinfo.Name(_Symbol);
6 r; ]' \  i. x- l0 }; n//--- Set the timer...6 z7 o, h' s" _; ?' U! K8 M
EventSetTimer(1);. O- S" a& R3 y% |& s7 _
//--- Set the base of the random number to have equal tests...# Y) x6 j9 W1 V5 U! x3 h7 G
MathSrand(0xDEAD);. d# q" f' H  i# X) z- l
return(INIT_SUCCEEDED);' a, t! E( T1 ^2 `! |
}
8 t4 Q; M9 z+ P4 F//--- Since we set a timer, we need to destroy it in OnDeInit().
% _+ g6 T( t. r' rvoid OnDeinit(const int reason)# H7 ?9 i% h  o
{
  H! Y% v- S2 a# J3 u5 `* iEventKillTimer();
. B9 s5 N2 k  T; I, s9 O% H}( y; F/ v1 x/ F. w0 G+ E
//--- The OnTick function only informs us that we have a new deal
$ e4 X& B5 a+ j5 Y( Kvoid OnTick()3 X  x/ g- l9 c
{# |+ U* ?& N) C' I
tem_tick = true;  v, @( {2 r7 R- S3 ?
}
  B6 v3 u' x7 U6 X& B//+------------------------------------------------------------------+
) v4 C& _- M# v+ W//| Expert Advisor main function                                     |
+ z$ l6 j3 b0 ?$ }//+------------------------------------------------------------------+! n' h' `' N1 D1 v9 m3 h
void OnTimer()5 y" n  G% c4 m
{& j& t* N, U* e
MqlRates cotacao[];! i+ g% x' m- F* `  ?/ d
return ;
$ V# L- ]7 V; P1 nif (negocios_autorizados == false) // are we outside the trading window?
$ p) c' V* _' E( y, Wreturn ;4 m; V$ B2 U" X. f5 u0 J/ e# r4 K
//--- We are in the trading window, try to open a new position!: w* \0 O- W' A5 |/ Q7 u
int sorteio = MathRand();
0 ~: X* s0 N$ c1 ^5 z//--- Entry rule 1.18 Y" `# o9 L0 _+ L+ _5 I5 m( \
if(sorteio == 0 || sorteio == 32767)& Z) G$ s6 B$ A
return ;! L/ P1 [% p% F1 D
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  f2 n5 s# X# }3 ~5 A7 a+ N
{
9 e5 k4 j9 M  K" k& k- Knegocios.Buy(info.LotsMin(), _Symbol);  g5 G3 R3 ^' ?8 {) A, o7 X% w
}
" D0 d( P& X( h) t/ m  Oelse // Draw rule 1.3 -- odd number - Sell4 Q1 H* U' N8 z, K  P0 c  ]) N
{, u/ ]/ c2 J  x2 R
negocios.Sell(info.LotsMin(), _Symbol);; j+ W2 d$ }: w5 D* I. P2 P8 X
}
' I) S7 X1 A! g+ f) ~& u}
8 a' H: i# n: }. B' `1 Q2 [& I6 x//--- Check if we have a new candlestick...- y8 T: M7 o2 W1 P2 F' H
bool tem_vela_nova(const MqlRates &rate)3 @9 m  |+ f+ ~
{* m2 |6 ]; S: U
{
5 G( [5 u- z2 R/ k- R, dret = true;2 J/ n6 M! K$ A/ I. k
close_positions = false;& e0 S+ }4 o7 s9 ?$ ?& K
}
% z2 ?1 }/ @, ~5 v4 delse
# D+ d" B7 P/ q9 k{, q, R: }, Y: A1 ]
if(mdt.hour == 16)
( }- |* @) O* D  ]$ ^close_positions = (mdt.min >= 30);
; b0 {8 K3 t/ F5 l2 |3 v! Y5 C}
/ M+ v  T% z  t0 [, @" E}
0 y( K7 o2 S2 m: h8 yreturn ret;( u9 `, S2 n- F5 S
}  F6 @3 e$ ]! g) U* z3 g* e& s% P
//---. L' Y9 b+ ]6 G
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 @, C1 y) v4 V6 e8 b
{% d5 S3 w0 X6 S/ V
if(PositionsTotal()) // Is there a position?
  ]9 P: e! o" a; f/ s+ ^; x; |; M{7 u. L) i/ i+ @8 A
double offset[1] = { 0 };0 @2 u$ ~" ]# b$ i) F8 N2 V
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 z) E$ n; L# `' v# s, d
&& PositionSelect(_Symbol))  // Select the existing position!3 v: B) o/ H6 I) Z) Y3 k
{) y: U' r# z6 }) ?& r
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
4 v; `" e% h' `( c8 ~& R: udouble SL = PositionGetDouble(POSITION_SL);5 W( ~. Y# |8 v* P+ {. a  [/ t7 }
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));; U! Z( m9 f  b& ]
if(tipo == POSITION_TYPE_BUY)4 q+ c* ~# p" k5 V1 l$ r  |) F
{
9 q& m' `. M: q! A  _if (cotacoes[1].high > cotacoes[0].high)& H0 i% i) R0 _( G! P; r) G6 {
{1 t/ S6 _: T- Y) }* @
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# R0 a: |+ t/ s2 A, [* T/ [4 I
info.NormalizePrice(sl);2 Z; r6 `8 K6 ^
if (sl > SL)2 M! L! F2 l4 p( y3 v
{
* G. {8 t/ f& @$ o! C, jnegocios.PositionModify(_Symbol, sl, TP);
& |2 L/ p: @. l: o' {( h& @$ K}
  u' A0 G/ W$ }! V% J7 g}
* }7 n$ K9 M3 x% S, U}3 A. I2 d& a, i' p$ d
else // tipo == POSITION_TYPE_SELL
* I% W3 l. ]+ ?6 w6 g4 m{5 `( k0 c$ H8 a; f: U
if (cotacoes[1].low < cotacoes[0].low)3 b" g& t. X" x3 Z
{
) o- y- _  f5 P. J+ O; K( B1 V$ Y; vreturn true;
1 j6 i$ S2 |$ Q}
% V- u+ a* h* l// there was no position9 h! a% f1 U3 r6 m
return false;" k4 x2 z+ [/ N9 a2 t1 D3 t
}
# }1 L$ A7 ]5 ]# J我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 a0 V: k8 [. v. F$ r' j- ^到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-4 22:13 , Processed in 0.410573 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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