私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
- ]* x0 o. V0 U3 v9 n' h在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
" o' L- D% b4 R为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。& Y% f- k" c1 L; S
以下是制定这些规则的代码。2 w/ t. [1 P- X, t( B2 r
//--- Indicator ATR(1) with EMA(8) used for the stop level...
( p" \$ Z0 h) a$ T$ ~& z7 R! d4 F+ yint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);" K, b2 p, W; a
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);6 @; x7 v+ N! `) N
//--- Define a variable that indicates that we have a deal...+ h8 u+ ~2 a9 S  E& t
bool tem_tick = false;3 L2 K7 d  p& v  `" w; p
//--- An auxiliary variable for opening a position  y+ w) U# ?' m, g7 x0 L5 T' {
#include<Trade/Trade.mqh>
1 ?. d  G! h# z! O: K' O#include<Trade/SymbolInfo.mqh>6 y+ w3 H1 v6 S4 i
CTrade negocios;/ T% J/ n2 O; Z
CSymbolInfo info;0 D4 f2 f8 V4 b3 ]! n' h
//--- Define in OnInit() the use of the timer every second$ {* t4 f# u* A1 C
//--- and start CTrade+ e5 Z7 E: o/ M$ p
int OnInit(): A( }& G& J5 E
{+ z: L1 v/ e# v
//--- Set the fill type to keep a pending order8 g* O9 d) b6 }4 x
//--- until it is fully filled! h2 M, k+ p9 @. e
negocios.SetTypeFilling(ORDER_FILLING_RETURN);' T- f: E7 i/ }) ]1 a9 Q2 j- B
//--- Leave the fixed deviation at it is not used on B3 exchange( _. P8 w9 s; D( M5 r
negocios.SetDeviationInPoints(5);- i/ x, t" I5 F* g+ n
//--- Define the symbol in CSymbolInfo...9 N. e; o! r' C! r
info.Name(_Symbol);
- L) G$ Z, m% @/ K: S8 o: m//--- Set the timer...
( H$ m: f0 c$ p; K' P  l3 wEventSetTimer(1);
3 f1 ?& D% s& g9 ~! @//--- Set the base of the random number to have equal tests...
+ P5 u; V4 k; _: `MathSrand(0xDEAD);4 h5 o1 ?; }8 a% b) j3 N3 m+ n
return(INIT_SUCCEEDED);
) q& e$ O. @) Y}# }9 T2 N3 G) Z; e7 N# L
//--- Since we set a timer, we need to destroy it in OnDeInit().2 o8 ]! D, m, R! L5 {6 f
void OnDeinit(const int reason), q0 s% D+ n0 w9 I: |8 n/ _* c, H- L
{
1 H! M+ A, }5 z# S- b% fEventKillTimer();( c8 w* Q( d" I' @, E+ Y
}
& o, q$ k6 f1 }$ m//--- The OnTick function only informs us that we have a new deal0 l' ^6 w" F# H' [# |- g7 `" ~
void OnTick()
  y. M; `# n7 j' e9 z0 t% h{0 D. T6 ]! o4 a3 r
tem_tick = true;! H, Z+ l! I' l* Q& m% N
}1 l+ R* N, S0 ?
//+------------------------------------------------------------------++ A' N5 @8 K% R4 f: K) O
//| Expert Advisor main function                                     |2 E; ?7 f. H6 [: B) y
//+------------------------------------------------------------------+; D, q7 `- x2 _5 C2 N6 Z+ q
void OnTimer()
6 J! s! U1 F3 B{
+ w4 o( U* v  j0 R. w% k$ w4 SMqlRates cotacao[];
6 N4 x% l+ k$ U" a  [2 h) {* Creturn ;; `( o- B% ~, v4 m
if (negocios_autorizados == false) // are we outside the trading window?
( n  a/ Z% U! l9 }% o  N. Ureturn ;7 h; q, b& y  ~
//--- We are in the trading window, try to open a new position!
! z, h4 E* R7 b( Jint sorteio = MathRand();5 |! K* @4 h# y
//--- Entry rule 1.1
* q0 E/ U5 G) p5 v2 `  Jif(sorteio == 0 || sorteio == 32767)
4 d2 t: d7 F: Ereturn ;. b7 t% T, e* {; _1 I. s
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy
: p7 E; E8 f; _& @{
5 p* }0 B# W7 Q" y) Q# pnegocios.Buy(info.LotsMin(), _Symbol);
: Q: Y. T# {; g& g7 W  `' n}( ?* N: V& K5 q2 I% |  D
else // Draw rule 1.3 -- odd number - Sell
4 ]; F3 g5 G( r1 d{
0 x- B/ h! N8 V7 X6 m* k0 lnegocios.Sell(info.LotsMin(), _Symbol);8 h% {* c! G; Z' I9 h' g; X1 T  g3 U
}
+ J1 X/ p% A2 @}- G  B" J; `: ~; \- E& t" [
//--- Check if we have a new candlestick...
% [8 D% p0 Q7 p& `4 I7 A# M+ v2 Lbool tem_vela_nova(const MqlRates &rate)
1 O- V; F" D9 ]{2 @% B& \" _' X9 [
{
! M2 V" o6 v# ^- V0 M/ \ret = true;
7 }" c3 V, l% w1 ?+ ~5 n, x5 h, Xclose_positions = false;4 j" F  X+ b! o/ f: i
}
- W" z. T$ R6 B' `: o% Xelse
: K3 h- Q! V( r+ M& ?{# j0 d' D1 K7 d. M) m
if(mdt.hour == 16)
  B" x8 S9 y9 ?/ F- sclose_positions = (mdt.min >= 30);
5 y- U  V. C' H! ^7 ~7 G}# H3 n, A2 [  k" i9 V3 g. H
}
. G& f& [7 |; t' S- G2 g8 |return ret;3 i+ g. Z/ m8 }6 Z  h
}
. C" G( A" h$ C" }0 v# K//---- w+ B) P: ]0 c6 n9 y9 z' s
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])) f5 Z- u+ H) q' T6 D& z7 c
{& k5 I( G$ L# e) m; v5 d5 M* _( X
if(PositionsTotal()) // Is there a position?
; S1 }7 |: ]* I3 K: L8 Q' p{: g( Y& j0 W6 s
double offset[1] = { 0 };8 k/ K' i" F; r
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?  {5 o2 g5 [9 C0 ~3 w
&& PositionSelect(_Symbol))  // Select the existing position!
0 a+ R9 ?: y# [% Y" e& h{
- L" G' t/ P1 T  A8 sENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);5 k, }+ o' u# K5 O
double SL = PositionGetDouble(POSITION_SL);
2 t. C6 J) E6 `! k' `3 q. odouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
  w+ {0 r: |  t( Z- j; Uif(tipo == POSITION_TYPE_BUY)
  f! z5 K& C, z( ?% T) M; B: X6 C{
! S' H% W4 Q8 d7 {  @$ vif (cotacoes[1].high > cotacoes[0].high)# X' p: f: f+ F; E8 C5 I: ]" b
{/ _! s9 o7 M7 q+ O% ~: P' T
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];. t% B* j: y, T2 T' w3 ^3 l4 T
info.NormalizePrice(sl);
4 a$ s3 ]  A, Y2 q0 y2 y8 mif (sl > SL)
' r0 U! T, F5 r7 o{
; I' a0 b5 _+ Y* H' j& Wnegocios.PositionModify(_Symbol, sl, TP);4 P1 r& O: |, U, b, ~/ d
}+ |  c  A4 l* k
}1 K# ]0 Q, B' s! l9 m1 f
}% w! }& i7 j- Z6 X1 G
else // tipo == POSITION_TYPE_SELL
" |* q9 d0 c8 ]% N{
2 k; c, ?. C' R% v+ tif (cotacoes[1].low < cotacoes[0].low)
! G# ~7 L) a  ]- r{) j, i7 F9 n) d3 i$ F& M' b  U
return true;
! M; @" d& X0 C3 U, X9 o}) U2 X, h. J( [) K5 A6 N
// there was no position& a7 E0 `# z2 ?! ^
return false;
& ^& _; Z+ v+ J}
. ^3 G& n$ F8 m7 O我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
8 K6 }# f- x5 H" \* F: E* v到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 11:00 到 4:00 之间。 第二个是管理窗口,它允许算法管理持仓,移动其止损价位,直至 16:30 — 此时它应该把当天的所有交易了结。
http://www.simu001.cn/x287863x1x1.html
最好的私募社区 | 第一私募论坛 | http://www.simu001.cn

精彩推荐

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-17 22:43 , Processed in 0.411289 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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