私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA0 M  t( s7 e1 a; F7 Y- c, X
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) |" S1 f% p: A- j' J
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。0 H4 ^: b) H# ~- v5 Q- Q$ f
以下是制定这些规则的代码。) B! i# D5 f; A. S
//--- Indicator ATR(1) with EMA(8) used for the stop level...
$ u- t/ z0 L" U. g% z+ n* ]int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, ?! u1 C& b; |* z' k$ O* _int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);( e( R6 ~. e* L/ J1 h' A3 ^1 `& o
//--- Define a variable that indicates that we have a deal...
2 q! B% I9 T& X8 k, I7 V1 Wbool tem_tick = false;4 `6 M, v/ a* y8 u
//--- An auxiliary variable for opening a position
2 z  Q$ J$ ?; P#include<Trade/Trade.mqh>9 _: L" g3 b: {; o: o
#include<Trade/SymbolInfo.mqh>
$ W2 B$ H1 t7 C3 DCTrade negocios;- e1 p4 B+ Z5 f6 X8 q' L( D+ G0 b
CSymbolInfo info;
6 U" g0 M% ?6 J( ]6 ]: C//--- Define in OnInit() the use of the timer every second
( p9 c' B! n/ D. j//--- and start CTrade# b; q3 B' |: F
int OnInit()6 O1 ]" k) S% X- @
{
) o7 N! o* ?+ U3 l//--- Set the fill type to keep a pending order
) n9 s) S) O9 N/ ~//--- until it is fully filled
' x% I9 a4 B+ J* L5 Cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
; e$ `+ Y! D+ \: y$ Q: G' N//--- Leave the fixed deviation at it is not used on B3 exchange7 W  X' G( s# n/ G: n
negocios.SetDeviationInPoints(5);
8 a1 P+ _2 u* p+ ?) t. c//--- Define the symbol in CSymbolInfo...
: {/ N6 l9 a5 f, @  linfo.Name(_Symbol);. a- b5 W; P0 C
//--- Set the timer...
! C4 d! w- n4 X! m4 V+ }9 ~EventSetTimer(1);' d  }, m- p) a  m: \
//--- Set the base of the random number to have equal tests...
& E$ z2 ]5 a5 A) gMathSrand(0xDEAD);
+ X5 d9 {- F/ ]0 f/ lreturn(INIT_SUCCEEDED);5 b# D! J# b4 ]
}) y  q3 |0 @' `: V6 l* k: z
//--- Since we set a timer, we need to destroy it in OnDeInit().
. ?+ n7 M; a# s" |void OnDeinit(const int reason)  S  r* N1 A: Z) G% g: d
{* G+ H, k7 o; i7 Z" u
EventKillTimer();6 P! E5 N# l# u/ w. i8 c
}* w) ]- e9 {4 y0 X) {
//--- The OnTick function only informs us that we have a new deal* M3 K" P' [  W7 [* H/ Q
void OnTick()$ p% ^3 _9 z, V4 \( M4 }; Q8 ]) T9 ^
{
- y6 O# G0 F" p3 V0 ^tem_tick = true;
" S+ u- X% m2 q9 g- B3 H4 K: i$ Y+ Y}6 b( k" G+ E- o" @1 n" S
//+------------------------------------------------------------------+# u  S- F8 V3 B' P9 I
//| Expert Advisor main function                                     |) Z; g' W$ F: B5 m" k  @7 z
//+------------------------------------------------------------------+
8 s+ B; e3 ~) o6 H0 T& Xvoid OnTimer()! ~, s& _/ V) N: o+ b1 y8 `
{
9 R3 k  b$ G4 [- T9 kMqlRates cotacao[];  u/ `( A4 ?; W* B8 c
return ;
5 I: }& `- X  _& h' Gif (negocios_autorizados == false) // are we outside the trading window?
/ u, s+ m0 V; o! V7 d0 j, O7 ]return ;, T# J8 S% Q1 z, S4 |
//--- We are in the trading window, try to open a new position!
) ^' C- |1 R9 j$ ?$ rint sorteio = MathRand();
8 M: G( }$ C. g//--- Entry rule 1.1& I" |& {& H* O& d# g, ?
if(sorteio == 0 || sorteio == 32767)
3 u0 j5 j5 r0 j# Zreturn ;& v0 ?8 s  \1 L0 o/ o7 i  B5 [6 w
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 {% H$ [5 S8 f3 E: D0 h
{
- o! q$ H( M# K( z8 o* }/ Qnegocios.Buy(info.LotsMin(), _Symbol);
2 m  s" U$ B3 `}
' B6 l4 r' f  relse // Draw rule 1.3 -- odd number - Sell
5 P! F; ^8 r* {6 f{; s# j* [( e0 w  W9 o2 Y; x; E
negocios.Sell(info.LotsMin(), _Symbol);
, _9 A( \! n8 S+ W- E0 [, l& I}
7 p- n8 I7 e; b  y+ f8 b* k}
3 s$ Y) j( {$ t* a& k+ Q& j9 ~/ o//--- Check if we have a new candlestick...
  i7 h/ e: ?. q! H+ [% l* B  Y" y7 Ybool tem_vela_nova(const MqlRates &rate)$ {6 b2 y3 a- Q' z8 \$ A
{
# P! M, T8 h$ v, ^3 l. w{8 r$ y  ^0 r( ^! q, I
ret = true;5 @' E6 a( b& x& M# b9 c
close_positions = false;
/ Z+ Q6 t! R6 O6 O}" A  R$ l  o, m
else- v% t! z% H% J! V' o0 o
{' t/ s: Q$ z! Z9 Y6 r( M
if(mdt.hour == 16)
" B; m; {( R! p' n5 g& Jclose_positions = (mdt.min >= 30);: @# ~3 I5 Q% c
}, U$ W: t, R& _# ~
}
' j  j8 s: P4 S: D# u' D7 Preturn ret;
( T- ^& \& F3 D6 B}
& P- N0 E4 J, p, u//---" _0 b' p* I& [9 X8 [! P8 h# f
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])% L6 S. [4 G8 }7 a. x4 M9 C
{
  V  E5 r/ ^. u3 k, W0 cif(PositionsTotal()) // Is there a position?
8 O4 A/ h& N/ ^# ^3 g' ]{
* l% j" X+ H2 W. P; g+ Odouble offset[1] = { 0 };5 p, h) F5 S! Q* A1 c: a0 F5 E, t$ t
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
, L& `1 F& \. M/ Z  r&& PositionSelect(_Symbol))  // Select the existing position!3 e: N+ o  p2 F& l! w
{  M" \( u: Q0 Z
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 j9 y/ v, K# Z% u# k, |4 Edouble SL = PositionGetDouble(POSITION_SL);0 A* C, O! o7 w8 k- @' x+ b) Z
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));! P5 o2 A* w/ s4 @9 H3 o6 M1 P
if(tipo == POSITION_TYPE_BUY)
& A$ P* q* J0 g* ]{
( @) ]1 z5 v6 z+ m0 {. `if (cotacoes[1].high > cotacoes[0].high)6 S9 D9 Y- M0 O+ `$ j
{
% ~* c! ?5 p. W1 l; Bdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: C5 G/ |! X7 Q% E- b' i6 m
info.NormalizePrice(sl);  [. K% E9 Z' x  D' _/ V
if (sl > SL)7 p& T* d( l, D- G
{( h% Q- q( m, z- ^7 R
negocios.PositionModify(_Symbol, sl, TP);
! p/ _) N2 p, F9 I}
& x$ Z* p9 B& _4 h# L) M- k}
3 C7 Q* B1 ^! j* ~; }* d}
6 v" i+ t5 `8 f1 i: Jelse // tipo == POSITION_TYPE_SELL3 t  }& Q; R# [( \  u
{& c+ n; H% g, _5 h5 }# _3 M1 I
if (cotacoes[1].low < cotacoes[0].low)1 l* c4 D2 w  Q7 ]
{' Z! r4 `9 t; s4 W
return true;
& n5 Z1 K4 h* D( R/ V- k- @}
+ d0 @. Q2 o- U% x// there was no position
6 O% M& Q. ]8 r  O0 J* @return false;
9 T. F  @* [! e! J: k}
8 h! x1 S$ t. ~5 z! v9 G: o4 u我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
2 a: V# q+ B( Z$ x! x到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-19 15:57 , Processed in 0.425574 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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