私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
( `$ W% Q9 _0 t- ~8 L在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
1 \4 e8 J! F; V: M/ ~1 W, w+ {' e7 c为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
% ?  @8 F; c4 D以下是制定这些规则的代码。9 S2 s& ]- L' i  p, D
//--- Indicator ATR(1) with EMA(8) used for the stop level..." k; }9 o3 n9 {( V6 @7 C3 x8 `
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);* m' U8 K! h( W" Z  c
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& ?4 a- Q" H. F. q3 E* d//--- Define a variable that indicates that we have a deal...
6 _8 X( @1 C( e5 v; u: Y5 r8 {bool tem_tick = false;
: l$ B8 N$ W8 O/ u: n1 O) w//--- An auxiliary variable for opening a position6 l; i- T8 S6 Z9 b' ]! |
#include<Trade/Trade.mqh>
  `* |" \& d8 X#include<Trade/SymbolInfo.mqh>6 r# l! L/ z1 ?" \6 u6 B  I
CTrade negocios;4 c: O7 b/ x' n
CSymbolInfo info;
# w% q. \8 ]  T- |% m0 v; g& ?7 s1 A//--- Define in OnInit() the use of the timer every second  H* O2 n& Y! B0 Z0 [6 `8 w
//--- and start CTrade
8 b( p) E! v) Yint OnInit()' k5 [/ W' [! \9 [3 D
{
( s2 k, k0 J: Y//--- Set the fill type to keep a pending order
# U8 }! X+ [% D3 D# G: Q//--- until it is fully filled* Q, U( K# h" m% m5 T9 D
negocios.SetTypeFilling(ORDER_FILLING_RETURN);; g9 [5 z* o! _; T) _- M
//--- Leave the fixed deviation at it is not used on B3 exchange
' `& c3 R: `' tnegocios.SetDeviationInPoints(5);" f- B8 ]; e' R7 ]/ W, l3 J' c1 i
//--- Define the symbol in CSymbolInfo...
/ a. q. i. ^! w7 `info.Name(_Symbol);! E! P' f" X. v! H' G; A: T, s
//--- Set the timer...
8 R; I/ b% N/ D* cEventSetTimer(1);3 l/ x" L3 H. `
//--- Set the base of the random number to have equal tests...
& X  \8 L  Q1 c+ ]$ {9 eMathSrand(0xDEAD);; V2 l/ U; U1 u- j: v- F, {# B
return(INIT_SUCCEEDED);. m) X4 a- w+ Y/ k) ]( v4 \6 Q5 F
}& A" W7 i# a5 O! B- f  F  G) H+ T
//--- Since we set a timer, we need to destroy it in OnDeInit().
# L- n3 u( ~. h4 h1 lvoid OnDeinit(const int reason)" A0 ?6 o1 f: n1 C3 }' n
{
1 h! w1 s7 {* g* x; N. E6 MEventKillTimer();
4 j8 r5 W  y4 Z6 G0 h1 W}% b6 t- r  h- |2 E
//--- The OnTick function only informs us that we have a new deal
) n3 X3 h+ U0 ?, ]# ?# R2 a! ^1 @# Bvoid OnTick()1 u- t* }( T2 ~
{5 z- {0 K/ T, m& ^! I; v2 M
tem_tick = true;
  N. Z/ g: n6 ?, ~/ m6 `. j: s, V}
  W6 q8 Z2 h2 a" x. `0 q//+------------------------------------------------------------------+$ e7 N. X2 ?' a! U2 y' o' o
//| Expert Advisor main function                                     |: `& t2 @; y' b
//+------------------------------------------------------------------+, G$ `$ r- \4 z% X) S
void OnTimer(); W9 M  [# ]: v% H8 v% M7 c1 h) i
{; Q0 o$ @: r+ U3 k1 O; K
MqlRates cotacao[];. s' D6 L/ O1 t7 k+ O1 F
return ;6 G% O* }. F. l/ |$ i& f% t
if (negocios_autorizados == false) // are we outside the trading window?
! K* L$ m4 c: ~$ }* }; freturn ;
% a+ Z/ P! _% N) d3 r0 \//--- We are in the trading window, try to open a new position!8 g: v- @1 r6 K2 N# L
int sorteio = MathRand();/ h8 C( K7 \5 x3 K
//--- Entry rule 1.1
" z' t6 P2 S# k. [! Kif(sorteio == 0 || sorteio == 32767). K! y. e5 f8 g) \' \( n1 w% V
return ;6 P3 H) G+ r& b7 T- B" j3 B+ s& r# Y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 C7 l, y# Q- n( U{
. x- T. @2 ?- F3 n% m/ F! q: l; tnegocios.Buy(info.LotsMin(), _Symbol);& Z4 \; e# L5 O. h
}
8 O  G# r5 B% r4 ~0 Q  n5 Uelse // Draw rule 1.3 -- odd number - Sell
) @# \" p2 A* b  m{' U  q9 _! k6 r/ ], l0 g- r/ S
negocios.Sell(info.LotsMin(), _Symbol);' r9 o* o) D% `9 ^1 p$ v( z4 V+ k
}  {' Y- H4 u3 y4 O
}
! q0 N0 b0 _: M" G7 f6 a//--- Check if we have a new candlestick...
+ B4 e- J& {% B( ~& lbool tem_vela_nova(const MqlRates &rate)
4 l/ E" Q' b; j& F) \{
) L0 ~8 t  x5 y" Q{1 y1 s, h; g8 Z" r4 \
ret = true;
: r. y5 k/ z3 D" q# v7 R1 K9 Gclose_positions = false;3 c1 u9 @% \/ x: \3 L9 s" I' V/ R  Y7 f
}9 ?% X) v, \5 a% `
else
9 \" T- |$ S6 E% i2 A{
; A$ ]6 o) a* t6 |. pif(mdt.hour == 16)5 v5 D; N! K# ]) N  g
close_positions = (mdt.min >= 30);
, j* b. E) [! ~}
% U: }' o% R0 q9 l}
2 X$ U: n4 A1 C! b5 i4 g5 Treturn ret;- c0 Y7 _. `. G8 _
}
! ]2 y( Z$ e$ b- G1 Y//---
  F- V8 z" K0 ~& ^8 q/ Wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
; e% n7 h7 b/ Y{
  {" W4 j7 c3 tif(PositionsTotal()) // Is there a position?
" q7 H1 {* Z0 b( k9 ?9 U- Z{% _* F# S$ \0 b3 w$ l1 N4 Y
double offset[1] = { 0 };
3 @2 r7 E( Y& L8 dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
( A! P* x  [" b( f&& PositionSelect(_Symbol))  // Select the existing position!( z. T( S1 e$ W; N
{/ O6 c. M# J- f, ~" g  ?
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
+ y. h. F( T) Q3 rdouble SL = PositionGetDouble(POSITION_SL);
) Y. s5 w$ U$ b/ o* ~double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! R! a% e7 z1 l( J; {
if(tipo == POSITION_TYPE_BUY)
$ v( H+ u6 Z- [6 C' L: `& d{# F# l& D% ]- g+ g& a
if (cotacoes[1].high > cotacoes[0].high)9 {1 A# P# {' i4 W" F
{
1 g- S- |* T1 X) M8 Ddouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];2 H/ X$ q9 _2 x
info.NormalizePrice(sl);
4 E- G: D: H7 \. M, Y% y6 Oif (sl > SL)
& x( ?# h5 s& E) `0 x{; Z. \$ X/ \$ H7 K6 `
negocios.PositionModify(_Symbol, sl, TP);4 [$ y3 ^6 {  j0 K" `
}+ B  {# C7 T9 g5 c" H
}) n6 \; ~# L/ ~, a. k
}# i  @; H2 l9 O3 q5 j
else // tipo == POSITION_TYPE_SELL
' k, ?3 V) Y- g& ^8 Z& ]{) u" q# H5 h* W. p+ q8 I/ _5 I  U
if (cotacoes[1].low < cotacoes[0].low)
4 [' L2 H. i! }{) b) ?) h+ I5 S9 R, r: M
return true;
8 C% X* n) L4 I- |$ T; W8 c}0 F4 ]8 {3 z" ]' M6 @
// there was no position& D) Z2 u( V* h" y1 H  `; w, ~% o/ H
return false;
2 h4 m) B4 n; l. n8 B# Y}
& Z9 |9 i) \& A% D, z4 I4 y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。' q9 o& p+ F8 W7 D
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-11 07:33 , Processed in 0.748525 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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