私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  x' e, B; [% B# O9 B) U
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。& D0 B2 z6 w- f3 v
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。5 @/ Z' T2 C" H1 g
以下是制定这些规则的代码。2 q9 ?' x7 H  w* e* R% b* Q
//--- Indicator ATR(1) with EMA(8) used for the stop level...
, J% h. V" N6 N; ?# i& Fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
/ c: {/ C9 ~# s. ]' Eint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
+ v% I0 J; D$ K; P2 u" c2 U0 [//--- Define a variable that indicates that we have a deal...
* B5 w! V: c$ k1 W7 i0 wbool tem_tick = false;
! E$ K- T  M+ V//--- An auxiliary variable for opening a position
& U0 {6 R  V' O# s#include<Trade/Trade.mqh>
7 n1 \) d0 ~' t! y& _#include<Trade/SymbolInfo.mqh>
( g: x) _' A/ t5 t& ]! R& ACTrade negocios;
, r  R' H, Z* {& tCSymbolInfo info;
0 _& W, w; r) w: W//--- Define in OnInit() the use of the timer every second
' j' d: A! c# O# _+ m+ g* A//--- and start CTrade8 d# d; N3 O, I. R5 q9 ^- j' J' W
int OnInit()
0 d! ]* d4 O7 W  j9 }; A$ Z{% j5 ~, g4 l/ J" @9 b. S
//--- Set the fill type to keep a pending order8 m& C9 L2 M1 E( K
//--- until it is fully filled7 ~, e1 k' C  ^8 ?* s" q
negocios.SetTypeFilling(ORDER_FILLING_RETURN);+ K. t. R, A) d* i
//--- Leave the fixed deviation at it is not used on B3 exchange" y$ w7 |, j% K; C5 K' e+ M
negocios.SetDeviationInPoints(5);
8 U! k; x% g7 e% V! X! D0 x. i( S! q//--- Define the symbol in CSymbolInfo...% N% e+ X5 }; b: d! R- f! a& S
info.Name(_Symbol);
0 k/ V3 ?7 y: P4 d9 \9 y% Z//--- Set the timer...& j/ i, k( H2 m
EventSetTimer(1);6 w0 r. M2 [+ N2 F$ }" Z
//--- Set the base of the random number to have equal tests...
" Z) a0 ~( L( L) R- J/ GMathSrand(0xDEAD);' T& Q) O! K: y4 ^; f  `9 ]/ v+ u- L
return(INIT_SUCCEEDED);
6 u5 ?4 Y0 Q: V5 `5 z! h}' p& }( U+ l, T. Z; D$ i
//--- Since we set a timer, we need to destroy it in OnDeInit().
' _: H1 b, U* D7 N" tvoid OnDeinit(const int reason)
; B" ]( Y$ B- S$ _% {4 g5 R{/ ^  f6 Q9 f! L) {' C, |
EventKillTimer();; u6 |6 W6 e0 r2 h+ C
}3 r. k" K  U7 `5 Q6 E
//--- The OnTick function only informs us that we have a new deal
. {' k; [& A8 b8 V3 cvoid OnTick()' b3 [5 v' y' C0 f6 n
{
  {% f6 W: ^5 `( D' x  Ptem_tick = true;# M  n  u1 o) A. y( W
}. `0 o( a) m0 b6 R& W5 F5 Q
//+------------------------------------------------------------------+- G$ \  d! `* s' [  d$ z( ^  X; |
//| Expert Advisor main function                                     |% ~; {5 ~; `& S# E4 Z% P
//+------------------------------------------------------------------+
2 N4 C3 y8 j( F2 Kvoid OnTimer()
! e* n4 b4 U$ f$ A1 c{0 ?; w* ~7 F3 p/ F- p
MqlRates cotacao[];
" a# s7 q/ q. K/ w3 Kreturn ;' w8 `* p/ I1 @% h
if (negocios_autorizados == false) // are we outside the trading window?* D' S! ?7 {+ E% d3 O- c) c
return ;: A) r$ g' h4 |) p; Z* U6 d2 ~- i9 o  s4 L
//--- We are in the trading window, try to open a new position!
4 d! S& `8 k& o6 K) I6 V/ s( o, Q. @int sorteio = MathRand();5 e- f5 B8 P5 P0 z3 O1 w9 e, @5 k
//--- Entry rule 1.1
9 t5 Q) T& a2 U. b5 r6 Y; |if(sorteio == 0 || sorteio == 32767)% q# Y3 h4 ?& {% V
return ;0 ~+ J: Q$ w+ I( N7 S* |8 b# n0 Q( |
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy3 Q% t# _; b' h! U
{) _( P0 t2 U' q: ^
negocios.Buy(info.LotsMin(), _Symbol);
. c2 i) ?! N- _' u6 t1 ^) \}
' _. \: A1 t( S7 V3 W. Zelse // Draw rule 1.3 -- odd number - Sell- Q1 L/ _0 x  }
{( v9 `5 N) f$ I! f
negocios.Sell(info.LotsMin(), _Symbol);
9 h# |7 E$ ]* @0 s) ~/ Z( B}
9 X- @  |+ c$ l4 L+ m}
) V  Y1 A- D8 T2 d//--- Check if we have a new candlestick...
9 f7 ~5 Q2 H- B& q# cbool tem_vela_nova(const MqlRates &rate)! e% g7 o& ]# }+ S0 Y; Z, O
{+ h3 A2 B- I/ I" ?" d
{
6 Z7 F, P9 |) m( B) p$ m( U. xret = true;
7 r6 A2 [$ c( g; D, q2 y% W  p9 }close_positions = false;+ c3 J& n: f, P) z3 j( H( F
}
8 u. c4 M; r( _4 p% Ielse
& e! ^3 T! y! Q+ B2 y" I3 B{' A) y9 @0 `$ T$ a% j8 T6 j# B
if(mdt.hour == 16)
2 R9 Y8 R* s& p" t9 Lclose_positions = (mdt.min >= 30);; Z/ u8 `1 V/ K6 s% |
}
& V9 f* F7 Z" q' {: g" u! ^3 G, b}8 U9 m- @2 V4 L
return ret;" r' |8 S2 S4 C0 z' O, I* U
}( N- u2 w8 x1 H1 K( Q9 b
//---
; X' i0 i, ?* mbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])$ W+ f5 p( N. d2 }2 o" h4 [% G
{8 v% E# [8 Z( S- Q& q
if(PositionsTotal()) // Is there a position?
4 l  |. q) R6 k& P9 s, G" R2 l{
$ ~# Q, G0 x7 Z! W2 i, N: ?double offset[1] = { 0 };
' ~  h; `7 C3 }( bif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?8 Q7 U: D; U9 A3 T; q
&& PositionSelect(_Symbol))  // Select the existing position!0 Y8 {5 u; I2 Y3 R
{
5 }/ H$ J) o- BENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
$ Y0 z  }7 h/ F( |& S2 ^, sdouble SL = PositionGetDouble(POSITION_SL);( y( z7 d, p2 J' ^  B1 \5 Y" @4 {
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 D+ x  H# \5 U4 vif(tipo == POSITION_TYPE_BUY)
4 b! K: j7 G; j# f: k6 a3 P{
: K. {3 O  H/ tif (cotacoes[1].high > cotacoes[0].high)
$ _% D& ?2 ^3 s" k+ J& ~0 O: V! E{4 M" h7 ], ^& V
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
+ B; v8 N! L5 X8 B+ M: hinfo.NormalizePrice(sl);
7 _% ~% y" q3 Bif (sl > SL)
+ Q  Y% l. Q. x/ X( y1 r% ~9 U2 t{
3 V5 [# W9 O2 k) Gnegocios.PositionModify(_Symbol, sl, TP);
5 `  n. w: ~- X- `}
" j7 u5 O/ w( y* x; `2 B1 r}, v" C9 K, N1 k. q* j
}- N& Q! B" g4 E  Q4 R( t* H- z
else // tipo == POSITION_TYPE_SELL" X0 d% Y2 I% t+ v2 `
{
( E0 r" l0 l9 w/ w+ q  Cif (cotacoes[1].low < cotacoes[0].low)
. B5 X7 K6 _- O3 ?{
$ F, K- }' h# B6 _2 rreturn true;% M8 x5 c/ x/ R" j
}( f5 ]+ u- v5 ]/ U" a! e
// there was no position7 s. @+ n0 C. ~1 B0 f+ j/ B
return false;
5 z3 E& C0 }+ z; U" O+ u) A}. h5 `6 R3 M" Y% }6 {$ o
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。8 D% s( N, L8 e( \
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 18:27 , Processed in 1.203997 second(s), 32 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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