私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA' a6 R) J" G' H7 g
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。3 A; _1 ?8 m5 I" H7 D" u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
. Y' B( H0 P" [. o/ m以下是制定这些规则的代码。
1 b& B" h9 y" _4 j0 }, R( |, R//--- Indicator ATR(1) with EMA(8) used for the stop level...
, L3 b% `* ~4 ?int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
- x+ M. Y& g; X' u" s& }5 V. E, W. gint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
1 O# B  W' R$ }3 i1 T. ]8 @% U//--- Define a variable that indicates that we have a deal...
$ r' k% M& f, f1 W' d7 `) o3 Hbool tem_tick = false;
  d+ ~: w' v/ L& l  P//--- An auxiliary variable for opening a position
3 B: s2 N# i# g0 ?#include<Trade/Trade.mqh>7 q) ^8 Q- Y2 F: B% p$ ~- b' _
#include<Trade/SymbolInfo.mqh>, V% |7 v5 t& @. @# x& q2 p
CTrade negocios;2 e) N! h0 z$ U7 r5 H
CSymbolInfo info;
9 N7 k& U3 _7 _/ ]7 g& f//--- Define in OnInit() the use of the timer every second# {! H2 T& b7 m3 ~! W- d. q6 Q0 H
//--- and start CTrade
* R- h( Y; u9 e& D( @int OnInit()* h3 A2 e; z4 Y
{
, p# j' _: v9 P//--- Set the fill type to keep a pending order
" Q9 @, h5 {8 |: |: O+ L: M//--- until it is fully filled& A4 e+ }( G4 ^% S( X9 s
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
  S/ j3 [. F3 ^. b9 N6 }& I//--- Leave the fixed deviation at it is not used on B3 exchange( A3 a  \  U% e
negocios.SetDeviationInPoints(5);9 ]! s. i  Z3 I" x
//--- Define the symbol in CSymbolInfo...1 }$ w9 [6 @1 {7 @3 L4 M2 s
info.Name(_Symbol);
# j5 F% P8 p6 N. l//--- Set the timer...
" r" }/ i8 l5 A9 J# n5 k7 f0 XEventSetTimer(1);& l$ J! @+ E5 o" r
//--- Set the base of the random number to have equal tests...
. R3 y, w% E7 d5 ~6 z2 QMathSrand(0xDEAD);
- S9 z4 ]: M3 y# Xreturn(INIT_SUCCEEDED);; M  u0 O8 Y3 Z2 v1 \
}& o' N  m0 {* Q. y+ ?- E! ~; E
//--- Since we set a timer, we need to destroy it in OnDeInit().
) x% s' g$ V1 c9 cvoid OnDeinit(const int reason), Z$ s" d8 B' o0 B, F* {
{# d" }3 \' V: j
EventKillTimer();7 L$ `& l, {; N
}
; [" j$ |( c( X5 z& S& o) c//--- The OnTick function only informs us that we have a new deal& B2 X7 N0 M4 {, W4 s* v9 ?/ `4 u
void OnTick()
  Y4 ~) b/ s7 [# X( Y$ v) H{
6 G: k: h5 o! E& j7 htem_tick = true;- s1 |) i# K: p6 w
}& I: L) ^3 X; Y  \
//+------------------------------------------------------------------+
" V. P8 j. i, f4 \  y//| Expert Advisor main function                                     |
$ k4 Q& v* W$ E//+------------------------------------------------------------------+
9 }+ Q9 g5 x' lvoid OnTimer()
1 _, ^6 X0 z6 k+ K8 Z1 n{
1 l6 q8 |  H4 V# s! J) [8 fMqlRates cotacao[];
8 X! S& b0 k9 s( ureturn ;
" o1 G" s8 A# i/ K" h. d# H# Jif (negocios_autorizados == false) // are we outside the trading window?; w# w: m7 m7 A) Z
return ;
4 f2 e$ L0 k2 j2 g/ P, `//--- We are in the trading window, try to open a new position!& R- T3 J4 t7 z0 C/ @
int sorteio = MathRand();
1 V; ^; T6 y- q+ z7 q//--- Entry rule 1.1, R) a! A% A3 F- g/ n$ ^1 j) t& @. ~
if(sorteio == 0 || sorteio == 32767)
( b) ?6 e9 Q! p+ W" Freturn ;
# [- Z- \' L* ]& d( S% e7 k' @if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
" Y0 M* C" a0 x7 Z- U{2 J  [$ K, N* M) u1 T0 l) f
negocios.Buy(info.LotsMin(), _Symbol);
0 K; P; d$ |. C: B% y. c# k+ q}
6 j# y6 V4 x( F6 G( C; j' Belse // Draw rule 1.3 -- odd number - Sell3 H9 _2 F9 R' C( p; w' e5 D
{
) a: E, k/ K! mnegocios.Sell(info.LotsMin(), _Symbol);) _0 F8 N0 U- g/ K. ^0 ~! e
}+ B" M" t& K9 e6 m
}
% e5 z) o5 C* Q6 ?, Y% [& |//--- Check if we have a new candlestick...
8 {5 S" n; Z, ?0 K& A4 Ubool tem_vela_nova(const MqlRates &rate)
# i; K6 z5 |! r3 c2 H2 w1 T4 j{
1 S+ d/ t# _5 c$ u1 O! }{6 X: H& V1 e+ T+ t4 v; d4 R: \
ret = true;5 Y4 ?- ?# s! L7 B
close_positions = false;
4 X3 O5 |8 w. q: }4 J( b}. \$ h4 `5 p' x' p
else3 Y+ E( C# P  O; \4 d
{/ V9 X. a9 C' O/ ~0 |0 x$ V
if(mdt.hour == 16)+ u9 m& }+ {. @- [/ z0 t
close_positions = (mdt.min >= 30);. b* ^9 p8 Q2 F% J2 w4 K
}, b" X3 J7 i/ Q
}3 X, C5 k* q+ G. f5 ]
return ret;
7 k2 m) ~6 W& r: X* J+ [}* A2 W# D5 Q; E& f
//---' Q3 J7 k8 ?+ A8 z+ }% L' |
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]); N# A4 G3 U, X, I+ ~
{
$ e, }, E5 G! T4 L/ k) `if(PositionsTotal()) // Is there a position?2 [8 @3 b9 o4 J
{
8 X* A4 N8 ~: y2 \7 }  M( S5 ^double offset[1] = { 0 };& a3 u% O6 p1 G# f
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
0 Z( c3 i2 q+ f8 d* D&& PositionSelect(_Symbol))  // Select the existing position!% n3 G& B2 d0 {
{& J7 x' I- D, p  C4 c
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);3 l" _9 M3 a8 K  X/ \5 j
double SL = PositionGetDouble(POSITION_SL);' t; G' \$ I2 X
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
$ ]( c8 A+ s7 c4 B7 zif(tipo == POSITION_TYPE_BUY)
! K3 D, b) |8 P" @{: U+ @0 g: i9 g! J
if (cotacoes[1].high > cotacoes[0].high)
: ?# \8 d+ i: X9 G/ A' |{
; `! s, U) C3 {9 x8 hdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];5 ^0 _$ H% P+ q: z' h9 ]' G- a
info.NormalizePrice(sl);
7 j$ x- F- ?' H4 Z+ @4 ]( qif (sl > SL)0 `' l( }5 F2 d2 x9 [5 Z
{6 \1 l0 k2 |2 h9 d( g9 |" O
negocios.PositionModify(_Symbol, sl, TP);
$ L' i" Z7 S& E  E' N3 |' L5 w' l}
6 V: u, X/ d4 j" i" R7 D! l}+ N3 ?: ^& x. f, y
}
! G2 R. M6 e5 B$ z3 _) Helse // tipo == POSITION_TYPE_SELL
9 r; ^# b# |6 M  v4 x( b{+ `- F& ?+ `% }+ m7 q2 M1 Q
if (cotacoes[1].low < cotacoes[0].low)# V; a* m4 u# Z, J# U
{
7 j5 Z$ D* J( C" Greturn true;
3 O0 r# A; K) {! w2 e}
: e$ F: M) h: _" M9 A// there was no position1 ]5 [  E- E: i* X* s5 }
return false;
* H3 W+ V( _% {}
7 F% h; m$ {  Z% x我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
5 K1 `5 G' _. i$ G% p% w0 Z到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-24 05:43 , Processed in 0.445516 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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