私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA  ?' ]# D% x2 U- {6 C
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。8 `& B/ \+ _' j/ Q9 i
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。; W  k2 V$ D: G! }* b% t
以下是制定这些规则的代码。
7 i/ d# \: H* ?( \, w//--- Indicator ATR(1) with EMA(8) used for the stop level...
; K! |5 b& L% A7 X5 i/ g$ U; wint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);( L) s0 G$ U5 |9 j& S/ M
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);2 c5 v4 D& F  j: |2 [7 }) i9 ^
//--- Define a variable that indicates that we have a deal...
* R! v9 A3 t7 k7 ~+ \4 K6 d' N' Z4 ]. ]bool tem_tick = false;) q  h9 d- [& L0 q
//--- An auxiliary variable for opening a position
) Z6 X  c3 I: I- v6 C#include<Trade/Trade.mqh>
3 D  v7 a. ]1 k3 {( |. T#include<Trade/SymbolInfo.mqh>
. Z5 g+ A% N3 j5 u" a3 z  ~CTrade negocios;! V7 x! R4 Z- M% g+ E9 {
CSymbolInfo info;
9 o9 t5 e7 g- q/ ?# _5 \* ?//--- Define in OnInit() the use of the timer every second
( G" x% @& v( l0 e: K  [//--- and start CTrade  `4 K6 Q8 J+ p" g+ j% h' o
int OnInit()# R9 r' s5 J5 o! U4 w
{# `! s" u5 I5 B6 J9 a
//--- Set the fill type to keep a pending order
$ A- d( [4 d3 N8 v' i; i//--- until it is fully filled
4 p% c) g5 e+ M' m! cnegocios.SetTypeFilling(ORDER_FILLING_RETURN);7 G; X* t9 E& P+ K6 i7 G
//--- Leave the fixed deviation at it is not used on B3 exchange/ n" U9 Q. J* r* G; x% {; f' v
negocios.SetDeviationInPoints(5);8 E* I/ u3 G" X  _& v3 o- n! u9 A
//--- Define the symbol in CSymbolInfo...* K8 \4 N6 [, e/ S
info.Name(_Symbol);
' T4 J  Q9 E. J+ ?//--- Set the timer...
1 C; E  h/ q! s- _/ X5 kEventSetTimer(1);1 ]$ j! N: b9 M1 _+ r* \
//--- Set the base of the random number to have equal tests...) K0 x% X. c& C$ e2 c& [2 m. k
MathSrand(0xDEAD);
2 p3 B6 ]  E7 C$ n4 R( wreturn(INIT_SUCCEEDED);" c6 a# q2 S7 V. U" Q' `1 v4 _
}
$ {/ r$ K0 Y% t//--- Since we set a timer, we need to destroy it in OnDeInit().
; E! s" s0 Q  P6 p" lvoid OnDeinit(const int reason)
" R" Z: y! w% `1 {{
6 b' }7 W" X% t) L8 ?$ hEventKillTimer();2 c, j/ g$ l$ x/ \6 v) k# \
}
; o; w% f# ^1 ~; z//--- The OnTick function only informs us that we have a new deal
# q# G& s; k) z" q$ U: _, Tvoid OnTick()
) {" Z) I: Y1 t1 K% \) }  O{* [& ?- W2 D/ O3 u' O
tem_tick = true;
1 |6 y4 ~9 J* _* ]2 b7 a}& x7 f: z/ }6 @' j$ A6 n
//+------------------------------------------------------------------+& g1 m  U+ v1 ~1 E3 m# L
//| Expert Advisor main function                                     |
5 |3 N/ N" p! X6 ?4 R//+------------------------------------------------------------------+" _+ g) h/ f6 C
void OnTimer()
( E: R, {/ s/ j3 S{# U( N9 f: |# w  j# E
MqlRates cotacao[];: |9 m5 {$ |, a( ~  R) L2 x
return ;7 F) k. j) u" W7 `$ c3 e* W
if (negocios_autorizados == false) // are we outside the trading window?
$ v- p+ T) A+ F0 B# e' wreturn ;1 l$ `9 X8 Q  n7 O- n) J" F
//--- We are in the trading window, try to open a new position!( f6 w  Y" e0 d0 K
int sorteio = MathRand();
  C8 Y5 ?1 D/ ]  w# N* {//--- Entry rule 1.1( X+ e9 W+ ?+ P, q( j' F$ d+ r
