私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' l: s1 ?  H, S, T+ `6 i在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。3 V/ b+ t: ^# x! R- ^  F0 A2 E
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。# t" q' E6 h. U! s. _% w) w# C
以下是制定这些规则的代码。
, P; h( Y* F- z7 c2 K, k//--- Indicator ATR(1) with EMA(8) used for the stop level...
% m9 Q, i9 g0 E3 j: fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
; b& O3 L0 O5 I" v* nint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
2 O6 M0 ^# S. r) @( ~4 d: ?1 C//--- Define a variable that indicates that we have a deal...
1 w; r6 K. J4 q' F2 _' r% ]bool tem_tick = false;
! M* E) ^, Q9 a+ _//--- An auxiliary variable for opening a position
* q+ y+ f$ u% F6 y( Y2 X2 \( b- b#include<Trade/Trade.mqh>+ Q: X0 e+ A5 V& t
#include<Trade/SymbolInfo.mqh>6 X" G& ?: c  C9 I6 H/ j7 U" Y
CTrade negocios;" p' s9 Q1 P4 g/ p" G4 n1 \7 B
CSymbolInfo info;
* o, P( P. z- l//--- Define in OnInit() the use of the timer every second
: U& v/ N& Y, v- e; I* I//--- and start CTrade
" q9 R) s* N7 m4 `/ m9 ~7 Jint OnInit()
8 v7 f4 J0 Q+ r5 {0 D& e9 \- q{. y' o9 Q' ?2 R) {
//--- Set the fill type to keep a pending order. ~' f% H/ F, C% V
//--- until it is fully filled( \; @- o# r# L
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
4 J" e, ]& z6 d' Q9 l, ?; \. A) C# d//--- Leave the fixed deviation at it is not used on B3 exchange
9 ^/ p+ f4 K" {2 r! l( Lnegocios.SetDeviationInPoints(5);" ?0 A& j5 ?3 q' [
//--- Define the symbol in CSymbolInfo...
7 G/ d& F9 \* U9 Finfo.Name(_Symbol);& `' z# K$ W& C8 ~6 K, H+ d" x
//--- Set the timer...& ]- @4 g7 J# s% |
EventSetTimer(1);
  P$ H0 y7 ~& u/ p/ F2 }- F//--- Set the base of the random number to have equal tests..." ^. S4 {% Y/ X& v
MathSrand(0xDEAD);
" g  X% M+ K7 E( ^* b; nreturn(INIT_SUCCEEDED);6 k8 \" x# l% c9 h
}3 V( ?8 a) l6 [* k9 l3 n2 S
//--- Since we set a timer, we need to destroy it in OnDeInit().# j% c9 B% d3 Q4 _
void OnDeinit(const int reason)
! m% x' X" |# f{
' c% y5 j2 W$ a% ^EventKillTimer();$ l8 w/ P  D( p
}
& X  p# G; b! U2 K7 x, Z: Y! |% S' I//--- The OnTick function only informs us that we have a new deal0 k: t2 C2 n& y7 C$ {* \9 s
void OnTick()
: e3 d" t) O' ]" m$ n( \& N. n{
. p) {7 r! x: i5 V1 ztem_tick = true;
7 R4 o% c! _' q. W}1 K; @* C) R5 T( D; ]
//+------------------------------------------------------------------+0 W/ [& S- O/ I: {) |$ w
//| Expert Advisor main function                                     |
- ]2 Z0 o" M# n' g2 I) J//+------------------------------------------------------------------+
- M: q  a& o4 |" K1 D. m2 M: Zvoid OnTimer()0 m7 [) Y# Z* E4 y+ A5 P4 v
{8 z5 n4 X! Z$ K* N: j
MqlRates cotacao[];
  n6 G# \6 w4 M2 N6 Sreturn ;5 a; w0 D. _$ H; m! q  R0 U7 C9 n
if (negocios_autorizados == false) // are we outside the trading window?
6 }4 S: x& l+ r- T- B5 Ireturn ;3 _- X  O- U1 }8 I0 O* |. a! e
//--- We are in the trading window, try to open a new position!
1 Y' R1 Z7 c" s" t. o8 l$ b; Dint sorteio = MathRand();- Q1 w- m4 r/ m( W- f) q
//--- Entry rule 1.17 ^0 q/ Q$ N  M: n0 x( ?% }! M
if(sorteio == 0 || sorteio == 32767)
0 C1 ^  g# b, preturn ;
1 k3 F) W: k8 X8 h6 M7 I8 t2 pif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy7 Y$ C7 N3 s0 c
{
3 @8 o7 r/ N3 U2 Xnegocios.Buy(info.LotsMin(), _Symbol);" i- U% j# O5 D3 e, ]
}
, X3 K. r/ [, Telse // Draw rule 1.3 -- odd number - Sell7 P( `$ C: S7 j
{" S, n1 k3 e0 H9 V6 O6 d7 z
negocios.Sell(info.LotsMin(), _Symbol);
2 c( j8 W! z/ `% F5 K. w# f* H}
9 y% g% [) @% X- _6 Z2 B7 I0 T% t}  z$ I8 M7 K7 w9 [8 [
//--- Check if we have a new candlestick...+ |) _' y( {1 q  s: H& v" F
bool tem_vela_nova(const MqlRates &rate)% F5 M% ^+ `/ n' `, K
{
+ q3 p! v% s8 O, Y8 Q{
4 ?4 E/ x1 e# e2 C3 w$ m: ?  D4 uret = true;
* S, H2 B: x# S' y$ h" Bclose_positions = false;+ G& Z0 c1 ^5 Q
}
1 N2 A' N! p- x. L; selse
) A7 h7 E5 K1 _& U{
9 U& f6 ~: U$ Sif(mdt.hour == 16)
# p( O' x, p5 r- ~, C! d% Hclose_positions = (mdt.min >= 30);; ?+ H9 `) n8 C+ }
}, {6 D) j# ?/ K
}; I9 [5 c. m- m% ~5 P
return ret;
: M1 D4 |) C# L4 Z. i! |5 @1 N0 b}
7 u# d9 }1 ~4 Y5 N6 H//---
3 |' c3 y3 s) ?7 hbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- K$ ]  H; u3 n{9 J% J8 M7 F8 g4 m4 I
if(PositionsTotal()) // Is there a position?
: |% ?' w) x; @+ Q4 m{9 d5 I0 N& s$ W: `9 f) L
double offset[1] = { 0 };
8 T) m& i8 q# \7 {8 W5 H) Tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
" W$ j4 W6 B4 Z4 Q; }% l&& PositionSelect(_Symbol))  // Select the existing position!
  E7 H7 X) {9 V% H  i- E{
2 N$ u, U& `, Y- A- b$ l( v1 v/ zENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, `8 ~+ _4 h& O; Z+ E$ W' ddouble SL = PositionGetDouble(POSITION_SL);
" D5 Q( o! c/ Gdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
9 F# F1 E6 |3 E* c; H& }# f" uif(tipo == POSITION_TYPE_BUY)
6 [  \" |% ^, I: G) B$ z8 v) J, B{
3 S& m% d! k& s: y: |if (cotacoes[1].high > cotacoes[0].high); T  m& X; Q7 u* O. L: E9 w0 w
{3 W: R: n0 A' y% W: W  D8 H
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: F" O# ~) A4 }! p. u
info.NormalizePrice(sl);
4 j( L3 U+ a% _/ l3 F! @+ \- l' Tif (sl > SL)
7 F& R' n' H) d( ~* ~1 ~{
& m. Z+ l% d8 P% n0 j2 ~5 `7 k2 Hnegocios.PositionModify(_Symbol, sl, TP);+ j  C1 ~- W2 W+ ?0 S0 {- ]
}
$ [6 @1 b' E0 l; g1 p}6 }# ?. B- O4 }
}# Y0 U8 M( y' D! k+ G
else // tipo == POSITION_TYPE_SELL
/ _  ], _. y% [0 [3 p# T) J{
& n+ H) s' ~. d) U- \2 z5 Lif (cotacoes[1].low < cotacoes[0].low)+ k: F; m; k" F/ M% }
{
0 U4 L# u* P3 v% P3 S) C4 B) V7 ^return true;! y- q" w; I- @3 o! @6 c5 \7 b
}, k7 w) h' o: j
// there was no position
: |' J9 R% o' |return false;
" d- o- I9 L6 ^" |! L}
8 v) z& ]4 M* U9 Y+ o- g  [我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
9 G8 \# i# H, ^9 \7 Y% |1 H; A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-30 03:53 , Processed in 0.370203 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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