私募网

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

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

[复制链接]
发表于 2024-3-31 08:42:56 | 显示全部楼层 |阅读模式
启动交易模型,并构建 EA
* {9 n5 ]  H4 A- @2 e在解决系统品质因数之前,有必要创建一个能在测试中使用的基本系统。 我们选择了一个简单的系统:我们先选择一个随机数字,若它是偶数,我们就开仓做多;否则,我们开仓做空,因为数字是奇数。) J$ O( U, V3 H! C0 S! b
为了保持随机抽取,我们调用 MathRand() 函数,它提供一个介于 0(零)至 32767 之间的数字。 此外,为了令系统更加平衡,我们将添加两条互补规则。 有了这三条规则,我们将努力确保系统更加可靠。
" C  N+ W/ c6 i" O4 u! c以下是制定这些规则的代码。* V9 G5 \  C$ N6 @0 |  G7 l
//--- Indicator ATR(1) with EMA(8) used for the stop level...
% }# m: n# H+ `# y& d5 W! Oint ind_atr = iATR(_Symbol, PERIOD_CURRENT, 1);
" |& T/ R; M, {6 mint ind_ema = iMA(_Symbol, PERIOD_CURRENT, 8, 0, MODE_EMA, ind_atr);
  y0 i) O! U0 V//--- Define a variable that indicates that we have a deal...) [- t+ o: o" _" x- @
