私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 `! U+ H. D+ g+ I% @' K9 q在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
, E' _. z% z" v9 l$ Q' U3 I7 f* D为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。6 `2 g% c9 @9 b) l5 ^
以下是制定这些规则的代码。
$ r" I: v: `8 {1 _, }//--- Indicator ATR(1) with EMA(8) used for the stop level...
- E- b4 g! _- S* L; F& bint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
1 [* X5 o# E) K1 o( q8 ~' ~% zint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);/ A1 ~9 E0 Q+ q6 D) p: V
//--- Define a variable that indicates that we have a deal...
: R( l- U! {' jbool tem_tick = false;, ~: H. M2 |- N( Y) V- }5 B- R# e
//--- An auxiliary variable for opening a position
: l7 \. ^" E' X7 }1 D" w#include<Trade/Trade.mqh>5 [( D! a7 ~8 e# @  _
#include<Trade/SymbolInfo.mqh>$ @  h& E' H9 R9 I. k" Y7 _
CTrade negocios;
* {* h* a& |9 nCSymbolInfo info;
# l9 N) Z* M- f; j0 O//--- Define in OnInit() the use of the timer every second- K1 A/ ]8 B; b4 h
//--- and start CTrade) ?+ f0 f8 G8 A* T/ `, K& Q
int OnInit()# g: P: e2 {( M/ y
{: \0 d, K0 o6 x; ^1 w) j
//--- Set the fill type to keep a pending order
/ |7 n1 z4 n8 B' u//--- until it is fully filled
8 ]7 ]/ x/ h, C; l4 ynegocios.SetTypeFilling(ORDER_FILLING_RETURN);
, r! j: m" e+ V" h; E" d' E//--- Leave the fixed deviation at it is not used on B3 exchange6 K  _  v1 s2 s
negocios.SetDeviationInPoints(5);5 c2 C5 r) T* h; ]; r2 i
//--- Define the symbol in CSymbolInfo...
6 d* C9 y' g) i1 t( Rinfo.Name(_Symbol);8 |% |" D( e& h  E# w% |; w% f8 j7 Q
//--- Set the timer...
+ K0 v9 T6 u8 IEventSetTimer(1);
9 C0 ]! R& s- v" d7 j) N  b//--- Set the base of the random number to have equal tests...- W7 w% l- P. h+ q% F6 F
MathSrand(0xDEAD);
0 b0 e: l: S1 N( S1 Ireturn(INIT_SUCCEEDED);
  u* c& f& t8 Q, l, H& s}. u9 J& \1 l% w2 n5 [
//--- Since we set a timer, we need to destroy it in OnDeInit().
' Z8 E' l3 f9 h( d1 @; [/ Rvoid OnDeinit(const int reason)
( H- c; L: P, E0 A0 N{; n* j) W* H; u& |# c
EventKillTimer();; \) T6 I6 D* k) ]& T& C
}
. J) p' d$ @  y5 G* f$ }$ [1 ?//--- The OnTick function only informs us that we have a new deal# s0 G0 d8 w% D, i
void OnTick()
5 t7 F+ _1 t5 j2 C2 A' i{$ i+ d0 Q- z3 G& s( J2 A2 X8 f8 q
tem_tick = true;2 Y$ ~8 S  ^1 l7 u. t* P4 G* w) K
}+ w6 p6 _& E' @: ^- ]8 x
//+------------------------------------------------------------------+
$ q: D8 j+ ~7 V, ?- {//| Expert Advisor main function                                     |1 C* ^! F# y$ T% U3 c
//+------------------------------------------------------------------+  _) [3 Y5 @) n. M# i( g
void OnTimer()2 s% R; D0 C# H' }2 g
{  O) j  h; K6 ?7 M' c; M5 m" ?4 i
MqlRates cotacao[];! J! Z5 n7 T1 \' Y* K9 W
return ;5 a/ I: w$ B9 N+ Z0 |
if (negocios_autorizados == false) // are we outside the trading window?
1 W" X  K9 J+ {8 hreturn ;
* s$ E% D9 l" p# U//--- We are in the trading window, try to open a new position!6 W- R# L9 ~% L+ k; P/ ]# d0 U; @9 D
int sorteio = MathRand();
' \( D5 Y  q# K2 U/ a' a+ Q& i+ w5 t//--- Entry rule 1.1: K0 F5 R$ S: I9 ^
if(sorteio == 0 || sorteio == 32767)/ u7 `1 h! k7 n9 J/ V9 E
return ;
" P' ]3 \' q  f  Pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy( z- ~/ Q3 T7 R' S# l" t
{" {9 h' E  U$ X* T+ K% s% i$ j6 }
negocios.Buy(info.LotsMin(), _Symbol);
; R' S! E1 K! s1 N1 ?2 }: X/ _+ ^}. y$ p: X2 _; @7 g$ T
else // Draw rule 1.3 -- odd number - Sell
  u0 d. m( Y1 N% u) i8 @{
, T" ?& j8 y/ \% p' _, Xnegocios.Sell(info.LotsMin(), _Symbol);! r# b4 T! _) E1 _; ~9 Y
}
; F$ G/ O+ M# k% v0 O4 S4 x7 u}; F# B1 R* F1 t; N- y
//--- Check if we have a new candlestick...
2 E* r  U9 o0 E2 `0 c/ ebool tem_vela_nova(const MqlRates &rate)
7 }2 b  g3 y! l* }# v! z7 u{
/ U! d; l6 u* H{
7 s3 ^) T9 t4 J& {3 q( |8 J4 p" jret = true;% J* J, P& Y9 o
close_positions = false;4 \& r- n& G* r$ f9 V2 u0 C+ @
}" g3 S8 V* V+ g' K. z
else, G& T' M: P# W) q# D
{
2 i' z5 v' Y; c7 R! q% H& Aif(mdt.hour == 16)
) N( [" E( S! A" N6 u0 v4 Zclose_positions = (mdt.min >= 30);) B6 c6 e& t3 i. ?$ q
}4 B* H0 T2 r% N
}  H* v7 z7 m! [( H3 c! I
return ret;
/ s. {. }. w% p}
- m" P  G5 F/ [6 m; w% @$ w//---
% u7 \; D! b! V+ X. \. l/ D; _4 ubool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); O* d; b4 E0 u3 x( z
{- ?% S* b# [: k- o! {: X
if(PositionsTotal()) // Is there a position?
% _2 N0 v4 ]# N3 \9 j$ A5 K{
- ]2 {* n" X* D. Y+ K, U7 H" cdouble offset[1] = { 0 };
% `" [! }7 r1 r5 f$ Mif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?$ E) b: D" \: r" j3 `
&& PositionSelect(_Symbol))  // Select the existing position!* r$ C) v$ W0 R% k3 y$ w, @
{' W: I. l0 r/ \" T
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);$ y* T& s3 [' i6 p6 K# @  O$ i% n
double SL = PositionGetDouble(POSITION_SL);
8 z8 X8 @6 T) i& s; ydouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
, _5 L" m* d) k: fif(tipo == POSITION_TYPE_BUY)' ~% Z- C' H6 Z/ R& M* N! y
{
' E- K, V- ~# R  h) S" Pif (cotacoes[1].high > cotacoes[0].high)
) f2 o6 n0 j9 ?{# u8 R* r; \* K. h; d. q7 m9 S
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];* Z/ n# S) z% ~, V* I/ w
info.NormalizePrice(sl);4 E! A" e( r  ]8 ^) O
if (sl > SL)
0 ]/ z6 t, ]/ u- d{
& X& @6 W/ r5 e  C: _, qnegocios.PositionModify(_Symbol, sl, TP);. X3 j8 W- h, g' N, n
}
- j; x/ _7 k+ C* z+ [/ N}1 l6 x% G3 @; S  Q0 {9 T
}! v, R" h8 ~5 T5 |9 ^/ \
else // tipo == POSITION_TYPE_SELL( d% `! k: n! Y
{
) |- \3 _2 c4 r3 _- Vif (cotacoes[1].low < cotacoes[0].low)
/ j* ~1 N+ j1 M5 C' _7 c2 W{) d( H1 M- R9 i7 ^  ^: R
return true;
, r, |5 I  ^# ~& }+ |+ {+ w}4 e2 g! L: W: h# |
// there was no position
* H* ^% J. z7 J% E6 X2 wreturn false;3 K6 \1 ?1 _- R" A$ ?7 C) o  B( X- w
}% y7 o9 _0 p, Y+ R1 P
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: ]$ M4 H% e$ n/ J) R到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-27 00:58 , Processed in 1.024312 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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