私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA+ N$ w, o$ Z4 F8 C- I- I* k
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。6 i5 J* }9 f4 \! B& t# ~
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
8 |' f! E- ^; u& Z9 E- B; @9 ]以下是制定这些规则的代码。
: T7 d4 X5 P8 s& j//--- Indicator ATR(1) with EMA(8) used for the stop level...
( e) B6 R! |6 P5 P+ M; ^int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);. r& m, S8 H9 O* a2 e+ l2 [6 u0 {
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);+ h; n1 w: b) U; y1 G" }, ]/ c
//--- Define a variable that indicates that we have a deal...! ]; d! Z( X, W  h& Q& Z
bool tem_tick = false;
( C- ~1 p( P0 {3 k& A//--- An auxiliary variable for opening a position
! H3 s2 b. w9 u+ G* D#include<Trade/Trade.mqh>
5 b# s, ^8 c% X$ m4 l#include<Trade/SymbolInfo.mqh>$ a2 M1 o: {( b, i0 l  i
CTrade negocios;2 A" z) G/ z+ C" n
CSymbolInfo info;
+ X( w! T# B6 a1 @//--- Define in OnInit() the use of the timer every second
. L; _& n4 I' ~: b) ?//--- and start CTrade
0 j* i# y3 G& N  D4 }int OnInit()
+ }+ l( L. f. Q! @! \7 y8 f{* |. L# x+ O" ?; H2 C
//--- Set the fill type to keep a pending order
7 s& d. ~7 D* t& m' H1 H//--- until it is fully filled- x4 @8 O. c9 p: n2 M3 K
negocios.SetTypeFilling(ORDER_FILLING_RETURN);  u; V- d: V# z. |
//--- Leave the fixed deviation at it is not used on B3 exchange
7 v4 M" w4 t' u' H  L! `1 c% dnegocios.SetDeviationInPoints(5);
/ N* K# u/ H# G7 Q' Q+ C" z//--- Define the symbol in CSymbolInfo...
, p) x" G4 A0 K  winfo.Name(_Symbol);
/ K1 A, H1 m4 }0 v! S: n) p//--- Set the timer...4 }+ i: B* D! H$ W
EventSetTimer(1);
! _9 A0 O# \8 _- |% X( r//--- Set the base of the random number to have equal tests...
7 B" t, \/ |" Z# GMathSrand(0xDEAD);( \) i/ Z0 K: b. ?* R
return(INIT_SUCCEEDED);8 j" l* U8 y+ ?
}2 y7 v& v, Q. V+ B* o1 F
//--- Since we set a timer, we need to destroy it in OnDeInit().1 a/ \# k* C; w& q; s
void OnDeinit(const int reason). O( d2 H2 c" d; V( c4 q  n! P
{5 Y0 n. K3 G2 P0 \6 v' X
EventKillTimer();
, C& W) f8 D! `( O, O}. l: s  L/ R3 I" w
//--- The OnTick function only informs us that we have a new deal
3 Y; x0 O) `( N; d) @void OnTick()8 z  i0 c% O/ J
{
# i9 E. [, v  j7 q. v& Z0 wtem_tick = true;
8 {7 v8 b9 E2 [& X}% f" S+ c9 L% S( d* Z) U1 R
//+------------------------------------------------------------------+
* W1 \/ W- k6 ?7 k1 v//| Expert Advisor main function                                     |
3 T; o+ `! _" n; ~5 ^- P//+------------------------------------------------------------------+
4 H' R6 P# ]  U) G' h* cvoid OnTimer()
  ?  R" R, x' Z{
" l% G0 A, ^$ q7 aMqlRates cotacao[];8 h: a8 d# i' d& `2 [" J0 \: j
return ;6 z" T# Q, G  y$ ^9 M
if (negocios_autorizados == false) // are we outside the trading window?
& B: z" |4 u3 y7 S8 Kreturn ;
0 d, C6 C( M6 U  J$ l6 w//--- We are in the trading window, try to open a new position!1 s8 L# U$ ]' g
int sorteio = MathRand();3 q; P/ |. W1 ?7 U( T( h
//--- Entry rule 1.1
- ]! j  s  q; W. x- G/ b. _. Z4 Pif(sorteio == 0 || sorteio == 32767)3 n: r  V- Y' D0 U" H- \- y% \
return ;& L, V8 f+ \2 a7 g
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
4 g' y0 j, h0 @{
7 Y; M- {! Z  anegocios.Buy(info.LotsMin(), _Symbol);
  [7 y. Z4 a) A: b( Q. C( G; R. B}8 E! W$ X0 F4 d0 h* s2 P5 W; d
else // Draw rule 1.3 -- odd number - Sell
" l, ]! v2 z$ _4 ~2 n! M* n$ Z{- ~# [1 j3 [2 _8 @
negocios.Sell(info.LotsMin(), _Symbol);2 y) n( ?$ m! J3 R! U. f# r
}  L; j! D, k. }8 B+ @
}
; K5 P0 e  x9 Y9 \" C6 ^/ `//--- Check if we have a new candlestick...
( ^- X, v0 S" F( c1 fbool tem_vela_nova(const MqlRates &rate)
' m. R4 \% o2 u2 S/ f. y, B{
" p3 `4 j; o% k4 }9 h4 r, u6 S{
. J, ?3 P  X" \ret = true;
# c4 \% U, ?/ y( wclose_positions = false;$ d5 v2 Q: }( o1 L
}
$ S( T: v9 W$ B( l* y$ I! uelse) s2 l+ N* H( k5 {
{$ z' \/ @2 K6 ^! s( Y/ c! @% m8 p
if(mdt.hour == 16)
( p  h# [/ P' u$ O) o8 d; dclose_positions = (mdt.min >= 30);  N3 Q  g" c' Z3 N
}
# |3 _+ x9 S' h# `. b}3 Z6 w! T* V" Z2 u/ U9 t
return ret;
0 a* T, V5 q& y3 g$ w}
# L; S0 J) |8 S9 B//---5 v% D3 H3 R7 J: F1 }# H
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- i, C) t/ ~0 c7 g! s{2 r, Y( g4 t  m$ Q; k) n
if(PositionsTotal()) // Is there a position?4 ?% K+ b* X! A* D9 z
{
5 f) N4 k2 d9 i0 U% C+ F& e' Zdouble offset[1] = { 0 };
) a. |; I  V' r; D( T, q: wif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?7 @. y7 L" h2 d! }* y2 U: k& {
&& PositionSelect(_Symbol))  // Select the existing position!
6 c* N% D: O+ Q9 ^8 ^{6 p9 J% g0 m3 ~0 H) ?' x1 g
ENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
; ?* m6 ^* Z! n. Fdouble SL = PositionGetDouble(POSITION_SL);! ^0 \; b: i: N# k
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));8 s" s4 M( W) v+ J% Z$ l
if(tipo == POSITION_TYPE_BUY)6 e. y( K% k! R( W) r8 h
{
. I; K; C* T# O2 l4 sif (cotacoes[1].high > cotacoes[0].high)
5 R3 e) R* j" V; O0 G{9 v( O4 u$ [" P/ I
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
$ Z  d6 o9 s5 S; D6 j7 Ninfo.NormalizePrice(sl);- {' u% d6 p; p2 s
if (sl > SL)
, P. `9 P( u' i# e7 ?: T{' b  W! F$ ]' N, ?! ^( s
negocios.PositionModify(_Symbol, sl, TP);
; c% T( h$ R: f) C2 s& m% E}6 F. o7 I0 R$ C5 A( b6 X+ \$ L
}7 j4 y* f! F3 G4 e/ @# x/ i: X3 l+ F: w
}
4 Y! |2 B# H! `else // tipo == POSITION_TYPE_SELL
/ X3 ~) H& {6 @6 C" j( A0 {( S{
7 s' G: {2 d- z; Z6 c& B% ]4 Kif (cotacoes[1].low < cotacoes[0].low)+ F9 E3 T6 C5 ?5 g
{1 S4 ^& i8 v$ a3 N( @* }! O: z
return true;
( @! M1 P; X- k9 ^}
4 X" }8 L/ V$ B# @; d1 G; b// there was no position
% f) @0 q+ u2 }' n" {3 e( wreturn false;+ Z( h8 ]: r* U
}4 o* D- v; M/ e
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
: ]4 F; U& }: X$ }5 ^到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-5 06:35 , Processed in 0.435280 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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