bool tem_tick = false;
- t7 c6 N/ G9 \( V1 O- q; V% v//--- An auxiliary variable for opening a position5 F( q  ^3 z7 K9 P' `' H3 ~1 T; {
#include<Trade/Trade.mqh>
, I! g* ^( {+ A0 ]#include<Trade/SymbolInfo.mqh>2 ~; H$ M; A9 g  {& X  o; S# m
CTrade negocios;
4 l0 k7 H) K/ G$ x. J% x! ]1 tCSymbolInfo info;7 h. c) Y- ]5 R
//--- Define in OnInit() the use of the timer every second
& k, D. A; |, v- ^//--- and start CTrade
; C- ^( ^! x- P% s! hint OnInit()( T" l6 Q( {# {" c9 H6 k9 ^2 v
{8 K) V! E* ?3 _1 h3 ~5 {
//--- Set the fill type to keep a pending order
9 w/ `* V" w9 j, F7 ]//--- until it is fully filled6 k) s* G0 \- L4 g/ P$ v
negocios.SetTypeFilling(ORDER_FILLING_RETURN);
* V( E( u8 _( w* d8 i. s//--- Leave the fixed deviation at it is not used on B3 exchange# \- e/ h4 b. Z$ p# L
negocios.SetDeviationInPoints(5);
4 a  b$ ~7 A" \/ G. {' b//--- Define the symbol in CSymbolInfo...3 G$ |: T& ~) I0 V; G8 Y
info.Name(_Symbol);
8 M" G/ ~: z5 Y//--- Set the timer...
% c; p5 U; X) u2 \( R/ sEventSetTimer(1);! V; h0 b2 b+ Y+ A
//--- Set the base of the random number to have equal tests...2 W' L7 z; h1 t4 f! m' ~2 k
MathSrand(0xDEAD);
8 V* _$ N! L& o2 _  _* c8 B. C- P+ }return(INIT_SUCCEEDED);
- D' q% k. g/ ?- b# R5 \0 L}, S" L) T- M3 `$ G, P2 o
//--- Since we set a timer, we need to destroy it in OnDeInit().4 _& O* t2 j1 O( g0 R
void OnDeinit(const int reason)% X' }* z7 p8 N  h
{
( ^2 I$ @6 I& @- E) bEventKillTimer();" |7 ~* h2 j0 M' R9 }  \
}) K" [' w9 q4 V
//--- The OnTick function only informs us that we have a new deal
, v& x0 j) u1 R8 tvoid OnTick()
, }# F9 K9 y) |* v+ V! L: Z. ?/ w{. e/ y3 i3 \1 S3 X2 |
tem_tick = true;9 _9 H& l; I  T$ o& E' c9 F, Y; M) j
}
2 k' x2 [$ b& |- P. F//+------------------------------------------------------------------+$ g( \7 z& W. {7 M: a
//| Expert Advisor main function                                     |
8 H( w7 h3 ^4 l5 v$ Y3 j- _4 _+ A; ~//+------------------------------------------------------------------+. g: g6 |) C% c1 @* P
void OnTimer()4 {1 Y& p/ x+ g- l
{$ q' I" n/ y5 k7 ~/ Z; Z) ]
MqlRates cotacao[];. P+ Q0 `& c( O' G. o
return ;* P' ~) ?9 n1 p8 w8 y6 Q! x- T
if (negocios_autorizados == false) // are we outside the trading window?
1 A8 W* [/ H1 G. @3 R  a4 I. ?return ;4 D/ m7 k8 p- n
//--- We are in the trading window, try to open a new position!
) N) [) p6 d# ]9 I- z: }1 |: \  yint sorteio = MathRand();
% d; t  Y0 s$ r2 r6 K- d//--- Entry rule 1.1
6 D9 y: U) _4 _4 {  {+ d' Yif(sorteio == 0 || sorteio == 32767)
" \. R; U  j$ @% w5 qreturn ;
# U7 e7 A5 `# hif(MathMod(sorteio, 2) == 0)  // Draw rule 1.2 -- even number - Buy4 Z# q. C* n. @" f6 f* Z/ A4 J
{
) x8 X0 \; j9 i; tnegocios.Buy(info.LotsMin(), _Symbol);
/ C  y' U5 \5 d. q}
: G6 g0 H" M% D2 X: z+ @- I# jelse // Draw rule 1.3 -- odd number - Sell% L  F8 ^4 _6 i( A  @
{
4 O/ @- j$ R5 v  K; x0 a; H, c8 B9 c  ?negocios.Sell(info.LotsMin(), _Symbol);
: L2 a( ]$ x5 U5 O2 X3 `}5 {' y0 p. M4 `- X* T$ f6 U0 B
}
; Y& G* Y1 X% T" X//--- Check if we have a new candlestick...0 U$ \' b6 B/ s; Q! }2 n
bool tem_vela_nova(const MqlRates &rate)/ q* r% D- D6 ?0 l5 i& F
{5 F1 y9 ^) e! a2 k$ g, |! Q
{
# _% E' s* M& |# Q! Fret = true;, C& \) i" I! g) Z6 _
close_positions = false;
0 L& J7 C8 H# J  k6 I! Q7 d7 v}) m8 j. G) }( R+ T$ B
else8 ?# L2 V0 l8 p9 y
{
2 J6 |+ B2 }8 vif(mdt.hour == 16)
) u2 Y& m! Q9 A7 j2 e9 Y/ {( Y! y$ dclose_positions = (mdt.min >= 30);- Y9 K& Y+ C2 e' o0 y
}
" T: x/ s6 B" ]) x" N. A; Z}# F) E+ h0 W- b) e3 N2 m
return ret;
' B( f8 h% S2 h5 ]3 b* l}* S& {+ D$ A3 b2 @
//---) A8 A9 p: p- T* }1 G$ F/ R
bool arruma_stop_em_posicoes(const MqlRates &cotacoes[])4 e% E# W& H( M8 K
{7 H0 H$ ?; l: z) @% Z# p
if(PositionsTotal()) // Is there a position?4 `, z4 r; C0 f4 B0 M
{
/ \9 i- r4 d% _2 |+ Vdouble offset[1] = { 0 };
$ U' v1 ^( [8 vif(CopyBuffer(ind_ema, 0, 1, 1, offset) == 1 // EMA successfully copied?
. P- y$ ]3 y6 I4 P2 U&& PositionSelect(_Symbol))  // Select the existing position!; d; `* }. E$ N& L
{
7 d2 n# W4 ^- M5 N) Z5 nENUM_POSITION_TYPE tipo = (ENUM_POSITION_TYPE) PositionGetInteger(POSITION_TYPE);
' J; A7 x- q; c7 R- i; g! f9 Z8 Udouble SL = PositionGetDouble(POSITION_SL);
3 l! h* ^  U& b: g! l% \2 z* cdouble TP = info.NormalizePrice(PositionGetDouble(POSITION_TP));" W2 p, o( n9 r- k
if(tipo == POSITION_TYPE_BUY)& ]" L1 X$ }; \& K$ f, w
{! H1 c* k9 c) Z, p, a
if (cotacoes[1].high > cotacoes[0].high)4 e4 M5 ]. N% @( c" A* z
{  O& A0 I- g" \& h' v4 D
double sl = MathMin(cotacoes[0].low, cotacoes[1].low) - offset[0];
5 J3 b# n9 V( }+ M4 w' {  iinfo.NormalizePrice(sl);
+ v% K2 A3 q+ R6 r+ O* ~( i6 z6 lif (sl > SL)
" ?; h, a, K# r. N+ W& |) c{
: g/ A' B& y. T4 Anegocios.PositionModify(_Symbol, sl, TP);& S, j% W, F; i# E" r% u# {2 o
}/ n# Q2 _; k5 {4 z3 A
}) m3 N, {# X: N/ p+ J
}" o3 o3 W# S( j
else // tipo == POSITION_TYPE_SELL
5 `! Z( b7 _6 s9 o: L{7 z1 ~; k% [8 T6 p2 t1 I! _
if (cotacoes[1].low < cotacoes[0].low)! x) p; {! V: j% m' I, L
{
5 y. M& I5 ?* Y9 Q0 ]5 ?0 c8 ~, ?* Sreturn true;, p3 k0 m; T: p+ y) F8 P1 q
}+ s4 w+ B% p4 j( U& p: M
// there was no position
8 ^  |. `& N5 b% o% yreturn false;
$ P$ T/ ?4 T. l* t- ]}
* C$ P3 t' Y# x8 `5 K# k我们简略研究一下上面的代码。 我们将经均化计算的 ATR 值来判定止损步长,即我们发现当前烛条超过前一根时,将止损价位放置在烛条的边界。 这是在函数 arruma_stop_em_posicoes 中完成的。 当其返回 true 时,已有设置,且我们无需在 OnTimer 的主代码中前进。 我使用该函数取代 OnTick,因为我不需要针对每笔执行的交易运行一遍大函数。 函数应该在所定义周期的每根新烛条建立时执行。 在 OnTick 中,设置 true 值表示前一笔交易。 这是必要的;否则,在休市期间,策略测试器将引入暂停,因为即使没有前一笔的交易,它也会执行该函数。+ B7 v3 x, Z2 _/ w
到该处为止,一切都严格按照定义好的计划进行,包括两个指定的窗口。 第一个是开仓交易的窗口,在 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-19 09:04 , Processed in 1.834592 second(s), 31 queries .

Powered by www.simu001.cn X3.4

Copyright © 2001-2021, Tencent Cloud.

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