私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA# _' Z9 B5 L9 H. q# |5 ], A5 F1 C
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。/ j  `; O: X9 p9 j
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
0 P  ~. V1 N) f6 m, }以下是制定这些规则的代码。$ ~4 Y: [8 B, y* m. J' s+ N
//--- Indicator ATR(1) with EMA(8) used for the stop level...
9 x, l% {; `, V1 p9 R3 i( Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
8 M5 k% B! A* E' q, Y' nint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);7 p8 V. h. B+ D( g- x/ D1 a
//--- Define a variable that indicates that we have a deal...2 e+ I1 H) {. i* W  z/ ~
bool tem_tick = false;
  K" b. Z) R% {0 t2 O//--- An auxiliary variable for opening a position
, r2 v/ `6 b: {, I; D. z* V#include<Trade/Trade.mqh>5 c' c" {. d) d) D7 J/ E+ h
#include<Trade/SymbolInfo.mqh>
3 o% {8 z4 s' ]- i- iCTrade negocios;7 N$ D; d, w: V5 Z
CSymbolInfo info;" T0 U% I, ^/ S  ?8 R" J$ {
//--- Define in OnInit() the use of the timer every second- z  N4 b# ^% F- o2 w
//--- and start CTrade! \+ o& w& Y: F+ U
int OnInit(). U: A% K0 Q/ N
{
) \  j% t8 H0 T2 a, \  ]//--- Set the fill type to keep a pending order
4 |  m4 f0 {- f! Y5 L0 R//--- until it is fully filled) G: ~9 k* L0 x
negocios.SetTypeFilling(ORDER_FILLING_RETURN);. V$ Q# k% Q5 P
//--- Leave the fixed deviation at it is not used on B3 exchange- A$ o2 U. F3 k/ j2 s
negocios.SetDeviationInPoints(5);
, P9 ^2 {9 j' M: w//--- Define the symbol in CSymbolInfo...# I) X5 _- B5 p: i3 @; n
info.Name(_Symbol);
4 r6 A7 w1 k2 }) {; f, `, V: a+ J* m% g//--- Set the timer...7 w& C6 R+ k9 r
EventSetTimer(1);! I' e! b8 K3 U. _( X* \5 J2 N
//--- Set the base of the random number to have equal tests...
' j5 f# u0 s* t6 `9 nMathSrand(0xDEAD);$ `1 x! e9 _- u1 e/ P
return(INIT_SUCCEEDED);# ^4 L- Z0 I# [! d
}2 L! m( \2 `3 V/ N  G5 Y
//--- Since we set a timer, we need to destroy it in OnDeInit().  i4 d4 j* x0 \) R/ o
void OnDeinit(const int reason)
% a) g8 g7 f9 @1 p; u2 O; m5 {3 c{
. d1 Q/ S1 ?# S7 ]4 C" z* ^EventKillTimer();
" l% _: J# m# h}8 E, O1 r' I( p$ k8 C3 n
//--- The OnTick function only informs us that we have a new deal$ F2 E7 B3 n5 F' _  I( K6 E
void OnTick()2 c( R% l# T% m0 R5 n* |
{
0 s' d/ z0 |& Q0 ^tem_tick = true;6 r" p2 S( |7 ^% K! g$ `
}% c. C+ `4 f8 X5 G2 J5 l
//+------------------------------------------------------------------+/ W& i$ V% H0 B4 W
//| Expert Advisor main function                                     |, G/ Z( I' ?2 M3 U$ g2 E
//+------------------------------------------------------------------+. h  f8 [- T! t6 T  z3 k1 M
void OnTimer(), l- y. W: d) Z/ k3 q& k0 \2 g
{, k0 u, b5 r, Y% s3 f
MqlRates cotacao[];
) f" V8 x. E3 xreturn ;
- ~* Q9 d1 }. V$ ?, @& }if (negocios_autorizados == false) // are we outside the trading window?
6 r1 k3 d7 O; r  R, C0 Freturn ;" s/ Z% P6 r& F% {* S. m
//--- We are in the trading window, try to open a new position!
! B! V* U( ?. F7 Y( dint sorteio = MathRand();: L$ ?& o) i$ Y7 ]& B8 |9 k$ d
//--- Entry rule 1.1
% b* ^+ t; ]# Jif(sorteio == 0 || sorteio == 32767)
* {- a' c7 r) Q; C, V" \) B# wreturn ;: n$ M; a+ y/ h# P. t
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
8 t2 Y: n3 y& E* \) m# `{- U+ c( W. v! N3 ~
negocios.Buy(info.LotsMin(), _Symbol);/ g$ ]8 T: }7 b" T% G' e
}1 Q  h: m- a6 v+ _
else // Draw rule 1.3 -- odd number - Sell
# y9 ^3 P: g* R5 r{7 m( U: B, i8 p" H/ F1 n* |. k0 ?
negocios.Sell(info.LotsMin(), _Symbol);7 d1 o0 R5 Z' m
}
+ `5 ]3 ~9 I1 d7 t* |! u# t1 c# G' E* y/ y}. ~/ A* G4 q! h$ Q. k" `
//--- Check if we have a new candlestick...
; s' C8 ~; q  Z! W' t! G* \9 gbool tem_vela_nova(const MqlRates &rate)
. u( m) k4 C3 ~8 K# E* [* Y- e' C" h* {{$ h9 K+ Y4 L/ L  G1 t! i4 h8 Z
{* n! m9 M* H* ^& z# @1 e
ret = true;
$ m! _0 u1 t$ U) R7 k7 xclose_positions = false;
: H" k  ^1 C/ z7 u. u9 {}
; ]4 \/ M  E$ q! \5 f0 Melse
" Z* s* G6 n- F: e{- g( P: w. p& N) M- Y0 H8 D9 f
if(mdt.hour == 16)+ u; ?& N/ Y+ p  D/ X4 R' v& g6 k9 T
close_positions = (mdt.min >= 30);. D* g; y) {) G+ h9 ^  R
}8 U  P6 k0 x3 K
}
( s! F; c) E6 r2 F  {4 Dreturn ret;
0 X- B6 P5 B% ?4 P( ]  N  ]}  B7 D( C: t- ^$ v
//---
- ?( `5 J) i+ j, k, z0 g% ?3 vbool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# e/ ^& ~$ ]. L2 g3 ?
{. Z, R& R3 Z5 U" Y; \5 Y* I! N
if(PositionsTotal()) // Is there a position?
, H, i. f: v# I{
& e8 x* I0 l( \+ Mdouble offset[1] = { 0 };
9 |# i$ B- _/ [if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?& N! K8 j7 _2 |+ _8 w
&& PositionSelect(_Symbol))  // Select the existing position!8 W# S: I0 D( ?% B
{" E! G- N0 L# I" q$ U
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
5 E6 q- Z$ T$ `- e9 |- Wdouble SL = PositionGetDouble(POSITION_SL);
4 N! f" W8 i+ `" Adouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));- J1 e- N( j% n  _
if(tipo == POSITION_TYPE_BUY)) q9 G" k5 j# ~$ T  c( p
{
; J& j* @7 k) w' eif (cotacoes[1].high > cotacoes[0].high)
7 q& \9 e. d* G# Y8 d{
1 I& ?7 y/ `0 y$ Fdouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];, D# G1 J. ]6 F6 C/ C- y; N
info.NormalizePrice(sl);, v  r2 X3 Z* Z; J- n, j
if (sl > SL)/ E" B: w7 d' |6 T' H0 |$ J
{
% d2 p1 E. \1 C# u7 M' jnegocios.PositionModify(_Symbol, sl, TP);5 C) t  g( Y6 a1 ]% ?
}3 s2 c! n1 u6 |3 Q; G1 T
}
7 q  w* b# B9 T1 |* P# b}  K* J) _- }0 K! U, Q
else // tipo == POSITION_TYPE_SELL
/ |. q8 v; f  O{
! t. q. s/ @; k8 ^4 c+ pif (cotacoes[1].low < cotacoes[0].low)
4 @$ n: V  s! H7 k% [, ^( z{
3 |* W; d' H/ l& Hreturn true;1 [2 k# U5 u8 @/ }
}# K! M: }/ N9 W: ]  ~& @
// there was no position$ w$ [8 Z" N, w: @1 u
return false;3 t7 y3 C$ G( M$ t: x; i
}
% b* ~' Y4 V) T# X: b4 W! \我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
% l9 w" b, F! B) b& J5 E到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-17 12:55 , Processed in 0.413933 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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