私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
1 G9 w" d  V9 N! Q/ W在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' N/ I" `( E( i7 G8 k
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. k- w! W* K+ ^/ s( {
以下是制定这些规则的代码。
# V1 m4 s. ~" M0 v$ `( c( w//--- Indicator ATR(1) with EMA(8) used for the stop level...9 w1 C5 R6 u; l$ I, g& O
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 {. \( I) r9 N& Tint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);0 }8 K3 c) e& J7 B
//--- Define a variable that indicates that we have a deal...5 |% g. r3 F; m; A
bool tem_tick = false;" Q3 w3 S; w2 S5 O& m
//--- An auxiliary variable for opening a position2 u' o7 _2 h) n& T. T
#include<Trade/Trade.mqh>
! {+ S, z% U) A$ D#include<Trade/SymbolInfo.mqh>
1 t  a, A) U. OCTrade negocios;
! O$ D: @" L0 \: e+ G; b: BCSymbolInfo info;' r- B, C) ^. g. b* m+ m
//--- Define in OnInit() the use of the timer every second
% c5 U& E+ u. M1 O9 ]//--- and start CTrade* W8 J2 G' r) o
int OnInit()
2 k- o3 I! t' _$ `# k3 g{
! B: j9 C$ w$ |/ ~2 t5 P' @//--- Set the fill type to keep a pending order
! w0 g% s2 o) f! t//--- until it is fully filled5 j  V5 o9 x5 G+ c- B0 r
negocios.SetTypeFilling(ORDER_FILLING_RETURN);( M; L* ], L$ N- H& I
//--- Leave the fixed deviation at it is not used on B3 exchange
* z4 T3 S# l5 z6 J: f9 j7 @3 Unegocios.SetDeviationInPoints(5);9 a) b1 t! x2 N# f4 R4 \1 o" O" e
//--- Define the symbol in CSymbolInfo...
$ {, g, D/ X' L4 f) j# C- C4 jinfo.Name(_Symbol);; p, V7 h' k/ z8 S6 k5 A! X, ?9 q
//--- Set the timer...
, Q( t! [+ D5 G$ `/ d' V/ dEventSetTimer(1);
8 C2 V3 G/ y. f" \8 ^' Q$ b//--- Set the base of the random number to have equal tests...
0 K- X" ^! R8 \( f* WMathSrand(0xDEAD);" S. A0 L" t- n0 P: L6 l0 X
return(INIT_SUCCEEDED);: @( \/ e& F6 y
}- k% V0 t5 r# A. w6 D% e
//--- Since we set a timer, we need to destroy it in OnDeInit().7 O# H8 p9 n& Z
void OnDeinit(const int reason). j+ o# c0 ]; f1 y
{
6 m, K! f5 z) GEventKillTimer();3 a# k8 B+ G4 m/ B4 X3 R' O/ W
}' k/ P8 |* s; t) i, I
//--- The OnTick function only informs us that we have a new deal
4 ~2 k* {" [* V$ Ivoid OnTick()
5 }5 T! l0 O. C/ N" u% j6 x% g9 c7 F{
3 ]  Q2 k" `1 Ctem_tick = true;0 s5 x3 M* X2 z& v2 E- l
}
1 p& s  X8 C) c5 z5 c! D/ O9 F% c//+------------------------------------------------------------------+
4 v: }& E5 {7 [; l( y# [0 J//| Expert Advisor main function                                     |
, L" l: h+ u2 |6 S* `2 }//+------------------------------------------------------------------+0 @# V% L/ ]' V. i
void OnTimer()" z, G; u0 e& i, D9 Q4 ~5 o9 x
{
& V3 b2 Z: M0 X* {- y( N" o  DMqlRates cotacao[];- K$ x% q, e" t2 g  `
return ;
1 T- z: b) y/ o4 i9 Y5 H/ s( Nif (negocios_autorizados == false) // are we outside the trading window?
5 Z' L. ^$ z- Freturn ;! U7 n/ b, j8 r
//--- We are in the trading window, try to open a new position!
" C' v+ }6 K5 ]int sorteio = MathRand();7 R4 j/ |6 M+ d) C( s3 A# }$ @+ [
//--- Entry rule 1.1
" T* H5 H' Y; x# s( Iif(sorteio == 0 || sorteio == 32767)6 E7 S6 n/ z6 T' c8 k. p
return ;
+ z7 t" P: {- J: g: wif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
* _% l! }  f/ Q( |7 B/ Y{7 D7 G$ f; b9 Z
negocios.Buy(info.LotsMin(), _Symbol);4 ]' d6 M  F+ }+ Z& f
}
4 E/ `' _" B* X1 Uelse // Draw rule 1.3 -- odd number - Sell
- [9 F3 i& ?% G2 C, b/ _$ Y5 ^{5 }/ b# y% M& t, W
negocios.Sell(info.LotsMin(), _Symbol);
+ `7 [, O4 ]8 K' T& ]}
' V$ @0 g2 C- z% O4 H9 Q}
! p; ?5 w+ ~$ u2 L# E8 J//--- Check if we have a new candlestick...; V5 Q- ?0 \4 K
bool tem_vela_nova(const MqlRates &rate)1 I  o; ?2 f& Z  x
{+ j! ?* D: \  Z
{  `+ w1 r. \+ c$ \" s
ret = true;7 c9 I, @7 H( w) t- k
close_positions = false;
3 ^0 E6 z% w8 Y) f! f! p: N" `}
$ A9 |0 m# r, [/ helse# K4 W5 i* A+ B  z
{
: v6 O" [* j4 Gif(mdt.hour == 16)
3 r4 k1 j% Q* d  Pclose_positions = (mdt.min >= 30);
2 B9 d! u2 g! N3 ~}
: t. R9 l. g% r2 s' y/ J# V/ w: [( v}
# b' ^- x- ^8 W- a6 b$ T! H0 `/ ~return ret;1 m! r8 }6 I$ e1 o
}  Y  c" ~% o$ R) y, i2 A
//---" P& E4 r" H9 {' A: }- L
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); U; c! }& H2 S  U, \, p; z& X
{1 }. c' |3 }% V, X0 I% L9 E
if(PositionsTotal()) // Is there a position?
: e- m3 m+ {% X. w  l" e& _3 ?{
$ W, h5 B" q9 j" T+ edouble offset[1] = { 0 };/ s5 ^2 X" f% y6 p9 n
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
; I& Z4 V. b# A, s6 ]/ e&& PositionSelect(_Symbol))  // Select the existing position!: H( c6 j; c! w  q" ~3 B3 S5 |
{/ C! D5 c  b' S( v
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);. K% Q. H; z3 y# @2 Y8 j7 c
double SL = PositionGetDouble(POSITION_SL);. l- I6 \' y$ r. S) v: F5 J
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
2 k! k+ U  z. aif(tipo == POSITION_TYPE_BUY)
9 z% F8 `+ o) W& r5 \) A3 C" ]8 t0 d{
# X* d+ J9 ~+ mif (cotacoes[1].high > cotacoes[0].high)* K6 V2 w! a1 n7 F2 m, t, a, N+ Z
{; E8 c# z" r3 m$ `
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];, u. E2 |' e4 c. p- o
info.NormalizePrice(sl);  X: y6 h$ G# S
if (sl > SL), D0 W5 T% R0 U1 F4 t" {
{( p6 {4 t2 j8 l3 ]+ n
negocios.PositionModify(_Symbol, sl, TP);
, i; ~  J; ~& _* y}
2 U; x0 [+ v9 K3 W1 D}& R/ d, v" z8 N# v3 ^
}% b3 f  t2 W# B* m
else // tipo == POSITION_TYPE_SELL
8 a/ P% z# X% K6 A) Q8 f6 r6 d4 O{: [6 Y6 c+ }( Z' j- r
if (cotacoes[1].low < cotacoes[0].low)
0 |# h+ j/ C5 y( m; f{
0 y. I( `. X1 b0 k+ y1 Ureturn true;
9 C0 Q/ |. ?% t! n}* I4 S1 i8 {2 d3 H  W
// there was no position/ v9 }. y0 O; m5 X+ L. j
return false;; d9 |' \6 X7 n
}  p8 v5 f+ J6 x! d5 B3 V9 M. ]7 F
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
+ f0 P: J  V1 v0 A* z9 @! }; ?3 f9 o到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-25 00:04 , Processed in 0.479605 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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