私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA6 F: Z, _$ Q4 x1 W' P6 k$ g
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。+ ?  z. K8 E8 G4 f  j- W
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
) a. c' t" W) R! s# L" ~- ~以下是制定这些规则的代码。, o. h7 i1 L# p2 @* h
//--- Indicator ATR(1) with EMA(8) used for the stop level...
) i4 M! O8 b0 Hint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
. ~7 s& M( x# J& cint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);4 p) }, A% ]3 }
//--- Define a variable that indicates that we have a deal...* c3 r; Q/ H& ~
bool tem_tick = false;* c; L# h1 ?. U1 X
//--- An auxiliary variable for opening a position
# K/ C' z% K% y0 `3 l, ]#include<Trade/Trade.mqh>
  E( B+ g0 s; h. u3 B  K1 H4 d#include<Trade/SymbolInfo.mqh>
, d4 Q& \' ~% Y9 f/ q. B8 [" wCTrade negocios;
: [7 a# I1 \% C  b  z) ~5 l6 b0 oCSymbolInfo info;
3 S) S: h( H# X7 a# {# a//--- Define in OnInit() the use of the timer every second
+ t/ v# d" T2 q" a+ w( [//--- and start CTrade
8 k  S% F2 z( b: `int OnInit()
$ s9 O* h! v& {4 X5 B{: l+ p4 s+ T" K) ~6 |4 V
//--- Set the fill type to keep a pending order
- C  G! `1 r1 M/ {1 c, _//--- until it is fully filled9 |9 n* B) `: Z# x" k5 _+ m
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
  p; [7 I; ~% ^0 d2 A: l6 _//--- Leave the fixed deviation at it is not used on B3 exchange
: Y+ K0 u6 J) Rnegocios.SetDeviationInPoints(5);
, w- s5 ?0 a/ H7 x! c+ t//--- Define the symbol in CSymbolInfo.... }+ R& P7 e. L( q8 l* G$ {
info.Name(_Symbol);+ S- `2 m2 n( x. m( \
//--- Set the timer...
' Y$ O. ?8 m1 g# a2 f% KEventSetTimer(1);
6 c: s, c( t! g1 b1 y( ?4 N//--- Set the base of the random number to have equal tests...
6 Z2 X8 n* b* eMathSrand(0xDEAD);
2 Q, V' V1 `; l2 T( O- }return(INIT_SUCCEEDED);, F3 b8 A3 G; e: M7 {; E( G! j1 j
}0 Q& K8 w6 l  E1 k& y$ Q
//--- Since we set a timer, we need to destroy it in OnDeInit().
- {: A; {! T% E0 Svoid OnDeinit(const int reason)
4 ^! {' v+ H$ N) ]( E' o; z( m8 v1 O{
# C+ A" n7 K% l& [# _; ?& LEventKillTimer();
' f% I2 c% `3 g}
% P! i/ I: j5 S/ N6 _5 h) P& D6 ]//--- The OnTick function only informs us that we have a new deal3 |1 W3 Z0 {, W" ^! P1 O1 d
void OnTick()
  M" w6 s7 ]5 r% z  o. Z{* f$ W7 X' g  s1 G7 ?0 q
tem_tick = true;" A* Y/ l( K9 ?; q! c0 |- I
}
! m3 O# B& `5 k$ p//+------------------------------------------------------------------+: v8 @0 u+ M! H
//| Expert Advisor main function                                     |3 x0 G4 h/ q/ C9 c5 G: `
//+------------------------------------------------------------------+
, d' m5 k4 H- [0 O* ?; ]9 D6 lvoid OnTimer()! P7 L+ N  r5 Y% X
{
; }( ^, o& c. B) hMqlRates cotacao[];2 U. v- ~$ J: d
return ;6 ^- M' p& P( I
if (negocios_autorizados == false) // are we outside the trading window?
4 F8 k3 _- g! ?8 t' yreturn ;. K0 ]" J- @; `# T
//--- We are in the trading window, try to open a new position!
& @3 Z" z6 C1 W9 w$ Kint sorteio = MathRand();1 M/ h# c% d$ \2 E6 i3 \# i0 d
//--- Entry rule 1.1
6 O  P+ `# v9 Mif(sorteio == 0 || sorteio == 32767)" c. u) t" w6 v5 k6 P) V
return ;
5 V6 m9 Y* W9 y/ P3 _  n2 m; gif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy5 w% w( H, ~+ x* d: J
{
: D/ S6 A5 H2 C, h; B" A& {negocios.Buy(info.LotsMin(), _Symbol);8 L2 r( T% K+ N9 K) S
}) v, o% a0 W9 k+ P# r; H: _$ ]# b
else // Draw rule 1.3 -- odd number - Sell
! l0 O0 i4 c% x1 ~4 H{( s8 \( V- n5 w8 e9 S
negocios.Sell(info.LotsMin(), _Symbol);
1 n# E0 _% K& `) o}
; M. G0 m' m' j. q' q2 B2 i}  o* O, x$ q* `6 \
//--- Check if we have a new candlestick...3 F; n& U5 P' F4 ]- O- n9 x2 O  S
bool tem_vela_nova(const MqlRates &rate), s( i  t4 c; t
{) k: y  n" ?. u* K
{0 z, ^! g9 S0 o" G& @: x. N
ret = true;
" u$ q3 q' B' t, A3 x, @close_positions = false;
( f! C) z/ G. t0 S' e- j- `7 f}
* G; R- r9 m+ D$ g  A( p, delse/ `9 T2 r; {% B; J7 T" ]" ~
{( }" ^; U2 R, a7 S: T. k
if(mdt.hour == 16)6 g$ r; c  u7 a6 d  C
close_positions = (mdt.min >= 30);
; J3 {. V. @. ]+ @+ C# l}
2 ^4 p- a; ?! X4 r  z}: o! p! X7 x5 r+ f4 T2 L3 [
return ret;; R- g$ h. e$ \0 v0 R% m) J5 \
}
3 e* i& [) f7 l//---" ^9 R& a6 q% @8 }( [0 O9 k( @: j5 q
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])
- L6 {. b; L6 N( l4 B1 n{+ M& d& Z7 x4 V9 |! M
if(PositionsTotal()) // Is there a position?6 o! g3 A- S; e! C- ^4 }% d* v
{
# D, {, s" n) udouble offset[1] = { 0 };4 Z& S, y/ A* j0 L
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
  ?# `' k0 U" V1 c&& PositionSelect(_Symbol))  // Select the existing position!1 h) M6 v$ e, X" j( Z! n* i
{
* d0 I/ ], {9 C) _4 N, [+ Z% xENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
, t( L  |$ |0 z+ [: ^2 j6 r3 {8 Qdouble SL = PositionGetDouble(POSITION_SL);
( R+ \' `, f) ^7 o6 F/ kdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
: {0 q! h) v1 `; j4 T' o' Fif(tipo == POSITION_TYPE_BUY)
, i( j* b/ [2 q; K5 v3 F5 A{. {' k2 r4 _' o3 E! k: B% d' h$ o
if (cotacoes[1].high > cotacoes[0].high)9 c- }% X! E: \% `0 Z% |+ n
{$ d& l, {" k" H# X7 ^8 {
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];7 P8 f! K! R6 E+ ?0 N3 B' U) h
info.NormalizePrice(sl);
, D2 D! M  _& J6 i9 ^" uif (sl > SL): H- U9 G! n& q- q& R" v& I9 h
{+ w3 X1 e  S' o& n! t1 O
negocios.PositionModify(_Symbol, sl, TP);3 t- ^: Z/ C  S$ H; ?) E. Z
}# L! ?, d4 r* B8 x* x' E0 }* ~: A) O
}; V$ H) f. z! U, w" V4 ?: S
}
: o. J% R. F! helse // tipo == POSITION_TYPE_SELL
. x" |( O( l1 T* ^+ \! f5 @{
; Q" j4 X; T5 Wif (cotacoes[1].low < cotacoes[0].low)
* H  t$ A) L9 K( z( }# d{* i$ A1 }' }- F) }1 k+ _8 E3 C
return true;
9 V: a8 V8 U& X. S}; g) a% ]. Z0 }9 m
// there was no position
) m0 G' k8 q* c' c- Xreturn false;6 P( {( |0 q$ S
}
# a" {* e4 M2 U我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
! Y& }5 w" s, j' Q8 t' A到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-13 21:40 , Processed in 0.397960 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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