私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
' W' d+ B) E6 F3 n4 J在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
; d( \& T7 `( h! g6 y+ C为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
4 C7 E1 P) A& Y7 U& d' D以下是制定这些规则的代码。, S" G3 T- K' @# Y5 a  U
//--- Indicator ATR(1) with EMA(8) used for the stop level...
( x7 c) T6 q8 Pint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
  I( W% ]. _8 ^; J/ @4 h, b( s. J" sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);$ h& w/ o4 o+ E
//--- Define a variable that indicates that we have a deal...6 S* c7 ^$ ]8 p4 w8 i
bool tem_tick = false;
& c" ?; k, j/ k# b& l//--- An auxiliary variable for opening a position6 ^0 Z6 E+ o# M# z
#include<Trade/Trade.mqh>
. S/ G3 ]2 u; A; b! \#include<Trade/SymbolInfo.mqh>* j2 e+ \% \$ v' \9 i
CTrade negocios;6 V+ d1 D+ A3 B  V5 ?
CSymbolInfo info;
3 f: ^8 W  r& l0 T" a//--- Define in OnInit() the use of the timer every second
; z+ A, R; q7 f* X) U- z# u//--- and start CTrade
7 t& ~. }3 b2 I6 N- X$ Jint OnInit()
+ [: ?! r# k/ f% X% }/ Q{
7 R6 t: N, T7 {& H6 t7 P9 O//--- Set the fill type to keep a pending order
8 h' Y' Z' O& ~. U7 D( l//--- until it is fully filled7 ~3 {' t9 J% H3 t% U# r
negocios.SetTypeFilling(ORDER_FILLING_RETURN);9 \+ H/ j- _1 l, X8 ~2 w
//--- Leave the fixed deviation at it is not used on B3 exchange
$ R+ b$ }; g  s/ @) \negocios.SetDeviationInPoints(5);& m+ v2 M6 [( x" R4 W
//--- Define the symbol in CSymbolInfo...; f8 `0 @; x1 Q* X, R# f
info.Name(_Symbol);
" }" J# d% j% r* Z  v9 D//--- Set the timer...
7 {. M3 [7 Y, r# m6 g. b+ f: cEventSetTimer(1);# X) t! V; W, s* R
//--- Set the base of the random number to have equal tests...
+ |# o5 p8 D0 I. M" K2 cMathSrand(0xDEAD);
/ s6 Y. f" c$ l: Nreturn(INIT_SUCCEEDED);, M0 b7 @$ k5 f0 D& Y* y" O
}2 P$ ?+ `7 T0 G" q3 T" Y& ~6 y# u
//--- Since we set a timer, we need to destroy it in OnDeInit().
( l; u4 N$ R. Bvoid OnDeinit(const int reason)
: z( X# z- l$ w6 ^& C/ _+ h{- Z: Q; L$ M* k
EventKillTimer();
( C* E7 I" z* y/ g2 v' e. m, I- c}5 g2 D3 L0 e+ ]2 V0 _: L
//--- The OnTick function only informs us that we have a new deal1 u7 o# p0 k* d  n) s" Z# N
void OnTick(), x& d8 y# ]7 m9 c4 C" n
{
) D* X8 @8 I3 q: C) P$ c+ H6 J# F) @tem_tick = true;
4 d2 R# B/ G5 d) k}
! o, k0 j: V& M: t! t3 s//+------------------------------------------------------------------+
5 w6 P# Z; `  Z4 ]7 Q0 P6 }//| Expert Advisor main function                                     |
/ ]5 ]+ k( ~7 B/ L, {. d  c' S//+------------------------------------------------------------------+
+ w% Q1 _2 M8 h( @( |void OnTimer()
2 a: I  ]/ _5 f8 s4 G" N{; z/ a7 ?) c& H2 V4 y+ _
MqlRates cotacao[];
% d0 R  }, z% N; |6 R; b4 Creturn ;
1 w( ]. N8 x# M/ S6 L  c1 f7 Oif (negocios_autorizados == false) // are we outside the trading window?5 O5 {: j; X- g
return ;) _* G3 G$ s8 S+ a6 S2 R3 c' G
//--- We are in the trading window, try to open a new position!
) |2 o( R  i/ q& ]int sorteio = MathRand();
4 W9 {  d* S4 B" U//--- Entry rule 1.1' `2 e" i! T6 N0 o: Z" e! p1 K
if(sorteio == 0 || sorteio == 32767)
9 F8 F" m$ g% P* Z* C8 C3 p& [return ;/ a* [1 [- [; I( N
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
( @/ ]( ?; {: V6 x5 p* l4 e{
; s. B9 n% v. W# z! Enegocios.Buy(info.LotsMin(), _Symbol);# Z6 W, A. z0 W, c  d$ d
}$ t. y$ R' r! C" ?/ _
else // Draw rule 1.3 -- odd number - Sell7 V* q% n# Q& z2 o
{; [: R9 _# t- z. T0 @  K7 Q
negocios.Sell(info.LotsMin(), _Symbol);+ K, r# ?) E5 N, Y$ ]
}* K& h* k# [* h3 m6 O' ?; E& v" V
}
! x6 v4 |5 _# f//--- Check if we have a new candlestick...4 C) s- [' t6 m* d% Y& L
bool tem_vela_nova(const MqlRates &rate)
/ y8 l$ I3 H8 p8 k{
, X, F: J& k! |( a6 w4 P6 o{/ \8 j; s& V5 G$ j
ret = true;
/ q! [- G3 H  ?% zclose_positions = false;6 j7 L& w( W% d4 }% q& \2 K' W' f
}
) B9 R  q9 @& K( }% ^else4 j2 b2 a" m1 F8 Q4 L. {4 O
{! J2 M) `2 T  U" n5 l& T8 f0 r
if(mdt.hour == 16)
) ~8 n0 P. W7 a& rclose_positions = (mdt.min >= 30);, _& R1 U; s: b
}; J- A- K/ u2 F1 V) ^0 b
}
: o, F1 @6 T' m$ `return ret;
# e3 F' i8 j. G# t3 u}
) J+ d/ ^. Q# m! ^//---, x5 D+ K$ ^' w% r
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
& L; U" s7 `6 ]8 R+ C" [{
5 d! f. v0 D! @# T* dif(PositionsTotal()) // Is there a position?
7 N7 ~  U! @, c* j! R5 I3 \- J% A{  G* O0 ?. {6 t
double offset[1] = { 0 };/ C9 }1 P( [6 _+ z) s* W$ s
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ x4 B4 `+ O$ A5 j&& PositionSelect(_Symbol))  // Select the existing position!
5 z. V5 I; w) G% X{
7 T8 n$ O; m/ i5 k0 JENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);" G1 x2 W* _9 k: N6 y! J" l. l
double SL = PositionGetDouble(POSITION_SL);7 h. R6 ^; K( Z# [: E, `
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 b* L: m1 E% i5 z: V3 s& ]! h9 s/ ~if(tipo == POSITION_TYPE_BUY)( b* d2 f% G! _. f; o
{% H7 s: m1 Q7 h- z" \
if (cotacoes[1].high > cotacoes[0].high)
5 V6 r4 g5 i. y4 p- o, J! \{. F& i+ I+ w7 \5 ?" D
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 b4 T3 r* W# L$ s5 o0 Cinfo.NormalizePrice(sl);
' p6 A) Y6 A, zif (sl > SL)
3 V; `5 D" o7 `- M, u{
: A+ z2 }& p+ P: V# Inegocios.PositionModify(_Symbol, sl, TP);
% {' X1 s0 I* _5 o}7 }7 g0 P% @! A) R3 j
}
5 A6 m3 A9 w" E* Y8 r2 `8 i7 B$ H( V}
9 W! r: p" C& w3 gelse // tipo == POSITION_TYPE_SELL
3 j5 K& `7 T2 n5 _{
4 t; ?) e* Z, S) l& k4 Tif (cotacoes[1].low < cotacoes[0].low)" k2 Z8 L- G8 f( S! F. S
{7 m+ c; w" t! y4 W3 f2 {$ P5 y+ r3 ~
return true;% ~. e+ \. K4 f  ~$ c# v
}. J, d: r' J/ D& G% G6 u
// there was no position
* r5 n8 f: y# ~6 I3 u4 ^6 p# W' creturn false;
$ w0 X+ P: V% p" I1 K7 Z}
, u4 u6 a7 i7 r8 l我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。' P% s- W7 e5 q( l3 F7 e! A' @
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-2-3 06:53 , Processed in 2.054380 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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