四目观天下

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 ~  |% `' n6 n+ ]" K  O在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。5 L4 [- L; L! u" Z$ z
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( S. t' a7 X5 f
以下是制定这些规则的代码。
5 }& y' F3 V3 s- d* l//--- Indicator ATR(1) with EMA(8) used for the stop level.../ |: W. B) f# ]8 w2 v0 s
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" l0 }0 n% x. \: ~* @) M
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);: G' ~2 C* x: Z, C
//--- Define a variable that indicates that we have a deal...) h+ d  \3 F' {5 @4 j4 c! S
bool tem_tick = false;
# i7 i5 `; N4 A//--- An auxiliary variable for opening a position8 m8 H1 G7 ~/ M% l  h7 Q6 Y  N1 H
#include<Trade/Trade.mqh>8 Z( E2 x+ Y% q4 ?1 o* L
#include<Trade/SymbolInfo.mqh>
4 ]0 N, G0 g, Z& sCTrade negocios;% W' K3 O- X2 e" z+ d$ j: {6 I
CSymbolInfo info;) p" J& X9 z. l4 b+ ^
//--- Define in OnInit() the use of the timer every second* v2 C9 o# t( Q7 ^6 w
//--- and start CTrade
8 ~& e% r6 L% n; Hint OnInit()
. g; n8 o; }; X& ~! u3 Q9 N{
! O* w: j& a& p6 t2 t' r7 j! z" D# u# z//--- Set the fill type to keep a pending order
0 f4 f7 f7 ^- A9 Q1 o) j  J: w//--- until it is fully filled
6 n& t) o8 Y1 R/ s; |negocios.SetTypeFilling(ORDER_FILLING_RETURN);9 d0 a& ?! C6 X9 G; d
//--- Leave the fixed deviation at it is not used on B3 exchange
; O* e* I5 o0 h( ?. D4 y, gnegocios.SetDeviationInPoints(5);
: N5 S7 g+ D- o6 Y' _- h//--- Define the symbol in CSymbolInfo...0 l  u' h# c2 g& E
info.Name(_Symbol);! ]; b% v/ E6 i/ B$ ?
//--- Set the timer...
. L) E& G8 T9 V' z. aEventSetTimer(1);/ w9 W1 ]) z3 e! D. a
//--- Set the base of the random number to have equal tests..." u4 Q. o8 e! M3 }1 E3 O
MathSrand(0xDEAD);
2 G0 x1 `% B2 K+ t7 _, p5 Ureturn(INIT_SUCCEEDED);: }$ k( n; ~. I
}. U: ^. j4 U& Z1 H7 V
//--- Since we set a timer, we need to destroy it in OnDeInit().
, Z- s+ [- P/ tvoid OnDeinit(const int reason)
8 R7 [2 n+ e0 o2 }7 ^* T2 O{$ R, u$ c/ Y( |" l- S0 [  g
EventKillTimer();* ?8 [  M  c) m& K3 i" L. {3 F, T
}  J1 ^4 }5 W" r# T( Q( ~
//--- The OnTick function only informs us that we have a new deal! A2 }! u3 t8 @  Q! n$ d
void OnTick()
/ n( W8 z, r; E" M/ W# a  d{
9 d1 f& u# `! Y5 _# o; Otem_tick = true;3 c3 D) D, M9 e  O
}, i4 O' q6 E2 a* W0 Z
//+------------------------------------------------------------------+
; f1 Q& u  |# ?: i  L; l//| Expert Advisor main function                                     |$ f, r4 d) |$ W$ L; L# x
//+------------------------------------------------------------------+* V3 A' Z2 k8 G  n
void OnTimer()
. R+ R7 A: h3 n8 s9 i! ?( _: X. E{
+ l5 h4 i, J% K9 KMqlRates cotacao[];  n) j8 B' R* F/ Z
return ;7 a' Q/ @/ _  k% a, O/ K, K
if (negocios_autorizados == false) // are we outside the trading window?
0 Z4 Q* a2 ^% {  j" W$ dreturn ;9 \7 D5 X/ Q3 a! {  O- K
//--- We are in the trading window, try to open a new position!# k6 k' N8 b/ @5 E' G
int sorteio = MathRand();( r. A) J# B# }+ z% |  P& w# U
//--- Entry rule 1.1. w3 u2 D& k3 M- Z
if(sorteio == 0 || sorteio == 32767)
& w4 h/ _* W$ A+ \5 |' x# ^return ;
5 W5 n6 t& q- z4 S" u) Cif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
# j* n3 j5 a5 T3 m8 c9 D{
9 F( n: w/ j+ Cnegocios.Buy(info.LotsMin(), _Symbol);8 L2 }) ?! @" o8 y1 l3 b
}
$ ~, y6 @% g4 U' h/ Yelse // Draw rule 1.3 -- odd number - Sell1 }0 L7 z  p8 \( J# {/ m0 s7 x
{; q2 F: d2 ?* {) W
negocios.Sell(info.LotsMin(), _Symbol);
/ i! g3 e. _5 \5 [" r/ z: N; D}9 @% v/ D" @, h  v9 o6 {( f; g
}7 G. i/ B; p0 ]& e, w: X
//--- Check if we have a new candlestick...
) i7 M- j7 s5 [2 F1 a/ D2 abool tem_vela_nova(const MqlRates &rate)
9 e( J, L# A( f{
5 N( j8 w2 B. `9 @{" A" f% x# b* l; b- a% G
ret = true;" \$ M6 b7 u+ k: C0 W% X
close_positions = false;5 @% i; }% [/ K2 G) H
}
& a  @/ O  U4 X  V) b4 ~else
) `; B( R& X/ w2 h  A6 A{, H/ C# T; I) w
if(mdt.hour == 16)* \% b  ?5 X$ U* M
close_positions = (mdt.min >= 30);% `, l4 F" C2 r% G, J
}3 Z6 G6 A9 Q( ~$ ~# z# V3 j- F; j
}( P# j/ E3 M; `1 X$ i" @. W
return ret;
" e( y7 V2 i: {3 J}# T* Y" i' y2 R, m
//---
) `, l+ V# F  H2 ~0 }% Q$ `bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
0 f, Q$ D. G1 M* ^6 _0 Y6 D* J{- m: [3 D* o; z" c0 g' V
if(PositionsTotal()) // Is there a position?4 r7 _5 M2 B/ e5 d
{1 ]4 _1 D# R& _* s& g
double offset[1] = { 0 };
& N/ F! J9 Q; O0 G1 fif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
4 P( F& x7 e  h: A&& PositionSelect(_Symbol))  // Select the existing position!
* ]& i) Z6 @1 [: `9 }. O6 L( f{8 ], N) Y; {5 ^0 `7 H1 f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; Q  ?1 B1 f8 Z) y- G' hdouble SL = PositionGetDouble(POSITION_SL);9 U& U/ Z3 o: S5 e4 V2 s8 G; P
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));  o. Q: p; \4 N' ^- P* d* T+ C
if(tipo == POSITION_TYPE_BUY)% Y3 K4 h9 F* n* F2 s1 R7 ]
{, n5 e9 }8 m5 [! g
if (cotacoes[1].high > cotacoes[0].high)
" p+ B3 r2 U- S0 C{
4 n, a) N+ c9 A: Fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 b  V! S& i2 P, xinfo.NormalizePrice(sl);- k( L$ p" a2 x7 ]1 w& A; Z0 w
if (sl > SL)
, U( ~8 a7 q3 y. c{& e7 y3 f% a  e" A4 A3 `, e
negocios.PositionModify(_Symbol, sl, TP);
9 @" m/ \7 ^3 g& v}
( `: A7 A1 ]9 ]- O9 x8 p  j}
3 F/ K0 [! n+ z) i9 j. w/ {, y}8 O( [6 y) j0 H5 b
else // tipo == POSITION_TYPE_SELL3 z3 I" {+ r' d$ p/ g! {
{% D& ~. _3 c; Q% N
if (cotacoes[1].low < cotacoes[0].low), |8 G: F& F9 i  v& I
{
& ~- h& w& Q# Z. B& a, n9 d+ jreturn true;) J1 D  s: `# v) z# T# c! P  b
}6 H2 ^' f4 d4 E3 V/ L  T
// there was no position
' z; l/ j; Z+ t$ creturn false;
% p9 }" A$ R/ Y# g, Z: l: S7 y}- O4 P9 U; ]) N& m( Q
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
, ~2 v9 a$ m/ @0 b* b2 T, w到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Archiver| ( 桂ICP备12001440号-3 )|网站地图

GMT+8, 2026-6-27 03:10 , Processed in 0.741715 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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