私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 u) n& T3 Y8 q在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" i3 D% z9 K1 Q- s为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。4 O2 l! f( O% {5 a
以下是制定这些规则的代码。  d! [$ X$ v! F5 q! S1 Z+ a5 ^9 l3 Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...3 P/ K: C; d3 n& l' j
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);0 P% J8 X& Z& ^3 {' J
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);/ S# [' O: l3 V. R" I9 v3 Y& }- p8 v2 Y
//--- Define a variable that indicates that we have a deal...8 K  u0 _* m( ~
bool tem_tick = false;
1 z! w8 g& }6 n( u! }4 o//--- An auxiliary variable for opening a position
0 @! u) Z7 i6 \7 L# M2 w; [#include<Trade/Trade.mqh>: U& L. n8 ?7 z2 {: p* o1 x
#include<Trade/SymbolInfo.mqh>
- x  x: ?9 i  |& ]1 SCTrade negocios;5 y" n/ y# n) i4 r, h. b9 w
CSymbolInfo info;
# k; K( F& u0 C) M: l3 c* U. ]//--- Define in OnInit() the use of the timer every second0 y1 ^6 D4 e. D9 d
//--- and start CTrade
% h) W4 n9 {$ M6 M4 Rint OnInit()
3 h7 j( d6 f( Q# e{
# v* ]4 g5 {3 H) g$ A2 L" E//--- Set the fill type to keep a pending order3 L) y5 _2 F' f+ G% A6 @
//--- until it is fully filled+ s* T# f; I5 A, {$ R3 E! D
negocios.SetTypeFilling(ORDER_FILLING_RETURN);4 h7 G/ L. Z- W
//--- Leave the fixed deviation at it is not used on B3 exchange
6 |, n- Q. W/ F1 Tnegocios.SetDeviationInPoints(5);2 Z) H5 A' h0 i/ d! k0 w
//--- Define the symbol in CSymbolInfo...
( k" l8 P: r; K/ P$ u" Rinfo.Name(_Symbol);8 }# K8 J4 G" e' R0 u8 V
//--- Set the timer...
/ Z5 o# F' H+ J  _& }2 LEventSetTimer(1);
( v& o) K' [& ^; h" j: c//--- Set the base of the random number to have equal tests...
5 O) j6 q0 f. u7 zMathSrand(0xDEAD);
8 u1 l0 R- L$ H/ M1 F3 [, J7 k% ]0 Oreturn(INIT_SUCCEEDED);
+ a9 Y0 m- x$ A" w6 r) V}! Y& T5 {" i% l! L
//--- Since we set a timer, we need to destroy it in OnDeInit().
% E& d4 j. W" ^) Hvoid OnDeinit(const int reason). v/ U1 r- q/ b( S& a% `" D
{
0 @" I( Q2 _% m+ Z7 pEventKillTimer();9 _! Z) z. A. z
}
; O4 I# R1 S9 u8 j$ a7 K% t//--- The OnTick function only informs us that we have a new deal
2 G( G$ t8 a& N8 c/ A7 M" e9 yvoid OnTick()
+ q6 s( X' r+ A0 _6 w{
; ^8 N* [1 z9 l; U0 wtem_tick = true;
' l8 y: D0 ]: a: G. y}8 P: j/ @0 s, h, Q
//+------------------------------------------------------------------+* }. j4 S6 ^8 u( B
//| Expert Advisor main function                                     |
: t- p8 W1 ]8 |- I/ X4 Q//+------------------------------------------------------------------+
+ u6 ?0 B+ {7 ^; \$ Qvoid OnTimer()
9 C2 V- O% K3 m{, D4 ]+ p; n, A$ D5 l4 \; p
MqlRates cotacao[];
3 h0 R9 ~2 n+ {" ^" sreturn ;( R. h7 c' }- m8 L* ?( J4 F  `0 e
if (negocios_autorizados == false) // are we outside the trading window?: f- E! k+ Y- j0 N3 \
return ;
( _' S" h/ X# g7 Z: D//--- We are in the trading window, try to open a new position!$ H) y( e( j* V# S, G6 _- |
int sorteio = MathRand();
$ R' k5 q  d& U! H+ U//--- Entry rule 1.1
! b/ V5 d: Q4 E  X- P$ [6 Nif(sorteio == 0 || sorteio == 32767)
' F- J5 ]0 v4 {& v; s* V8 A2 lreturn ;/ [( K. t$ d5 X6 y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 J, V& y+ V' a2 [6 b( A
{
3 m1 m2 N5 E& g- H7 mnegocios.Buy(info.LotsMin(), _Symbol);1 x7 H- r4 _$ t& t
}3 K. u) F, h1 h4 _% k2 `
else // Draw rule 1.3 -- odd number - Sell
6 w2 H' F! x/ n% S{
& A2 J/ K  T- J  N- cnegocios.Sell(info.LotsMin(), _Symbol);
( a, P# |' X- q' s6 H% j}% Z9 A, E' k; \- F4 E
}
2 j0 q- f. H, M, V  x4 ~! O//--- Check if we have a new candlestick.... N1 t  s( B5 V4 a9 ]$ A4 @
bool tem_vela_nova(const MqlRates &rate)! d* `* c7 l& `5 Y% d
{
% A- Y1 ^  [0 r4 ^* i{
% p# I! c0 @& {( @3 W2 Bret = true;
- b1 [; }3 E9 yclose_positions = false;
+ B! g- ?5 H$ _% K, a; U; K}
4 Q0 t  z0 n  Relse( G4 C9 v& H/ A7 B# H/ P
{
& L0 }8 ]" Y$ D# k2 n+ j2 ^if(mdt.hour == 16)
3 L3 F; |/ D2 Z# y0 hclose_positions = (mdt.min >= 30);: X' F, a2 ^9 s3 \; [
}
3 [2 {& E9 R. h1 k/ C" R}6 y4 f! X% c( n0 q! C5 y3 k
return ret;/ b2 s! j& T9 ]2 n0 |
}
' }5 E) d8 t1 d; j//---  ~4 M" h! Y$ [! ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])2 @4 ~, J0 ]3 |5 ?( Q
{- {8 Y6 Z3 ^, b
if(PositionsTotal()) // Is there a position?/ Q4 g4 P3 Z! Q( [
{
+ Z8 L! q( ^2 B4 ~  Tdouble offset[1] = { 0 };
; F, w! P$ A9 |% N) @$ ~* eif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?: S; p% @* L" c- x% |# f
&& PositionSelect(_Symbol))  // Select the existing position!) o$ s6 S0 ~. z
{
. x( L  f0 x4 ~3 @8 \ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
* |2 O. d, ?1 b' J: C+ H/ idouble SL = PositionGetDouble(POSITION_SL);0 D& s, k4 k! E6 ^6 V' d: e  {
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
8 M! L1 m1 k- S' W8 Uif(tipo == POSITION_TYPE_BUY)
4 U7 K, y: s8 C3 v0 v) F7 L{
# D6 T8 T$ C/ Y( Hif (cotacoes[1].high > cotacoes[0].high)
8 f+ z6 T1 d* ~% n{' `7 R' z% z8 r3 K' x% f
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
6 g! E7 T; b) cinfo.NormalizePrice(sl);
: M. @/ x' F0 o7 m) oif (sl > SL)' |8 K9 {' W& s. e
{7 Q% u# b* G/ F0 ]  e5 o7 ^
negocios.PositionModify(_Symbol, sl, TP);7 C9 y5 O9 Y6 Q1 v& g
}: U6 g  A( o8 D+ p8 ]
}
# A1 j, t$ _5 k  d0 r0 M& D. j}
/ {3 X& k9 u# `1 a6 D0 \6 `else // tipo == POSITION_TYPE_SELL/ H) N7 j% w, c- a" r+ {4 G8 R
{* \1 i( @2 V' x5 A4 M' F
if (cotacoes[1].low < cotacoes[0].low), }! Y, n( M$ \, r
{
* j0 }! H6 e; f: N, c; |) lreturn true;
8 y/ W8 K6 W, S! d! a}% h3 y' |+ A0 |% g/ _$ [' F  W
// there was no position1 v9 Q! B3 ~" G* D2 k) B
return false;9 Q" b+ a1 B6 H( ^" R
}4 f; o- w4 r8 `+ z% q
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: R; D, _9 ^/ p# m到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-21 07:05 , Processed in 0.432009 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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