私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
" V$ d- w( U& p' v- A$ s在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
3 Z$ ^! p; f; |. |4 r为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。' X  ?9 J8 \% a
以下是制定这些规则的代码。
2 h: G2 x* Z- O3 ~" M$ q//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 q0 A5 Q1 F& A. xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);5 B0 L! Y$ y$ @+ R0 I: C
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);# I8 k8 q! u& J. V/ }( g  S
//--- Define a variable that indicates that we have a deal...
& B! f4 {9 a" t9 {bool tem_tick = false;
& b; o8 e  H$ `3 z//--- An auxiliary variable for opening a position
1 Z9 `6 X5 ~, u#include<Trade/Trade.mqh>
8 A$ U& h  v# v4 Q1 z# D#include<Trade/SymbolInfo.mqh>
$ r3 i0 q7 ~8 f& W3 ACTrade negocios;5 d" ^9 `& M6 `1 Y5 _, U" R4 I2 @
CSymbolInfo info;
' J( q  ]" b8 P//--- Define in OnInit() the use of the timer every second
3 h' M5 |8 s; K/ [6 J. J5 r: t//--- and start CTrade
, l7 r3 }! l% \* Tint OnInit()- {( O3 z5 W, N$ s0 C1 N
{
2 E7 b+ I, _$ U9 F//--- Set the fill type to keep a pending order0 v. j' x1 e/ i1 {" q" A
//--- until it is fully filled, Q  V% m6 d! l3 j0 V8 a& T. J
negocios.SetTypeFilling(ORDER_FILLING_RETURN);" f/ ^: w. {. Q; K
//--- Leave the fixed deviation at it is not used on B3 exchange
! g$ o, K! Y8 B7 q% X, s8 nnegocios.SetDeviationInPoints(5);' L' b7 @( y) A( K  K
//--- Define the symbol in CSymbolInfo...
5 T' s3 L% l; Q. ^info.Name(_Symbol);, z5 d- d; N1 Y/ P' a! O: d" T% ^- P- d
//--- Set the timer.../ U: G. B) Y% E% y! Q' j6 |& O. X
EventSetTimer(1);0 O) Z, f8 ]* Z$ [& g" c+ X
//--- Set the base of the random number to have equal tests...) m9 V4 Q+ X( C" r
MathSrand(0xDEAD);5 w" l7 Z7 p! N
return(INIT_SUCCEEDED);2 G' P6 l& z* t/ A; |
}
/ n1 I# s  }: n" Z- S# M//--- Since we set a timer, we need to destroy it in OnDeInit().( T  m  q; \& P; i0 q
void OnDeinit(const int reason), R: b7 T/ O8 ^
{6 N% P- h- \+ Q, b
EventKillTimer();7 v! a( z4 d- _- ]1 Q
}
3 p: ]& h$ Y& M0 d% D/ h//--- The OnTick function only informs us that we have a new deal
+ X0 |3 s7 `' |$ Ivoid OnTick()
% N0 e. z7 Z9 H5 b. t  m8 y( ^{
2 T0 N6 |1 P$ xtem_tick = true;
+ E. i8 m/ R8 ^% J8 z" l}+ K3 ^( m& }2 t9 H- ~; ^8 H+ U
//+------------------------------------------------------------------+
& y- q" B2 _9 p0 t" [0 q* i//| Expert Advisor main function                                     |
: v, Q/ @: Q$ p& F% E//+------------------------------------------------------------------+
1 v0 ~2 M. k. o* p' D  mvoid OnTimer()4 r- z/ `6 E0 c- a! v
{
# @" s! q6 F  f) d; Q# H7 lMqlRates cotacao[];( b' `9 X' W; ~
return ;* O  V" I  b, Q2 ]# U
if (negocios_autorizados == false) // are we outside the trading window?
1 F; Y$ z1 _. ireturn ;$ K, K8 s' m$ H8 [: f6 _" @0 g7 X
//--- We are in the trading window, try to open a new position!
- o# Y! Z0 C9 u# ?8 Zint sorteio = MathRand();: p$ g4 o- _5 e! s, T
//--- Entry rule 1.17 b# ^0 l3 `5 x
if(sorteio == 0 || sorteio == 32767)
* _8 U7 m9 _3 I/ i' H3 h# L3 Breturn ;
; t# X0 I" K% _0 _. a& dif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  O$ J% n5 o" Y" i: `
{2 r# g0 g9 x0 e% S, G1 k
negocios.Buy(info.LotsMin(), _Symbol);
8 l/ y1 F) M  F/ m. A}
8 j' Q0 i7 R- t  w" j; Q% nelse // Draw rule 1.3 -- odd number - Sell, @; Y, [" O% s3 [% J
{
9 r$ C* w; A& b7 M) |+ k7 Dnegocios.Sell(info.LotsMin(), _Symbol);
  j3 h6 p' D6 h# m9 [+ @. [}0 Y; A) S( G# P! B8 ]( [
}
& ], `! I3 |) ^7 _: Z, k3 ?//--- Check if we have a new candlestick...- I( k) m& g) G, N5 ~
bool tem_vela_nova(const MqlRates &rate)8 w' ^8 O1 J# q- f. m
{2 _  l: o, W# ]! ^% t
{! [5 |4 u$ p. D1 Q$ H6 m" T% v
ret = true;
: M$ F$ [2 L( \# dclose_positions = false;" l7 @, [- s( j' [7 g8 E
}2 P1 O- c$ M7 e
else
' a( n/ E/ E1 v9 ]/ V+ ]{" m  X( e4 ?- G. u. m
if(mdt.hour == 16)
/ T  \3 |) @( E; x1 Z9 @: D# kclose_positions = (mdt.min >= 30);
+ H5 H  u' k4 j) _4 @" l! Z: s}6 {2 ]- X7 I" C) g& ]4 m
}& v+ U  i, K9 T" \3 G
return ret;: b$ p$ |# q" J
}
* r, t$ Q2 m  ?6 D7 ?, S( d//---1 Z# r& M" c$ w! g
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
* K) F3 v- O0 S+ q+ T: g( q9 w  @{
5 W6 \. O9 t3 ]: G: ]- S- {if(PositionsTotal()) // Is there a position?
5 L1 f: Q2 I5 x. w{
  ]; m' Q; T3 L; K1 H2 idouble offset[1] = { 0 };
# L- P8 u8 p4 J7 K. \' h3 Z  H1 Dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
3 Q* _) t  D' U&& PositionSelect(_Symbol))  // Select the existing position!9 ~1 `+ k- V  C
{2 b- z0 s6 l& [4 p4 q
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
1 J4 ~$ L& y! }: f* ~double SL = PositionGetDouble(POSITION_SL);
; J/ [/ E. L" O+ B3 `double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));6 c9 U6 t+ }2 @
if(tipo == POSITION_TYPE_BUY)
1 s$ Y4 T4 ^( s! [6 ^' O1 }{; {( g* W$ b1 m$ J4 I& ]4 @7 N4 p
if (cotacoes[1].high > cotacoes[0].high)5 f( Z  E' v( L4 E% _7 I: Q* @
{! V6 j5 V  t( i5 w3 z9 R4 M
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];! ^8 n6 n# x2 X: U2 e
info.NormalizePrice(sl);
: h8 R+ N6 ~9 h5 z' s) Oif (sl > SL)  v" ^' u7 A2 H5 p
{
. Y9 t' m" e0 Q$ L5 C5 n2 Pnegocios.PositionModify(_Symbol, sl, TP);
) a9 o# E' h- e* Z' M8 P}& n- y' {' E! p3 ^
}
: y( i/ o8 ?3 r6 `" |+ J, i}6 s4 T9 j7 h+ C0 ~7 g1 h
else // tipo == POSITION_TYPE_SELL- A7 D% ~1 ]1 c. {$ l4 Q
{
: W& w& p) ^! I; Y8 l% sif (cotacoes[1].low < cotacoes[0].low)* g; Q3 S$ }, ?( v
{
3 o4 S" p4 D$ I/ l0 \; w4 Greturn true;2 O$ ~- F) Q1 ^1 [- I  M( g
}" ~8 L! V; J0 R# A) V# b) Q
// there was no position
2 e0 k: n1 [+ l7 e& k* J5 wreturn false;" h2 v1 K) y9 s
}
+ z6 N8 A% `* l1 Y我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
7 _2 \% E$ Q) G6 G到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-30 11:08 , Processed in 0.466364 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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