私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
5 l0 s! p- Q8 }4 f5 m4 o, k在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
% K/ _0 }, X; R( [) S- d) r& P1 @为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
  _& s/ y2 v- Z7 Y8 S2 X以下是制定这些规则的代码。
) j7 ?! s4 C" s' Q) R  G5 w//--- Indicator ATR(1) with EMA(8) used for the stop level...
. {- m2 J: p- A, |3 iint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);0 D+ C+ E/ C( [$ I8 k4 ~, j. W
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);* t6 @8 g) L8 i; O; {. z( v
//--- Define a variable that indicates that we have a deal...& z4 }* O  x1 U% e7 ^! Y
bool tem_tick = false;
  P9 ^+ ]1 x* k. e$ ?//--- An auxiliary variable for opening a position
6 n% p8 p; }: n6 X3 U5 H#include<Trade/Trade.mqh>9 n, _* u; I: N% B) g
#include<Trade/SymbolInfo.mqh>
9 g7 I6 s9 s7 }  cCTrade negocios;: J% B1 L3 i3 h# c9 y4 I/ f. d
CSymbolInfo info;7 V, M: H# y( \) {
//--- Define in OnInit() the use of the timer every second% H' t. m& Z  d; f
//--- and start CTrade5 M, }$ _' L# U' x) X" w1 _3 F
int OnInit()* Z2 O8 i" a( [
{
4 G, s  `1 H# L% [$ R! t# q8 c8 {//--- Set the fill type to keep a pending order
& T: P8 R! t1 g' w# g# i//--- until it is fully filled
* h3 T  F7 z+ Z* vnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
2 t# c( p  A4 N//--- Leave the fixed deviation at it is not used on B3 exchange" g* x/ N4 v5 M7 p3 f0 ~  s$ F
negocios.SetDeviationInPoints(5);
9 _* u4 d9 d0 m  a7 l//--- Define the symbol in CSymbolInfo...' f. a7 d0 K% Q1 p8 f! r/ `* [4 c
info.Name(_Symbol);& k8 Q- ^8 B; r. ]8 ]
//--- Set the timer...
/ C2 v% ]: s9 I( yEventSetTimer(1);! H! s2 n, k+ ?2 o3 T
//--- Set the base of the random number to have equal tests...
( |6 C4 [8 j4 g, k- aMathSrand(0xDEAD);9 c% i+ Z( ?9 ?' d! [; k
return(INIT_SUCCEEDED);
5 j4 p2 T& }) Y7 m$ q2 o. R# {}
/ l& K& T2 _% B; K5 `0 y: J% M; S//--- Since we set a timer, we need to destroy it in OnDeInit().2 @7 k" i" o& {0 \) j( a3 y$ @
void OnDeinit(const int reason)
. o) i6 D- ]4 Y/ ~  }{6 j7 n$ F4 |# v
EventKillTimer();
( ]+ v8 n# @: d2 ]% O}
2 q1 M* i+ J  N. J) k+ J' i- \//--- The OnTick function only informs us that we have a new deal
# v( g5 D. d6 F( q! lvoid OnTick()3 I9 [# s1 s6 q; u0 _! b/ p7 e
{8 {8 R; t' |4 s0 ]  F/ x
tem_tick = true;$ i* c/ r: o2 E8 r. e
}& d% j7 K0 y% ?
//+------------------------------------------------------------------+2 l0 m: @  }" g( x2 J
//| Expert Advisor main function                                     |
% Q& t+ a$ T+ B& E. T8 N% J//+------------------------------------------------------------------+
* h( ~7 X  Y$ w- |& m5 f5 Rvoid OnTimer()
) B" `& N2 G; J% v& W* a" d' L{
/ B  ~# j* Q: {; uMqlRates cotacao[];% ^2 L8 o6 |7 p0 a  a0 j
return ;+ Z3 b% ?+ E0 G! t' H9 U' I. ~$ `# b0 x
if (negocios_autorizados == false) // are we outside the trading window?+ `# {8 r3 m7 H5 h" w9 b/ b
return ;1 m6 _% u6 F9 [% X2 ^8 w. n1 ~) ~
//--- We are in the trading window, try to open a new position!8 u3 J% j- \4 D  [" k
int sorteio = MathRand();
& U  L) e# p& C- `: l/ G5 D//--- Entry rule 1.1
- F2 O) r% U1 `+ Z# O: Z) n' |. Bif(sorteio == 0 || sorteio == 32767)
( V6 a! }' A7 c* z: [0 O5 O) Creturn ;* j, E  }1 F& y! t0 L
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& r% D9 Z! c6 M+ o: O3 R2 z{
! e& n/ S2 s. C. y3 j( Gnegocios.Buy(info.LotsMin(), _Symbol);
2 N7 h9 Q3 r* S}2 \! i6 i, `% m/ u9 p
else // Draw rule 1.3 -- odd number - Sell5 U$ P* y" u" s1 U5 K$ r: U# M& d' d
{, e( }" _' d8 L$ r
negocios.Sell(info.LotsMin(), _Symbol);% r" v. M, q( [& h/ h" {' |
}! _( P9 n; J- }# n) b6 D
}
+ c. b* ]( W+ |( y//--- Check if we have a new candlestick...
2 }5 v% b' H5 e6 `bool tem_vela_nova(const MqlRates &rate)
' e; b$ u/ h8 p# f  k{0 J! h& o0 u6 g# P
{' [0 s: K- q. @) ]0 E' |( F2 ~
ret = true;
1 m& A0 ]! S# L$ J. ?+ e4 U, n( pclose_positions = false;0 e' w/ e( X( @: T3 z9 [
}
8 O# g1 ]. i- K$ P  o- J# D* gelse
9 P6 o7 N/ x$ Q' I' g{* T% M- I, q3 ]
if(mdt.hour == 16)
; D8 }& }! H% J+ I- Rclose_positions = (mdt.min >= 30);8 {% R. g9 h! s- h0 D
}+ Z& N, i. K& B: |- }' ^- ]
}* j! g( M. L9 e& z. g- M$ }
return ret;
" Z+ O) v/ O# U, U: E}4 O2 n7 O" W# k: @; F5 I
//---" h. w. O  Q/ \) o* M
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])! I( v. Y; u7 h7 m; a) o
{" Q% z8 c5 j/ k- ]) s
if(PositionsTotal()) // Is there a position?
: Z1 H& ]" f# H{1 [9 C1 v, E, F+ Q5 Z# p9 M
double offset[1] = { 0 };
  s( S/ y& w5 s, i/ r- Z7 Aif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. p+ ]  @1 E) Y/ I% W+ E&& PositionSelect(_Symbol))  // Select the existing position!
