私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* \3 W: N' c' q0 k0 m
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。+ E' P* L& g4 r' N4 u
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
! H9 {& U- {" K( w" W以下是制定这些规则的代码。3 r+ }" @& B) g( I+ k+ Y
//--- Indicator ATR(1) with EMA(8) used for the stop level...# ?% p! {9 ^1 Z7 N
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
% ]4 _+ G5 f2 J" b5 yint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
3 A3 u1 ~' ]& R9 U& l//--- Define a variable that indicates that we have a deal...
6 Y4 J6 j  Q0 M5 s3 bbool tem_tick = false;
; r. S0 _4 ?* ?, u% B//--- An auxiliary variable for opening a position
) m$ }! v+ r" c! I6 T#include<Trade/Trade.mqh>
) E7 w$ P% V9 G. p( M/ a- K4 u- s7 C#include<Trade/SymbolInfo.mqh>( K/ `: C  k( y; n
CTrade negocios;
. v" O$ f% {% G& V8 S- MCSymbolInfo info;9 B+ E' O+ p8 D3 `' s+ Y# t0 m5 v
//--- Define in OnInit() the use of the timer every second
9 {& P, a% K& |1 L//--- and start CTrade
: y/ E) h0 H( @! T3 ~) ]% A4 x2 Lint OnInit()
% |5 M5 B! W. |# u( Y) S{
4 H' u% J8 h) P5 ]0 _4 F//--- Set the fill type to keep a pending order- m+ l0 B9 T& ?7 M8 C
//--- until it is fully filled
/ T; J! a% I1 ]1 W9 X) c4 }) Z3 \negocios.SetTypeFilling(ORDER_FILLING_RETURN);9 L( l7 t0 }# q9 s
//--- Leave the fixed deviation at it is not used on B3 exchange
1 ~& w' b4 L8 D6 Rnegocios.SetDeviationInPoints(5);2 |/ H8 ?3 w. |
//--- Define the symbol in CSymbolInfo...
- U' Z3 p& u  C" u/ |" linfo.Name(_Symbol);
, a2 ]' S, J" x2 z1 c1 g//--- Set the timer...% r; ^7 B9 H6 T! f
EventSetTimer(1);3 k7 Z# M6 d* [
//--- Set the base of the random number to have equal tests...
+ p; W$ ~6 m9 q) d; J0 s  DMathSrand(0xDEAD);9 A3 y+ H* ]+ X1 X: f" J
return(INIT_SUCCEEDED);) c; k0 P$ V" ?$ u8 o9 k
}
" T- j! {5 Y# p& O: K# c# k//--- Since we set a timer, we need to destroy it in OnDeInit().
5 S9 v7 s. g( ]4 j& lvoid OnDeinit(const int reason)
2 L) q% s9 b2 |6 |' T8 d{
8 v$ ?  A. n3 H4 H2 zEventKillTimer();- F6 L3 C( t( M7 m9 d+ K. S- u
}8 i% K0 y/ {, ]5 z' b' v( v
//--- The OnTick function only informs us that we have a new deal0 P8 p( V# n: q9 a( k# k9 K  {/ u
void OnTick()
% ?* c# Z  E) S4 r6 A{2 e& f; m2 ?- I
tem_tick = true;4 ?  Z; `7 u5 v5 O0 }6 {% H
}# _$ ?/ [3 `. j9 ~) S
//+------------------------------------------------------------------+
1 t( r# a# t- u6 V0 H' [  m//| Expert Advisor main function                                     |
7 u) Y5 S: ^. O2 `) s! q//+------------------------------------------------------------------+
' l& E1 Y  I. L4 o0 u  ^: o6 }# E- Vvoid OnTimer()
; F& f( F) x7 w8 b4 Q{
0 j; d1 g5 B: r2 J+ b8 y: xMqlRates cotacao[];
& B: ~' q' ^+ J% t5 m0 }8 w% p' N. A1 yreturn ;+ b* {+ U* v, x& E3 j' B
if (negocios_autorizados == false) // are we outside the trading window?
9 d; \# ?# Q8 D! n" n* greturn ;0 X- _. h, P4 V; N
//--- We are in the trading window, try to open a new position!; Z/ x3 l; g; E# L, J
int sorteio = MathRand();) C8 W+ _* u: }! s# }1 ~
//--- Entry rule 1.14 \+ b: w- i9 F3 h% H. v6 m
if(sorteio == 0 || sorteio == 32767)
: I$ {/ Q4 g# l4 m- l0 h+ ~return ;: m% [3 ~) s3 e4 f1 \, p
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy2 g# ~0 |8 e1 _1 Z9 L' H* _
{8 N6 w4 G/ s0 {4 `% P! I
negocios.Buy(info.LotsMin(), _Symbol);
5 y6 @, T! G* `; q* F# I  L4 h}
, m# B: Y- p$ _. n. A4 Celse // Draw rule 1.3 -- odd number - Sell# S6 a* D6 z' W( V, N
{
2 o# {0 r/ b4 ^! N. ]( D- _! Onegocios.Sell(info.LotsMin(), _Symbol);% t9 |/ I. u+ t& N% `. [) d9 {4 S
}
" \7 X/ U5 u! q}
/ f" Y/ X8 B/ D1 E. V' A//--- Check if we have a new candlestick...
  P& U. d% ^7 N8 \bool tem_vela_nova(const MqlRates &rate)8 m/ |5 t. ~+ y
{+ B; Q# U2 o$ v4 N/ H( N; G9 Z
{
0 A8 V% ?0 K) r4 w/ Mret = true;+ @4 a5 ]$ r. g! t+ ?6 y/ H
close_positions = false;8 c9 F6 {4 M* i* y* F' f
}
+ s( V8 J: v# r2 q7 T8 D+ M& ~# @8 {& B% Relse
) s' Y% Y5 j3 a% B! A7 |{
7 P8 K. F- R( q2 f% [8 H. ~7 U- u  Bif(mdt.hour == 16)1 J3 p3 f7 A3 Q! H  k
close_positions = (mdt.min >= 30);: |9 H! E8 `3 O) G* J% S( h* Z
}
* o# m: w( m" k}
! O7 ?' H5 V' s0 u+ R7 Lreturn ret;
! {4 Y1 I" F7 a* P4 o/ r# I}, Q) w; ~! h/ @% J+ a) }# |
//---5 g9 ~7 h: X6 r
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
! v3 U0 F4 [/ r, B{( g( }8 g. ]& N# B5 w2 o7 j# f7 W6 b% ]
if(PositionsTotal()) // Is there a position?+ o& p+ ]- h5 N, z; F! h1 C9 N
{- n7 B0 [: V9 X& j& Y, M
double offset[1] = { 0 };) ?  _& `; H  J6 w  m  N
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
& r* M& E& x) |0 o&& PositionSelect(_Symbol))  // Select the existing position!# S: x# c& V4 L: ~* }
{; V4 X& Z; o7 [7 I/ [6 r
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' z3 u  R' }% }; S+ K3 S/ V+ V) ?2 K
double SL = PositionGetDouble(POSITION_SL);9 p5 f" B6 |  R3 b) G2 f
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
4 A0 l8 I- Z! {3 N9 U0 oif(tipo == POSITION_TYPE_BUY)* X! [, E+ C6 x8 W% R( w
{
! Z, b& z& U6 L6 o9 Tif (cotacoes[1].high > cotacoes[0].high)3 Y" q4 {4 j+ i
{# x9 q9 f1 y* a7 N, p7 u& l$ E
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
% _. n# u+ _! g1 p* t+ @5 \; binfo.NormalizePrice(sl);
0 N; n1 o- G" Bif (sl > SL)
3 e2 R5 N9 j  q{
/ V' Y$ {9 a* a+ Snegocios.PositionModify(_Symbol, sl, TP);
) k. o: Y. @: K: W, |}
: |( E* _% M9 X, b+ D. n6 m}& r, G  y) l( e: e6 u1 {
}# k1 J" D' a/ j, t# h3 i( d
else // tipo == POSITION_TYPE_SELL
$ i8 y8 L% h* v# s{* |& b' X& c8 T8 Z5 b! z# |
if (cotacoes[1].low < cotacoes[0].low): `2 ?) W3 w* Y/ R/ J: V- z! r
{0 n4 H" M& N" \$ Z7 A
return true;
# C8 s6 o% ^5 l) b) [}) \2 a* e% [8 T2 Q9 D& c4 v
// there was no position+ @; J5 m. l1 n) z" r) _
return false;9 t6 J/ W5 \! c+ M9 }6 }
}
" Y+ r/ Z/ ?: O- d: T我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
# N2 l. E$ F$ D- c( b到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-1 17:07 , Processed in 0.411971 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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