私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA3 b. Q/ }6 I1 m( `6 l! Z
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
* z# q% ~2 f+ ^为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) `1 T6 K9 f+ ~) A7 j- m; \8 w以下是制定这些规则的代码。- f- d2 `( [5 V$ C; [# {
//--- Indicator ATR(1) with EMA(8) used for the stop level...4 V6 m/ Z4 O+ \# D  O* E
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
7 h$ c/ M4 D6 a1 sint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ f" S# [  h( P. t; |4 ?1 Q
//--- Define a variable that indicates that we have a deal...! S9 m" L! k  |4 V1 @& h
bool tem_tick = false;' W: ]4 J" y5 B" ?1 e
//--- An auxiliary variable for opening a position
4 o" p2 K0 [1 i  x# I( J#include<Trade/Trade.mqh>. T% {- k) S  c" ^0 e
#include<Trade/SymbolInfo.mqh>
: o$ A- y% U# f7 _1 `CTrade negocios;6 k" W% h/ k" `9 L, o
CSymbolInfo info;
2 d( H" ?7 Z9 [( t" V& M6 k7 Z//--- Define in OnInit() the use of the timer every second
2 ~. w# u( r* K5 {+ v# Q//--- and start CTrade2 X' D; ]6 Q+ R+ I6 E
int OnInit()4 R. Q1 B, ?, c
{( K) @4 g+ N  l7 i' ?8 P
//--- Set the fill type to keep a pending order5 N3 d5 E! K' R
//--- until it is fully filled
4 Y9 ~- d% J) v5 anegocios.SetTypeFilling(ORDER_FILLING_RETURN);1 a+ ~( i+ b1 ~6 u# x: u
//--- Leave the fixed deviation at it is not used on B3 exchange; V0 F) F: p, N% D
negocios.SetDeviationInPoints(5);
& O- Y5 z! z& O, N2 m% e//--- Define the symbol in CSymbolInfo...
% O& z& D0 F' K% v1 Xinfo.Name(_Symbol);3 Y* \# i  E; F. g+ z4 L+ ^. V  }5 e
//--- Set the timer...
& J* v" `. W! j7 I' Z* U. XEventSetTimer(1);' y  L! |% I" u, s  A2 ^
//--- Set the base of the random number to have equal tests...+ a! t* M5 C1 A8 C9 y) k) }
MathSrand(0xDEAD);. J0 @# C/ C: \! w+ x- W
return(INIT_SUCCEEDED);
- v4 `- {: c0 a/ g}
7 w8 N5 z8 l6 z/ g# f% R//--- Since we set a timer, we need to destroy it in OnDeInit().% e# K. |8 H# E. e6 P( H6 S
void OnDeinit(const int reason)
3 U' |5 ~; F# {7 [" G" T0 L{( d& W( ~% S" E9 V
EventKillTimer();* \+ F% z; j  s8 a* }- S
}1 k6 w4 K/ k/ f
//--- The OnTick function only informs us that we have a new deal
/ r! @, _# p+ b4 M2 l5 w* C: e- i  lvoid OnTick()* M; k4 O, e' {5 v6 x
{* J7 j" K( Y* M( V
tem_tick = true;
) i! X8 x5 M; ^3 `}
. T& D- |7 x' A& N+ T//+------------------------------------------------------------------+
3 A+ W1 o1 P. K" r+ `4 B//| Expert Advisor main function                                     |% m, `+ P9 m- S7 a& M7 T
//+------------------------------------------------------------------+" T! y# O* ]/ J) Z" g8 f1 c
void OnTimer()
8 J: m' E4 q* V+ f" {$ y7 e{
1 q' h. ~/ }# Q; s7 e( W+ F3 qMqlRates cotacao[];) i" a6 A. @7 Z+ d- t' c+ n$ ?6 f: J
return ;
, B7 u7 D) ?! G1 v/ ]# gif (negocios_autorizados == false) // are we outside the trading window?0 `# c' ~+ }8 R: x0 ]$ O5 G
return ;: l. y$ i% b3 w! Q) q, A0 }5 B
//--- We are in the trading window, try to open a new position!5 ]1 B* e% N9 L3 f# D' i  v
int sorteio = MathRand();
5 j2 [$ o5 z4 A/ a1 J9 N, O//--- Entry rule 1.1
, u& G0 P2 B8 W7 uif(sorteio == 0 || sorteio == 32767)
4 u* _6 X" ?& Y  m3 qreturn ;; }8 ~+ o8 n4 y1 s
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy  ~( `4 q. X8 M2 f/ t
{
7 _, M& h; y) H- `negocios.Buy(info.LotsMin(), _Symbol);8 }; C0 D$ x4 `, b) X: L1 ^5 o5 B% f
}
& n( q1 j7 d7 lelse // Draw rule 1.3 -- odd number - Sell
" Y+ L  m! l$ S{
2 o: ~& u1 D: R) k8 b6 `' N5 ]negocios.Sell(info.LotsMin(), _Symbol);/ d, X" h# M. @% @. I
}
0 J, M: ]; O7 h8 K# G}
% ?+ c9 ?) T2 D( |5 g( @# y) b) e//--- Check if we have a new candlestick...  P7 Y7 z) L8 h6 A/ q
bool tem_vela_nova(const MqlRates &rate)( j9 z  g& |( M" A" Y1 C* \# v
{) @6 G6 b, B2 O. D/ Z8 z7 h
{. q2 [* O$ Z# K4 Z1 W. H
ret = true;4 F: Q7 V5 \+ j% d) D
close_positions = false;+ K. t6 t  @; r9 z. V5 k
}- ~* o" n% v, e4 a. o* M) o
else
- X. k% k  A- l! y7 k( w* M( e; I{
4 N3 y" z, t' }5 M+ O1 aif(mdt.hour == 16)4 r0 K, j2 n3 {; c
close_positions = (mdt.min >= 30);  E: r9 Y+ X- a& v+ W; u
}
  R% {+ v# Z1 T* f}
: q8 @& P9 c/ p0 J, o5 V% g6 ereturn ret;
! w, i3 g4 a/ m$ y& C2 m) w0 |6 [}5 E' X/ ]1 e" z9 Z$ Q% g  r$ H+ R$ |
//---) a: ~4 P" D$ C
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
" _$ v% _& u$ d/ r. @# w{
4 {  }5 e" s$ ]5 Y9 uif(PositionsTotal()) // Is there a position?) E( v. ~9 u  \; r  G
{
& M5 f# l' G# M$ _; K7 }1 jdouble offset[1] = { 0 };
1 n/ |' ]7 V. r7 K4 q; t1 Qif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?. a) I- ^( q/ }7 b! m+ t2 p
&& PositionSelect(_Symbol))  // Select the existing position!
, V2 {0 N6 |5 R& {4 X- B{
& b0 G% U- o( }  @0 EENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
7 F5 j1 O) g1 k- F' h: J. qdouble SL = PositionGetDouble(POSITION_SL);
* k- u& p# |# t* rdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
* l" N3 j1 I. P+ j  r# bif(tipo == POSITION_TYPE_BUY)
- Y0 [  p# z6 r# g; v  P. k( r{
* F4 j1 d+ h+ b, c' Aif (cotacoes[1].high > cotacoes[0].high): w1 G- y4 {9 X. h
{) E. B3 I" ?- x0 ]) S, w
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];; `# R5 V( @! n7 O* }6 g8 i
info.NormalizePrice(sl);
! Q" ^$ u- O. W, x3 Uif (sl > SL)$ \- q: j7 L  N0 ]
{
( F6 {% `& g: [& ?2 r- A3 [& u# snegocios.PositionModify(_Symbol, sl, TP);0 O4 z( U1 R* y, J+ O
}
  e0 x# {1 @  M}3 ~- `) r  Z( j! {; K' F
}
% b7 `2 w4 }+ Y: y. i* nelse // tipo == POSITION_TYPE_SELL
+ O- E5 ]4 g, Q) V% f0 Q{  W7 A6 N5 ]2 ?6 _
if (cotacoes[1].low < cotacoes[0].low)
% b5 \. H# r; i# ]; ]{8 p, M* Z9 W  i! x
return true;
* C- E& b8 i( S! ^! ?}+ `+ a8 ~7 R: O. r: {' d% R& S
// there was no position8 n4 x! P; k3 Q' g
return false;) d$ _) z0 |$ x( {
}6 e! a9 u& n8 h
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。" M% @  t) S+ C$ M7 U3 X# T
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-7 15:54 , Processed in 4.532425 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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