私募

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA7 Y) ^! K' w2 y
在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。
' ^* {" z# g" b' F1 \3 I0 ^为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。3 V: J0 ^" [$ C/ @
以下是制定这些规则的代码。
6 h" T" }/ `# t1 J; y" ^6 Q- S//--- Indicator ATR(1) with EMA(8) used for the stop level.../ w0 R$ ?5 [5 P, z( W. |- f
int ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);9 y- ?0 A. U" D" C( Z6 [) Q0 Y
int ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
: b: A' W" O$ X) E! f//--- Define a variable that indicates that we have a deal...' ]0 d$ L9 i. \; i
bool tem_tick = false;, l+ k7 y. L% ^
//--- An auxiliary variable for opening a position
  L1 J, J; D3 N7 l( t: C- I5 h. U#include<Trade/Trade.mqh>7 K$ {  c' n: h
#include<Trade/SymbolInfo.mqh>
( c  W, n/ a$ b$ K. bCTrade negocios;4 i3 F: `" R. B& q) U
CSymbolInfo info;
. ~& }; C, \$ E- ^2 v8 l: Z  E//--- Define in OnInit() the use of the timer every second
" j* t* i+ |: x# L; d//--- and start CTrade  Y( E8 M  h( I6 x& N& O) O
int OnInit()7 p3 X# `3 U2 q
{( ]1 i. ~% X" }# h3 ?
//--- Set the fill type to keep a pending order# t- I2 w3 T* M  f
//--- until it is fully filled+ L# \: ?) m# g) M3 a8 p
negocios.SetTypeFilling(ORDER_FILLING_RETURN);! L; i9 W8 k$ ]0 S7 q
//--- Leave the fixed deviation at it is not used on B3 exchange. L. A' \  X8 x( W0 L# G% a8 @
negocios.SetDeviationInPoints(5);
8 T6 y: c$ ~8 G8 `' C. t- M//--- Define the symbol in CSymbolInfo...# P/ Q; q. A, I0 N. S  D
info.Name(_Symbol);- ~- w& ?6 y) L. l* J" N
//--- Set the timer...
7 |" x% h- ^! X. b+ l7 g, H) YEventSetTimer(1);. {& ]9 h# C& Q5 c! o
//--- Set the base of the random number to have equal tests...# R3 n2 }6 B2 U+ f, A
MathSrand(0xDEAD);# D1 R1 J5 h; X1 T( [
return(INIT_SUCCEEDED);3 W) f& ]* m1 W+ N7 s) q
}
) I) X. A$ B( _; F" `//--- Since we set a timer, we need to destroy it in OnDeInit()." V+ {  [: U% d8 v+ j9 w, n
void OnDeinit(const int reason)7 B! ^" k1 F% b* J
{
! y9 r# m6 P1 O6 ]( \0 Z+ R& ~EventKillTimer();. W; c9 b. m* @
}9 Z' l! x0 o( c- J* h
//--- The OnTick function only informs us that we have a new deal
/ C) S+ b) c3 k; ]void OnTick()% O" u' Z& f5 p5 Z, `+ Q4 c
{! x# C/ g( q5 E; q* k; ~# f
tem_tick = true;3 x6 P7 T" p% O8 x% ^( {
}  j2 [. b# O5 t- G* ]
//+------------------------------------------------------------------+: _5 T. a: G% x' C' n( k4 v; H4 Q. @8 f
//| Expert Advisor main function                                     |  O6 f+ U( g1 }! F+ f
//+------------------------------------------------------------------+
5 V/ B) o7 B! B& Q; Dvoid OnTimer()% q5 W2 S* l9 H* U/ X& u
{1 D% z' X( v: j. V; X8 R
MqlRates cotacao[];% V8 o; c( B3 s6 T
return ;
/ I" ~( f- @  K( [) e% \* V8 Sif (negocios_autorizados == false) // are we outside the trading window?5 t9 K* ^% `2 e7 G# k4 Z
return ;
: v+ F. d/ F; Y5 t//--- We are in the trading window, try to open a new position!" d" ^- r4 {9 b% G  h5 H$ _* x
int sorteio = MathRand();+ [% m% a* F) ^7 R
//--- Entry rule 1.1
) Y+ ?+ X' F5 j% A( fif(sorteio == 0 || sorteio == 32767)$ T/ P6 x! |4 [& a
return ;( U5 t8 L$ f- b! [
if(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy. U# T$ V, [- j  ~# f/ i
{
' A( U# o3 L5 _4 q+ J5 B$ M# ynegocios.Buy(info.LotsMin(), _Symbol);
7 Q0 v" j" ^1 k- p7 }$ {% Z}; S7 R: t3 x' p
else // Draw rule 1.3 -- odd number - Sell
' e: \1 @1 S4 r{2 K% R, R  Q9 e
negocios.Sell(info.LotsMin(), _Symbol);( l6 ?( z3 a& u$ a
}
7 g2 s* T# |; I7 s) z) C}
; h( u/ N- I8 O) O0 K: ^//--- Check if we have a new candlestick...
4 J$ c: Y0 I6 M& G, _  V! ?! k0 Obool tem_vela_nova(const MqlRates &rate)
* d% P8 Y  r6 B; b# {5 T" y{3 [5 r" D6 A% H' Q/ J7 `& W
{7 n8 ]+ `& }" I/ e& m7 j: Q
ret = true;5 D4 ^: V2 X% d
close_positions = false;
) n# o' K0 ^7 @4 c5 U}
# Z7 N* w) |6 c; s- U. b  Welse) d3 \$ ^0 k% e1 \5 U- {7 _: S7 B
{
* H1 y$ K: p: b( T0 E# ?# qif(mdt.hour == 16)' T' r- a( n8 x. h' e
close_positions = (mdt.min >= 30);
( e' O; D1 k& v- g}8 U& \$ j: P1 F# d
}
; Q: L9 m( p8 z& Xreturn ret;  u( L. s7 c8 }6 [8 u6 Q
}* E9 q/ G& N8 L2 G! K
//---7 W4 U- r0 c. |+ ^
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[]): b( z3 N+ O' r* D& O1 b* ^
{8 c- _2 M0 R1 R9 }
if(PositionsTotal()) // Is there a position?
, k3 p# D  K9 H+ q3 W{
3 v! }! T. G7 f4 N- Rdouble offset[1] = { 0 };1 {8 L; d# e! \, w; P
if(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?' ?& c0 M' I/ n9 J
&& PositionSelect(_Symbol))  // Select the existing position!$ S, e" x) d8 _- P- F; V7 ^
{
- g0 E3 S) P, c5 {2 xENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
& E. Y8 ?1 \7 D8 `. \double SL = PositionGetDouble(POSITION_SL);7 k: D; }# R  d- S6 F5 P
double TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));
7 q+ J1 h$ j, e/ N/ U& wif(tipo == POSITION_TYPE_BUY)) g% ~& {, V! K1 t5 g' ]0 u* {/ B3 }
{; e& y% g( Y8 A2 F2 a, I( d
if (cotacoes[1].high > cotacoes[0].high)0 X% n, y3 K9 d2 |/ N! C) A) R
{; }2 A- Q/ P4 _0 u
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];0 N: {  x# d1 A. _+ V
info.NormalizePrice(sl);
) ]: x: U2 o* `; T! @if (sl > SL)1 I! b2 A$ k2 P" p: J( J
{
- e4 b: }7 O. v' ?# N; H/ nnegocios.PositionModify(_Symbol, sl, TP);
3 `1 m# O0 G; O7 [}
6 G9 |, _: V. L- M}
: G7 f; R  v! [8 i2 O3 Q' m& E1 ^}
* e" _* b% |. E" }9 X$ Zelse // tipo == POSITION_TYPE_SELL
0 }6 E2 R) a  L- C" i. Z# I/ H# [{# Q' Q) L2 F, l
if (cotacoes[1].low < cotacoes[0].low)
/ h$ p$ U- S" v  Y, K$ a{6 ^7 B% `* m8 `" a0 g4 R1 _
return true;8 C3 a: `2 h0 {
}0 h- d. G( @5 L5 H+ Z) b& Y
// there was no position
% _. T9 }& d6 W9 ~/ k: Greturn false;
  w% v0 Q; ^" l9 U! _}# i8 x. V2 f  _+ O
我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。
. z6 i3 z5 s% m, T5 a# l2 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-1-12 15:01 , Processed in 0.456642 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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