私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
4 A9 I- @( v6 R# V) }0 M" I; Q在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& T7 p* }/ |" j; v  e: n/ {为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# M6 c0 Q6 \) |
以下是制定这些规则的代码。
( G8 }& I' S/ e; r0 q* S//--- Indicator ATR(1) with EMA(8) used for the stop level...
( o* N/ ^; s; b" h6 Y1 b+ p9 wint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 K. m$ L" [+ w' s/ o6 _
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ c) O" `( e* r
//--- Define a variable that indicates that we have a deal...7 a2 v  e" Q$ q
bool tem_tick = false;
0 j! m& i. A2 t2 \) C//--- An auxiliary variable for opening a position
4 j8 B& y9 W9 L" R$ {3 k#include<Trade/Trade.mqh>
( Y/ P& w. x, ]; |) @+ E: N. d" M#include<Trade/SymbolInfo.mqh>
* ~5 [0 g6 M" e! f* S1 oCTrade negocios;1 e6 m0 C2 K8 i
CSymbolInfo info;
3 v. D: P+ C5 X, P7 \: H! c/ S//--- Define in OnInit() the use of the timer every second
+ o* c7 F. q! q" t9 |8 Y+ }% ~: |//--- and start CTrade& z8 L. B# k1 e: [' o
int OnInit()
2 o! G$ X" `" `* d{6 S# X" ?' u  S3 x8 X
//--- Set the fill type to keep a pending order/ ^; y3 ~( @0 }" V7 s  u. {  X# s
//--- until it is fully filled
% j6 J& p% Z  W) q; V# o* c% ?, enegocios.SetTypeFilling(ORDER_FILLING_RETURN);( W; @7 l. \% P
//--- Leave the fixed deviation at it is not used on B3 exchange9 h4 V8 e6 i4 x& ?
negocios.SetDeviationInPoints(5);! z- |& a. x1 Y" `) |0 a8 k
//--- Define the symbol in CSymbolInfo...# h, M6 {& R$ v+ K; D+ O. x7 A1 L# m
info.Name(_Symbol);
6 h2 h8 o, Y1 N3 C( e//--- Set the timer...
% k% s8 z3 O5 jEventSetTimer(1);
( d# V: \- @( f//--- Set the base of the random number to have equal tests...
# s" `7 z- V4 R: `: WMathSrand(0xDEAD);' G" R" Q, R/ O* ]
return(INIT_SUCCEEDED);( L1 n! ^/ ^1 u5 ^! x
}
0 o; w# Q+ [8 F; y5 G; ^//--- Since we set a timer, we need to destroy it in OnDeInit().4 w  W5 X4 r/ r3 o" m' o) n
void OnDeinit(const int reason)* P! r. I2 ~" W7 `
{
2 S, R3 S& N- M3 ?2 y( q: bEventKillTimer();
2 ~( v3 l: @, G; z7 O}
) T& ?+ D! V" C, E: O/ H//--- The OnTick function only informs us that we have a new deal
: Q- y& g4 v# J! J- ^% W( b6 rvoid OnTick()
+ L* ?6 {) q6 ]2 |+ n' y{2 q: a! o( ?4 y" `% L
tem_tick = true;
+ T$ j& r8 |5 {}
) [! D) j* z+ M( c1 s# Y//+------------------------------------------------------------------+
3 s; u% b$ t& W* Q, q//| Expert Advisor main function                                     |
# m4 n; I2 V1 Z; G. E//+------------------------------------------------------------------+
0 b* b9 {: ]# I9 S6 `void OnTimer()! F% A, V. Q/ H) {3 X2 w7 B5 W
{1 u+ j* x3 x% K' h( v- j- a
MqlRates cotacao[];
& q- ^" R' [: V% R: B& ]5 lreturn ;
* c; N& A% a. ]; ^2 Kif (negocios_autorizados == false) // are we outside the trading window?
  s$ ~( ]1 V! P- N: N1 L( d) {8 F) Greturn ;+ |- A! j2 ?% `
//--- We are in the trading window, try to open a new position!# l0 A* ]! c0 c; h
int sorteio = MathRand();
4 v6 a  Z# u& Q6 x5 G//--- Entry rule 1.1
) H' [" N3 Z. {) h' Y7 Yif(sorteio == 0 || sorteio == 32767)
% n% t4 \9 G: Greturn ;$ z3 f$ a$ i9 i+ D- Y" ^* e0 n
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
2 ?0 W7 H( P. U+ y* l" j{
! H% D6 Q7 B* ~. n, F% mnegocios.Buy(info.LotsMin(), _Symbol);
( j) U1 b  m+ f+ j4 B}
: q1 Y4 ^+ [4 A/ j7 |' `# X: Qelse // Draw rule 1.3 -- odd number - Sell9 G, p, j/ }+ }9 E$ ^/ I  a
{; D- I- @( K5 C
negocios.Sell(info.LotsMin(), _Symbol);4 R# A; q6 n% V. h
}
' u. P! }) ]" F: P) K  u7 L6 \}1 z7 a* H; B( g; i2 N/ a+ O
//--- Check if we have a new candlestick...2 O1 q9 F' ?$ U+ I/ L. A$ Y
bool tem_vela_nova(const MqlRates &rate)
1 g4 @8 N' f; u/ Z7 c# o# T{1 x' Y4 X* I5 l
{4 Z7 {4 d+ h7 V( ]) X
ret = true;
& W4 v% z& B4 Y7 e, g' Dclose_positions = false;: N; Y1 ?) Z6 C! X7 |
}
. T6 r9 L5 T2 e$ w5 Gelse
, r( h( z8 s! N{
9 O* {: d" o2 T4 |. m3 C1 Dif(mdt.hour == 16)7 D0 ]6 `7 x: a
close_positions = (mdt.min >= 30);
  d6 q/ n  e! w' t! R}
, R. y2 o5 C* r; f}+ l/ p+ u) O" E4 X2 h
return ret;
& X3 i( P8 C+ R) T1 o}
  G# \5 M  z4 u. V3 |0 {3 N//---
# Y  T- I  I7 t0 s1 K  ?bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
/ [* u* [! h& t" G2 R8 G, ?( C( H{
1 U8 L7 {+ D' L9 Gif(PositionsTotal()) // Is there a position?0 `' B# g/ I- R8 R6 X7 V) @6 i
{
, ~& a0 G* @8 y6 I% Edouble offset[1] = { 0 };7 ?6 Y4 I* U0 x& r: P
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?; U' H+ b' C, W
&& PositionSelect(_Symbol))  // Select the existing position!) {! _' m7 i  P1 n& ~
{( `; N: l5 f3 J
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 M: y+ H, q/ [* f4 {
double SL = PositionGetDouble(POSITION_SL);
, O8 p! m7 O. P8 Cdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
+ ]5 R( \& q! uif(tipo == POSITION_TYPE_BUY)4 k$ Y+ {  w4 ?
{1 G' T5 v8 d$ F' H7 @& R% ]8 f/ j  v* h
if (cotacoes[1].high > cotacoes[0].high)) g$ b3 v6 }1 ~, F
{3 _# ^3 c) g& P1 d3 \! [! C! H
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
# n/ ]' z* M1 M* t3 i$ p; {info.NormalizePrice(sl);
/ |: F/ z9 z3 Z' T3 q4 Xif (sl > SL)
! L, K7 D+ G$ V/ W3 E% G{
0 i. z  _2 t3 ^& R3 Y% qnegocios.PositionModify(_Symbol, sl, TP);. T3 s( C, D& k0 u0 V
}
+ w9 O3 [, ^$ q( _0 j5 x}. q  D' [* v9 Z4 J% g% l( @" w7 r
}  n1 O/ R1 z0 l/ B  y4 N  P
else // tipo == POSITION_TYPE_SELL0 Q9 u# ^8 R% E2 Q' Z/ A; L; B
{
8 p) N' N3 J0 v% r( B& s& v' pif (cotacoes[1].low < cotacoes[0].low)
1 l# o! ]7 h9 R{1 P5 ?2 S6 J% x" Y
return true;
; G( r0 M8 T5 h; A8 ?1 t! x}+ `' d* R1 h% U' z6 i1 n7 B  q: o
// there was no position
5 h& ~  U4 C; }- @return false;
1 b* B2 r  e2 Y  @+ j4 p% m7 V8 Y}9 U- K" k8 l. h  ^, v& s
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。2 p4 G# n. R) k3 A2 P
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-2 21:13 , Processed in 0.837788 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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