私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA, v1 @3 z5 N" Y4 c) F
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" V! _7 v6 {# U  O为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
6 `) ~9 r4 K2 Z0 z以下是制定这些规则的代码。' s) v) J5 S0 u
//--- Indicator ATR(1) with EMA(8) used for the stop level...5 l6 C# n/ i* f
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
6 u/ H; V) O0 dint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
& H) o  f- u7 |: b( F) }0 A//--- Define a variable that indicates that we have a deal..., G  w; {: Y4 d9 \. q4 |! U
bool tem_tick = false;8 e/ J* G/ w- ~' h/ h% B4 J# U
//--- An auxiliary variable for opening a position  L3 p- U1 ^6 Z2 @% k
#include<Trade/Trade.mqh>
& Q6 [2 D6 t3 B3 w. G5 ?" Q* c#include<Trade/SymbolInfo.mqh>; P3 D  R: A& j! n( t* c  G
CTrade negocios;* h0 c# |/ U, L" N, s
CSymbolInfo info;, S& H# \. t$ w$ D$ p
//--- Define in OnInit() the use of the timer every second
/ W6 f! ~7 z1 _/ l& @1 {6 A//--- and start CTrade
* v* n' A  U% ^! n) T6 n$ Qint OnInit()9 r7 ~: I( g- \& X
{
  }2 L$ n  R  S' ^+ j//--- Set the fill type to keep a pending order$ V, v4 C0 i$ |: C0 w- N& h( a# U
//--- until it is fully filled
2 ^# a; h! A1 T/ Q& P, }negocios.SetTypeFilling(ORDER_FILLING_RETURN);
+ [) V5 ]' h# \//--- Leave the fixed deviation at it is not used on B3 exchange  T7 T+ |6 M5 @* X" g7 Q
negocios.SetDeviationInPoints(5);
0 u( I6 l  V$ M7 o* b. \, |2 W//--- Define the symbol in CSymbolInfo...; Z  D2 Z1 n. V9 _  b3 C2 r2 v" w* \
info.Name(_Symbol);* D& `+ z2 ]$ R: L
//--- Set the timer...' D& m5 a$ Y2 c9 Z, k
EventSetTimer(1);
* G# F% H' a4 Q2 n  n7 ?! D% x* t1 w//--- Set the base of the random number to have equal tests...
- w3 l4 l7 b' J: R( |% _MathSrand(0xDEAD);% u- l( H( T$ z
return(INIT_SUCCEEDED);
% [1 o$ A8 o: b1 q}
, d5 y: r& H1 |5 e2 e//--- Since we set a timer, we need to destroy it in OnDeInit().
; K" ]- Q" n9 h- `void OnDeinit(const int reason)" T$ m6 U" `) ]) \8 a0 w
{& Q/ l; B9 v" _
EventKillTimer();
; \# H! q" w+ w: _! {( a}+ X+ q9 g( f) L2 o
//--- The OnTick function only informs us that we have a new deal
+ Q4 J1 C4 m# ^$ Q8 |) N& A2 |2 Uvoid OnTick()
& s& I+ E! `  z; Y! [6 l8 R{
7 q1 f; V  a  M# m9 i# q( Qtem_tick = true;4 M  A6 e, D  u( u, }) D- [% n1 W
}' T' ^/ O& Z% P. S; ]3 D: ^# ^4 D
//+------------------------------------------------------------------+' w/ C/ r; z2 n( Y3 o* F/ _3 x9 I( v
//| Expert Advisor main function                                     |
5 B. U; Q6 V% y( ~9 W) s/ i  o//+------------------------------------------------------------------+
, G: M9 d; X* w$ R: P8 Pvoid OnTimer()
  W# z1 y6 l$ e! d; V/ j{
# H1 l! L8 A7 G: OMqlRates cotacao[];
) t: b. i9 ]7 {% _5 Preturn ;) [. ]( N; K' t
if (negocios_autorizados == false) // are we outside the trading window?- ~6 f. i" q- t. t* n5 s6 k+ @
return ;1 B/ f. b# f8 w' P& z* c/ S+ t
//--- We are in the trading window, try to open a new position!
3 j' u+ p7 v6 e! m. _3 R" lint sorteio = MathRand();
- u" ]! O9 K. Q4 v' H- |//--- Entry rule 1.1
8 l/ y" k& K" i; f" ^if(sorteio == 0 || sorteio == 32767)
5 h% H- G( B9 ?  y# l- A; e# t5 F. C& G7 [return ;
: }* S# L! f9 e! ~+ F& j# ~if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy& D0 U+ p. E% y3 R
{/ Z+ o& _& r# O. F' X4 e
negocios.Buy(info.LotsMin(), _Symbol);7 n3 ~1 G( R$ Y
}, }# O! S) t& j5 H
else // Draw rule 1.3 -- odd number - Sell
2 Z5 M% q/ A+ i6 P0 l+ h{
/ D: U$ _  x8 n% C* O. ]' C/ Snegocios.Sell(info.LotsMin(), _Symbol);
# g( O- c0 m  T/ Z! r  S}
. N! h7 p3 J0 K9 o8 G: F6 Q; F% [$ B}, R$ U: _5 N3 T
//--- Check if we have a new candlestick...
, A0 {$ F6 z4 p  Bbool tem_vela_nova(const MqlRates &rate)
- T7 ?' E* y+ v  i6 j% |{
7 ]( @; U* ?! |6 P( o{
; h9 o. G. U0 H0 j: X! Nret = true;
5 Z- A6 \+ }9 R3 N9 Zclose_positions = false;# ~6 ]; R5 B5 V. ]
}
) D, B) w$ ~. r6 ~& L2 Relse( Z- x8 t- d( h
{$ y) g+ l9 C. ?  F, E# }# Y' o# j
if(mdt.hour == 16)
  K3 y. c1 d  K/ k# Qclose_positions = (mdt.min >= 30);' K7 l# ^- R* P
}
) Z4 [. f$ u. q# W7 V5 _6 n0 t}
- `# V: N+ }' I0 ~return ret;
. P' ]9 F+ e5 d6 `}2 H! Z, c0 o% n' J7 h! X6 d
//---
- @# n  Y/ S0 @bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])0 x6 w! `7 ^8 @
{
% R* a% @! Y; i* G0 Kif(PositionsTotal()) // Is there a position?
( f! M* z+ ~1 x: w% R6 G{
& C9 @$ ^5 o3 }4 }double offset[1] = { 0 };
$ a- \* O- N* Z) Y. z  d6 yif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?6 j, d# P8 Z0 J' _% ^% y! W& ]
&& PositionSelect(_Symbol))  // Select the existing position!
" l5 [) Y. G" G7 B+ l{! i, s2 {% A  t
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);# g) {& M- S: l+ b6 R6 h: K
double SL = PositionGetDouble(POSITION_SL);/ D! D4 ?% H8 L! d
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));7 J$ Y; ]6 p% T4 T
if(tipo == POSITION_TYPE_BUY), M, Y4 s: u3 t& r4 U
{# M: v3 v' i. M0 @
if (cotacoes[1].high > cotacoes[0].high)
+ a6 K  h) X. ^. S8 \{' p/ J5 I( Q% W' T$ h
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];: J" b( Y! e# E2 x. _9 S
info.NormalizePrice(sl);
/ f4 z2 F! I% cif (sl > SL)+ X4 y2 X: b9 Q( x( T% ~
{" `" v: Y. k# d7 d" p! Q' q
negocios.PositionModify(_Symbol, sl, TP);$ q  T% z/ e2 ~" [- f9 c
}
/ y* d3 P+ q5 }}
( p3 w( e/ M, Q" {. `) P& L' G! c  \}2 K9 Q4 W( W/ R9 O) ]+ u: i
else // tipo == POSITION_TYPE_SELL
6 t# D. J% B# |  Z" z' O{
" N! {8 ~3 Z; E$ v8 xif (cotacoes[1].low < cotacoes[0].low); P0 |9 j  Q: g& K8 Z
{
; q$ `/ B6 |- J2 D- }9 I3 O/ mreturn true;
# S  W" D, K9 a- c8 L; A; Y}8 G6 r4 G' C1 [& `
// there was no position4 ?2 P' B- b8 a! X8 |. k
return false;2 I$ g' l6 J3 Z) L  J
}
. @% j$ Q; Y& G8 V我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。! X, x# j5 a  d5 ?" x; c
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-5-4 04:17 , Processed in 0.421407 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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