私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA0 l, y/ }  U1 W- G/ ]; a8 G
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
5 l! }* b5 t" q! [, g- \为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
4 ?) z3 R& V  h+ E以下是制定这些规则的代码。
2 {. P) @2 o$ F1 f) w- D3 C//--- Indicator ATR(1) with EMA(8) used for the stop level...
! {+ b$ D2 e5 r+ w3 n& ^$ |1 \+ Bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);( [. Q/ N' s0 J# R5 |$ p$ W
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
! q: I6 q6 q( f& i5 s+ i; i//--- Define a variable that indicates that we have a deal...& S: `. h5 a9 w6 K. c
bool tem_tick = false;
" _/ E% C1 l) ^4 f+ w//--- An auxiliary variable for opening a position
: n1 E& g6 z5 h1 ]5 y* Q#include<Trade/Trade.mqh>
' Z: y- W" a4 i  M- R+ J. o" k. X#include<Trade/SymbolInfo.mqh>. N( C4 L9 U9 ~3 H! g: O4 S
CTrade negocios;
4 @% t) v* m0 a# h( O8 I8 f9 qCSymbolInfo info;
5 M" `6 x+ ?9 G" u$ Y% T0 p: U//--- Define in OnInit() the use of the timer every second+ O( s" |, c( K! o6 h
//--- and start CTrade* D1 ~# X9 Z* d' U+ ^
int OnInit()
9 o# G1 P! t& Q$ Q# a1 V7 V4 Y* J{6 @9 \1 G: S$ y* h- K0 [. O
//--- Set the fill type to keep a pending order0 _; o) d  x4 c8 Q. Z& J2 h
//--- until it is fully filled
5 ~+ K: m$ f( y/ L- ?* Lnegocios.SetTypeFilling(ORDER_FILLING_RETURN);, c9 Z: S, s  O8 Q' }! S
//--- Leave the fixed deviation at it is not used on B3 exchange
5 h; s. c( Z4 C  j8 \, P; Pnegocios.SetDeviationInPoints(5);9 c$ \: g% M: m0 z$ j
//--- Define the symbol in CSymbolInfo...6 R5 ]9 o0 {2 U0 Z, K
info.Name(_Symbol);3 f4 {+ |: S7 i; L  K* {' ]
//--- Set the timer.../ i1 h0 Y+ w. i; M3 m
EventSetTimer(1);
  H: @. h9 _, s0 }) ?, W//--- Set the base of the random number to have equal tests...
0 O" I8 F& o3 L; W8 fMathSrand(0xDEAD);" m8 H1 u+ x0 h! Z
return(INIT_SUCCEEDED);( s* C1 X2 g+ w
}
/ @) [9 {7 s5 S//--- Since we set a timer, we need to destroy it in OnDeInit().
  Y$ `9 v( Q" _, ~void OnDeinit(const int reason)2 M- }+ }+ H9 c. }) p) Y
{
) P" W1 t# p, M9 E+ z5 E8 d. mEventKillTimer();0 ^$ I* b+ I7 h# a- _' y, B( z, ]
}
; d, B. H8 @1 G# f5 w: }//--- The OnTick function only informs us that we have a new deal- L( P; g1 Z1 a. K' S
void OnTick()) _! `( j6 G2 w" v
{7 S3 R( i& c9 b/ a
tem_tick = true;: X  v0 e9 D7 D/ `# Q" V: P0 I1 \
}  y7 Q; @( q% L$ {, Z4 D- s
//+------------------------------------------------------------------+
7 X4 e/ J5 ?# f//| Expert Advisor main function                                     |
& J% a4 i4 _6 F2 m) z1 E$ p7 V//+------------------------------------------------------------------+0 Q8 _& n# X1 l5 i( n  v
void OnTimer()
; X( m; h6 f) @, Z8 `6 a{
' N8 _  Z0 s: N: mMqlRates cotacao[];9 }0 \3 {$ M5 \
return ;
/ f0 ^: T% Z. Y6 f2 v4 uif (negocios_autorizados == false) // are we outside the trading window?
  j- G2 O( w, yreturn ;* e; B' k8 L8 t8 q. X! U
//--- We are in the trading window, try to open a new position!
, X9 f$ o) F. O& _( B" Bint sorteio = MathRand();
3 [4 C, [( K. H( ?//--- Entry rule 1.1) x* a( U5 ~" K' A. \6 B
if(sorteio == 0 || sorteio == 32767)4 J$ k6 o: d* x" A
return ;7 |) {3 @; _$ D
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy4 k3 T- v$ Q7 v! ]* j
{5 t" q  u% }7 X3 D% H
negocios.Buy(info.LotsMin(), _Symbol);
/ m2 _2 s$ \" u- ?. K- Q; a# q2 e}1 R' P4 P% F3 Y% b2 A4 l4 Y* ~
else // Draw rule 1.3 -- odd number - Sell
& N. Z2 y' j* U1 Y1 B: F, B1 W  D{1 X) r- o" O- U9 r0 @. a
negocios.Sell(info.LotsMin(), _Symbol);
3 g$ _# H% F4 u3 N4 |}$ ^% Y. n! U, O2 S
}8 X8 i8 M, e- w8 L# N* b- i8 r! F; ~
//--- Check if we have a new candlestick...
7 o2 I! u# I2 g; Z# S6 F- ibool tem_vela_nova(const MqlRates &rate)+ @% K$ u( \! O, W3 m; l
{
! Q1 m8 V9 n) w* n# g: X" d{
2 Y  i2 r8 o" W& k  L% `; Nret = true;3 q  ^( v. F7 p
close_positions = false;
1 `( H8 O  [$ S9 w* x9 B}
* C" Z- D) k* j9 f0 @1 Kelse
4 L" g& B* Y$ e0 c, S* o& E3 `{: [, Q& z% k1 x4 q: o
if(mdt.hour == 16)
$ r3 D# n  C6 ]' k. Hclose_positions = (mdt.min >= 30);5 j, ~/ ?9 s9 |2 X: \
}
3 }7 g8 O4 U& R0 T! }: |# P; ]0 f6 n}
3 L; C* G  Q1 \6 Treturn ret;5 j4 L, t) W: a4 ~7 G$ x* h
}
6 T6 z2 ?  ^) ~4 H$ I" @$ C//---
9 Q+ x, A; T" m+ \bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]), W9 w* E# W% Y! U
{, Z9 {" u4 ]' v# Q6 [6 q- [7 V8 x
if(PositionsTotal()) // Is there a position?- L# J& G* [" n; t" N* T
{" a$ Z7 n6 ^9 o# E+ M
double offset[1] = { 0 };
* d4 p& D1 G& e/ ~' N; J5 T9 cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 [. ]  y7 W! w9 f! }3 O* B, j0 `* ~&& PositionSelect(_Symbol))  // Select the existing position!
% Y7 T) l6 y; y; Q! b0 Z{
% Q" t  i; ]0 WENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
$ _8 l1 b, t6 d5 O% n, Adouble SL = PositionGetDouble(POSITION_SL);
6 J( Y# [; N1 H- c2 xdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 q& e6 {- @; v1 ~- Uif(tipo == POSITION_TYPE_BUY)2 i( v  H/ ~- H
{
* ^9 @/ _, w9 U- Jif (cotacoes[1].high > cotacoes[0].high)
2 y$ T* j" ^: v. J: ?  z) }  \{
9 D" q/ p8 ~. H9 f( l: |double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
( Z( c+ P  Q9 {  ainfo.NormalizePrice(sl);% T, H5 K8 ?1 Y; Z  `0 C
if (sl > SL). E; ]4 b& d* P+ x
{
: _; I5 u7 m3 \) e+ F% Lnegocios.PositionModify(_Symbol, sl, TP);6 w6 \* a' P' V- V" P1 u% B" [- e
}5 z- q" l( I4 E$ V+ J
}
1 {! [8 A! i* C6 v) `}; F$ ]: Z; E2 y
else // tipo == POSITION_TYPE_SELL
' P: r$ m. ^) C( L( c7 Q{+ Y) V3 i7 R, |
if (cotacoes[1].low < cotacoes[0].low)
! _) B' L  ~" V8 P1 p% v" c, t) H{
0 a4 j$ F# U! }1 e  y0 q5 P0 freturn true;
: L# ^* O4 B6 |6 J, b}
9 m8 w1 s! _6 j+ g. F+ B5 c. m) r// there was no position) s  b; W9 ?1 B/ Z! K
return false;
6 z. I8 T& a) N}
6 v7 T$ u% V+ K; L0 [6 o3 e0 Q/ i( J我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 y3 V+ T% |# K" ^9 g5 H6 o. I到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-13 17:45 , Processed in 0.400542 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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