私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA2 D3 k: ~7 j( Q% T
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。/ _& K7 A- o6 o8 l3 p
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。$ w+ l- u+ S& K% D- v$ X9 @
以下是制定这些规则的代码。( B' ~' E, w. R) b
//--- Indicator ATR(1) with EMA(8) used for the stop level...
. [) w6 {) n+ K' p4 E7 \! l1 V/ rint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- t3 q( o- ?, Zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
, z7 A4 Q- t  I* t//--- Define a variable that indicates that we have a deal...
5 h6 \2 u4 D* B2 T0 a1 rbool tem_tick = false;
; h, p; ~  p: ]- v$ J//--- An auxiliary variable for opening a position* J% H) }# a. ?" o4 l
#include<Trade/Trade.mqh>6 D# y/ K5 q- X# z4 K$ q2 e
#include<Trade/SymbolInfo.mqh>( N$ g* ~0 X+ |/ ^
CTrade negocios;! A, Y! E2 L& @3 b* Y
CSymbolInfo info;: n- V5 R( X0 {( ?& E2 i
//--- Define in OnInit() the use of the timer every second
4 o* n2 G3 _6 x8 ~6 g% w( {5 m' V5 n//--- and start CTrade6 }; v/ A& D% J6 o2 \
int OnInit()
' T* R% C4 R4 t4 R{, R9 G& I& v. `! f
//--- Set the fill type to keep a pending order: J4 m/ @; n+ O3 q% H: L
//--- until it is fully filled
6 E' h5 H; e9 N) k0 m! Fnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
3 x; N- v8 ?  i5 T9 i//--- Leave the fixed deviation at it is not used on B3 exchange2 w+ o! q2 g: G5 p4 u4 x6 G) U
negocios.SetDeviationInPoints(5);
- i; P% H4 }# X//--- Define the symbol in CSymbolInfo...: q, n, O  p' ?
info.Name(_Symbol);
! I( c! j3 j( k+ ]$ C! P//--- Set the timer...
! N4 E7 Q, D- R' O# {EventSetTimer(1);
: z7 G! G% r! V; e  K& _5 u//--- Set the base of the random number to have equal tests...
  c; ?' ~+ `# j/ B; J, yMathSrand(0xDEAD);
/ R- U/ J4 Q0 ureturn(INIT_SUCCEEDED);
8 l& ~% s) n, f7 o% d& M}
, t+ f  I/ l* F/ L- H- x- b# k//--- Since we set a timer, we need to destroy it in OnDeInit().
% i  p+ q# ^% X0 _& u3 Evoid OnDeinit(const int reason)
  j9 H7 }0 ~8 k8 z+ l$ L1 |. B" P( J{
& n5 T( M' h4 N; X" TEventKillTimer();0 o, a, w, [, G6 j+ f+ a( @
}! z5 R  J, M! D% h4 ^( k
//--- The OnTick function only informs us that we have a new deal) T2 m$ K9 b  M3 A
void OnTick()' G) N6 o6 t3 m: A' n5 h* [# [
{7 R0 [( ~+ `4 Y  p
tem_tick = true;5 P& ]2 D& m4 F& u7 n9 A" Q
}( c# Q$ Y% `+ u) a' S
//+------------------------------------------------------------------+9 ?( B9 D( w  }, \% v! ?0 A! I
//| Expert Advisor main function                                     |: O. Z! ~3 y" d& }) r& q+ W* v
//+------------------------------------------------------------------+
3 e$ B$ C0 P3 B6 Uvoid OnTimer()
' C: M0 L9 y8 \6 h, s/ h{+ Y- P# `( `# N& U0 J" u' e
MqlRates cotacao[];
" w* M0 X5 a  M/ mreturn ;6 C4 }+ y3 u* I! }/ W$ K" H8 b
if (negocios_autorizados == false) // are we outside the trading window?2 f: f: b0 a  z. X+ k, F
return ;
$ y- Y. J" e  T//--- We are in the trading window, try to open a new position!
" z1 t9 y* y# m$ m6 s& }int sorteio = MathRand();: C, E3 N1 M5 R, W
//--- Entry rule 1.13 S/ b3 f, j  u( A4 M- Y# M
if(sorteio == 0 || sorteio == 32767)5 a- ?9 W$ f7 j( z8 R4 n/ j! W. x
return ;
; q$ K) a2 \  J9 c# o6 |1 W: [if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 r$ E$ Z% N2 I) W# d. w
{
) N9 |6 @8 E! [  W7 ]; }negocios.Buy(info.LotsMin(), _Symbol);1 ]! T+ }4 ]% S, U
}
6 w% F9 F9 j$ A) z. \; _else // Draw rule 1.3 -- odd number - Sell
; j7 }9 F( i& K  G" I4 x. a: F2 f{: P. S- Q% |, {1 {0 v
negocios.Sell(info.LotsMin(), _Symbol);' m8 A, R3 [* ]( k1 o$ g8 M
}
& I" {' O2 u- c3 M}7 F% H; n. I/ @
//--- Check if we have a new candlestick...! h% r: ~/ q/ i4 Z- U$ S9 Y+ n
bool tem_vela_nova(const MqlRates &rate)) }' G6 V" T, N# {" P( a5 \
{, U2 ?0 c( G2 n7 _8 w, v& `- J
{
8 m; c: W- g/ aret = true;) D4 @/ _( c" s$ R' y& x8 r" _7 ]
close_positions = false;* C/ m4 _9 d' o8 Q. L0 P7 Q" b* {7 B0 Z* c
}
: r: R" p0 w0 d6 S& telse
+ }' H5 w3 V: _1 A- L5 n5 M{
2 z# s, L6 b/ C# v) Fif(mdt.hour == 16)
1 j4 p" g. Q0 s# r9 [6 F* sclose_positions = (mdt.min >= 30);7 l6 V; E6 T  l% P8 N# m+ v3 m8 \2 T
}
, t% N3 r" w% @0 f6 b}
( i) z: A  z' m* o% [return ret;- t9 I$ Y; D0 F" b6 \; K
}
, w: Z& J3 S& w3 Z/ F4 \//---7 Y& ~" s: p. r/ L7 N( Q. ^: N
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])9 ~# u- ?! l# Q  q& |3 d6 A
{
" A2 A1 X! a  d6 c- V# @' Wif(PositionsTotal()) // Is there a position?
6 U1 x0 t6 a' L$ t7 l/ M" l2 {% `{
) f/ F! f# [0 l  u6 ]double offset[1] = { 0 };7 K1 _+ a- R8 D* ?: `2 N" x
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?* _5 `+ ?9 g7 m# l# O2 p
&& PositionSelect(_Symbol))  // Select the existing position!
3 [7 h' D# v: Q% h3 n; a3 Q( s{
2 S# w# |8 S! I) HENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
. g  v. V6 g0 {double SL = PositionGetDouble(POSITION_SL);: I% g6 l2 ^; k# J8 C0 e9 J$ _% x9 r# ^
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));& k9 n$ b$ J- L! D4 D1 t5 `
if(tipo == POSITION_TYPE_BUY)
, h+ y/ a+ y5 S' E6 n5 f- T/ V{
- e% E! R, v0 u8 l3 xif (cotacoes[1].high > cotacoes[0].high)) |6 V4 m. U, _
{+ ?$ L* t1 G3 `7 ^
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
- F' y- T! \- N4 Ginfo.NormalizePrice(sl);
3 I+ S7 w$ \6 \% `' vif (sl > SL)8 Q3 f3 z$ R0 {& S! V
{
* x/ |3 p4 r6 ?& `8 K: u* P4 A+ Snegocios.PositionModify(_Symbol, sl, TP);
% M- b, \- c# O}
: W/ T3 S. D; L% s}
4 b! F4 D9 k( W8 a: M}/ }7 B. x) E. G/ U+ ]/ E# L, ?; _
else // tipo == POSITION_TYPE_SELL, S! y: l* e( F1 P2 R) |
{+ E0 T( I5 k, k. S
if (cotacoes[1].low < cotacoes[0].low)
: X2 z2 \. H0 M% U2 u7 R{
8 X5 c7 A/ j1 Z+ l5 i# Z# d# o* R0 areturn true;
8 x7 c5 d/ v& N( L! @: J}% s) F  [8 ^6 [3 a: E
// there was no position
9 D6 a6 k, M6 l8 L) ?return false;
% w# B* v- @" C6 }* `}+ @; x) k+ c$ {& w1 l/ S) n
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。3 F" }; x% ?) ]3 @0 }% U+ a
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-5 03:43 , Processed in 0.788489 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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