私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
( C5 ]  F8 a; }+ t4 F4 O' ^( a, I在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
+ Y4 Z* ]. C3 _/ l, f  W- g为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。- l* c7 o  J7 _, K8 F! Z% q
以下是制定这些规则的代码。/ j5 h& q6 ]; S
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 c' C' E/ E% Uint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);' `3 n3 H! p; Y* w* _$ n5 @
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' h7 K( }+ B8 }" A- q! R2 C
//--- Define a variable that indicates that we have a deal...1 \; E4 I, k; c! Z% A4 z
bool tem_tick = false;
" O8 c5 O# P! @2 z1 a" @//--- An auxiliary variable for opening a position! B! P, I7 }# S" |! H- u2 P
#include<Trade/Trade.mqh>5 A. N9 a5 A% t6 R
#include<Trade/SymbolInfo.mqh>  R+ u2 e! W& F& D
CTrade negocios;3 Y- {/ P8 {# G) O: p* O
CSymbolInfo info;
5 |9 y* D1 \0 a) c//--- Define in OnInit() the use of the timer every second0 X+ C: v" I% t
//--- and start CTrade
( V( Y" I6 ]3 Z+ P6 B) Oint OnInit()7 Z; H) x9 S: v; Z% g/ a$ U
{
- t4 j+ l. T% \6 y9 r; }4 C//--- Set the fill type to keep a pending order( M- H( N$ \$ J5 j
//--- until it is fully filled& H) V: o* y. W! M9 x9 c0 j
negocios.SetTypeFilling(ORDER_FILLING_RETURN);9 h& w1 k5 x5 ~' m
//--- Leave the fixed deviation at it is not used on B3 exchange/ I# [5 O# r+ a
negocios.SetDeviationInPoints(5);
( }) r; J9 V* j  _+ }6 o$ g//--- Define the symbol in CSymbolInfo...
8 @' P$ V  q" C( e9 \, }( `% G! Linfo.Name(_Symbol);0 W; m& p! `- G6 A
//--- Set the timer...
" `" ]! [+ [! LEventSetTimer(1);5 @4 g0 w" B5 M* k" p+ q( n3 A+ i
//--- Set the base of the random number to have equal tests...: Y; T9 G- v/ _" [3 g2 W& m
MathSrand(0xDEAD);
( g5 A' q( e/ x0 I( ]2 f0 G) U. Xreturn(INIT_SUCCEEDED);
/ U1 x! r8 ]# J6 j6 [- u: s}
2 [% }3 V" ?; R//--- Since we set a timer, we need to destroy it in OnDeInit().
, f2 Y2 m% X/ V% B. ]4 ^- Bvoid OnDeinit(const int reason)1 I  P+ A$ j( [# e4 |. B
{
" i3 q. j7 V+ l" Q" |3 m2 QEventKillTimer();
$ H. E  J9 J& |4 n) f5 l( [* B- x: o}  D4 j0 i5 x( z5 C; K
//--- The OnTick function only informs us that we have a new deal- h, q. m5 ~9 S$ ~' l6 n3 b7 p
void OnTick()
$ b1 K* K5 o! `3 D8 j/ ~{. U0 d4 c' @+ _/ g: c. l
tem_tick = true;
3 T  Q+ Q/ s5 \+ s}
& ~& Q* P% c, z//+------------------------------------------------------------------+/ _' M6 E9 ?: A, I, K
//| Expert Advisor main function                                     |& A; B- d$ T. q1 Y6 w
//+------------------------------------------------------------------+" o9 |& ^( i- Q! e- X  E+ |9 {
void OnTimer()
- P3 |) D2 M" L3 @# K{
* _% S0 j1 O; kMqlRates cotacao[];# \: h, c1 w5 q, \" y( ]
return ;
, K2 i; {5 S/ j/ ^( y* z. I0 \5 T3 cif (negocios_autorizados == false) // are we outside the trading window?
9 {' u, a) t+ w1 U) C" {% breturn ;
& C7 w/ w# B* Q//--- We are in the trading window, try to open a new position!
& j. K7 P; u/ N6 H; H$ R- {% L$ e1 ^int sorteio = MathRand();7 u3 K$ g; X' x( `. d6 U, E  `6 P3 Y
//--- Entry rule 1.1
$ v" H) f* S: ~' y1 ?7 d8 Nif(sorteio == 0 || sorteio == 32767)1 G: j+ K5 y. M5 h: T1 F0 p
return ;) r1 {- r) ?) b( M
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy0 x. i2 y. G/ m  s. ?
{  U! q: Y% A4 Y8 O+ s% F" H# n
negocios.Buy(info.LotsMin(), _Symbol);+ P; Y& ?9 [( R
}1 G+ U% q: y% s& m& W9 F
else // Draw rule 1.3 -- odd number - Sell" g, y9 S+ R8 n* D5 W
{5 O2 V& f$ M! l! |8 t8 q  q& {" q
negocios.Sell(info.LotsMin(), _Symbol);3 z& s8 A" R) [: |
}+ K  j/ q* ~7 I5 t* t' D$ v3 ~
}
# u6 w) o( A+ ~4 W  M//--- Check if we have a new candlestick...
  I: D7 M# G6 o' O- zbool tem_vela_nova(const MqlRates &rate)
( G0 j$ C. k# `' X6 u$ B{. j( q$ `' s4 I
{
* J3 w% I7 K3 Eret = true;) d; H$ B: g2 [  |
close_positions = false;8 P6 ], W) P  Z! B7 c2 @
}
4 |3 F8 g( }& }, c3 _5 Celse
  r8 i3 |) R& T1 J$ c* B' u{
3 J7 \8 t2 g4 oif(mdt.hour == 16)/ K) I! H  B6 h4 C6 B+ |$ r& m
close_positions = (mdt.min >= 30);
+ i7 e; l9 I2 O5 v$ ]4 B9 J( e}3 X& b# I' G8 b/ f4 b6 D! x
}1 ~5 L) J+ E! e; i( a9 e
return ret;5 P& l: f' o0 G/ i6 F" H
}
, r' [  D+ @) g+ w1 p" P0 z//---  J$ B+ _5 @1 m8 y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" d, K; a) D  f0 g9 e( K{7 `2 B2 v2 }8 g
if(PositionsTotal()) // Is there a position?3 G6 c' C3 i0 D# `
{+ ]; u0 Y( e/ O* k$ q
double offset[1] = { 0 };7 m( J4 [& Z1 {- s: P: B, N
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
+ S1 c) M: U: m& R% T1 W&& PositionSelect(_Symbol))  // Select the existing position!% D. X- J% Z/ ~4 K( V
{
! Y  P( R) p( [7 A: x* JENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& m9 }, D9 y, }double SL = PositionGetDouble(POSITION_SL);
- U6 j! x3 @: M/ Gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));" c% E6 m  k" H
if(tipo == POSITION_TYPE_BUY)
8 Y: M) i  {, H! o  U{
/ d4 W* s- v, C6 Y$ dif (cotacoes[1].high > cotacoes[0].high)2 M7 D4 T7 \% C
{! ~: m0 h, V2 }$ W
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
( _1 F) M+ j: p3 ^7 Y7 A8 T3 cinfo.NormalizePrice(sl);
6 E( _4 k5 O% t# O: P9 n7 ~if (sl > SL)! \! x9 J. b, |5 x" u4 Z* \
{
' L4 [0 C) J2 J& Q+ ]/ `" Xnegocios.PositionModify(_Symbol, sl, TP);
( D5 }" M% E/ G# l}. [- H% [+ z6 c
}9 t# I9 I) N$ T5 ^3 ^: p8 }, j" f, @
}
, J; J, g, }/ F2 a7 U  r# delse // tipo == POSITION_TYPE_SELL
: k; Q1 J& d* x! n8 V  T{: b& h9 r) @& V0 x$ X8 d
if (cotacoes[1].low < cotacoes[0].low)( V; h  f7 u& S7 A0 e
{
5 B4 E  y2 P2 M; M( ~( ^return true;
/ R* M5 \' T- b$ x3 c}0 @- ^! i' n2 H& R" W9 B. I# P' L
// there was no position
+ Y0 f$ G" _1 B/ Q) S! dreturn false;
# p. _5 V% P! B}8 r! w; ]* j3 a' l" o) q/ Y
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ \2 O. j. y! w7 `2 q+ j4 F( F/ R到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-22 06:44 , Processed in 0.726846 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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