( \  P# W: }$ d6 q8 [! d{4 `, Q( L/ N7 l3 W7 a7 A4 Z
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);6 E5 v8 |! I+ t' q3 i) `+ Z
double SL = PositionGetDouble(POSITION_SL);
' w, ]# t# q: N. I: J3 Adouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 ^6 \7 [( L1 p: `6 aif(tipo == POSITION_TYPE_BUY)+ j7 @. i. O$ Q! ~/ h! I
{
: n) b/ e9 e9 h) I9 K/ Iif (cotacoes[1].high > cotacoes[0].high)
! C# y* S8 t: r{' P$ O, A+ a' L, i. \' n3 P7 i+ Y5 p
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# m) V5 [+ C1 u2 a7 k  q
info.NormalizePrice(sl);. c) M; J4 j. I
if (sl > SL)  g; Q2 X; H( @9 ?
{6 Q6 u& c% J( N! i6 t. m3 @1 v
negocios.PositionModify(_Symbol, sl, TP);6 n8 [( W4 Q) E2 I6 `
}
' y, a! m# M  h" n}
8 R& O2 @1 l# h$ b}
+ V, C% D/ R( i) b1 C" {3 Helse // tipo == POSITION_TYPE_SELL- t, S; x% n- k% d! W; `' S7 n
{; F- C- |4 O; @3 b4 L
if (cotacoes[1].low < cotacoes[0].low)6 s3 I/ [3 f8 D0 f, A
{. `8 Q& C* O$ ~+ W$ t: P8 g9 X
return true;
1 Z' s: [, ~/ s. O7 g! R/ `( R8 s}+ z7 h* g8 y1 L( b% b
// there was no position
4 e9 L3 ^7 t7 w# @) Xreturn false;4 k" {% W  |2 X: @6 {. W
}3 C+ O. S% L8 G7 w4 c
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 [. t1 K2 K( u, d. V% F7 ]( z; j
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-10 07:48 , Processed in 0.961358 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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