私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA1 u- i3 N( \) }% k8 G) o
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) \4 ~+ p  S. D为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
3 E0 q, c- w# j/ c以下是制定这些规则的代码。5 X0 ]" [7 R, Y9 s: i8 n5 z' ]
//--- Indicator ATR(1) with EMA(8) used for the stop level...  k& V0 [8 n5 G4 W9 u" l
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 }1 V1 h# v+ }5 a$ z, p  `/ P
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
" W7 z" f& ?# P4 z5 B//--- Define a variable that indicates that we have a deal...) ^2 f4 |/ @$ p5 `% L( }
bool tem_tick = false;
3 v" J+ P1 [" m//--- An auxiliary variable for opening a position0 s( D. [! i& D
#include<Trade/Trade.mqh>
5 o$ d" R1 e% N7 z5 C6 q#include<Trade/SymbolInfo.mqh>4 ^5 _; Y2 p. L0 F
CTrade negocios;
- Q- q( o' Q, |CSymbolInfo info;3 ?2 c2 w$ C, b( g2 B% I( Y
//--- Define in OnInit() the use of the timer every second
7 H) V* h: s9 i" q% N" r/ I# W4 v4 Q9 e7 v//--- and start CTrade
3 I; f$ R) o3 F4 u& `$ Dint OnInit()6 Q" K" j0 e3 B; \& `6 B! y1 L1 j
{
3 m& |6 E' m. @1 P1 u$ ]/ ^//--- Set the fill type to keep a pending order3 K/ f6 ]; |2 d0 v. c0 K; x
//--- until it is fully filled0 F- [3 g5 [( y7 X/ H
negocios.SetTypeFilling(ORDER_FILLING_RETURN);" Q7 K- h  }7 D. C2 T1 w; Q
//--- Leave the fixed deviation at it is not used on B3 exchange
# w& A' S; J+ Z9 l# H% H: Lnegocios.SetDeviationInPoints(5);
8 z1 r3 H3 U( ?% R//--- Define the symbol in CSymbolInfo..." K6 T( J6 b! h: l! A+ r
info.Name(_Symbol);
/ T1 o! A' O; h, {//--- Set the timer...# s- B! K. O4 q8 I* e7 S
EventSetTimer(1);
" U( W5 m( k8 |" M6 U" d//--- Set the base of the random number to have equal tests...
5 z7 z" ?- N, H5 hMathSrand(0xDEAD);$ i. Y0 U6 \5 X/ n. O; _
return(INIT_SUCCEEDED);3 I" J' T1 u7 `, f" v: c5 N6 ~* N0 Q
}
- n. Q# W, u- D5 I, n0 Y3 P: ?7 d- R//--- Since we set a timer, we need to destroy it in OnDeInit().! C+ @7 f) O$ O& [7 a: V
void OnDeinit(const int reason)( q6 h2 R: h4 {  G& J$ r7 e7 s
{3 [) b+ _3 ]# i+ I4 l
EventKillTimer();9 D3 p  p" D; v, S# P7 z% C* R
}, \  b& }" c# r1 Q$ o% J
//--- The OnTick function only informs us that we have a new deal2 ]2 s# Z4 V2 G9 B, O% S7 J! |
void OnTick()! [$ I/ S$ s8 l$ u
{
+ Z5 m( M8 ?4 O! r0 h% P/ m6 _tem_tick = true;  r9 V, j0 j5 }; I. h5 g1 L
}
& \( {! w# ^" k! x3 S4 G) C: g//+------------------------------------------------------------------+1 F/ s# z( A/ R" c; ]! \5 H# g$ x
//| Expert Advisor main function                                     |& B  Z0 S7 W$ r( }8 @
//+------------------------------------------------------------------+8 @# o$ y0 J1 B1 e
void OnTimer()
: s- v% n$ B4 V* \* {; J* p' k{
2 g5 ^$ |: V4 s* RMqlRates cotacao[];
8 T  `, `6 D# qreturn ;7 S% K, k' S( |! c. y9 K
if (negocios_autorizados == false) // are we outside the trading window?5 ]6 m# W: f+ n% G
return ;
; Y% A0 h+ M2 M5 X//--- We are in the trading window, try to open a new position!% C3 W3 O4 M1 f5 C/ y
int sorteio = MathRand();, t- q3 ~+ s5 ^1 r  x9 c9 Z, c
//--- Entry rule 1.19 f! }$ l2 y9 _
if(sorteio == 0 || sorteio == 32767)
; N5 N& F; }6 Z. V, Creturn ;& g3 o* t' k+ @' C3 F
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
) B' z7 q( s. V0 X, L0 |' i' h{
- r; J$ M# S& k; vnegocios.Buy(info.LotsMin(), _Symbol);/ l9 `! A: j/ E* j2 m, X4 p
}% |; b( s: X8 |8 B4 \
else // Draw rule 1.3 -- odd number - Sell
! s1 N: P' i$ ^{
/ G( i* ~/ H- a8 _. Knegocios.Sell(info.LotsMin(), _Symbol);2 A1 a( U0 l  `
}4 R2 ?# O( T7 D, U' t' T
}0 [' ]: d, a* r
//--- Check if we have a new candlestick...
- e9 x7 k. U# o$ lbool tem_vela_nova(const MqlRates &rate)
5 S6 d' @' x& ?3 d{
/ T3 Y) b/ v6 n# `. b* A1 X& ]{
; s' x5 H5 t; w: m. xret = true;
; V3 P$ V& u: b4 v4 ?9 U- I& ?close_positions = false;( R" P) t0 I; d$ T  j
}
: V+ r, V3 f0 ?9 Relse% W; Q) Z( u) U( m+ |4 }+ I
{% h7 P7 s6 z0 x8 K( n" q
if(mdt.hour == 16)9 D4 O1 d; I$ v8 W8 x
close_positions = (mdt.min >= 30);
; W- l. n* A# `6 S- H: [}
  ~, F5 ~% N6 v. U1 \}. V. V7 ^3 d* l. {* B& K
return ret;- g1 ^: V0 I) K( n
}5 U7 Z$ T# m  h: i" B
//---9 m  {# _: {& K  C
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- z. t1 u. u6 p3 ?, G" G+ z8 x1 B{/ @& A4 \% B/ E. c2 H7 q
if(PositionsTotal()) // Is there a position?; r+ i$ x" V; c
{8 d3 V9 N8 [6 j4 K
double offset[1] = { 0 };
. m) d9 @8 w* I3 M0 jif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
9 e+ K. B& k* f2 \. V0 d&& PositionSelect(_Symbol))  // Select the existing position!+ @6 K0 e5 h2 ?0 K/ @
{
  b9 ^* c* b* _, M/ p' WENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);7 V% J, G" X) W, S) z1 k
double SL = PositionGetDouble(POSITION_SL);
6 N7 M" x$ x' V& j5 J# o+ _* Odouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));1 N) Q( X/ a7 D% t
if(tipo == POSITION_TYPE_BUY)( w9 E9 E( E7 g7 x9 y  D4 R4 r
{/ D) p6 B3 ?" K* Q
if (cotacoes[1].high > cotacoes[0].high)
% B% N. z+ c! T8 ~4 ]: Y{9 S/ e6 |+ v6 A' I
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];+ _4 Z0 M; r5 j. L( T
info.NormalizePrice(sl);
8 x+ c1 G9 j5 P0 U4 O( Z5 T2 pif (sl > SL)& ]6 _& D* t' v6 Q: F* v
{
1 H: E( N6 E! @) B6 Unegocios.PositionModify(_Symbol, sl, TP);8 m1 c3 h" b' L
}
, U. f9 C3 E' d4 Q4 m}1 ], L" o0 H) G, ]1 i
}
  a; M1 b+ D2 [else // tipo == POSITION_TYPE_SELL0 N0 d3 f+ ?3 y1 Z1 k: X
{3 B0 z9 \8 \: ^  g! h: Y, \
if (cotacoes[1].low < cotacoes[0].low)
* ^8 i& V! G/ x, Y  x0 v/ F{' q/ H2 k2 ^1 C! N: _
return true;" ?# ?  O" A; v$ u( D4 w* W" j! T
}+ G) U  i: v/ M( o; r/ q
// there was no position
) x* R0 g5 R5 j$ u6 \4 N5 Preturn false;
; z7 ?% @( S1 D0 P9 z$ E7 s}: v' }* L( ~" z' l. ?/ F6 V
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。$ j% Y+ T& f5 m% }$ _2 K5 E- h7 m; F
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-1 15:51 , Processed in 0.432537 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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