私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA. J$ z5 H& r9 T, o
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。; w) ^$ @( O% Z2 d' Q8 k
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* K% V& M7 S: W# A- ~, s' B以下是制定这些规则的代码。. {0 P. x) u) p" z
//--- Indicator ATR(1) with EMA(8) used for the stop level.... q- q" b, h$ e; ]6 n9 n$ ~( @
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% P% k2 e2 ^9 W% Q; a6 eint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ y( Z4 q7 ]6 P, N, e$ `) @
//--- Define a variable that indicates that we have a deal...1 Y. u' f4 o4 C+ c7 \8 s
bool tem_tick = false;" \4 d) F* J' `* \
//--- An auxiliary variable for opening a position2 S- ^; L7 a) v' t9 w* ?7 n
#include<Trade/Trade.mqh>: O5 ], {9 ]" H$ Z# L$ U  W
#include<Trade/SymbolInfo.mqh>
, v7 P; d3 h+ g3 \% L) LCTrade negocios;4 X1 T  E, Z8 \7 d- T; T+ u6 A
CSymbolInfo info;
; d; P- S. o( n* A) V1 j9 F//--- Define in OnInit() the use of the timer every second
& k1 F! k8 Y+ a//--- and start CTrade6 }" b5 l0 t+ D1 q( S# }% g
int OnInit(). `% P" ^$ x$ u+ P( L
{. x3 x& p& ?) |* f7 w
//--- Set the fill type to keep a pending order
, Z$ s9 h' l0 ]$ @' C//--- until it is fully filled
6 [4 A7 P4 d$ u. anegocios.SetTypeFilling(ORDER_FILLING_RETURN);' W5 o9 |7 @9 s
//--- Leave the fixed deviation at it is not used on B3 exchange
; R) @$ k) O+ u/ y0 U2 ]negocios.SetDeviationInPoints(5);& I+ e6 @; g) s( S! `$ O
//--- Define the symbol in CSymbolInfo...
3 @, I: I5 Y- W0 v' v& Einfo.Name(_Symbol);* M! r" J6 y/ _
//--- Set the timer...
" W# l$ k) q& [! |! D4 B1 \9 Z( Y' j0 ZEventSetTimer(1);. O* e  o  V) n& C/ r1 \4 `: F
//--- Set the base of the random number to have equal tests...( j. U, g$ P1 n- ]
MathSrand(0xDEAD);: U9 D' s  U1 d# k# D& l
return(INIT_SUCCEEDED);; _9 D' m3 }$ Z( D& O
}
' ^. E( {' h) z4 I. k9 n//--- Since we set a timer, we need to destroy it in OnDeInit().
. N3 ]7 Z- ?( x% j7 qvoid OnDeinit(const int reason)
6 t! D9 s' J; `) b2 g{
  c' I9 i6 Q" D7 \EventKillTimer();
9 ]& i6 r; Q2 A' Y}
+ w/ k$ b' O' T' T//--- The OnTick function only informs us that we have a new deal
- q& B3 W" Z; ~2 V3 g& b$ Pvoid OnTick()
, r; s; W, v0 {. B9 _: ~% w7 I3 z0 v{7 m9 L' ~" A; x) E, S1 t8 x/ b; w
tem_tick = true;
9 \0 i6 s; \* u% s}
( b: _$ S3 ]/ p//+------------------------------------------------------------------+: E! i; `9 d; L3 c8 i5 ?* U5 D$ V1 F4 U# h
//| Expert Advisor main function                                     |
9 K, h7 X1 t7 X: {, c//+------------------------------------------------------------------+& u8 {; ^0 |& k, l1 m
void OnTimer()
, O9 H  j+ n" ^{/ W' c9 G$ u' R, O
MqlRates cotacao[];# \: H  Q) q5 Y- D5 z8 T
return ;( e4 `5 [4 W8 w: k
if (negocios_autorizados == false) // are we outside the trading window?' ~! p8 N" r( F+ e, u
return ;: W# M4 ?) r( v$ q: A$ N/ w
//--- We are in the trading window, try to open a new position!9 a7 A' y6 ^+ M6 G" Q1 q' Z! ~4 I& ]; L
int sorteio = MathRand();
( u# x. i8 d1 \& g4 t% J- Z6 `//--- Entry rule 1.1/ r8 I& B+ S0 J: l0 H5 d
if(sorteio == 0 || sorteio == 32767)8 L  h9 @2 \. o. a5 @4 F1 L
return ;
" v6 F9 w; ]7 K8 e! d) ]7 w  sif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy2 ]$ v' c7 e3 k6 a, I6 |9 d  h
{) o" M; t1 t* `, C
negocios.Buy(info.LotsMin(), _Symbol);1 E. b: e" U) Z  g: b& z5 c
}6 _3 o: V8 k2 ?2 q) i
else // Draw rule 1.3 -- odd number - Sell3 u/ R! Y2 ]. ~' @1 Y
{1 V+ x# B+ _& q5 t* P* ~
negocios.Sell(info.LotsMin(), _Symbol);
" M: M& Q% h" F1 |. }2 V}3 u/ x0 j* q; }5 q' `* {9 X
}) V5 C4 E' W/ @) r" h/ x. T' q- M2 N
//--- Check if we have a new candlestick...
, \, A) t( C: p  C% g: U( ?- {, Jbool tem_vela_nova(const MqlRates &rate). X6 L1 f9 N) f: R8 _% _
{
; i2 c' V0 ^" V# p/ j3 k2 \{
  e) w# F! _: r: Y0 W. ~$ `2 U- Fret = true;' B1 M0 N$ I, J" H3 z! h% _
close_positions = false;
/ y! t, w, U* n0 N: o}( e2 b4 s; H0 R+ U0 }- e( l) D9 }! ]2 O
else
! |: t. x* q3 _0 ^* E( Z{. w. O' h) ]( @7 H! D. z& S
if(mdt.hour == 16)6 a5 E2 t: u; s4 j; f$ E
close_positions = (mdt.min >= 30);% B/ y: O' \6 m5 Z. e$ a
}
, p8 b/ E4 m1 S( o% @' [& n}
" W+ W1 G1 w' J6 areturn ret;
: E, x7 x6 r$ w3 y8 ~}
) D" ]' X- H2 Q5 c8 E4 ]* h//---
2 e7 j" X2 Z* P. }1 U1 Q$ T7 Wbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 W# H  R5 z4 u+ A! a) G# T
{
+ i9 x" ~5 a. t, @! L) |if(PositionsTotal()) // Is there a position?
- p5 _0 E( ?( i2 o1 V6 d{' e& s1 a( o9 u) b8 @% A9 [- Q  q
double offset[1] = { 0 };  w, W+ }3 o# o" U( T
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" g) f" |$ |3 V3 r&& PositionSelect(_Symbol))  // Select the existing position!
( J3 h/ O4 W5 A{
7 n' J! E/ E! M0 @: @ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);1 l, T; _: o* p5 e+ c# p' t
double SL = PositionGetDouble(POSITION_SL);0 j/ C8 A3 ~" y" w: e+ n* I
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));, o( i: d0 u/ B# o. w' `
if(tipo == POSITION_TYPE_BUY); P4 T5 k' p% K- Q% u# o, \
{
. d7 }2 y/ E+ }2 ?5 q& ?if (cotacoes[1].high > cotacoes[0].high)' h, V$ b0 ^4 i& x' m# P" U
{
! |4 n3 S. s- Mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 @0 ~; E' n9 U, W6 j, P" K. dinfo.NormalizePrice(sl);
* c7 o8 a; G' n* I9 z4 V7 tif (sl > SL), {) q) ?* F& t8 V4 B
{
% `! }6 Q5 b1 I! e, _* K, Enegocios.PositionModify(_Symbol, sl, TP);
. Y2 ~: g, ]6 p" `# J}
7 a' N; _9 Q( U# c* o0 w}  [, V/ V+ @4 t7 e- m
}1 C3 k% R$ ]% W' |
else // tipo == POSITION_TYPE_SELL
% b/ _% Y8 {' y& F: t{
" y9 p; J6 {# O1 [% K7 ~if (cotacoes[1].low < cotacoes[0].low)
0 j" H( J. I- n{
) b; ?* s9 v. w4 ~* [, preturn true;/ A  g) i) ]( |+ D3 ~  _
}1 N' P) ^) S0 `. m" |- Q9 C. R
// there was no position
0 S% e% C# l- o' p6 ereturn false;7 w' k0 n7 k" U' z" e! g4 @5 _" l
}
6 n4 q( m% c; F' V我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
- @& b- y( _1 U4 k到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-7 09:02 , Processed in 0.580388 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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