私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
; W$ G6 |. c& _$ x在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, r- ^! K- ?! V$ P$ n7 q6 T% G( j, ~- A
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 b1 U  u) O* r9 ^) T3 w0 s以下是制定这些规则的代码。$ b8 E) C8 v5 h% ~$ e
//--- Indicator ATR(1) with EMA(8) used for the stop level...& d4 t# P! s( ^$ q
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
# V: o; Q0 o9 l9 n+ cint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 ]$ I3 d) R- ~; r//--- Define a variable that indicates that we have a deal...1 s3 v  N. w" n  j9 }1 R2 |
bool tem_tick = false;2 p9 C5 m  Q( N
//--- An auxiliary variable for opening a position
; O" K- o1 a/ b8 o#include<Trade/Trade.mqh>8 i7 C( B5 r$ e% u  G$ I
#include<Trade/SymbolInfo.mqh>% q0 ~. k0 r1 y1 x6 r& W0 l
CTrade negocios;
5 `& i1 N3 l8 L( ~CSymbolInfo info;
/ _- Z  Q5 v) k! j4 m//--- Define in OnInit() the use of the timer every second& x: m/ T, Q, `7 A( L% i/ d
//--- and start CTrade& |9 w; V- R. _3 H! w* @) v! e
int OnInit()
" A. Z1 ]' B6 S1 `{: S( U! D& A6 j& b+ X6 T" }! }
//--- Set the fill type to keep a pending order( g$ J* n4 w; f5 q: V* U+ ~6 Y/ Y4 m
//--- until it is fully filled
: [/ N1 k1 T8 tnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
3 D6 i* F# R3 x, y//--- Leave the fixed deviation at it is not used on B3 exchange
5 t2 v5 J, h" Y4 Nnegocios.SetDeviationInPoints(5);
% t! n' M4 J% t( x//--- Define the symbol in CSymbolInfo...2 n4 R' {& M4 m7 I: Q3 d8 z/ @
info.Name(_Symbol);- _9 o: f; L- \, |) {4 \
//--- Set the timer...
2 P3 V4 P0 D9 w* W, {EventSetTimer(1);$ Y& w, E0 E  j: Y5 T' _
//--- Set the base of the random number to have equal tests...0 J6 [! I2 M! X
MathSrand(0xDEAD);
, p$ ^/ D8 t& F9 p9 nreturn(INIT_SUCCEEDED);
' T, I$ b6 T8 u: i# Z( S- L}
5 T. o  I+ B) V. u2 i//--- Since we set a timer, we need to destroy it in OnDeInit().# t6 \0 _% U0 ~: E7 t6 m' v
void OnDeinit(const int reason)# w1 \$ Q4 c; O; [
{
' a* k+ O# D8 r& L6 [EventKillTimer();; ~9 a4 k3 Z  E: A6 g3 L
}* k; Q, P) }1 }+ x, E; o9 {9 D( ?
//--- The OnTick function only informs us that we have a new deal' G' Y" X* y9 C! L6 j: K
void OnTick()7 w: S- W- g/ [' @( _1 ?+ \4 S
{6 j; t' z$ j% D2 `! Q
tem_tick = true;1 h. F; a4 K3 m5 O: c7 r* R. n
}# \- D, e$ ~' s
//+------------------------------------------------------------------+
5 H. M9 q1 [( I$ S. {//| Expert Advisor main function                                     |6 n0 I9 x  M$ ]; K) e! W
//+------------------------------------------------------------------+: U) f, J0 t0 R4 q
void OnTimer()
. z9 w" G, C% n) v! \{
* g0 H1 n7 J/ @7 P" c; N/ ]MqlRates cotacao[];) V% F7 C- E5 |+ x8 K* X& v
return ;2 x4 \7 P  \, u
if (negocios_autorizados == false) // are we outside the trading window?
5 \! |7 m% T7 _. z) w  Kreturn ;
. t1 r6 ^' p/ ~//--- We are in the trading window, try to open a new position!
! K& q7 ?5 K% u" |int sorteio = MathRand();4 Y# w5 ^: M% p0 A* ]
//--- Entry rule 1.1
3 i9 a: v& ]0 A' R( p5 oif(sorteio == 0 || sorteio == 32767)
3 N) I  R% A* |3 j" ^0 }return ;
% i1 A' J4 v7 bif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 ^3 b8 c# {4 d+ T, a5 c
{
+ h8 `5 [4 ~$ Gnegocios.Buy(info.LotsMin(), _Symbol);7 r, K4 y9 |) o. h# ^2 W: H5 a
}2 x) L% p$ R7 v: x
else // Draw rule 1.3 -- odd number - Sell
: [+ s+ B1 S$ Z: g+ O{
' P' Q4 M3 e7 _' C1 s: m  q3 |negocios.Sell(info.LotsMin(), _Symbol);
- k! n$ _, d; U}2 b9 r1 E: Y( P
}
! U2 c5 v7 f* _% f& u5 b//--- Check if we have a new candlestick.... I% V0 _8 y" J3 S  r& H' p7 P" F
bool tem_vela_nova(const MqlRates &rate)
0 h! r5 R- I8 Z{: ^4 f6 O, H5 y  T2 J
{& f' `9 Y$ P# [+ w% @4 F
ret = true;" c8 K+ r, C7 F6 O$ w: s* r" z
close_positions = false;
, T- u) }$ u+ t1 m  ~}
3 [) Z* \  j" u/ Zelse
5 P' `* q  J% w7 P{  Y5 W" S4 ~1 s
if(mdt.hour == 16)
6 h! y  \* r! O. H4 W7 I4 aclose_positions = (mdt.min >= 30);
9 M0 n7 e* l7 X1 |1 S}, }3 D, J: D( r
}( T# w! Q; {  ~% b
return ret;
/ G( ^  F2 i8 K5 w  V! V}
  ^/ G. C6 X1 N, g0 b4 Q- k) Q//---
