私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
& |8 H5 Y* B  n" P! r在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
6 {7 k- I* z: D/ a为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 W( V% d4 \7 {# A; T以下是制定这些规则的代码。
8 Z  ?2 {% U5 F2 t8 i8 s% E. R//--- Indicator ATR(1) with EMA(8) used for the stop level...
% ]: W0 x" k1 q# H, d4 x. xint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);; H7 D2 v0 {$ u
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);' D8 \! a5 I: w5 \2 R( G
//--- Define a variable that indicates that we have a deal...
! R1 [$ q8 c; s) v1 g- _' V' p% Tbool tem_tick = false;: T3 Z, x, k& ~5 w% u, W
//--- An auxiliary variable for opening a position4 K( C5 x$ a3 H+ K9 e' q8 R9 a
#include<Trade/Trade.mqh>6 R' j  L3 ^$ R
#include<Trade/SymbolInfo.mqh>
9 V" i8 s+ t# S' R1 L' UCTrade negocios;
+ E' l( `$ B- r& [9 ?7 p0 ?/ JCSymbolInfo info;
8 T$ B" Q! V3 J8 T: P8 L3 E//--- Define in OnInit() the use of the timer every second
8 V; \1 ]& G6 k* l) H3 _% h//--- and start CTrade* M1 k' v" W+ n1 V, p
int OnInit()$ v# D7 T1 h! \5 d
{. r: l% g3 Y2 {# s0 F: c
//--- Set the fill type to keep a pending order& s2 Z+ ^+ j4 O# o2 v0 S; T% e
//--- until it is fully filled
$ U4 ?" `* D  C+ K' @: L! Jnegocios.SetTypeFilling(ORDER_FILLING_RETURN);' W* Q: e* u" e. m: t& g$ {
//--- Leave the fixed deviation at it is not used on B3 exchange- q  f1 Y" W7 E% p3 k( j4 k9 ~
negocios.SetDeviationInPoints(5);
  @- a! n8 N+ l$ G//--- Define the symbol in CSymbolInfo...
( B  x4 f: l1 G( linfo.Name(_Symbol);8 ~. N9 P/ b% Z  L" C7 D% U* c
//--- Set the timer...& ?6 H+ W: f5 ^# E& Z4 F0 O
EventSetTimer(1);
% D7 y. g0 `8 A7 E//--- Set the base of the random number to have equal tests...
2 k( i% n2 C3 J5 V# e4 ~MathSrand(0xDEAD);
5 M' ^" Z8 n+ f" x% s( t" n7 greturn(INIT_SUCCEEDED);% z/ ?( p7 S7 ^7 R0 i
}& W; K4 P. o/ \3 n; ~/ i  f
//--- Since we set a timer, we need to destroy it in OnDeInit().
( i* A! `3 F; }+ ]) f8 f* x; @void OnDeinit(const int reason)& \/ I/ |5 V# {6 Z. w
{1 V, B# B& b, z( w/ d
EventKillTimer();
0 x6 m! b" i. a; p}7 u$ u) {, J3 C8 t. ~$ Y
//--- The OnTick function only informs us that we have a new deal" {3 ?2 g2 j. Z
void OnTick(). A+ Q1 [( h1 ~( @5 M8 G7 f2 n2 g) K
{, s) O2 R9 l# I3 {4 X
tem_tick = true;
9 h) Q6 Q# P6 X" y6 D}# v6 y- T/ f0 m3 g3 Y$ y
//+------------------------------------------------------------------+
4 s5 z& ]+ z" {$ Y  O+ b//| Expert Advisor main function                                     |
+ ~5 ]9 s2 z! N2 F//+------------------------------------------------------------------+
: F6 M% [' Z) M$ Svoid OnTimer()$ }  D' a/ F) W. q) e
{
5 z( g. P! P( cMqlRates cotacao[];
/ j$ d) a( r* M$ creturn ;
, w! o8 f$ y# i5 |' g$ T- Q1 H. k/ }if (negocios_autorizados == false) // are we outside the trading window?7 x/ u4 ]( b* p
return ;
+ Q; g  W/ @# m# A2 R, h//--- We are in the trading window, try to open a new position!
# a: ?' w* l, \& Vint sorteio = MathRand();
' `! p5 @3 ]2 t! V//--- Entry rule 1.1
6 b& n( W! M% r! t7 aif(sorteio == 0 || sorteio == 32767)
# W. M' c  E$ |! preturn ;
0 ^$ e# L+ t8 Y: x# gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy6 K0 c1 K' R, X
{) y- H! r$ a6 k4 x
negocios.Buy(info.LotsMin(), _Symbol);
: ?- }. v& J- O}- o8 E' D1 r9 i, O( J3 t8 t+ U
else // Draw rule 1.3 -- odd number - Sell
2 u' Z% L5 h- s* V$ u{
$ l& ^9 x! l' N5 A7 X  xnegocios.Sell(info.LotsMin(), _Symbol);' l! G+ v# c4 i9 T  s7 W2 \
}
" V0 X" s. X" q; i}; {* V/ K! y* s6 r* r' K7 x
//--- Check if we have a new candlestick...
' [( Y( |* T& |4 y6 I: p9 ?bool tem_vela_nova(const MqlRates &rate)9 m; ]! \" {8 V9 e0 M( Q
{6 e( k0 h% P2 [0 @, h8 ]6 a
{5 j& ?  a4 i, u6 G$ e) [
ret = true;& U2 B0 A2 @1 b1 k( y6 @3 k7 ~
close_positions = false;  i: [: r  p6 S. ^
}) ]' W/ o% L* g1 Y$ T; i9 T
else# ~) A( H" {$ V, |
{' ^" u# c$ d2 ?5 _2 Z: h$ |7 S
if(mdt.hour == 16)
# o, v3 @/ S# N* R( }' fclose_positions = (mdt.min >= 30);7 I) O- h  r' U* {7 X( L) p1 C+ Q" A
}5 c# T* \# z9 K/ A4 W* Z& b
}) Q% g- s5 b$ b; Z# h6 l1 w
return ret;# ]! f1 @1 h# N2 ^4 d
}4 |# b- [. ~* o
//---6 G: ^! n+ a  d7 R  A: a
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])/ `! `: A* Z9 w/ z/ a
{+ [  ]) T: F  h: f
if(PositionsTotal()) // Is there a position?
2 o' l* [1 Y* P, |$ E{
- ?+ A5 ?! o( |+ }double offset[1] = { 0 };1 ?/ {5 ^3 d; t7 K, Z
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
9 t, `! Z# `. @. u2 W&& PositionSelect(_Symbol))  // Select the existing position!, ~1 Y" @* `* l
{
7 u* p2 b" l# S* J( T9 pENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);* E5 @% @% `6 H5 Q# I( S% \
double SL = PositionGetDouble(POSITION_SL);
6 w8 k' H# y  w$ h3 ~- Ndouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
5 b! i/ f) E+ B; Q/ c% m' ?if(tipo == POSITION_TYPE_BUY)
9 Z$ p8 T+ E  E. R{
6 X9 s2 F" R( K3 |0 eif (cotacoes[1].high > cotacoes[0].high)
( M6 [$ x+ r0 d% R1 G: J{& e5 E; m% S- n* y* D4 `
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];, f. B% H9 @" G2 l' b- _7 V
info.NormalizePrice(sl);
0 ^$ w( c$ Q4 X$ m4 r: S+ _if (sl > SL)
: ~* v' z! B( @& `{
) w" M3 F4 X9 t/ Q8 Y$ {! c8 e& }negocios.PositionModify(_Symbol, sl, TP);6 D$ p2 Y0 w. Q+ V* i" W
}) M7 U( O) X3 \
}
; l6 L5 p; k& x7 \# x9 H}! I, g. ~8 C% A& R- g% R
else // tipo == POSITION_TYPE_SELL
5 L2 s& ]; }7 u1 R{2 ?* N# z. Y5 u
if (cotacoes[1].low < cotacoes[0].low)% r, A: Q9 {7 v) W  e
{8 w! b5 d8 ?$ d6 s3 U  u
return true;* H7 T* B8 ^' d" F
}3 Q. C* z* S+ S1 `. t6 C
// there was no position
% u# ?: |' f7 o6 V4 B& Hreturn false;: P3 P$ s9 c. X' K% L
}
0 }- `3 v; n4 \" L3 q. P我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。' Y, I+ z; T" H; h5 V
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-3 04:20 , Processed in 0.410430 second(s), 39 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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