私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA( Z( o* x% H6 H+ O3 O( @  J
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。# \+ K4 d3 J/ B; N5 _- q( M9 u0 l
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。. ^0 o8 O4 r4 F2 l+ @' i
以下是制定这些规则的代码。; y6 F: Q$ k# [' s5 _
//--- Indicator ATR(1) with EMA(8) used for the stop level...* w6 r. Y+ s/ W6 {
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" A5 L, T8 b; W& m, X
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);& c# c% }) T4 I
//--- Define a variable that indicates that we have a deal...) L% t: P8 T" j% N' k3 g& T0 z& M- H9 \
bool tem_tick = false;1 C, n2 }7 z% z* V0 H/ r3 b  X& d
//--- An auxiliary variable for opening a position
1 Q6 K8 O- b% P4 c#include<Trade/Trade.mqh>
% s& r% Z" ?$ q- M) \3 x+ Z2 c% u- c) b#include<Trade/SymbolInfo.mqh>
+ z) t, D/ q/ DCTrade negocios;% E* V6 X: z/ A& [) j
CSymbolInfo info;
, \, V! y7 c' |6 w9 W' F. y9 N//--- Define in OnInit() the use of the timer every second
: q! `/ Q3 @% [$ G( A//--- and start CTrade
; i& W4 y1 B+ I- N4 w/ Rint OnInit()8 ~& F2 Z/ K& k' s- s
{
0 l% l* i& I0 M4 G//--- Set the fill type to keep a pending order; D7 a* u% b5 d5 K
//--- until it is fully filled3 s9 i! `( M7 P) ?+ n
negocios.SetTypeFilling(ORDER_FILLING_RETURN);: R" i5 W/ }4 P- m0 w0 K
//--- Leave the fixed deviation at it is not used on B3 exchange
; E& P9 e5 }: J3 F% _: C; @# Vnegocios.SetDeviationInPoints(5);; o6 G- @, f' _+ c; {
//--- Define the symbol in CSymbolInfo...
. z/ h- ^4 s! |: A6 S6 F: [) \! P( \info.Name(_Symbol);
" |% ^, P$ x5 b( X  l9 X//--- Set the timer...+ A9 l% @$ d6 M. N
EventSetTimer(1);9 F0 u% C4 L$ c% m
//--- Set the base of the random number to have equal tests...
' L: Q6 e  k  d4 R4 z! `/ ]( }MathSrand(0xDEAD);
6 W, u5 D6 _2 C4 M- q1 n. e1 ~; ~return(INIT_SUCCEEDED);' i4 e; ~" c- U6 r
}! H) z5 O: |7 i6 M4 i9 }; G/ D
//--- Since we set a timer, we need to destroy it in OnDeInit().0 U/ I4 e. `! \9 M2 ~" q
void OnDeinit(const int reason)1 w  g& [  k/ Q5 h
{
5 V  A% L! ^7 w  W' o1 o" oEventKillTimer();. j2 a: _" {6 k" G
}
% z4 c$ b" ?: ]8 C, y( [% N! `//--- The OnTick function only informs us that we have a new deal
. {4 Y; u% g& s3 N0 lvoid OnTick()
& u7 u# {$ C, T# W' r) _{
  I9 |) n& |4 A- `tem_tick = true;, b1 ?; Y" |. `
}: N' t+ u/ Y/ Z0 r3 g
//+------------------------------------------------------------------+# |2 w* H4 C  ?
//| Expert Advisor main function                                     |8 p/ ~+ R& u! n, J3 n1 R$ F; |
//+------------------------------------------------------------------+* L' P& I  V. I6 [9 W8 Q
void OnTimer()9 f( y7 B; h* l
{8 E( _7 Y# L8 k: }2 d+ F+ J
MqlRates cotacao[];# k& k' v0 m& M
return ;
9 f& ~5 r4 o& d' K, ]if (negocios_autorizados == false) // are we outside the trading window?0 [# e8 G9 M2 l' t5 `
return ;
: E" f% W6 T) S, C8 _//--- We are in the trading window, try to open a new position!0 P- k7 X/ b0 M) T8 g7 A
int sorteio = MathRand();
$ z1 q. v9 i/ [9 g2 W//--- Entry rule 1.1" L- `5 J6 f. @! J6 N6 H
if(sorteio == 0 || sorteio == 32767)
8 l8 ^" \! F0 {" ^; B3 H2 b$ Mreturn ;- D+ @4 o7 ?7 E
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: X7 L+ b3 I0 T- M4 H{: U% a: `5 Y  V  w; Z, M
negocios.Buy(info.LotsMin(), _Symbol);; K8 t" V* D9 j1 I) C, X
}; f! ^, V! f5 J& v$ R7 H& i6 q
else // Draw rule 1.3 -- odd number - Sell2 G$ c% ?, m" e, ?
{; N; }9 l* B* s3 J  \, e* W
negocios.Sell(info.LotsMin(), _Symbol);( G9 X6 c' r, v/ u% h
}
7 ^7 h+ s) P$ g3 V}
* L* b9 q% ]0 I8 n//--- Check if we have a new candlestick...5 w& o! ?, U0 Y& w1 [5 v4 @; ?$ ]
bool tem_vela_nova(const MqlRates &rate)6 r9 {: y' C" Q5 ]5 y7 a& d
{
' i7 Y; ]9 v) y  ?{
2 O: [5 q  |4 g3 k$ j) R7 o; i4 V+ n  Pret = true;
  g' O8 W) @! d% r8 [9 H: Eclose_positions = false;2 {' K( I( V2 c; M/ a& ]( S
}, d* ?: M+ v4 W
else
0 v7 d2 I3 q/ K  `* {{& q) G7 Z: B4 N5 h" Q/ Z  @* A
if(mdt.hour == 16)
1 A/ |# ?8 J4 Y6 Q# Dclose_positions = (mdt.min >= 30);8 ^5 F* k8 l& Y# x/ b3 c
}
1 L& [4 Z. f  _}
! s: G( b5 d$ Vreturn ret;! G( d- X1 Y3 g2 w2 W
}* w( `, ?! m1 L0 I
//---
6 E9 }& \. s1 k! |% _! d/ R( X  Ebool arruma_stop_em_posicoes(const MqlRates &cotacoes[])3 g/ p: m( `- p
{9 c* l1 s% A( q3 X
if(PositionsTotal()) // Is there a position?
. D- C) S, d! d& s1 _1 ^{
  l' q9 t' ~: k7 j$ J% [- Cdouble offset[1] = { 0 };
8 N# ]2 P* i$ R6 i2 \if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?, l" v4 J, H: a1 A0 L
&& PositionSelect(_Symbol))  // Select the existing position!
7 p/ J( X4 O) Y1 Y{
) v- |- \9 C. I( Y1 E- ~: ~+ _) E: zENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);  @/ u: }% L3 G' D& j; E1 ~
double SL = PositionGetDouble(POSITION_SL);& {( R: p! O7 `' L- V
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));* b" M& X2 D3 O8 k1 ?8 `
if(tipo == POSITION_TYPE_BUY)5 I, {; W3 W. i7 ~; w
{
/ o7 t" _- u: j' u/ ]. Q$ aif (cotacoes[1].high > cotacoes[0].high)
/ Y2 {. P, a0 a) g4 w{: u; A0 Y0 A4 `; q  J. L
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. _& u* k. f' R5 p4 H8 e( T$ l
info.NormalizePrice(sl);0 y$ A, C8 r# G. }/ T
if (sl > SL)
. R. ?/ d# J; B% G5 D{. H/ Q& P' d1 }; c2 [# A) F5 S
negocios.PositionModify(_Symbol, sl, TP);
7 ?! ^2 Y9 ~* O' L}
7 p( X% b2 g; S}/ Y0 J# T. l: B  e. v/ J( J- S: ]6 g
}3 p0 c7 ?2 F6 c) q
else // tipo == POSITION_TYPE_SELL
) D6 i* X8 `7 }$ f& _& S+ L) R{: j) |4 H; t5 d0 o! x
if (cotacoes[1].low < cotacoes[0].low)
# D1 U6 m3 U2 h$ [' _; d{
& i, t, l2 C$ Wreturn true;3 I( m! o3 l4 B9 H0 c( ]& g* u
}# j% d4 L/ y: d; f7 I7 M( q" m! f
// there was no position8 h! X) i# ?; T$ m
return false;
! a# ~* |7 C& ~' ~}) O. \4 O' ^7 R: i5 r2 h+ B
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
1 k1 G; V6 N% d' A0 ]& A* w0 ^- K到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-8 04:13 , Processed in 0.863170 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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