私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
2 U" l1 |; n4 h  k6 ]8 F3 M在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# N. }  m! B! m4 a) F( U
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
* I2 G3 @  W6 ^: h6 P, i% [* ~. i以下是制定这些规则的代码。
& [2 i: A. c7 i8 E/ t- [6 w# k//--- Indicator ATR(1) with EMA(8) used for the stop level...7 u& _8 F/ X# Q% Z) P5 u
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);: a3 K& Z0 r  `7 T! m0 I! v
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);3 N3 [2 i4 D- X1 j/ D
//--- Define a variable that indicates that we have a deal...0 G# P; k. ~" ?& ^! y: G
bool tem_tick = false;
' ]. G/ s. l' q2 m//--- An auxiliary variable for opening a position
, o' C& G, J  r  ~% D9 Z#include<Trade/Trade.mqh>- X9 K6 Y/ C' }/ h& _- f
#include<Trade/SymbolInfo.mqh>) [" w+ m: ]8 P. i7 L
CTrade negocios;, P6 i0 n. v4 E1 a2 H
CSymbolInfo info;3 n+ p- |5 a7 ^+ s
//--- Define in OnInit() the use of the timer every second
* K5 M3 o0 e2 [2 {//--- and start CTrade
. f+ G6 |% a4 I  Jint OnInit()
9 `  Y% N, {( G$ [: W  A{
) c6 _/ r/ Z  V; z8 U//--- Set the fill type to keep a pending order
$ L3 X, {4 h0 i( ~, x9 d$ [, I//--- until it is fully filled7 Y8 ^% f% t6 q; w2 E: g1 e2 o
negocios.SetTypeFilling(ORDER_FILLING_RETURN);3 r, e) n2 |# J/ I
//--- Leave the fixed deviation at it is not used on B3 exchange
: K& L5 {2 X8 y& v8 J! h/ n0 |negocios.SetDeviationInPoints(5);1 r( ]. T1 x9 \7 X
//--- Define the symbol in CSymbolInfo...& `5 r0 A: g( \( c7 K
info.Name(_Symbol);7 @, v& D; l# I; [5 o9 j- q
//--- Set the timer...
' Q) [& Z7 K0 m+ N$ J, N3 xEventSetTimer(1);: Y) q: u9 ^: f5 S
//--- Set the base of the random number to have equal tests...! s8 w2 O2 O: b% Y. B, ~7 M* S
MathSrand(0xDEAD);% r  y7 F- a, h& c' T2 L! u3 Y
return(INIT_SUCCEEDED);& }0 L) k1 @. A4 d) _0 U' @* D* B4 P6 ~
}
& q  c! ^  Q( z//--- Since we set a timer, we need to destroy it in OnDeInit().
) n* u+ ?3 B; k' Y+ G. V! f$ Vvoid OnDeinit(const int reason)
  h% S; v4 N: e{
' ^1 R+ a7 t3 ~% m6 m( |5 k) tEventKillTimer();
9 R2 ^2 \2 i+ b9 `, j4 G}
' H& S* X8 z3 _9 X+ |//--- The OnTick function only informs us that we have a new deal7 Z( g5 l3 Q0 Z6 B& a
void OnTick()0 r1 n: M2 {  X" U2 g
{
+ Y/ ~) E- ?, ]0 l* Vtem_tick = true;
/ b! C; d& @) f- R+ U% P2 x}5 |! G+ O, E: i1 w1 j/ d9 o) s9 G2 |
//+------------------------------------------------------------------+7 ]! S7 c1 F* u
//| Expert Advisor main function                                     |5 A- @$ W# D' x( b
//+------------------------------------------------------------------+
: Q1 P3 K* `) E7 E" avoid OnTimer()/ i& u' y9 t* ~9 D, @
{
% T: Z- M4 y1 {9 S; F6 @7 I$ Q$ VMqlRates cotacao[];- z3 a8 D4 N1 \; z0 K0 ]9 `& K
return ;& Z3 Y4 X5 j4 E6 u
if (negocios_autorizados == false) // are we outside the trading window?: o) P# k" B7 `4 N2 o. y
return ;4 j$ i' O/ V! t0 }# t" ~: S0 H
//--- We are in the trading window, try to open a new position!
) j4 l$ T) Y4 v9 ?0 G: vint sorteio = MathRand();# m7 f' o$ n0 T  s2 M. o5 ?8 z7 l. S
//--- Entry rule 1.1- ~; p% s. D* e9 i) m' B1 t
if(sorteio == 0 || sorteio == 32767): U/ u. b. r0 B, u, i/ ?1 K
return ;3 D+ t9 W9 b7 b+ ~3 n% x% N& y
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy$ j6 d+ C+ j! L; U" }) N
{
; G+ ]7 o0 j" y4 R; r8 r2 Nnegocios.Buy(info.LotsMin(), _Symbol);
" S+ S3 I' }4 D6 u* |}" _0 \8 {* F: [  R
else // Draw rule 1.3 -- odd number - Sell, g1 n" C& U$ z- ?# w
{- Z( N' B% }: i5 L7 x
negocios.Sell(info.LotsMin(), _Symbol);
/ K3 I' P: `! @( V}1 D% V/ u; X# P9 s7 Q0 ~9 Z/ J
}
' j2 H9 H/ f1 [. o/ {//--- Check if we have a new candlestick..." Y8 V4 r6 w9 Q5 L
bool tem_vela_nova(const MqlRates &rate)9 R" G5 b* n. n% T1 Z. m
{, r+ |4 s4 B' t3 d! P; Q
{
7 |0 W; ]/ |$ f2 K& X/ @2 Rret = true;
5 r0 X, A' @$ P; M9 t" Mclose_positions = false;
& }* Y* W9 t; x/ K1 E5 p}
" n8 Q' F* z. k  V# f; ~else" O; T. K' i5 Y8 c1 j8 e/ M
{
1 v3 k  d3 a2 I% X$ @8 n+ @# V" rif(mdt.hour == 16)% h0 q& d: E5 P; \
close_positions = (mdt.min >= 30);9 R- {  G/ q; |
}
# [+ P7 @" Z, \; u0 _}7 D3 A1 `9 b; }8 }! E
return ret;
2 R2 x) p% f$ I; q" H% v9 O}' K3 ?) U. d( |. W; a5 D" N/ @
//---- {3 I1 ]& w& R- j6 h2 ^8 C1 M- f
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])+ C/ R/ M, R2 U" F
{: K$ c" T: S# d0 e
if(PositionsTotal()) // Is there a position?" ~! `( |& d# L. r$ l
{
# D) X1 _8 Q( y* d# adouble offset[1] = { 0 };
& K* u7 J1 L( cif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
1 E4 H; L  P3 p2 Z% X7 {$ d  }) i&& PositionSelect(_Symbol))  // Select the existing position!
$ Q/ l5 b2 M3 G$ R1 `: _{
7 x+ O. p* {6 J" p. Y8 `/ J! VENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
2 j/ y% |0 p' b/ xdouble SL = PositionGetDouble(POSITION_SL);
) G" y, r+ z5 J/ vdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
" o$ S8 w( O( S# d# k: [if(tipo == POSITION_TYPE_BUY)
; `3 Y& I9 G. L/ T8 E& B) f{
3 c' J) k+ |+ J" y4 O& rif (cotacoes[1].high > cotacoes[0].high)
. ?, X; Y2 g4 F) p; ~; {  ~- `{
3 \+ i% Q% D/ L. }double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];/ w6 q. H2 ^& O+ N& \1 W4 o
info.NormalizePrice(sl);4 `3 z6 n5 h! k9 @1 e. i
if (sl > SL); i  x9 F3 Z7 u! k6 W+ o- y
{
/ ~' ^5 Z2 ?1 H4 lnegocios.PositionModify(_Symbol, sl, TP);
( T" s) S- X: a. ]# E}& U* \; {5 Y3 M, C' H  U
}
2 x; d  B: ?& A+ C! u8 h* G}
4 J& x6 w8 A+ @0 Y2 i3 Welse // tipo == POSITION_TYPE_SELL
9 c' G7 B3 |2 z' N+ m& C5 q{' `& o/ u! b5 E/ w# n, B7 K1 @
if (cotacoes[1].low < cotacoes[0].low)& x* k& c- V4 f: O- o% y3 T
{& C6 _, _3 K/ ?) j. c" K, w. C' D
return true;
- P0 d# \8 y2 m}
9 C9 b, k/ c0 M1 L  l// there was no position' Y5 S$ z- _' ~& u
return false;4 v% U- k% W/ P# ^4 I& z2 `& n
}: _0 O3 J3 d0 ?: e% G
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
3 w" d4 `  w% W* e, x* b到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-10 05:37 , Processed in 0.410110 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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