私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA8 m$ w* N, s( s
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
) d4 N1 D# p" z- B为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 d; I9 a) v2 d8 k6 K: {1 t* H4 n9 ~
以下是制定这些规则的代码。
/ Y8 w8 \( B3 [/ N4 t//--- Indicator ATR(1) with EMA(8) used for the stop level..., t8 }0 m- {2 f: ?) M
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. y( r4 H" R8 W' kint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 p* \) g# ]$ `9 b* E/ V0 E//--- Define a variable that indicates that we have a deal...
6 X* _! W' O; I- W# @. p$ U4 Ybool tem_tick = false;
6 t2 M+ z* m# ?! D//--- An auxiliary variable for opening a position& p& ^  `# e3 }3 ^2 o) g# _# W
#include<Trade/Trade.mqh>
' p3 j# s$ |/ k& O$ G3 y#include<Trade/SymbolInfo.mqh>) F" l, R/ n6 w) L
CTrade negocios;/ w3 [( t2 Q/ _$ f" P
CSymbolInfo info;$ u2 u+ j& i. J) N7 r
//--- Define in OnInit() the use of the timer every second
- g% N8 r; U* M; H+ j//--- and start CTrade* u% @6 m; G  z8 W
int OnInit()
: S, _, Y& R& M3 Z{5 K3 ?! C: V; o
//--- Set the fill type to keep a pending order. }; i7 O: e7 d3 r: ~* E0 ^
//--- until it is fully filled4 V( [0 [; t$ ~, M
negocios.SetTypeFilling(ORDER_FILLING_RETURN);, Q0 E& i7 c  b4 z+ U! m9 Z& o* t# v. D
//--- Leave the fixed deviation at it is not used on B3 exchange% D2 s; f+ M0 P0 ]* [( I
negocios.SetDeviationInPoints(5);7 E' s4 |/ P8 [7 w  J/ t7 }
//--- Define the symbol in CSymbolInfo...
" H2 k6 d) o+ U8 p$ [" D" Qinfo.Name(_Symbol);& k. I. ~- \8 ?+ ?
//--- Set the timer...
: y. Q, Q' B& U) O9 P4 yEventSetTimer(1);
% }# ?9 l4 p! c! D5 y6 R' c//--- Set the base of the random number to have equal tests...
: H" K; c4 V; L) u6 [0 JMathSrand(0xDEAD);+ }/ I: V& ?7 y/ m2 g# u1 @2 [( a
return(INIT_SUCCEEDED);1 W) r% T. {+ e2 t5 d' I
}
; D2 i8 b# ]% R) ]: s9 `, @//--- Since we set a timer, we need to destroy it in OnDeInit().! U4 l0 X1 D/ Q, A, r' d' Q# x
void OnDeinit(const int reason)
; I% Y( K; `' I{
! J: \) x: @, O3 ~. ?) D, `EventKillTimer();
: M+ K% {! k7 a; g}
; j, v) ^" Q4 q9 |+ x//--- The OnTick function only informs us that we have a new deal3 c0 k% o  X4 K6 e' i& q
void OnTick()  |: V9 r" n  S" p. y+ z5 L. L* b
{" ~7 b! b+ V: Q  u8 ]- u: `( L5 Z
tem_tick = true;
0 D0 _9 i5 [8 N- Y}
  Q( T9 q5 o+ Z. Q' l//+------------------------------------------------------------------+
1 f$ Q- A! E, _3 c) t$ d//| Expert Advisor main function                                     |, @" C; a3 b0 t
//+------------------------------------------------------------------+, `- \  k- W5 O9 ~9 X  x3 E1 w: P
void OnTimer()
$ q% u+ e8 g% M) P/ V% x' Y! I{
6 s* w6 P# C0 F9 T* Y- i  V- JMqlRates cotacao[];
4 F0 @: a9 K' U% D4 s' ?( Ireturn ;( F: V( ]9 J) V+ H+ f# W
if (negocios_autorizados == false) // are we outside the trading window?
4 P6 q+ Z+ S- w  w/ Xreturn ;" L- w: h; @$ p! H6 N; H) S' E# h
//--- We are in the trading window, try to open a new position!$ o/ e& p/ C2 U$ O; W# r# d
int sorteio = MathRand();  q/ w1 l4 ^) X) \. t8 ?/ F4 |
//--- Entry rule 1.1, ^4 u) V" |2 m
if(sorteio == 0 || sorteio == 32767)( H, a$ _, e& K; V( x3 Z( c% Y% M
return ;. ^8 d# k: J- c
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy! J7 v$ M+ S$ C9 U
{
1 ]  f7 C0 N1 G" M8 u$ @negocios.Buy(info.LotsMin(), _Symbol);
& x: I, F) u( c/ D+ f}
% ~" a2 d! ^. T6 celse // Draw rule 1.3 -- odd number - Sell
9 |% J& J0 m; T; f; P9 E4 q% |{% P$ J/ V$ i: p# o7 L0 b
negocios.Sell(info.LotsMin(), _Symbol);
5 `( \7 N% h2 A3 B0 y}& T5 C6 N3 ]0 y6 v# W: \
}' e5 k/ k' ^/ y) }. }' t
//--- Check if we have a new candlestick...& k6 M" O* N9 J6 M6 O& p. K* r
bool tem_vela_nova(const MqlRates &rate)
% D% \# B& I' F3 k) t0 S; ?{  Y% E' h8 j' ^# l: J
{
/ T+ a. `2 }9 y3 Y1 yret = true;( x$ p$ d6 R5 j) W( ^9 h/ a
close_positions = false;. I+ D. ]+ ?+ }: S" W( X
}
5 S* N! Y# _7 ]  p% `7 ?else
: K6 t9 l6 z% J{  l0 H; m; i& r: Z1 \
if(mdt.hour == 16)
6 N; f% z5 j; R# R$ A2 zclose_positions = (mdt.min >= 30);* |, K- ]5 P2 z6 u, `
}
* {1 B) y; C) g}
; a5 k: S, K  l5 J# I/ @' D& L; breturn ret;
1 _5 X# L0 \8 ^1 V7 h- i3 b+ z" d) ^  `}
; x7 ~2 B9 o5 }4 e, d5 x//---% H3 L9 Q  z( [" T* E9 t
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])7 T9 w( ^9 m) H: G7 C- T% e3 K1 s
{
$ |6 o4 r* y& ~if(PositionsTotal()) // Is there a position?: X+ G# |; h0 [5 [# g% v" ^5 h( ^
{! L& A1 Z  `- Q# }/ J
double offset[1] = { 0 };
+ k( u) ^$ r( z8 _  ]; `6 q+ \if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?) p9 r/ ~) O' B/ p! _* n
&& PositionSelect(_Symbol))  // Select the existing position!
$ {. h4 s9 G3 O9 Y! E{9 m: e& |/ k7 a2 ~7 l
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
% p- Z, P0 F: M$ O, T3 o4 M0 \8 edouble SL = PositionGetDouble(POSITION_SL);
' w( ]( H' B. [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));4 c! K7 X. y$ s3 @; k6 ]% ~
if(tipo == POSITION_TYPE_BUY)
& }9 I) L! ]% ~8 ]5 U7 F{$ E1 z' ]! M5 W0 s7 \8 x( m
if (cotacoes[1].high > cotacoes[0].high)9 y! m( q$ c$ r
{3 Z, i! l& g* c9 k: r
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
3 q' V- L7 @+ Z! Q0 k2 f9 S2 {info.NormalizePrice(sl);
5 r: ~4 f5 M; Y- j. ?8 Tif (sl > SL)4 ?" ]* Y5 B; m& r% _! j4 w
{% |, o6 v0 ^9 B3 S
negocios.PositionModify(_Symbol, sl, TP);7 ]/ N* b) i9 y8 c' e
}2 f  H' O& v( J3 g5 Q8 @! a5 W. y
}
- P1 b! @, M/ L9 X9 l) J* n1 I2 R/ L}$ }0 F. ]6 C, B  w' J6 R; C0 P' \
else // tipo == POSITION_TYPE_SELL0 U1 F0 _. x% d' F  O! B0 ?4 J
{
/ r. S& L2 S1 Z. g3 T, Uif (cotacoes[1].low < cotacoes[0].low)
, J  _7 p! p5 L{) i. `" b0 P" n6 B
return true;
( q. x; ^% y3 m}# e( V& I+ P/ I- P8 `
// there was no position2 L" g% G* a& C3 g+ j$ K* i$ G  t
return false;7 S  z& r9 e& @0 e) S" g, Z: s
}; Y- ]& ~9 z4 V: Z2 ?4 t
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 j7 v$ v0 j! U0 L; ~7 A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 09:24 , Processed in 0.416462 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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