私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA* V2 W( W: \* d4 A; `% p$ W
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。! `3 F5 D1 m3 v( [9 ^. A$ X
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
7 S9 l- t- i2 p, t以下是制定这些规则的代码。/ a# G) M5 t$ ^+ G+ U7 H
//--- Indicator ATR(1) with EMA(8) used for the stop level...
1 e: w" ]9 ]" fint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
& ?  }5 G; `6 o2 ]" M5 w5 @int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
; F4 ~# T( k# X# i; H//--- Define a variable that indicates that we have a deal...( T8 S+ ^. M, A- G( G: o4 |( c
bool tem_tick = false;: B- x  U, _- f/ k' m( b( _
//--- An auxiliary variable for opening a position  n: q) s$ l' f0 s* d; x2 ]) t  R5 \
#include<Trade/Trade.mqh>2 e+ c- b' n$ ^7 t) B
#include<Trade/SymbolInfo.mqh>, J% m' w# A( f5 V" r" U, U2 A
CTrade negocios;+ n# C( M/ D7 T* I. H' ]& l6 d
CSymbolInfo info;1 t( g6 x" j' L, L6 v. s  b
//--- Define in OnInit() the use of the timer every second% a. p! v9 d2 Q3 e1 ^1 Y
//--- and start CTrade+ O) {& v1 _& s
int OnInit()
& r) c0 @' [5 j! p) k{
4 D+ \: z3 \  U% @8 N6 M9 d//--- Set the fill type to keep a pending order
! h) Z- Z3 O9 j( |2 O+ ~//--- until it is fully filled
6 _# x$ t: t4 w9 t& A" t! X( Q6 C% nnegocios.SetTypeFilling(ORDER_FILLING_RETURN);
5 P. A8 A$ t! F2 A7 q//--- Leave the fixed deviation at it is not used on B3 exchange
, x: X# z- }1 t3 c5 h+ G2 |negocios.SetDeviationInPoints(5);
/ D- s. E1 q* W7 \/ a& @//--- Define the symbol in CSymbolInfo...6 a# ^- w& R1 |6 C0 }( o
info.Name(_Symbol);1 b! z: c4 a' {: i5 S" X! A
//--- Set the timer...2 z7 o- _' S1 B% F9 _0 \4 n" R1 b  F
EventSetTimer(1);' G2 L/ F6 ]* J9 d0 w$ Q/ r" I1 b
//--- Set the base of the random number to have equal tests...3 |. V5 V- ^8 G3 a- X
MathSrand(0xDEAD);
. O$ p/ n6 v, Y. Q  i" oreturn(INIT_SUCCEEDED);
0 G! m0 e4 y8 l+ [) a. h7 h; j! I}
1 _( x5 X& ~$ R4 M* e" c//--- Since we set a timer, we need to destroy it in OnDeInit().
' x: U0 _7 V& D+ Pvoid OnDeinit(const int reason)
" W5 Y. F& h: V8 t{
1 c' h4 Z( a2 T3 S! I5 E' YEventKillTimer();
; ?- I/ W- k# y! a$ x}2 w( ]0 Y3 k( l  U6 g: b3 y0 y; p
//--- The OnTick function only informs us that we have a new deal
. `0 a% l8 y- A2 [void OnTick()
' e# F& l8 x( ^" x# a4 _{. ^( Y$ q+ v' s
tem_tick = true;
* G# G8 a; `9 t- f7 x}
% Z4 B. x9 E, N( n: G( F//+------------------------------------------------------------------+. ?! s; p, u$ n
//| Expert Advisor main function                                     |9 L; X, x; y5 m5 b
//+------------------------------------------------------------------+4 h6 E) S) p! a. ~4 R
void OnTimer()
% i( W7 u5 }0 a2 }{8 |  h& e! v. j+ u% G2 L
MqlRates cotacao[];
, b; |( f( ]$ ^: A$ v6 h! F; Treturn ;7 O8 p7 V8 r) U1 }% y
if (negocios_autorizados == false) // are we outside the trading window?* N: }- e/ f* V$ n+ k
return ;/ Z  d8 R, g" m; v2 Z
//--- We are in the trading window, try to open a new position!
- ?) u7 c: |" d4 Z! |2 h3 pint sorteio = MathRand();
- I$ A0 P) z1 f, T6 p& t$ C8 [; i//--- Entry rule 1.18 C, l3 G2 u3 V" `
if(sorteio == 0 || sorteio == 32767)
( ]# [8 ^2 j0 J" Qreturn ;7 a7 C8 [4 I+ s- h  h
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy( c. t% S  C" ]% f; K; z
{
' [4 B6 y5 `, o4 C- n. V+ Wnegocios.Buy(info.LotsMin(), _Symbol);' b& y0 c+ @2 l% l7 w( v# T
}
, W! r" e4 w' N6 Lelse // Draw rule 1.3 -- odd number - Sell
# I4 h/ n* F+ Q2 k) E{( o  I* f6 i4 A( i& D
negocios.Sell(info.LotsMin(), _Symbol);
, d9 V8 V* ?  l  b; ?; v1 i- p}
* H1 y( X8 U' s, f0 Y}: k% e" N8 \  h  n  Z' W2 z
//--- Check if we have a new candlestick...
  v+ |9 D* w+ j8 v! T9 Y$ Cbool tem_vela_nova(const MqlRates &rate)8 N1 h+ u5 r8 a. {; ^% l0 i7 b
{, I5 v: a4 _8 |- P) c
{1 J2 L9 j0 e& O! N& ]+ S3 Q0 z
ret = true;
( C7 E) q+ p; M. uclose_positions = false;9 q& h& N& `3 }3 s$ b
}
8 U! D' }. C" ]& C! b6 y/ x( [else& o! X  w5 n( l9 r
{
; r- i# A' ^, A7 X) Y8 A" `if(mdt.hour == 16)
  a% O4 p4 f7 N- Pclose_positions = (mdt.min >= 30);5 ~% t  a0 c! b7 P
}
* U# e) D0 v+ O0 Y) W}
% V8 g- v3 H7 a' _# jreturn ret;$ v& m! a# X( T: v
}& `5 v/ d7 [4 I$ @2 u
//---/ U1 d* r7 o% ~) v$ u+ y
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
; J0 q9 q  v& ^$ U- A1 ]{
; M) n6 ^  m9 @. w# Qif(PositionsTotal()) // Is there a position?
1 h; y7 A, I1 t" [: A8 N6 v/ F* {{
' s# Q% X) K1 \  Z3 t1 N* S8 k6 W  edouble offset[1] = { 0 };
6 w7 o& z3 k" ~3 Zif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
$ ^7 D9 d" h- x2 b0 ]&& PositionSelect(_Symbol))  // Select the existing position!
6 }0 ?6 U9 K) j+ T% `{
4 N9 [' X# J- OENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);' o& F! ?; W( a
double SL = PositionGetDouble(POSITION_SL);
. g4 H$ x1 G0 f. R9 Edouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));# N' u: [" n* A" F" L
if(tipo == POSITION_TYPE_BUY)- _5 u3 _6 W# O* i8 f
{- N; q2 w5 @) ?6 w- c5 F- j
if (cotacoes[1].high > cotacoes[0].high)
! A( W. e5 S2 g3 m{; C& b/ N, N2 l/ R
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];" _, c" a, I' H! W2 N0 V- y+ Q6 O
info.NormalizePrice(sl);
( ?# ~" `" \+ c2 v, a4 gif (sl > SL)+ y9 F3 D6 p% D: _
{; Z5 V5 a+ T! a. L7 k
negocios.PositionModify(_Symbol, sl, TP);
% L+ c, O( S% {" S/ {}
; K1 u8 w. f& [% t: Z' P$ M% r( X}! \3 y# L. c6 M; F
}2 u  L8 }6 V4 x3 W% G, E
else // tipo == POSITION_TYPE_SELL4 E- X4 U/ l4 C% ~( J+ P  b
{
  J! ^4 `3 n0 Q$ Uif (cotacoes[1].low < cotacoes[0].low)  D& T: e  k! R( R& ?
{
  E8 |7 k* w& Y& `2 s  {+ F7 f# ~* Treturn true;
/ p+ i7 x& e4 L& y1 J2 U: M}
$ ]' F# ^9 z6 [2 A# w4 `* l" k6 A% K// there was no position
. y! E# v  F& M$ Freturn false;
. |* F5 I- [; M) H1 e}
9 H: s/ T( A  ?' ~( m我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
6 C- f5 C( x) P7 I到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-14 20:36 , Processed in 0.915526 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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