. a0 a, i* H2 C+ i0 wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! T5 W1 }. ^, _/ @
{6 B' O" H4 V$ {6 H* x( \
if(PositionsTotal()) // Is there a position?
+ R! v7 ]% _3 f8 f{
5 l8 j* i9 U) F( bdouble offset[1] = { 0 };
: n* v7 ]9 |% a! `% t/ Pif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?) h2 ]% H, V) S1 T+ \
&& PositionSelect(_Symbol))  // Select the existing position!' R6 \- @1 h. ~4 O2 I
{
+ F; ^/ b( i6 g# nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 w- g0 z+ e  B4 h+ Adouble SL = PositionGetDouble(POSITION_SL);* [3 B( u( W2 m2 j; h3 c. t& D( s
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
- X$ V7 d3 d6 u, g6 T" a( uif(tipo == POSITION_TYPE_BUY)0 F4 Y. W: a1 L8 l2 p
{
0 v9 T- j$ g: o0 s# W# ~if (cotacoes[1].high > cotacoes[0].high)
5 s! }/ ]6 E0 x4 s{
( u# t# O' J. D* \; r4 `double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 k) J# R* R& o" L+ Oinfo.NormalizePrice(sl);
  r' N/ ~- B: `& pif (sl > SL)
. F$ B  L! B, Q2 k{
/ v9 F: ^, U5 N' v, j3 Fnegocios.PositionModify(_Symbol, sl, TP);, C- w- J4 C% f! X
}) q5 H4 t7 m4 k  B3 O  H
}1 ?! p& P; _5 O- v3 ~
}
2 ^% c2 _9 A3 I6 F  N( G2 }else // tipo == POSITION_TYPE_SELL- O1 ~5 X" x5 _% K, j; p) D+ d; ?
{$ c' H4 R  p( e
if (cotacoes[1].low < cotacoes[0].low)
+ I" `( E7 L9 ]% x  _$ q3 h{9 w' n2 K( {' |
return true;
5 k& H3 H  w7 {7 B  f}
6 o+ k& ?7 W9 |2 O+ P' b1 k// there was no position5 E& ~; I1 i0 g1 {( o, `
return false;, T$ D  q  |1 f; y( y& ~/ p; o9 |$ G5 Z
}( d7 }1 {5 A# d& V8 J2 S: R( a* a
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
& `6 s  t  r5 Y  q' |; C7 W) B' Z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-28 00:31 , Processed in 3.454353 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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