私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
6 _$ V& c4 w/ P8 W在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
( T1 s4 y3 E8 i* J+ W为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
. k0 j( y% ?& f8 O以下是制定这些规则的代码。- Y- e  H4 e* w/ b( G8 V
//--- Indicator ATR(1) with EMA(8) used for the stop level...% O! P; H  p+ m5 V7 m# R. f6 r
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
$ ]  |# p4 Q" m3 m. h% O) |( nint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
* P+ F% A" L! @5 h8 s/ G//--- Define a variable that indicates that we have a deal.../ o  g% J( T4 J$ h4 y8 e9 W* g
bool tem_tick = false;8 r& j( @& g- u
//--- An auxiliary variable for opening a position" u) z5 I% z+ u; q1 j0 Q
#include<Trade/Trade.mqh># G- w6 l# ^( H& m+ y3 U
#include<Trade/SymbolInfo.mqh>
% K3 p- N# |, ?CTrade negocios;
5 k* L8 t9 L. q* Z: cCSymbolInfo info;
- L/ m- |! A( `1 `//--- Define in OnInit() the use of the timer every second
. A' m  K4 ?3 m' c& i//--- and start CTrade) `; U& ~& k" u8 j2 J) N$ {% e
int OnInit()) ^# J0 M$ {, w. C$ t( S+ L; X
{$ m- V0 L: V$ N/ }1 o3 ^4 K
//--- Set the fill type to keep a pending order
- f; [/ Y6 J5 V7 v, ?. |//--- until it is fully filled
/ n  t' n8 {$ e' m0 D! rnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
5 E' w+ T! W" J$ x2 K//--- Leave the fixed deviation at it is not used on B3 exchange
' j7 }6 \+ h: Ynegocios.SetDeviationInPoints(5);6 q  x6 n( d; e# o: h; g  O7 K8 C
//--- Define the symbol in CSymbolInfo...) w0 w$ }7 ~9 d
info.Name(_Symbol);
' \* y8 z9 n: [0 W' p7 `6 a//--- Set the timer...
8 D: m# F: b2 B8 ^. h& x" G0 BEventSetTimer(1);
) b- ~# k7 \9 E5 n: S% P7 k* X  O8 |//--- Set the base of the random number to have equal tests...
: g* c3 E9 X) l" OMathSrand(0xDEAD);: w, n' d3 _/ z7 l/ T' b3 [% c
return(INIT_SUCCEEDED);5 R- j; J: M' ~
}
4 b  f7 n/ L' A$ U$ V& N, z//--- Since we set a timer, we need to destroy it in OnDeInit().' y! Q" q7 K5 [* X
void OnDeinit(const int reason)
# |8 B% _8 d* e/ s+ v+ J{
& z; k: Q' J( G; cEventKillTimer();
4 p/ A! q: I0 M8 J" A}
- b* f+ W- n- W8 ?//--- The OnTick function only informs us that we have a new deal
2 X7 K, B% Y$ C9 z, f4 yvoid OnTick()* a5 s) O" g3 \9 J: f, j8 x6 ^
{
: g: s5 c1 d. \9 {- dtem_tick = true;& E/ S  B$ z- O
}* m, U$ ^; f2 I3 ?/ P' s. n" D6 q
//+------------------------------------------------------------------+% g) B( W% G$ n+ c
//| Expert Advisor main function                                     |8 \( F* D/ X. n/ o/ I" \( d  }
//+------------------------------------------------------------------+( m$ P( l0 y' X2 d7 m4 w9 V! V2 t
void OnTimer()9 e9 H5 [3 q' M9 I7 B% O; H
{
" P6 y  ^3 B! BMqlRates cotacao[];
& ?' h% c9 R) n9 hreturn ;5 I+ @0 q- B# ^0 h2 [
if (negocios_autorizados == false) // are we outside the trading window?
/ F. L  B9 F, R: M# Nreturn ;
0 ]' U- j2 T" r! J( K2 K* d//--- We are in the trading window, try to open a new position!
. [8 `" S+ h2 w$ K; rint sorteio = MathRand();: L! h7 C$ |% R% M. }: m
//--- Entry rule 1.1+ ?) F& ~3 U, f+ E4 k
if(sorteio == 0 || sorteio == 32767)3 D) n8 p" H2 C  b0 h2 s
return ;: X. t/ K* \2 W" F0 _# ~- c! i: H( H5 Y8 g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy) M( E& `3 n3 T
{2 k4 D2 R" k9 g  s/ O$ K6 @8 X
negocios.Buy(info.LotsMin(), _Symbol);
. E3 k! k0 n0 Q* ^7 {$ E}
  J% C# A. H3 E. Belse // Draw rule 1.3 -- odd number - Sell% U& [0 U8 t( F$ t3 ]( a
{
/ R' \6 G! W; c- h) z( Y9 S" Snegocios.Sell(info.LotsMin(), _Symbol);. F9 k" m& |- O, G
}
: ^+ U  m+ x4 F8 H7 u}& y4 ~3 H& }  X5 D* G
//--- Check if we have a new candlestick...+ N  V: M0 T9 q9 G/ R; a
bool tem_vela_nova(const MqlRates &rate)% W$ o: Z' ]- I9 \2 _
{9 I7 a9 j3 T3 \
{
0 ~( `' c3 j. e) ~5 W  u8 ?' Dret = true;2 D: L8 B$ Z9 }4 [3 O; C5 y8 a; S1 h
close_positions = false;- F& W4 \+ n6 P
}
0 k) r) X) D  W* A6 ~; e( ielse
$ o& g$ {+ {! Q! b7 C' ~* q{
( R% `/ f" O5 u: l4 w) _if(mdt.hour == 16)4 M8 L/ p. c& V0 |9 }
close_positions = (mdt.min >= 30);
& }: B# g! p9 o+ v% r) w}3 L; k9 p* L) r5 [6 Q, H" s, |9 P! F
}
. X% R. u+ X8 ~% I  L0 a& J0 M. T8 yreturn ret;2 M" M8 }' Z6 [8 N7 l/ z0 h9 d
}
$ L/ l% U6 a& Q1 D% b0 G//---
; [; N( ?- l* M  [: u' F6 Y/ ibool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 F5 ?5 s8 }* S% T$ M
{! G- t4 {: Q; w1 o  ~, _
if(PositionsTotal()) // Is there a position?" }$ ]* H; `1 p. h1 s* n3 K
{
* F4 i5 x" V- M3 N& Xdouble offset[1] = { 0 };
' [: v" r1 j1 |; o! kif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?4 w$ Q5 P" }) W" z7 b" x! W
&& PositionSelect(_Symbol))  // Select the existing position!
9 F+ Y7 S+ x) S# V; D{
3 E# B: `; T- f" I, MENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
4 i6 Y9 F3 Z3 l, j5 j* r0 f' Odouble SL = PositionGetDouble(POSITION_SL);
+ U* R& N( |/ t- J4 `: adouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; k5 Q/ n: v) `  x7 Uif(tipo == POSITION_TYPE_BUY)/ L# `; ~* ?5 j4 ?& b" K
{
( j1 E7 q/ F4 K7 `. R/ v: Eif (cotacoes[1].high > cotacoes[0].high)
, Z3 W( h( A( j, E; z) T$ n0 l3 O{; Q) W3 d8 q6 Z' T; c: Z4 C6 @
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
' z# H# v& T6 l; ]* p% l6 binfo.NormalizePrice(sl);, _; {" H& b6 K  y- ^; O! A+ Y: o; ^
if (sl > SL)
% H% {' j9 f) E# E# J3 J: ~{; S& U5 Y/ G: R1 T1 F7 P4 ?5 X9 y
negocios.PositionModify(_Symbol, sl, TP);0 N  V: O+ E  \% q
}
0 ^5 m# @; Z, Y, @}" v8 [" @. c/ T+ U$ x3 J
}! o- f) V0 E/ l# l: x& H
else // tipo == POSITION_TYPE_SELL
1 i: _& ~2 O& L: |2 d' P{  o* B* I, G# V/ W; l% r* o
if (cotacoes[1].low < cotacoes[0].low)9 @7 c4 m; Z1 c# Z& n
{
& d/ _3 r& {6 |: w5 }- O! S6 n* freturn true;. \: D7 r9 U0 }. m* Y
}
8 l) E4 {6 \6 ]8 b: F// there was no position
& j9 ~/ r1 m; g' h7 y! Rreturn false;& x9 a6 o, t& b4 p# r" M* D7 m4 ]
}! z% q9 G2 f2 u  ?& b
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。0 e( A. |3 f, Q0 P' s' M
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-22 19:15 , Processed in 1.193365 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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