私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( t( \6 x$ G' _* ^/ A8 e
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
# N3 ?$ D) c% A" a1 q% Z: u" M) U为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。- P' r9 M4 x9 U' c% u
以下是制定这些规则的代码。8 M7 v  O+ I) ?# V6 }7 W
//--- Indicator ATR(1) with EMA(8) used for the stop level...
3 ]: [7 p# `5 {* c( \int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
, W% T7 w9 o7 P5 }& T2 A5 N5 r& Rint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
* a$ u, ]2 U9 b( w, ]//--- Define a variable that indicates that we have a deal...% s0 b" I& A( P( Y3 M" i' o4 v
bool tem_tick = false;' T) n( h0 Y3 c. |" ?
//--- An auxiliary variable for opening a position
- ^" X1 }4 v9 X0 i#include<Trade/Trade.mqh>" Q# x4 J5 H6 B
#include<Trade/SymbolInfo.mqh>$ r8 U, ~; t9 o8 l
CTrade negocios;
2 R; r. q# f0 F( z6 w! D" g# xCSymbolInfo info;* h0 k3 O$ @+ }0 r+ Z* g; l
//--- Define in OnInit() the use of the timer every second
+ ~, W/ x, v) z( l' |3 V  ]1 O//--- and start CTrade% K: u. [/ u3 S6 k+ D
int OnInit()' ?- g0 n6 m; p" m  e- U! P
{
' O8 p0 q) g& G' {# \//--- Set the fill type to keep a pending order
7 t# }& {6 d+ p9 R//--- until it is fully filled7 B2 X/ n/ T9 x* A2 A' X
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' ?. _; A  W/ _) Z, G
//--- Leave the fixed deviation at it is not used on B3 exchange
3 t9 X: m0 Y) b0 F- K: z7 C  x0 _negocios.SetDeviationInPoints(5);- Y6 z3 u$ ?, d* U7 {
//--- Define the symbol in CSymbolInfo...2 ?2 ~  |6 o) |" V4 z
info.Name(_Symbol);( r4 T' j: P: }* s' r
//--- Set the timer...
; C3 d0 W0 ^' V8 h$ UEventSetTimer(1);& [0 T5 j9 j3 K% X6 J# y! j+ ?* k
//--- Set the base of the random number to have equal tests...
4 l& q% |' [# l5 ?MathSrand(0xDEAD);
& U" i. T9 S* [6 Vreturn(INIT_SUCCEEDED);
9 u0 w$ _0 f3 n# J}" D% k* l; t9 E5 j
//--- Since we set a timer, we need to destroy it in OnDeInit().
; s7 {4 X& m: f2 \% s8 ~# wvoid OnDeinit(const int reason)4 U) i) r* Q- L- v
{* g& r: P( }+ e. C2 {
EventKillTimer();
8 j0 M% q5 U% V/ v3 Q1 U}
* V: w' I: Q: B, r2 g//--- The OnTick function only informs us that we have a new deal
& ]( E+ h4 Z) H. o8 M) n! Mvoid OnTick()
: `' p. W* a; T. R( O{# }  i; q0 l: U# o2 Q2 ?1 c
tem_tick = true;
. C3 B9 C0 Z: V5 U1 }6 A}- w  M6 Z) u9 {9 p8 q* M/ k0 T) s5 c
//+------------------------------------------------------------------+
& g% B& S# ?. M0 T. l  x/ `//| Expert Advisor main function                                     |
* e, ^( V7 n: W5 f; e/ [& {$ M//+------------------------------------------------------------------+
/ J4 v" n& H, c8 F% L* Vvoid OnTimer()
* @0 y5 b% I- G; e; \, x{
' g  b! q" _1 U4 j' `- O2 z+ {MqlRates cotacao[];
6 S) `8 p, m7 L0 ureturn ;
4 R6 N, {0 `$ z3 [, \+ ~4 Hif (negocios_autorizados == false) // are we outside the trading window?
9 P* r' o) T9 H% U" m3 t# o5 ?% |0 kreturn ;% z4 o0 }. _) o" ^8 w1 I1 y, B4 ?
//--- We are in the trading window, try to open a new position!9 z* T5 u8 r& g* ]  V& J
int sorteio = MathRand();
+ L3 O9 j; I+ Y//--- Entry rule 1.1+ z3 f4 C, z4 g
if(sorteio == 0 || sorteio == 32767)3 G2 i  U$ S$ Y7 F( j7 c
return ;
* L" C7 Q  p: @" L2 M* D5 \! }if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
# I+ k0 N& |# m8 W4 p{, m% g2 _) _& a) M% s
negocios.Buy(info.LotsMin(), _Symbol);/ ]' u0 d& {/ X# a) _' d
}% t* D# W6 i7 u7 O  x
else // Draw rule 1.3 -- odd number - Sell/ v( Q! i. G  D1 t/ V' a3 p/ R
{
, p' n" i' ?: o" ?negocios.Sell(info.LotsMin(), _Symbol);& Y$ v+ T1 R4 ~1 Q: l. W# A; {: R. W
}
8 Z  m# I3 r3 Y}
. p7 f7 @. n5 B+ Q6 l//--- Check if we have a new candlestick...6 U; F% b/ H4 t, Z( K
bool tem_vela_nova(const MqlRates &rate)
9 K2 E/ B2 q2 y$ t) p; a8 i{
& B( S# y( U4 N4 u  r& a+ ?{3 k0 j, W! t& n6 l
ret = true;) i! |/ p, s. k8 O% O
close_positions = false;
$ e& E1 ]4 B( \8 N0 p2 ^}
% h8 ^+ Z# J+ R7 E- [1 Y1 qelse
# O1 f! }. I+ p% h0 X! I{1 Z& t/ S3 G) `' x% y' Q
if(mdt.hour == 16)  x& q% I2 r$ r: f  T; v1 T" ^$ t7 S9 p
close_positions = (mdt.min >= 30);
, ~4 o+ M* ~7 a) o}: f+ Y% B5 k8 b7 P6 Y
}
* b$ _8 m; i+ t! C+ r& Sreturn ret;
( e0 E7 h, D! Y% \}
/ z1 W# Z; H: ?7 ]; c+ C//---
, d: {! A3 j' |- ?: obool arruma_stop_em_posicoes(const MqlRates &cotacoes[])- r1 `9 m4 G% {
{7 a, E" D) k+ c" ?7 H
if(PositionsTotal()) // Is there a position?& D9 y/ u  a% U) g- g! ]
{4 z) W+ p5 ?% a- b
double offset[1] = { 0 };
5 N7 ], \, p/ C# [0 yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# r/ F3 J* K/ u2 ^0 S0 ]( H7 y2 W+ a
&& PositionSelect(_Symbol))  // Select the existing position!
, l, h2 |" e6 ~2 Q# _{
% l0 ]6 X+ c2 zENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& @! P6 k- o! `
double SL = PositionGetDouble(POSITION_SL);
* f3 q7 S# u# b* f8 [double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
1 V& R0 F" B) q' j7 U2 i! tif(tipo == POSITION_TYPE_BUY)% m; R: W% Q: X8 K4 m- U- {& _
{8 B8 j+ a# s, n& ]
if (cotacoes[1].high > cotacoes[0].high)
; `! _& e& T6 a! @# [& \  E{7 W) y* A) u6 R2 H, V' d0 m
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. z8 v+ [& x3 i, p. ^" J
info.NormalizePrice(sl);
; T+ M- H; r3 D% R6 Kif (sl > SL)5 r8 m, l$ M. D) A
{
' j2 j% N; p$ i# Z4 Rnegocios.PositionModify(_Symbol, sl, TP);9 X& K5 n# g# Q9 @& ^- T
}
" t( H% }; p3 r& ~" S" l! b}
+ y7 J# K4 y9 D}* g3 d3 w, ~* G/ S9 g
else // tipo == POSITION_TYPE_SELL) ?' f$ ^  }. Q6 N) N; o% O
{: m% A; _0 z9 \6 F4 v
if (cotacoes[1].low < cotacoes[0].low)5 E) u: h% H/ }) t; M  c' N# S
{
; \' d0 F1 Y! q- i6 P# `return true;$ T& `/ L5 V6 Y8 P7 ^$ m* h1 E+ b
}7 e- {8 Z+ c2 {
// there was no position3 h0 U3 U' A& K4 _  K/ `
return false;
" f) c& f& y2 H' U! b2 }}4 f, G6 J+ Q) B" K# w% K  W
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。/ h1 s( h4 O1 i# J2 O' P. ^# Q- u% L
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-20 06:12 , Processed in 1.984740 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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