私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA9 ?2 C  d* `2 j; U# F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。2 ?4 v2 G4 ]# P5 X1 [; J
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。& j$ Z% ~  h2 {  w. Q
以下是制定这些规则的代码。
" P1 r0 E, x# Q7 F! M//--- Indicator ATR(1) with EMA(8) used for the stop level...4 N+ K4 H/ p+ d2 ]
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
4 c& Z8 i. X1 X; D9 t( B! `int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
: b4 j4 k. r, A9 s& Q//--- Define a variable that indicates that we have a deal...
) ]# Y9 L/ B4 j6 wbool tem_tick = false;
0 b& S2 Q4 p' c//--- An auxiliary variable for opening a position* P, E  A- F1 E* d
#include<Trade/Trade.mqh>+ R5 U# Z6 }3 S# H
#include<Trade/SymbolInfo.mqh>, T1 C/ t4 {; L) o$ u8 t
CTrade negocios;. N3 I4 u' Q# j* |' q
CSymbolInfo info;2 F6 u& P. v  P0 U9 k
//--- Define in OnInit() the use of the timer every second& }) a: }7 Q5 V! M. B3 K0 S/ C
//--- and start CTrade% ^. |- @; p4 @9 `& y: Z( @0 Y. p
int OnInit()
% H7 M# k5 s. T% n# V9 l; n{
3 x. u2 B* Q/ Q5 ?! K//--- Set the fill type to keep a pending order& |. j. j' r7 B( N" r3 ~1 ~3 ~$ r8 L
//--- until it is fully filled
/ t3 Z  J$ B# s8 H6 M9 @( wnegocios.SetTypeFilling(ORDER_FILLING_RETURN);  [6 P/ K' }. i
//--- Leave the fixed deviation at it is not used on B3 exchange( r/ {1 w! O, G- b. l
negocios.SetDeviationInPoints(5);
& N3 b. v# p  y( \, Q4 y8 \6 J//--- Define the symbol in CSymbolInfo...
$ a& a! E6 j* v9 X% N& @8 _/ Dinfo.Name(_Symbol);
7 ]  P4 b; {+ K5 Y//--- Set the timer...5 v% q  }3 F) _! [$ E9 B1 Y
EventSetTimer(1);1 `% s+ x, q- s& j' ^
//--- Set the base of the random number to have equal tests...
7 p. w' r+ o1 Q2 A* }MathSrand(0xDEAD);8 C/ w8 R1 h1 {, k
return(INIT_SUCCEEDED);
' d, t. D2 x  P" q$ h  K}' \- \+ u# e% Y# ?8 H/ Z
//--- Since we set a timer, we need to destroy it in OnDeInit().: {# F$ U$ w! s" b7 E$ t
void OnDeinit(const int reason): S9 y9 J4 P' B! n  w1 Q! z& k
{
: y0 s1 J3 Z) A1 MEventKillTimer();4 {  {# i$ T+ ~/ V& Y( `
}& k: T: v! {$ h; x' u
//--- The OnTick function only informs us that we have a new deal
6 L- J1 A, N8 tvoid OnTick()/ N# N- Y# `! }* S" h1 g
{3 v) ^" u- F9 O( @
tem_tick = true;
' D9 u# V+ K0 O4 U$ {$ w2 T4 r* \! s}
1 G( x% w8 u- s2 m6 ?//+------------------------------------------------------------------+
" c, p# T* {) d- i: p' c; }//| Expert Advisor main function                                     |
4 m8 Q. s% ?$ g0 B+ c- U9 Y//+------------------------------------------------------------------+
7 o/ ~% v9 T$ ?) [% `3 N3 s3 [void OnTimer()5 Q2 V0 h6 a9 @9 R- G* S
{
7 k8 \' Y* M( i: i. @* [MqlRates cotacao[];4 I2 v9 K8 p7 N) \/ K! _2 c
return ;( W0 D5 h9 _5 I5 i. U
if (negocios_autorizados == false) // are we outside the trading window?8 U' ], u. A. l3 B" g+ k; Q
return ;( n% A' u3 h3 q" x* F# ~! G
//--- We are in the trading window, try to open a new position!2 K& O( C. o  {* E$ x
int sorteio = MathRand();
  {- h8 j( }& f//--- Entry rule 1.1
  J+ }: c! |+ r; J1 a2 hif(sorteio == 0 || sorteio == 32767)+ E: u! A' l  U
return ;
( K; a7 q* G- w, Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
( A4 ^) |9 U2 n- ~: J6 Q/ d/ W8 E{
+ r% p" I% X! i7 |. s3 ]/ _# Cnegocios.Buy(info.LotsMin(), _Symbol);
& j$ e! h+ H9 v0 a0 P8 S5 u9 A}5 H  i* G5 U( d  h  p
else // Draw rule 1.3 -- odd number - Sell  S2 E0 j! b/ i: o5 y2 r# N) v& M
{$ ]. G* P8 k  I. i6 j; F. M8 ]! U$ W
negocios.Sell(info.LotsMin(), _Symbol);& O: |* n7 \3 ?6 G# D, k
}
$ s2 ?( G% T/ R2 ?4 v7 P- p}3 h# G. c3 ]" {
//--- Check if we have a new candlestick...
7 s) w/ Y6 n, P2 ^/ Abool tem_vela_nova(const MqlRates &rate)8 c& A( e" v8 \9 j
{3 C8 T1 p% z" i+ a* c9 ]6 R
{
5 X) Q9 B( m5 aret = true;
6 j/ l9 Z6 ]1 e9 c1 Sclose_positions = false;
; U' g- F1 ~  w6 u0 J: O/ l- s1 y}. _/ N& [; p" u3 F2 U. o4 O* z  [% D
else
& ^. y0 I' X0 @5 {8 g{
9 E! z' Z% U3 k) Sif(mdt.hour == 16)
" {. j6 W% d  n0 M' T& `$ }close_positions = (mdt.min >= 30);+ h6 }" }( x( K# L8 C( ^
}
* K  F$ w$ l6 z  l* o}! K- n9 B- _6 M# v
return ret;
* d6 s- g6 v; F0 d- v+ K+ L}6 F6 G7 [' }$ `! f
//---) m0 r+ M5 j/ Z; [
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])6 v/ h5 G( w9 ]
{
: @4 q3 c" }# g/ ?if(PositionsTotal()) // Is there a position?
& N, _( U9 u# z- M7 {{4 [# M! w' W- l6 ^, c
double offset[1] = { 0 };
! G* y1 @$ h' n( i" R. fif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
* A! h; b7 F) D  M; [&& PositionSelect(_Symbol))  // Select the existing position!
4 r* c+ k) S8 a2 E  ~* d2 W{
& ?& v" W8 Y1 s* l2 LENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' h* A& `7 C1 y: l
double SL = PositionGetDouble(POSITION_SL);- D0 I; E# o9 ]$ D
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# H9 ?' B, ~: h; A& n
if(tipo == POSITION_TYPE_BUY)
. b7 _( E& a3 X, O+ [{
1 [8 \7 J9 T/ m+ {if (cotacoes[1].high > cotacoes[0].high)
8 X( L4 d$ H; r{& o* n; i2 w+ l% V) R# N# M
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];2 Z1 \) A) q1 S# B7 N' R: D
info.NormalizePrice(sl);
8 e) {$ |2 P' W0 y8 q2 X6 [if (sl > SL)
; q- a& B- {: A8 O{
- a; N; E3 \+ c1 O4 Bnegocios.PositionModify(_Symbol, sl, TP);
! W0 }, _2 Q/ r}
! O9 h3 m# Q+ @; y' w# l6 ^}; K/ Q9 r/ j0 M' Q( q) k
}/ J* k& G7 D1 g
else // tipo == POSITION_TYPE_SELL5 I5 Z6 P- L0 O' }  s8 W
{
6 _1 h8 q' r3 |9 Cif (cotacoes[1].low < cotacoes[0].low)$ K% s( l) m2 L) d* E9 {- |
{% I: ~+ k% E! {  s4 }
return true;
$ |" e6 n: L0 J3 ~}
4 |" P) r9 x' R/ ?8 M2 u. l; n// there was no position
, Z3 S( q0 g9 \+ kreturn false;
" O) G! J+ o1 D% W}
) M* L5 X, D. K我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。. T& j9 f$ C8 Y2 F) |
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-16 13:36 , Processed in 0.411462 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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