私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
3 e# u  b1 G0 `! W0 b. h在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 X6 R% ]- \6 ~* a
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。( l7 D+ ^5 `1 x5 s3 O2 r
以下是制定这些规则的代码。7 p: J" i$ c- L3 ?9 M5 `) @
//--- Indicator ATR(1) with EMA(8) used for the stop level...5 p, ^8 `% e1 U; n; S. \# i' x1 e& R
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
9 l) T9 Z# I/ s: y! Uint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);! h, \' q" Y9 R- r4 u& u  a
//--- Define a variable that indicates that we have a deal...
+ ]. W2 {  l& k4 m/ X- q; Ibool tem_tick = false;
/ D# w3 E: ]4 g$ s( e0 w" o//--- An auxiliary variable for opening a position
* B4 @- G4 c7 g6 e# a' G7 F#include<Trade/Trade.mqh>
& x5 ]" C2 F, H' X9 K+ F( S#include<Trade/SymbolInfo.mqh>8 O* _4 k! O; k
CTrade negocios;
0 K/ X( }3 Z* d+ f+ u  NCSymbolInfo info;' B" M2 ^2 O. [/ |
//--- Define in OnInit() the use of the timer every second( T' g: M3 X  H5 ], g
//--- and start CTrade
1 W8 r7 d( Y& A6 wint OnInit()% y8 N9 `2 _6 `  S$ O; Q
{
% o8 V6 ]- ?" }$ M6 o' J2 _//--- Set the fill type to keep a pending order( K7 {  S4 k& J
//--- until it is fully filled" }7 Y! k* E8 r9 T- Y# I6 d0 O
negocios.SetTypeFilling(ORDER_FILLING_RETURN);7 ^5 E, A9 {* {0 H$ {- t/ B
//--- Leave the fixed deviation at it is not used on B3 exchange
4 Y3 z+ [1 X8 Ynegocios.SetDeviationInPoints(5);+ Y  u. {4 ~( K& S8 M+ Z( [! z# I
//--- Define the symbol in CSymbolInfo...
8 F" ^+ G& {1 j+ jinfo.Name(_Symbol);
) h# w( `4 I' S/ r- a//--- Set the timer...3 x  k" m5 i* i8 k3 t8 v
EventSetTimer(1);: t/ t2 R/ O, N
//--- Set the base of the random number to have equal tests...
) P9 }2 I- X1 SMathSrand(0xDEAD);
6 h) u0 M( M& t4 r  e/ m6 u# Areturn(INIT_SUCCEEDED);
8 s# i! V0 a, y' L/ K/ M- D}5 Z; h( W& k! a" \
//--- Since we set a timer, we need to destroy it in OnDeInit().- t+ J; ?8 g6 R
void OnDeinit(const int reason)
9 O1 f! i- ^3 _{, Y- y& ^; N% s% @
EventKillTimer();
( c# ^/ P8 U$ `3 s* }4 S# o}* m1 n8 S$ k! ?' x5 @9 r
//--- The OnTick function only informs us that we have a new deal
$ C& [" @# V/ @' d4 U( N1 X' i1 Lvoid OnTick()
! e* A1 p& a) D$ m6 |{
4 [; {6 }. j0 u7 Etem_tick = true;2 M  ]- R, g. s8 c( w. {' {2 {
}
# i1 E0 a8 |% a  E! t% J+ j5 S* g//+------------------------------------------------------------------+: e( m6 F  D0 E% o
//| Expert Advisor main function                                     |% e8 L) _( F) v6 R2 ?
//+------------------------------------------------------------------+
' l, s! {' c/ X2 U% g( F+ vvoid OnTimer()% ~8 v- ~, g2 E( u% s+ C3 g
{$ ?3 `/ j# h: o6 m1 ?; [: Q
MqlRates cotacao[];
7 L! M; i4 z5 g1 Nreturn ;
. m6 D( I% v" j2 y% iif (negocios_autorizados == false) // are we outside the trading window?, ?% r- |9 A, z+ v( z$ Z- |+ V
return ;
0 X, z  t' |* D- k, l" P9 a  I//--- We are in the trading window, try to open a new position!
% P+ e" Y$ m# g: o( R4 iint sorteio = MathRand();1 O  P! k; G+ f9 N  t% S
//--- Entry rule 1.1$ i) a8 ?( n, d, N
if(sorteio == 0 || sorteio == 32767)% E2 ]. }) G" N8 Z; {
return ;5 I# N' R( D2 Y& x
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy" c  l$ ^3 J( {8 M- n$ P0 B7 M
{( n( [5 E0 H6 V7 M
negocios.Buy(info.LotsMin(), _Symbol);
4 L, i8 E/ s( O" ^! q5 W: W}, E7 G( h" n+ M) W8 }! [
else // Draw rule 1.3 -- odd number - Sell
, r3 m) f9 @6 q- A{  @8 ^5 C% x0 K: p
negocios.Sell(info.LotsMin(), _Symbol);! ]3 g; l' K# J  G0 y
}: O# P" ?4 m+ g3 ?/ X
}
7 L) X" ~( n! I: F/ U8 j5 l1 E//--- Check if we have a new candlestick...
* x1 ?& P! j3 z% n! }' A0 _9 F4 Zbool tem_vela_nova(const MqlRates &rate)
, p0 E8 K! X5 S" t{
1 y( I5 g- I, g! s3 g{8 b% J4 F, \) l; m
ret = true;
/ W% r3 e1 g5 y$ @  c# qclose_positions = false;
5 j8 \- V7 K. t7 s}
# |4 P6 \4 B: ~6 X' @2 P- ^3 \else
8 z4 a& ]0 F# _, ?6 [{
7 O- Q. y& [6 ?3 V  f2 E  Fif(mdt.hour == 16)- U. p2 e" y( m7 M) U& A
close_positions = (mdt.min >= 30);
) r9 K  [" h4 V, V) S( w}8 E4 t2 n: [! |% @1 U/ ?  a
}  Y* L6 j" m$ r' v" }, N  G
return ret;4 q& r# J8 h3 h' w- w
}) B: d  M  Q4 s( J
//---
9 w" _4 R. d+ E" \  abool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
* q) {, L$ Z! A2 ?+ h{
+ O  U9 j& k- R7 d2 e; Tif(PositionsTotal()) // Is there a position?/ x; r$ K. H$ i, N9 O
{$ a1 D$ K! W$ d: h2 P4 X% M, Y/ V# L
double offset[1] = { 0 };9 d6 k" k: U' @: B
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?5 d* B& j! P- I; ~; B
&& PositionSelect(_Symbol))  // Select the existing position!
# `9 G, c) y) G{/ _8 b( c  {3 U' F3 }, f
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);& ^( |( s' m3 `
double SL = PositionGetDouble(POSITION_SL);
; S/ s" x1 D6 ydouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));+ P- w  P4 B  B% F% G( {
if(tipo == POSITION_TYPE_BUY)
2 T" D. C; u( b" p{
) Z, a' u$ L5 T/ q# X& zif (cotacoes[1].high > cotacoes[0].high)
5 W# M* o' r1 V, s# c{4 q' }7 u+ v; Q
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
! z4 y2 `2 L' K- n/ Z. k* Ainfo.NormalizePrice(sl);
9 e) w# q3 z+ |) K1 V0 Sif (sl > SL)# `# c! Y% n1 h) n# J. o
{0 y" Y9 L! w$ M  l- G6 a
negocios.PositionModify(_Symbol, sl, TP);5 E% {! o1 N4 `, i
}7 m  G* B' H. j, ^8 E
}2 f: n+ o& {" `8 j, a
}
6 e+ G: h) N/ n" O, xelse // tipo == POSITION_TYPE_SELL' F+ l% I4 Y4 U$ {  S' o' N5 K
{* b; x1 r! A5 }
if (cotacoes[1].low < cotacoes[0].low)& P6 v+ i4 h+ b1 g( T
{' M3 G  q) U/ U, W  U8 m1 |3 s( D4 H* m
return true;5 f( }3 i3 v. D9 n$ n# y7 A) K! y3 G
}
# B0 k6 P) ~# f( f' D$ U/ Z// there was no position4 B0 V4 w( X0 J0 N: a; r; L
return false;
% i' B7 E- u) A}
2 q5 p6 X5 |3 `4 |( ]9 ~我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
* k! ?/ a  h. k" L" T5 C到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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 22:16 , Processed in 0.647752 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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