私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
: |3 |* J/ L: C) l; L在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。, n; q* `! d  X6 i7 J0 \5 z1 O
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* M; m- W: D1 s以下是制定这些规则的代码。6 M/ d6 H% p: M& s
//--- Indicator ATR(1) with EMA(8) used for the stop level...
. a7 {# z2 d& @) s9 J. Nint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);, X' h3 C0 u- x9 g* A0 C! ~
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
. L, I2 N; B( H' C5 y* w3 n//--- Define a variable that indicates that we have a deal...
1 {6 `, z- Y. x- v4 fbool tem_tick = false;! I4 I0 U! @, x# `3 ^+ P# f
//--- An auxiliary variable for opening a position& t* g& ^: m" d( n
#include<Trade/Trade.mqh>5 |+ w$ r' N  x
#include<Trade/SymbolInfo.mqh>
! N( p, R7 M' FCTrade negocios;
3 I5 F8 g% U: k8 f' wCSymbolInfo info;0 c0 g. Z$ f4 C9 I
//--- Define in OnInit() the use of the timer every second
6 V, f0 r- p7 d9 k; X//--- and start CTrade
' P0 m' ~) d0 R+ f/ Hint OnInit()
5 F! x1 d8 P& D) m. N: v7 k{
% B, t+ D) m6 N) |//--- Set the fill type to keep a pending order
+ C' u  L' J$ _) d//--- until it is fully filled
0 j# L( R& T' `3 H1 d8 B1 u' W5 X' wnegocios.SetTypeFilling(ORDER_FILLING_RETURN);& N2 @4 W9 y' C+ `0 j2 c* b
//--- Leave the fixed deviation at it is not used on B3 exchange
0 `- k* ~- u3 cnegocios.SetDeviationInPoints(5);
& Y' U) e2 B. V//--- Define the symbol in CSymbolInfo...
! G! \6 e' h# C) oinfo.Name(_Symbol);2 [9 c" h# G3 Z: ~- R4 O5 J
//--- Set the timer...
1 Y- K; v. X* \$ ]9 |EventSetTimer(1);/ s1 @. Z' `/ H6 T' ~% }& z
//--- Set the base of the random number to have equal tests...
: J1 Z* U! }1 t$ G* AMathSrand(0xDEAD);
% |+ Y/ n; m6 n0 v$ ]return(INIT_SUCCEEDED);2 ?  h; z- ]! c9 @$ Y; U9 y6 ]
}6 F) D0 t+ Q8 Q: N2 `
//--- Since we set a timer, we need to destroy it in OnDeInit().
4 D8 `- v5 t+ ]' _void OnDeinit(const int reason)
. A9 b9 O4 {" _4 o+ Q5 C  X, t{: ]" s/ ?/ z  y' J( z+ T  W3 u  p" |
EventKillTimer();
5 c- Z; a% c/ m' d. w) D6 i}
, f2 i/ ?9 V- P- W; X//--- The OnTick function only informs us that we have a new deal
( p1 J$ _0 }/ Q; }" g6 C) Y8 mvoid OnTick()3 H8 E* o/ S' N6 [
{$ K' u! L  {! x+ J. T( ]
tem_tick = true;
! R! V3 k9 F/ Q( |) `: u( m}; B1 T2 g! f; Z! [% e1 D/ k/ U
//+------------------------------------------------------------------+- m7 x1 W7 Q  J  |$ y
//| Expert Advisor main function                                     |
  W0 C$ m$ p# f% H4 g+ [# u) S//+------------------------------------------------------------------+
: c  p" {" p! o0 o+ v0 Rvoid OnTimer()- l5 j, w' W  m. D2 J$ R) G
{
& Q, E/ N0 g, d6 ]" Q# KMqlRates cotacao[];
4 e. k2 [% S. x2 Q1 |- d! [) Areturn ;3 ^; N% X1 }+ Y0 T8 U6 L' v' P
if (negocios_autorizados == false) // are we outside the trading window?
- e! Y; q8 n2 Jreturn ;6 j/ S* @! h2 F
//--- We are in the trading window, try to open a new position!
0 p3 n! s% A9 c& j* zint sorteio = MathRand();' i" C* U* w6 q( N
//--- Entry rule 1.1+ t$ h$ j4 ?2 c2 J) V9 Q( z# T
if(sorteio == 0 || sorteio == 32767)& N6 d1 H1 U' y2 _
return ;7 D, O. T$ [& V0 B: n' e
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy4 j& K6 j* t7 z1 M
{% g9 B# Q6 D+ {
negocios.Buy(info.LotsMin(), _Symbol);
0 A9 {! S9 I2 E) J4 ?7 I, V  I}
9 Y" w' _0 i) V% n8 ?else // Draw rule 1.3 -- odd number - Sell
8 ~9 y, H& _8 S0 c8 y3 t. P{# K) s# p3 S) t8 H/ v, E
negocios.Sell(info.LotsMin(), _Symbol);- O$ p" y* w" d1 C$ g5 }3 x
}
% l. J, r) `/ D& X, \5 t}  }. ]5 j/ b" X+ h( E$ K; e2 q
//--- Check if we have a new candlestick...
4 @, |0 R0 i# A* @, ]' Zbool tem_vela_nova(const MqlRates &rate)
5 F0 O7 u9 \% y" S  k, w% l{  g( u- N! F! S4 g; E
{
; S0 ?4 f# `8 O4 s; ~6 ~* Wret = true;6 h+ d0 R/ h6 @6 L- s
close_positions = false;. b' L- G+ B. q
}
0 G7 c3 l* I, b7 s' Pelse
/ _: H  u* R5 R  d+ b. F9 F{
" A. ?4 k7 b! e: t2 C8 n6 s2 Wif(mdt.hour == 16)
, P# `4 I3 ^, s$ c4 i9 m* i2 Nclose_positions = (mdt.min >= 30);, _) \" i: v* x0 E6 J' j
}
) ~" A/ R/ l( y9 ^+ l}, F/ _6 n5 Z3 t
return ret;
7 C( j+ \/ u& w2 y% H}3 N( G1 Z) R& `  v
//---
; M4 X! Q1 r6 G* ^" ^bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
+ [/ R! L/ a5 i& R" \{
5 N6 z4 }/ M4 j4 n: jif(PositionsTotal()) // Is there a position?
1 u- s' U$ A# U$ i  n{0 E7 z" S) l6 Y, M6 `
double offset[1] = { 0 };
" @5 M+ @: H4 M  vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  w# t, G$ S$ L1 @$ o/ E$ r% M&& PositionSelect(_Symbol))  // Select the existing position!
' V$ e. z' L0 F7 d8 S# ?- W9 ?0 O{
6 m- v! k6 |2 NENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);  G6 P8 o% l$ G3 ?' s1 c3 r' {
double SL = PositionGetDouble(POSITION_SL);( E8 d# k1 r3 E  q. o
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ b, z+ {: g" ?% i3 Z
if(tipo == POSITION_TYPE_BUY)' T! G( t! p1 h# Y0 Q$ q, o- [
{
6 p+ W3 p! |5 s, q5 Zif (cotacoes[1].high > cotacoes[0].high)
; V& M7 ?- a% j{
- F& J8 g' e5 A5 t7 D+ [double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
& s) g4 X1 O. J$ |' n' @2 Tinfo.NormalizePrice(sl);  z( Y% K* e" `& r5 `. y0 L
if (sl > SL)
; a3 G( j( Q$ n3 \) O{9 o9 \' ?3 K+ u2 n7 C) [
negocios.PositionModify(_Symbol, sl, TP);
# \4 P2 Y% f- W+ A# r}
6 u2 G: N2 \2 m: A}8 e+ S6 g6 Z1 l# Q$ v( z  m* p% Z
}* `3 G! N/ M2 l; W
else // tipo == POSITION_TYPE_SELL9 \4 G7 F% V. ]7 T) x! }; g/ N
{
! r! b7 C  ^! M4 F: Pif (cotacoes[1].low < cotacoes[0].low)( t4 T: d0 v! `8 t5 `" o1 d
{3 \. x+ g5 T8 f3 n6 ~5 N
return true;# n) z* j3 J7 z4 ]+ `
}' F/ v: `: g& g8 b  ^7 @( y: z
// there was no position3 h6 J# X% G& v$ I" x
return false;
6 A8 Y  `. w- j) |5 O8 X6 l& p}  l* G; [% x/ d0 u. ^% P
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# [6 e, R$ \1 A5 {, h
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-4-12 10:24 , Processed in 0.398689 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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