私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 }0 s# E+ C$ p6 F7 u7 U* ~
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
& e7 X0 t9 T% a5 D/ k为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。+ b% M" y- [3 G6 T
以下是制定这些规则的代码。
- U, o$ N# x* i( @//--- Indicator ATR(1) with EMA(8) used for the stop level...0 S, @3 M' l9 V9 k8 \0 v$ C
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);8 P' s/ N$ D. m+ O$ [/ ^6 l' `, x; Q
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ |$ B$ C6 P0 q4 N) r: \) F/ E5 v0 ?
//--- Define a variable that indicates that we have a deal...
" P/ L/ c) a6 k2 g- Gbool tem_tick = false;% X4 N  D+ P0 \1 L- P
//--- An auxiliary variable for opening a position! v. @) J$ I) p* p, s
#include<Trade/Trade.mqh>
# i+ X2 H7 F5 P( @+ Z$ |# A8 e6 Z#include<Trade/SymbolInfo.mqh>
( ]4 m$ {; ~. G% V" X5 z+ h1 FCTrade negocios;
; A6 ~$ w9 w  H  |9 f9 w; G+ e- k; WCSymbolInfo info;
6 ?6 s) m! B: h4 b0 t, w//--- Define in OnInit() the use of the timer every second% u# Q6 {2 Y# \. h* R, T+ F& p+ `
//--- and start CTrade  J+ P+ A( ?" Z3 p" l1 O
int OnInit()
' b* L8 K/ g8 W' m2 }. M8 b# E' s{
5 o/ q/ M1 W8 ~$ X0 U2 T3 \2 {' L$ N' W//--- Set the fill type to keep a pending order
% e4 @( @, X3 L4 V/ F+ \% W//--- until it is fully filled* n1 y+ l% v, @0 o0 P# p: g
negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 D  t- U3 U' ~) s& G4 D; V2 G, y
//--- Leave the fixed deviation at it is not used on B3 exchange- X+ T3 a& m3 l8 D5 T0 ^
negocios.SetDeviationInPoints(5);
1 M: n- |! E9 f+ `//--- Define the symbol in CSymbolInfo...+ V: u! G' C6 p1 `, Q1 d, I
info.Name(_Symbol);) E  u) x- J- \: x9 u* D
//--- Set the timer...
4 r5 V+ N0 h3 n- F! R8 ~EventSetTimer(1);
- _% r1 i+ L# |0 a2 F$ l1 r//--- Set the base of the random number to have equal tests...
: e* P7 ~, E2 p- b2 G. pMathSrand(0xDEAD);* k' c' A: a" S3 F0 S  E8 f
return(INIT_SUCCEEDED);. t1 t4 e& u1 a6 G2 L
}+ \+ V3 s, p9 @/ j6 k3 i
//--- Since we set a timer, we need to destroy it in OnDeInit().2 v; U7 B7 j2 v% m) }
void OnDeinit(const int reason)7 e7 @! _, x+ I: m! Y
{3 W1 U! w( X( t5 c
EventKillTimer();/ c' s/ M% }9 G) V5 C& Y7 o0 v, `
}
% A. n9 ~! k2 b& {' y- A//--- The OnTick function only informs us that we have a new deal
( Y# R3 R+ j3 l3 Mvoid OnTick()
+ m, T) ?4 X' [: u5 C{( Y7 l; D) n$ Y! ^: B" |9 x
tem_tick = true;; p  I1 g; m) ^) q  l
}8 K" F+ Q9 N4 [+ A
//+------------------------------------------------------------------+, h% C8 y: j9 R, Z; T5 a
//| Expert Advisor main function                                     |
% l: x  E. R* Y$ e//+------------------------------------------------------------------+
$ o' x" n& E% M$ R! Nvoid OnTimer()7 P  k- X. ?# y  ~
{& b/ C1 ?* s0 z- k2 y! R/ t
MqlRates cotacao[];
( A, ]4 Z" Q" w" N5 w5 Sreturn ;
8 M, l4 o/ {' J! T9 `if (negocios_autorizados == false) // are we outside the trading window?
+ m1 S5 X2 j# `' ereturn ;
! q, t' `, {. V) k% @//--- We are in the trading window, try to open a new position!  G. r" e2 _. x0 P7 u
int sorteio = MathRand();) o) y! \) [( H3 Q0 [* x( A9 Y
//--- Entry rule 1.1. h0 `' ~) z2 v4 C8 S: i
if(sorteio == 0 || sorteio == 32767)1 r+ F3 l/ a/ |) f$ h' O
return ;. @  x6 `  W* P; P- w2 g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
& Y6 K5 r" h" y7 u{5 k  ^% ?2 ?2 `% a, R9 S
negocios.Buy(info.LotsMin(), _Symbol);; c' Y8 v5 a2 `. Z) N
}  R2 z4 M1 ^( s- Q( o4 Q# v8 j9 {
else // Draw rule 1.3 -- odd number - Sell# B+ z& x& K! c5 {" K4 R  j
{& l' W; A; j* l  |' i. s
negocios.Sell(info.LotsMin(), _Symbol);
! v# E5 K5 ~! @6 W% ~# t; v! M( k! f}
1 F: @9 m" F/ }( i3 x, i6 a}: S0 w3 C; l2 o+ u  F
//--- Check if we have a new candlestick...
( v0 ~: ]6 S( M; H: z8 L- ?bool tem_vela_nova(const MqlRates &rate)
/ V. G! b5 K& x% I: g2 ]9 R{
7 E: v. [! r- ^6 Q( q! w{. G; A- M. A  E4 c( o
ret = true;
+ g6 T& u9 H  D2 C  Tclose_positions = false;  W, m/ O( A7 m4 M6 J
}6 N% f+ i- A9 Z% G& V& l. V- b7 V
else
" B1 H0 }8 w6 ?$ Y. A* V4 v{
$ S( I" f- l6 u, m1 jif(mdt.hour == 16)5 g# V0 @. b- ~- Q: \
close_positions = (mdt.min >= 30);0 q# y& G# r9 g& O  i% e7 E
}( M# u* Z% ]6 i$ o0 @8 Y
}
8 d- l2 I) m$ e  @( Yreturn ret;
8 N" x) B- }- H1 k: `}
: n0 K* {3 y, W  r! k, m6 u# S( N9 F//---
' f, O0 y$ R% H3 o$ K; e) l# F: pbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- A8 X% p, M9 @{: R+ s# c/ f, L% A# @
if(PositionsTotal()) // Is there a position?
2 S, F0 q, \) D, Y" a- w% R{
& b( o, {; F; f! q4 Q* N, d# X9 Mdouble offset[1] = { 0 };9 x4 ^4 V) d1 ]
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
/ S9 i( q2 B- k&& PositionSelect(_Symbol))  // Select the existing position!
1 t& S! r! ]. |: L- l& n. S{
# K' x1 ^& I4 h/ L% I& w: O" |ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
  N+ Y2 n) j  }# `, I/ g: {double SL = PositionGetDouble(POSITION_SL);
4 [& O; s1 m5 T3 u7 `double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
" h/ @/ z6 |/ o' c. O" y+ }2 R# ~if(tipo == POSITION_TYPE_BUY)3 x/ A" [0 C3 s) k2 b2 [# P3 A
{( k. O/ w; I1 C4 n0 N3 W
if (cotacoes[1].high > cotacoes[0].high)
: x3 d: T4 W- k) @# T/ t- D5 j4 M: S" Z7 g{
: v& [1 F( w$ M& f+ ^6 mdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
* z5 x- c+ a3 y- zinfo.NormalizePrice(sl);  X7 Q. i5 v& d  E5 D
if (sl > SL)8 V' h5 ~' f9 w9 Q* o* v8 C
{
' {) w# c8 b( W: {( znegocios.PositionModify(_Symbol, sl, TP);" @( Y. B$ G& t  s; o3 x
}
( f" _( _1 \1 z1 ?2 [. V}7 s9 b/ C1 e2 |& ~6 o: K/ @# X
}
) K5 f" _+ H& b, gelse // tipo == POSITION_TYPE_SELL
3 H; q$ C% z5 P# X* U0 W{
$ y6 s  I1 l9 w; Tif (cotacoes[1].low < cotacoes[0].low)
1 h( L- I9 B1 o3 T$ U6 o* |{
8 w) N0 V1 \, o4 freturn true;7 A3 ?6 u2 z+ _$ O( m0 K* t: W
}' c2 J' H  c; w. J7 K. k$ C4 I: V
// there was no position
/ e& C! t( t4 Qreturn false;
% y& P8 G0 m& _4 P7 J4 Z7 k: I}) b3 n" E; Z1 j+ }; w6 J" q
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 i5 ^* s0 b! O; ?9 A1 ^' n到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-26 21:02 , Processed in 0.828692 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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