私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
6 u7 E4 q9 b1 P5 X/ @在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。  l( U6 a- h$ K, R
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。! V$ e" q0 E- z8 }: X4 F& @
以下是制定这些规则的代码。; g& O  ?* O. a$ v8 u: E+ P9 J
//--- Indicator ATR(1) with EMA(8) used for the stop level...2 i" k, A- O& @: s7 f/ t
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
2 |1 P9 F5 ?9 O4 p. k& Rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
( {" \# m( W' \: L$ |//--- Define a variable that indicates that we have a deal...
2 [8 H0 M. {0 W1 n& g/ D& ybool tem_tick = false;- x$ c6 K  M4 r0 a3 H
//--- An auxiliary variable for opening a position
# R3 |" d. j6 |7 h6 l6 {" w( u#include<Trade/Trade.mqh>
. ~' o  w8 {  i* `6 e' S#include<Trade/SymbolInfo.mqh>( w$ q6 c+ x& B* u& K" G
CTrade negocios;* F1 r! b. Z; r5 \! l
CSymbolInfo info;: D* O0 g1 C' F: W0 I6 F9 t
//--- Define in OnInit() the use of the timer every second# p1 |2 }0 _- `; p
//--- and start CTrade
- u+ G% \9 D$ w8 r2 a4 cint OnInit()
; r* g9 Q  K: `9 t7 A- K2 u: V! {{
  x3 q8 h( ^$ c. B8 ]//--- Set the fill type to keep a pending order0 ^, x1 K9 ^/ X2 Y5 E$ S! g; ?
//--- until it is fully filled
0 W8 ]8 Y. b" Q  B0 R7 C# C# ]* s! knegocios.SetTypeFilling(ORDER_FILLING_RETURN);
. K& u4 n- E; N! t* X/ d//--- Leave the fixed deviation at it is not used on B3 exchange
/ S0 z3 l( D$ {' |: B- W. S4 [% bnegocios.SetDeviationInPoints(5);4 |) ~) q) a* o" s) p
//--- Define the symbol in CSymbolInfo...
4 G  a% }* y, m; \( b4 V2 c7 k, n8 Minfo.Name(_Symbol);
2 L. R; i' _/ p/ B//--- Set the timer.../ k7 T0 G: W& Z/ C8 P- Q
EventSetTimer(1);; d6 j) M' W# ^5 |# t
//--- Set the base of the random number to have equal tests..." c0 c- r2 {' O
MathSrand(0xDEAD);4 g! ?. U0 t% G1 t( B* u2 {3 I
return(INIT_SUCCEEDED);
0 P" T- w+ s* P  G' j& V}7 v7 w# {' C) ^' z' ~2 V, {: b) j
//--- Since we set a timer, we need to destroy it in OnDeInit().
: u0 X( r, w2 ~8 fvoid OnDeinit(const int reason)
  Z4 K; e: ]) W4 b1 g: }2 H{
3 V4 A, @- S$ [/ l8 TEventKillTimer();8 s6 r( r. q! ]: k1 i4 n3 y
}
/ c& j7 I, {7 H: O! I$ c//--- The OnTick function only informs us that we have a new deal
* @( F4 g  _3 _1 v% Fvoid OnTick()3 ~- t5 S. i% e# q2 P
{
, {# N" Z) b2 g! G0 `* G3 }tem_tick = true;
$ n5 S( T: S" C& c( X9 z) o}; V. o) M: ^6 U5 Z5 Q7 Q8 i
//+------------------------------------------------------------------+
' L6 q* }, e; Q/ f# _, V8 X//| Expert Advisor main function                                     |
5 ^; c1 z$ @& `3 N- m//+------------------------------------------------------------------+( ?# z& T$ f# F3 ]
void OnTimer()
% c  }$ u* p9 ~7 R{
8 N5 o9 L, c* w8 a9 P+ h2 _) }MqlRates cotacao[];
: h$ U5 l& @  u  `4 H! ], Hreturn ;9 B6 z) R( _' {6 X
if (negocios_autorizados == false) // are we outside the trading window?
( u  T3 E' {! G. @1 rreturn ;
: V5 k0 r- A$ K1 G# A, _//--- We are in the trading window, try to open a new position!4 o' w+ h# m8 u4 y4 A$ P
int sorteio = MathRand();
! s, \* u& ]* k//--- Entry rule 1.1
$ f; M# i) l$ u1 Y' Oif(sorteio == 0 || sorteio == 32767)
  _3 u& i; f) X' Vreturn ;! y1 H9 M" Y2 U0 k
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
4 c7 d0 w- \  S; [{
# O8 q4 l& h0 @$ ?; z( _" unegocios.Buy(info.LotsMin(), _Symbol);
' v/ }7 W+ G) A8 I( {4 w}/ \3 l3 _4 P2 a9 ?8 S9 A
else // Draw rule 1.3 -- odd number - Sell6 F! D9 A3 T6 _' c, x* E3 n/ R  ~
{
, r# |8 |4 P. ]: Fnegocios.Sell(info.LotsMin(), _Symbol);! a8 `9 Q+ y  E% `! `+ r# t
}8 ^; d) n5 m2 i/ S3 H9 i- j
}, \0 i$ J$ Y; M
//--- Check if we have a new candlestick...
5 t+ n6 I. a0 i! H8 V& Zbool tem_vela_nova(const MqlRates &rate)
8 i! V- r+ \6 [{$ a/ J% ]  x. l
{
8 K% J$ D9 J  \, M1 T" {ret = true;( L7 N; y  K2 m
close_positions = false;
8 x# C+ J6 `# k2 `0 G" V, D}0 g+ r$ ^0 q# {& ?7 C
else
! ^- ?; ^' y5 w* v& g: }* F{7 V0 H. s6 }7 _
if(mdt.hour == 16)  |7 G: V: {& d- d) K2 c* r
close_positions = (mdt.min >= 30);7 e2 I0 U# x2 y/ `
}
  E3 f. N, O. D7 _, _( |. e' p}# s  h) u3 T- q1 e# P7 t( b7 x1 R
return ret;2 F0 D& W& b( p' j
}
, L& z: w& Z2 a9 j" U; F5 |+ }//---
& i7 X$ y8 x, B6 \% J" Jbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! F, e, G; K3 q0 m6 {{
+ Y" z! W1 L9 g9 R0 J; B! b1 eif(PositionsTotal()) // Is there a position?
0 y! q3 d* m! P6 B! {{
; z0 e7 g: [: W/ \. B. Kdouble offset[1] = { 0 };# Q9 x/ F% N- m, ]( O/ B# U# x
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
! N" s0 k/ @; q0 H0 g&& PositionSelect(_Symbol))  // Select the existing position!! m5 d/ W) r+ H5 q  N# n& l
{
; a( U+ ]0 n, h) P1 Q4 AENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. x* U- w: ~& p) }" h# @double SL = PositionGetDouble(POSITION_SL);6 @  l2 n9 {7 T" @
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));) S! m  @+ H( |; Y: g
if(tipo == POSITION_TYPE_BUY)
  u% @  m! C$ A1 @  l1 l1 _/ f{0 ^- g) H$ c3 E' G, x. W
if (cotacoes[1].high > cotacoes[0].high)
6 `& g) i+ e- @1 ?8 S6 X2 w# Y3 Y$ T{: q" c, J' E+ ^8 ]
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
" n! R1 Z* D, minfo.NormalizePrice(sl);$ N9 H$ t5 T8 C, w. U0 k, `% s" t
if (sl > SL)  e4 I9 t: z( v% H( ~; Z
{0 i3 d6 t2 ^% \; @# H
negocios.PositionModify(_Symbol, sl, TP);+ p4 S  c2 H1 _6 I
}' g% C4 }: }* X
}
1 p7 z) L$ V# l, y6 E  ?7 T}
, H6 P4 K: f% Pelse // tipo == POSITION_TYPE_SELL
% x+ @3 S, @) t; V0 d{
/ ~0 ]- r% W2 m- E8 S% ]4 Z7 u( Aif (cotacoes[1].low < cotacoes[0].low)
, l6 e! ~/ u7 ?{
) r2 L  `! _  U6 w0 vreturn true;7 m7 c3 E: k+ Z
}
5 f7 D) X4 x5 F  E9 j// there was no position
3 A5 l9 d! e' Oreturn false;
" P  Q1 P2 o# |! g. T% v$ c}
6 }  l; R7 Z# T$ p# V我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 p. u8 T8 \) S7 M  B4 S到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-25 16:23 , Processed in 0.793827 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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