私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 N+ K$ \3 _* W/ `% B
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 w: Q$ ]# u! s. B" ^! z6 I) b+ @
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。) i! D- T( h9 P
以下是制定这些规则的代码。
% T# u5 m/ ]. Z" d//--- Indicator ATR(1) with EMA(8) used for the stop level...0 s5 E+ _# C( Z6 Q
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);& d3 U1 N& q& {& z
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);5 j, p4 p- L9 i% k; V) c( K( }% v
//--- Define a variable that indicates that we have a deal...
) F9 g' c( F+ s7 p2 E! X9 e- D; |bool tem_tick = false;, Z  y2 ~% m* H8 Y; w" N
//--- An auxiliary variable for opening a position
& G! t9 l( y9 a- N* D- G) r: o#include<Trade/Trade.mqh>
4 c1 f& Y# l! `4 m#include<Trade/SymbolInfo.mqh>
& m0 Z& h  i( H9 b+ v% a3 lCTrade negocios;
, m) |& L- J, u" _2 l" aCSymbolInfo info;
$ x3 m. e( \! n$ X8 m. L//--- Define in OnInit() the use of the timer every second
* n& o6 n6 P, E4 A/ E//--- and start CTrade
: a) d* u; B. ^! k& G# z5 y6 i! eint OnInit()
. Z3 Y2 B2 O% @' B5 H{
- U' N# l# g7 S  w% e//--- Set the fill type to keep a pending order; `9 l5 q# V' i
//--- until it is fully filled
& B" H+ {7 V- }  X5 @$ Enegocios.SetTypeFilling(ORDER_FILLING_RETURN);
9 ~0 e, m( v; {2 j& V//--- Leave the fixed deviation at it is not used on B3 exchange" g7 U$ _9 h, B% c5 U; @$ t
negocios.SetDeviationInPoints(5);
: d* N+ U+ M+ F: g" Q6 u% P//--- Define the symbol in CSymbolInfo...
9 Z& o  V7 j+ S9 i$ L, k; tinfo.Name(_Symbol);
5 Y0 }2 q& v* t6 b, v5 t//--- Set the timer.../ i, Z% X& z2 _
EventSetTimer(1);: Y# B8 b; m8 R& [' @7 k
//--- Set the base of the random number to have equal tests...
" a+ S+ P' x7 N" g. ]MathSrand(0xDEAD);
' v$ c8 S9 [* ireturn(INIT_SUCCEEDED);  x: O) \4 B  i6 v" k& }$ m8 S5 @- U% \
}( I. E0 o3 i; S- g9 @+ `1 \2 X$ d+ S
//--- Since we set a timer, we need to destroy it in OnDeInit()., z$ b: X* @/ g& T- Y
void OnDeinit(const int reason)
. l/ v& }: u: z: Q( q) a{
, B0 K# I8 K/ M0 w7 {EventKillTimer();% F/ l$ q) H1 t2 U! h
}* k5 y* W1 y6 v; X* r# Z
//--- The OnTick function only informs us that we have a new deal
; `7 s8 D6 ?; e) T/ P2 vvoid OnTick()3 J4 F* T$ q) @- v7 t( w$ Q
{
9 i7 z$ V: j  a' m9 jtem_tick = true;
* W  G' X" u9 \3 o( x6 R; q}
1 x4 y1 u" Y2 n8 ?+ P3 }& q% E//+------------------------------------------------------------------+
% [  ~9 U. [  N9 W//| Expert Advisor main function                                     |- R  X4 U4 u, J9 J4 z
//+------------------------------------------------------------------+$ ]  F- q% N- K& }0 [1 T1 _8 C
void OnTimer()
* e3 G+ r, u* j" Z2 s0 d{1 x7 L, g8 w% _* T# P( O
MqlRates cotacao[];
, j; A3 x5 G4 C+ G. L2 Greturn ;1 ~- Y9 q4 E  K
if (negocios_autorizados == false) // are we outside the trading window?
( z; m1 i# R9 l& u# b  \7 r. Ireturn ;: |0 S, j( f( }; P( `. G* ]
//--- We are in the trading window, try to open a new position!0 G4 s. x0 i: q2 R6 z6 |
int sorteio = MathRand();  z! G$ B0 i  q( }) ?- O) E5 B
//--- Entry rule 1.1& ?  u8 o' F2 _+ w' y( G7 {
if(sorteio == 0 || sorteio == 32767)& D* f# ^' z; H( U% ]
return ;! f. E, B8 u) X1 Y" J/ p
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy/ d% M1 H: r+ `6 V) h, s: F
{( O4 w: X' |8 X" G8 i% o+ i5 J$ g+ J
negocios.Buy(info.LotsMin(), _Symbol);
, q& U  w; N+ t  t& p# Z& ~}  k/ t% y$ N" N
else // Draw rule 1.3 -- odd number - Sell
% f7 D6 L9 d4 ~& V' l{
: p& B  V: L1 k' L: Snegocios.Sell(info.LotsMin(), _Symbol);0 D8 d. b( n% i1 ~7 s  {
}
9 e1 I# P- ]( a: m9 @/ z8 T' ~}  s. l6 F8 `8 ^) b0 A+ z
//--- Check if we have a new candlestick...
' v4 G0 _1 U- i- {bool tem_vela_nova(const MqlRates &rate)
# h" S! u9 _, ]. J+ k9 t{( W9 w. i: T2 K  o
{
6 `4 r, |8 k, D7 M, _) S  sret = true;9 j( e; {& D3 I0 v
close_positions = false;
, N9 h7 o" D: e3 l' l1 y8 a( [}
! h6 g1 s6 z# J$ r/ A8 Nelse: i; c1 a4 y9 a+ v* H. q9 T
{
, _+ n) s) `% ?& bif(mdt.hour == 16)2 m7 W3 p# v$ K' R5 }$ L/ W
close_positions = (mdt.min >= 30);# j; H9 S# r  [4 ^* {
}' Z( R+ \' r8 k; j* e; r
}
1 B# J+ e( b+ B0 o! Oreturn ret;; g# Z2 K- Y% F' G
}
8 h0 R2 \3 l& S2 k+ ?6 X//---3 x# a: v( P+ s, S. L7 m9 ?
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])# y" J/ ~8 }' E7 m2 I7 Y4 R! A; x* C
{5 v  X: y2 M) J. A: j
if(PositionsTotal()) // Is there a position?* Y9 `$ w( v8 {
{( A/ h) U# e6 X6 H$ h
double offset[1] = { 0 };
3 D% T+ |% m" ?# H3 r( tif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
8 D3 l% x2 ^# z) Z3 o4 V&& PositionSelect(_Symbol))  // Select the existing position!
- U) {( \& c# U0 n* \7 r6 t{/ K- g% c& @, n. r3 ^: H
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);! c- I$ O6 M& s2 }. s, D5 u" @
double SL = PositionGetDouble(POSITION_SL);
/ H9 B: O' n) e& V0 Mdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
; w4 a  G8 t% B2 J" {' B7 Fif(tipo == POSITION_TYPE_BUY). |  i" D4 Y8 t
{
/ J3 ?  \) \# D4 [1 z4 U3 E( @if (cotacoes[1].high > cotacoes[0].high)
  r. {* E" E4 R& v/ F. d! g7 N{
1 y$ k1 p# ^4 K- S# `1 e; q8 c! Ldouble sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
7 O4 O$ E$ J6 a& vinfo.NormalizePrice(sl);
7 T: p* V, @! M4 c6 Y7 o1 zif (sl > SL)" K$ e& ]+ F/ M- m0 f* Y
{
4 i( f1 [1 f9 snegocios.PositionModify(_Symbol, sl, TP);
  b& P6 m& |- \6 m/ @+ n}: u1 V. A  @( F
}
( i: J3 _: C. q. ~2 G: k. h# U" Y}
- \& p! q6 K* h4 G. `1 zelse // tipo == POSITION_TYPE_SELL, Y4 z% u! b3 l7 r% e. J2 G
{9 P, S- N! n% x/ {- h7 [
if (cotacoes[1].low < cotacoes[0].low)
2 P7 B2 D( D- i" D4 @: k! `5 `{; d1 b  z2 I. ~3 u  k! W* I4 \
return true;
) X2 m1 B) T( b) a# j! X3 }}
* k) y7 k! f. t// there was no position
1 D0 s: D  O/ w7 x- D  Zreturn false;- g9 p8 X9 A) @/ Y9 a
}: W0 S/ h0 s/ V$ J& |
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
4 h" L$ o1 g' ~% w" e到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-3-7 11:46 , Processed in 0.424964 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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