私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
, E; _. S* X% ]4 R* \在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。' V2 [% y# X/ V/ K
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。% @; K% b$ l% `; o
以下是制定这些规则的代码。
# p" E/ d6 M" ~8 [1 y% y//--- Indicator ATR(1) with EMA(8) used for the stop level...
+ c( m* |% V( n$ n) n" Tint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);( b& {: h$ i0 d
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);/ x6 d& T) y) _. c5 \, E
//--- Define a variable that indicates that we have a deal...7 B2 `6 g1 e6 `0 E0 f# p: w; v2 Y
bool tem_tick = false;: T7 H% S  Q9 j& h9 V/ ]8 @
//--- An auxiliary variable for opening a position+ ?0 @( H& U3 ?0 V
#include<Trade/Trade.mqh>
& A: q9 t6 K2 j$ O" F. B, O- x- g#include<Trade/SymbolInfo.mqh>/ {9 x6 d$ h6 K; N. u
CTrade negocios;
' f0 o8 Z  Q+ D3 a+ BCSymbolInfo info;
: g8 n4 q6 h% V) H//--- Define in OnInit() the use of the timer every second
% k9 U1 P8 \+ j5 M! V( ?2 {( m+ Z//--- and start CTrade  Q  D$ F$ x2 @) Z! h: G2 {. F2 G
int OnInit()
5 [- H3 h( B( `5 w( F' x{' j3 {& R+ a% @; M" x
//--- Set the fill type to keep a pending order
" r/ |. T) g% `  R# S- b4 D3 A% |* M//--- until it is fully filled$ b& \& \4 z% b. D
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
& M+ a1 y% Q' W! h//--- Leave the fixed deviation at it is not used on B3 exchange9 k( p! [/ g- u. y. b
negocios.SetDeviationInPoints(5);* I9 ]/ X8 J& j; m$ `; |
//--- Define the symbol in CSymbolInfo...
; A2 P2 J5 s. g. m$ M+ hinfo.Name(_Symbol);7 V5 H) \! x7 q6 a: [
//--- Set the timer...
, P) @9 b: ^4 n. B% B7 lEventSetTimer(1);
9 Z$ C5 b& i! E) e( m# b; L& _//--- Set the base of the random number to have equal tests...4 W  q1 p; g0 b3 J" R+ M: n
MathSrand(0xDEAD);
( j* D. L6 e& G) K& s  L' x2 rreturn(INIT_SUCCEEDED);
, D3 \& l# ^( }  ?}, B2 I7 v4 X: D& J' I$ H, r
//--- Since we set a timer, we need to destroy it in OnDeInit().
# U( Z% `. _5 P" N9 nvoid OnDeinit(const int reason)
; g' f, Z. e& h( h2 w" u% `{
1 r$ a' s5 Y! _( J2 C% @7 BEventKillTimer();
) R3 C0 w3 A* @: p: G  D- d: l2 l}& \, ]. q0 {! Q5 c7 _
//--- The OnTick function only informs us that we have a new deal
7 {$ J: \9 d; `void OnTick()
& i6 M& H- }$ P: j" F& ~8 s{
6 F+ _1 x# b1 K: h' Jtem_tick = true;8 U6 p; H$ R3 w$ q8 A
}
6 ~4 [7 c0 E6 O( ~; W, `//+------------------------------------------------------------------+3 U5 [9 ^* J2 p
//| Expert Advisor main function                                     |
% ^! U$ }" S5 [( b! u//+------------------------------------------------------------------+
, R: j. k" l0 Q: r1 H  Q3 Z, T' dvoid OnTimer()* ~1 q" S5 @8 e2 I: T
{; W# t9 f" w' q0 O! D  E7 d
MqlRates cotacao[];
" p5 H! [( Q) }/ l9 l+ H8 S6 zreturn ;  D8 _$ R5 S5 e5 B% I! r! z3 T! u
if (negocios_autorizados == false) // are we outside the trading window?& S9 D3 }3 e0 ]
return ;9 m8 A. L' X) T/ J% G0 n7 v
//--- We are in the trading window, try to open a new position!
1 h  v) ^" U+ E3 r7 ?" K. y% Zint sorteio = MathRand();
! d& f( K2 v% i9 y8 f//--- Entry rule 1.1
- p/ p& _/ T4 G, G4 |! yif(sorteio == 0 || sorteio == 32767)" P* l9 c; Z( ]3 N
return ;* l9 U, w" T+ `# D& c: K
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
9 u% f" }1 J9 z5 _3 O8 w+ x2 _{
/ R7 x: V7 K8 Vnegocios.Buy(info.LotsMin(), _Symbol);, p( q4 q! p$ o5 \; [& F& }/ C
}
( O, O2 z- t) velse // Draw rule 1.3 -- odd number - Sell$ M! I2 O8 J& Z$ L
{9 ]3 l7 i1 c! A9 F) X
negocios.Sell(info.LotsMin(), _Symbol);( n- Y+ x$ C4 e
}, \* H! t5 c: x3 C3 Z
}! K- C3 L% I' g' ^, c9 S
//--- Check if we have a new candlestick...
, c0 R# S* Z' B# }+ H! e" cbool tem_vela_nova(const MqlRates &rate)) g* V) k' W; d) t$ ?# y
{* s% Q( k5 I+ H$ H
{' s- T4 y7 H+ u, x$ U
ret = true;/ z: z7 q2 i/ w( e( j2 U  M9 w3 ]
close_positions = false;
% x3 W& ]/ q% E: f: P; [- q; {- o}2 |8 v5 Y# N& V: l
else
0 X+ o$ U! W: P{
0 R. `  L) V, iif(mdt.hour == 16)/ m3 H. P! b  p: c3 @
close_positions = (mdt.min >= 30);
" R0 G- ~( ?9 T0 Y: `}
/ \: s: C/ r& \3 @6 J}
; I/ v6 x  |0 Y. o4 Nreturn ret;
/ l2 a& Z# g0 f5 @- @4 {}) G4 b% S, j+ r& O/ z* x3 V$ j
//---4 v/ w! m0 n" W5 s) i+ ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
7 D% l5 n; f' Z{1 u1 Q2 d$ h( T; Y. R2 o* x
if(PositionsTotal()) // Is there a position?1 _: w& O' q2 H  H# Z- J
{
. U* w1 l. y0 Hdouble offset[1] = { 0 };  s! L8 |# M. V. U0 ?
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?# [& O# D1 n8 p8 Y8 S$ N
&& PositionSelect(_Symbol))  // Select the existing position!
' _9 t6 o, }" `- e{
- M2 w+ u; J* YENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);+ P7 @) G1 D; s/ e2 ~4 h
double SL = PositionGetDouble(POSITION_SL);
* a: f7 A5 }' F4 O/ @% W1 U- }double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));7 s, }! P$ a% Y( Y8 ]& b
if(tipo == POSITION_TYPE_BUY)3 c' P7 S5 \0 E, W6 C8 n8 _8 E
{
/ P3 V" @8 e. ^3 s7 w3 T* Eif (cotacoes[1].high > cotacoes[0].high)4 j) f2 @4 b  o
{6 w6 }* U- E7 _" m; w3 T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
9 v/ ^, I  b8 @0 _$ ninfo.NormalizePrice(sl);+ ]# t, b) l% e) N4 w' t
if (sl > SL)
4 `" P" f! y2 z" q6 {3 b9 `7 G{+ Y: s5 n" a9 R) F% `) B
negocios.PositionModify(_Symbol, sl, TP);$ E% d0 R) ?% G( p
}
8 _& r7 B7 d2 l# d% R2 }}7 u  O! W2 P0 j! P8 l
}: _( ?& b  n7 b
else // tipo == POSITION_TYPE_SELL
: {* Q  t6 K+ ]# }: u$ M2 f{4 x6 o, e3 v) a  r
if (cotacoes[1].low < cotacoes[0].low)
8 e7 G$ f) J6 j4 D9 f5 \: V{+ b/ _2 J$ O* S/ y8 M$ _4 {
return true;
& |, \2 g$ N0 S) M4 F* {& E; V* ^' [}
. q% x4 P. y" d8 `  i! a// there was no position
. ^/ Z: u) @! b. y* P2 C) C9 rreturn false;, K) n+ j( B/ G( \; X
}
( {) |2 y6 p! V. u我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。( c7 W- L8 u1 ~7 e
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-19 21:44 , Processed in 0.374845 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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