if(sorteio == 0 || sorteio == 32767)
5 K; d4 O- N1 h6 N& X" o; Preturn ;
: O4 h5 z; P9 a  t; ~7 R8 c+ kif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy, W0 d7 N* }; l/ n: j6 T
{
0 _1 _! \9 A, M. i; Mnegocios.Buy(info.LotsMin(), _Symbol);
" F3 N* c4 y1 y}$ P( ?2 `  ~; a' x
else // Draw rule 1.3 -- odd number - Sell0 [( Q+ b4 c# V+ N
{5 m: |& G2 U, K6 t# p# _
negocios.Sell(info.LotsMin(), _Symbol);
) Y7 b# v( N8 q1 m4 @9 _0 e}$ Y6 e) A) U, N$ |1 v( c
}  n: p. f2 r& o% Y
//--- Check if we have a new candlestick..." ]; k. \" F2 u& Y  f+ k+ F
bool tem_vela_nova(const MqlRates &rate)3 d2 t# d4 f, D+ f' k7 S* K
{
+ O* q. v2 k0 P+ C- O{7 o+ R7 Q2 @0 i
ret = true;
4 f0 l% h. T. Z8 C0 qclose_positions = false;- r2 F, ^/ g6 o) m* F
}
. w5 o: L% f( s; F5 Q+ X: ?else" f' t1 W" y, l+ p- y
{+ o: J/ N# s7 A5 T4 p/ h
if(mdt.hour == 16)7 j% V* o. M! b  P/ z. f# B& Q
close_positions = (mdt.min >= 30);9 F  @9 f0 }- j
}
+ s; ^5 @3 {5 y; L' [6 K}
& x  o& H5 F6 k4 f7 Nreturn ret;
3 b8 }; O5 K  f  f}
5 f5 M* V3 ?1 [  B7 Y* B" K' l//---
) \8 q$ l; g+ s0 }! L( ]bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])6 S! g- ~% Z' x1 Y
{7 F7 L4 G# r/ U2 f% N! Q- O) k
if(PositionsTotal()) // Is there a position?
5 F7 A8 W1 |; c, Q{
- d& n9 [) e0 N2 W; H$ s' Gdouble offset[1] = { 0 };
0 R! M1 b& R6 R4 A: f+ Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
# c4 O0 z( V; |9 z2 d&& PositionSelect(_Symbol))  // Select the existing position!: u+ i. Z# a7 d% P
{" h4 y5 f! _, x; W, m
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);; [% }  U% G0 z
double SL = PositionGetDouble(POSITION_SL);
. D9 @6 H% `. L! Z# Ldouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));$ b( E  o, z% }, r) h: C. K
if(tipo == POSITION_TYPE_BUY)* X6 `  J) U0 q4 W1 O
{
3 S2 b% _, n1 i( w6 ^if (cotacoes[1].high > cotacoes[0].high)0 @" q) U$ [7 l" o: h
{, C9 S  [- r5 |2 }, G* T9 p7 D  k
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];3 B- a. G5 ]# {" E' X  p
info.NormalizePrice(sl);; _% S8 M. P0 s! B6 _# T- i
if (sl > SL)
0 ?3 b& `/ U- r' l6 L+ O% P( j{  b8 H+ Y* R* h
negocios.PositionModify(_Symbol, sl, TP);* \- _# T6 c! F4 m) V/ Q; F
}; d6 ]1 ^* `+ H, x3 t
}
2 T/ z) `$ y/ B/ Q9 ^: K' o1 i}) ^" i, q1 o9 M( @2 n& _0 h) m: j
else // tipo == POSITION_TYPE_SELL
* `( E4 w8 I/ s{1 e4 \' ^$ F( b7 |3 Z
if (cotacoes[1].low < cotacoes[0].low)
2 f4 m5 ~: U( M% G{8 t1 E, }% D+ a
return true;
8 `! w" K. W4 y$ |$ n}) Y0 B7 M2 ~4 q% ]1 [/ \# N& n
// there was no position5 N# t; a+ u* F4 x" Y/ x
return false;
- f1 F  |& b) X) a3 T/ N}
& P+ d/ Y# u! l1 H# |7 |4 c. w我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。# y- p$ Y. `+ k& D! {6 Z
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-12 22:38 , Processed in 0.417221 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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