私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA! j  y( e7 ]3 G' s
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。* |$ `" |) `4 V) ~8 n
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。! {$ a8 _" g8 f" ^+ t2 p
以下是制定这些规则的代码。- f. J! _+ p9 G( ]) U" [
//--- Indicator ATR(1) with EMA(8) used for the stop level...1 |+ B0 O4 s  c# {- d* [, V
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; P. n, s) r" vint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& O7 [7 I; P$ v//--- Define a variable that indicates that we have a deal...# r$ ?. R0 _6 O; R0 k
bool tem_tick = false;) g- V4 T) u) `5 B4 o
//--- An auxiliary variable for opening a position% g& t0 W* d* J1 ]- C
#include<Trade/Trade.mqh>
- I) u# t+ M% Z7 L5 i$ ^#include<Trade/SymbolInfo.mqh>7 X9 C; B* k1 H4 a" b5 J0 ]: t
CTrade negocios;$ J* p7 T. p) R$ ^1 L) ]" R
CSymbolInfo info;- Q8 K% n5 d: w5 W4 V  M5 N6 }
//--- Define in OnInit() the use of the timer every second3 _" k) H8 H3 y5 g0 P0 x  s
//--- and start CTrade6 k) O" m; M, N" |; F( u
int OnInit()# ~( S" f" q& w: {% _
{" Z# B& O2 L0 b( C; v
//--- Set the fill type to keep a pending order5 k. j3 S" e) F6 V( |) C9 G/ @
//--- until it is fully filled
$ O  Z7 D' K# L: s" Cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
$ p/ J# U4 C! T2 j" p. S1 ^$ u2 M//--- Leave the fixed deviation at it is not used on B3 exchange; |& `/ k. d+ d) z
negocios.SetDeviationInPoints(5);
0 t0 Q: R2 ?* G% l( S+ ?6 Z6 u//--- Define the symbol in CSymbolInfo...! \3 k# v2 j% J7 \# p/ |% u) J1 g/ Z
info.Name(_Symbol);2 Z0 g, u  @) W
//--- Set the timer...' o( \( w0 }+ @, O1 _" @1 C
EventSetTimer(1);
( J/ A3 A1 b" @: P; H* |//--- Set the base of the random number to have equal tests...
2 R5 K8 @; y$ v, G9 S: V' z; aMathSrand(0xDEAD);
- D0 B/ E4 ]  l) C' ]% F0 ]1 \" ereturn(INIT_SUCCEEDED);) u4 z5 C7 j6 k0 f
}
3 ^( B& a( I# {% O3 R% c) C& k9 S! {//--- Since we set a timer, we need to destroy it in OnDeInit().
6 n; i2 I* G( G9 ]+ z" Xvoid OnDeinit(const int reason)
1 w% o$ q0 o7 t! j, a& V6 y{* f6 i% \/ H6 L3 D# f1 G
EventKillTimer();( a/ q+ W. a) R  ]& _5 o1 y6 V
}5 y# h$ r1 v8 H3 ]( F% N; g
//--- The OnTick function only informs us that we have a new deal
: i# x% B- Z3 M7 p- W7 B+ Yvoid OnTick()
6 I: S5 w& r3 T, E. |3 b& k1 i! P{
- q/ |6 p9 C$ ]tem_tick = true;6 \& m! A5 V9 i: `$ Q7 A8 o
}' v1 J7 e1 ^7 }/ x
//+------------------------------------------------------------------+' z0 M. b1 r' {9 o9 N
//| Expert Advisor main function                                     |0 u0 g; H( A" ^) M* W
//+------------------------------------------------------------------+9 C1 w7 K" A5 ], C
void OnTimer()
* t7 r9 L5 B( n# |; |% d1 s$ d* a. p" }8 D+ W{
! G) F4 l# S/ _! mMqlRates cotacao[];
3 A+ b( W5 m1 k8 Oreturn ;( Z  q  B- E' r/ Q
if (negocios_autorizados == false) // are we outside the trading window?! [4 Q# `: g7 K, r) k( x  T" Z
return ;2 K+ Y( v+ Y' Q$ w& \
//--- We are in the trading window, try to open a new position!% h5 ]! B( t  K: R
int sorteio = MathRand();2 e0 F9 j8 V  b$ @4 d
//--- Entry rule 1.1, f' O: M+ u! U; X4 @" ~  Y; I
if(sorteio == 0 || sorteio == 32767)6 E: O$ d+ T1 d  v
return ;
6 r2 l! J$ c$ |. bif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy8 i' j9 @$ k% |3 q4 m
{+ U: p1 `  Z9 a% C9 A, c
negocios.Buy(info.LotsMin(), _Symbol);
: u! b8 s5 x/ F+ ^9 X8 @}
. R; a, A! P5 Xelse // Draw rule 1.3 -- odd number - Sell
" h8 S+ T+ e/ q{0 m* N) Q0 x! d+ v+ r8 o
negocios.Sell(info.LotsMin(), _Symbol);
* F, A9 y9 t) D6 q3 w0 K}) M" Z: G# Y3 h% S6 e% @
}7 x9 p& |/ Z  ]4 {
//--- Check if we have a new candlestick...; h; h: M- I$ w, m2 f
bool tem_vela_nova(const MqlRates &rate)* |) S8 s4 F9 @# X
{
0 \& ?' i+ J( c. o1 M1 |" L{$ l! \" }  O7 s9 \
ret = true;
1 z2 W% V5 q2 v7 z. ?# F# U' Rclose_positions = false;
7 x: w" O* _( r. n4 y}
9 S) H7 F3 d& K% m1 qelse
1 @' Z9 I2 X& \* _4 [{. b3 n/ ^$ v( Q# x0 |5 u. ?9 P
if(mdt.hour == 16)
1 `: [2 p3 u* V9 \close_positions = (mdt.min >= 30);
% H1 r& ~1 D4 r2 l}  v8 ]! m$ z! R$ @
}/ J' E) E6 A2 V/ |' E' `- ^5 w( q
return ret;" k& L4 n2 E1 g4 e  X% F2 S1 c
}
# C' Z  c: W' M//---0 x7 a) d# b1 h9 x5 V# Z& @/ Z
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])' k& [; k; q# o) ~5 {% T; a
{
- D* h9 f4 I$ y7 |if(PositionsTotal()) // Is there a position?5 J; V: x  s- k) k! d7 `, q: M
{9 i1 B' E' N7 K/ b6 g4 O. Y1 G
double offset[1] = { 0 };
  K5 l6 N$ r" Dif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
* z9 ]4 w* L5 x" \&& PositionSelect(_Symbol))  // Select the existing position!
/ ?' u% [. P' x( p1 q# \! H{
( M# d5 i. p- ?$ }/ ^ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
/ b  X2 Z' z3 h( l0 k: {double SL = PositionGetDouble(POSITION_SL);( j* ^  o2 D7 J7 P% V
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# {  k: I3 N( i; ^7 g- }0 ~
if(tipo == POSITION_TYPE_BUY)2 r& b' C; W1 Q9 e& S( o. H
{
& x( V" v! o/ N, Y" G8 h3 P9 dif (cotacoes[1].high > cotacoes[0].high)% j' T' p$ p1 g5 Q0 x. Q. P2 B/ U2 \2 A& V
{( d' G  g9 W6 w1 c8 h! V2 a. e- n
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];# Q# \& z- \  X# z
info.NormalizePrice(sl);3 r( [4 M& \+ G  I$ j
if (sl > SL)% B  e: k5 z8 T+ Z% Z
{
: p. ^! z5 j& \% X: Vnegocios.PositionModify(_Symbol, sl, TP);
- o! F1 U4 J5 A2 c}+ G4 K% v7 `' V
}4 _/ h: G4 S1 T: E& u7 [: j
}
3 }) r# i. A3 X* H- T: Xelse // tipo == POSITION_TYPE_SELL! p% a" h) j( W2 n
{6 |+ t1 g, p9 @( f; K* d
if (cotacoes[1].low < cotacoes[0].low)
' N( r, S  r* Q( c{
1 y% F, a& z2 }$ p- M" |& G6 Nreturn true;
' l4 d/ S, T9 S3 A}
1 k+ s* ]$ s: a  m1 ^9 h( R7 X// there was no position/ M; T; N* l7 o2 \# ?
return false;
" }8 N, n4 f4 g7 T8 R}" U9 ?" \7 E9 J5 [' \: `0 ^/ z- c
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
  a9 Z1 k2 a& f( h4 N& O8 i到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-1 20:01 , Processed in 0.440306 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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